Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
245cb65
Fix for issue #35. Corrected ActiveRecordTest's so that capitalizatio…
ipundit Aug 31, 2023
e1bb78f
Primary key has to be lowercased too because gettter/setters are lowe…
ipundit Aug 31, 2023
f6e2981
Fix for issue #35. Corrected ActiveRecordTest's so that capitalizatio…
ipundit Aug 31, 2023
67471f5
Primary key has to be lowercased too because gettter/setters are lowe…
ipundit Aug 31, 2023
83a4db0
Merge branch 'master' of https://github.com/ipundit/activerecord
ipundit Sep 1, 2023
1aea4e1
Merge branch 'master' into master
ipundit Sep 1, 2023
9639f31
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 2, 2023
52478ab
Running all the tests may take more than the default of 300 seconds. …
ipundit Sep 2, 2023
c323677
Write proper tests that demonstrate the fix for issue #35:
ipundit Sep 2, 2023
80ed052
Switched from firstName field to existing mixedCaseField to pass Post…
ipundit Sep 2, 2023
b8ecfc0
- List things in alphabetical order
ipundit Sep 2, 2023
23d00a7
Revert updates to test suite as ActiveRecordTest::test_case_insensiti…
ipundit Sep 2, 2023
e31ec4f
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 2, 2023
9a6fb32
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 2, 2023
ba7f090
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 4, 2023
d42a7d1
Implemented composer test <fileName> <filter>
ipundit Sep 4, 2023
fc79d97
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 4, 2023
568cfb5
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 4, 2023
650d368
Fix style issues
ipundit Sep 4, 2023
767e537
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 4, 2023
089d3d3
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
2eec6b0
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
6aba443
Fix for issue #55
ipundit Sep 5, 2023
c772578
Remove unused scripts and files references.
ipundit Sep 5, 2023
8291ea6
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
cf6056e
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
64e45b8
Remove unused items in composer.json
ipundit Sep 5, 2023
1dcad8d
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
7b1e20e
Merge branch 'master' of https://github.com/ipundit/activerecord
ipundit Sep 5, 2023
79d2182
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 5, 2023
1b822b4
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 6, 2023
6bd01fd
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 13, 2023
09f0732
Document composer style-fix
ipundit Sep 14, 2023
7993fc0
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 15, 2023
7080716
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 16, 2023
123d0f5
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 18, 2023
616ac61
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 19, 2023
1d43893
Fixed #64
ipundit Sep 19, 2023
5d22b7a
Minor style-fixes
ipundit Sep 19, 2023
46dee99
Merge branch 'master' of https://github.com/php-activerecord/activere…
ipundit Sep 19, 2023
e40a4c8
Remove an extraneous space
ipundit Sep 19, 2023
6120882
Moved quoting of column names to SQLBuilder
ipundit Sep 19, 2023
a2aa1ef
Added support for User::not(User::where(condition))
ipundit Sep 19, 2023
48be2df
Implemented Relation::and()
ipundit Sep 19, 2023
bc13deb
Improve code coverage
ipundit Sep 19, 2023
fb5b70b
Fix style-checking
ipundit Sep 19, 2023
982ff80
Defer the last of the quoting to SQLBuilder::where()
ipundit Sep 19, 2023
07ccd2e
Small fix to pass regression
ipundit Sep 19, 2023
158228b
Fixed regression for Postgres
ipundit Sep 19, 2023
1b4fe91
Moved escapeColumns from SQLBuilder.php to PgsqlAdapter.php
ipundit Sep 20, 2023
19c1384
Revert "Implemented Relation::and()"
shmax Sep 20, 2023
de381bf
simplify
shmax Sep 20, 2023
be49f75
remove unused
shmax Sep 20, 2023
4044226
remove file
shmax Sep 20, 2023
560cf17
Revert "Added support for User::not(User::where(condition))"
shmax Sep 20, 2023
a0ee90d
Revert "Improve code coverage"
shmax Sep 20, 2023
5c4199d
revert some stuff
shmax Sep 20, 2023
d7bd908
lint
shmax Sep 20, 2023
d9fad93
fix annotation
shmax Sep 20, 2023
156a156
coverage
shmax Sep 20, 2023
c7cef6a
lint
shmax Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Simply run:
docker-compose up -d
```

Then, the necessary services will be available and the tests should pass (although you may need to install PHP memcache extensions in a separate step, see below ).
Then, the necessary services will be available and the tests should pass (although you may need to install PHP memcache extensions in a separate step, see below).

When you're done, you can take it down with:
```sh
Expand Down
19 changes: 19 additions & 0 deletions lib/Adapter/PgsqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,23 @@ public function set_encoding(string $charset): void
{
$this->query("SET NAMES '$charset'");
}

/**
* @see Connection::escapeColumns()
*
* @param string $expression The where clause to be escaped
* @param list<string> $columns The columns of the table
*/
public function escapeColumns(string $expression, array $columns): string
{
static $quotedNames = [];
foreach ($columns as $column) {
if ($column !== strtolower($column)) {
$quotedNames[$column] ??= $this->quote_name($column);
$expression = str_replace($column, $this->quote_name($column), $expression);
}
}

return $expression;
}
}
15 changes: 12 additions & 3 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,24 @@ public function next_sequence_value(string $sequence_name): ?string
* Quote a name like table names and field names.
*
* @param string $string string to quote
*
* @return string
*/
public function quote_name($string)
public function quote_name($string): string
{
return $string[0] === static::$QUOTE_CHARACTER || $string[strlen($string) - 1] === static::$QUOTE_CHARACTER ?
$string : static::$QUOTE_CHARACTER . $string . static::$QUOTE_CHARACTER;
}

/**
* Escape the column names in the where phrases
*
* @param string $expression The where clause to be escaped
* @param list<string> $columns The columns of the table
*/
public function escapeColumns(string $expression, array $columns): string
{
return $expression;
}

public function date_string(\DateTimeInterface $datetime): string
{
return $datetime->format(static::$date_format);
Expand Down
4 changes: 3 additions & 1 deletion lib/SQLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,20 @@ public function get_where_values(): array
/**
* @param list<WhereClause> $clauses
* @param array<string,string> $mappedNames
* @param array<string> $columns Table column names
*
* @throws Exception\ExpressionsException
*
* @return $this
*/
public function where(array $clauses=[], array $mappedNames=[]): static
public function where(array $clauses=[], array $mappedNames=[], array $columns=[]): static
{
$values = [];
$sql = '';
$glue = ' AND ';
foreach ($clauses as $idx => $clause) {
$expression = $clause->to_s($this->connection, !empty($this->joins) ? $this->table : '', $mappedNames);
$expression = $this->connection->escapeColumns($expression, $columns);
$values = array_merge($values, array_flatten($clause->values()));
$inverse = $clause->negated() ? '!' : '';
$wrappedExpression = $inverse || count($clauses) > 1 ? '(' . $expression . ')' : $expression;
Expand Down
2 changes: 1 addition & 1 deletion lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function options_to_sql(array $options): SQLBuilder
);
}

$sql->where($options['conditions'] ?? [], $options['mapped_names'] ?? []);
$sql->where($options['conditions'] ?? [], $options['mapped_names'] ?? [], array_keys($this->columns));

if (array_key_exists('order', $options)) {
$sql->order($options['order']);
Expand Down
2 changes: 0 additions & 2 deletions lib/WhereClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ private function build_sql_from_hash(Connection $connection, array $hash, string
$table = !empty($prependTableName) ? $connection->quote_name($prependTableName) : '';

foreach ($hash as $name => $value) {
$name = $connection->quote_name($name);

if (!empty($prependTableName)) {
$name = $table . '.' . $name;
}
Expand Down
4 changes: 2 additions & 2 deletions test/ActiveRecordNotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static function conditions(): array
'hash' => [[
'name' => 'Another Book',
],
'WHERE `name` = ?',
'WHERE !(`name` = ?)'
'WHERE name = ?',
'WHERE !(name = ?)'
],
'in' => [[
'book_id in (?)', [1, 2],
Expand Down
9 changes: 9 additions & 0 deletions test/PgsqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public function testInsertId()
$this->assertTrue($this->connection->insert_id('authors_author_id_seq') > 0);
}

public function testToSql(): void
{
$this->assertEquals(
'SELECT * FROM "authors" WHERE "mixedCaseField" = ? ORDER BY name',
\test\models\Author::where('mixedCaseField = ?', 'The Art of Main Tanking')
->order('name')->to_sql()
);
}

public function testInsertIdWithParams()
{
$x = ['name'];
Expand Down
25 changes: 5 additions & 20 deletions test/WhereClauseTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
<?php

use ActiveRecord\ConnectionManager;
use ActiveRecord\DatabaseException;
use ActiveRecord\Exception\ExpressionsException;
use ActiveRecord\WhereClause;
use PHPUnit\Framework\TestCase;

class WhereClauseTest extends TestCase
class WhereClauseTest extends DatabaseTestCase
{
protected $connection;

public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
try {
$this->connection = ConnectionManager::get_connection();
} catch (DatabaseException $e) {
$this->markTestSkipped('failed to connect. ' . $e->getMessage());
}
}

public function testValues()
{
$c = new WhereClause('a=? and b=?', [1, 2]);
Expand Down Expand Up @@ -134,7 +119,7 @@ public function testSubstituteOnString(): void
public function testSubstituteOnHash(): void
{
$a = new WhereClause(['name' => 'Tito', 'id'=> 1]);
$this->assertEquals("`name` = 'Tito' AND `id` = 1", $a->to_s($this->connection, substitute: true));
$this->assertEquals("name = 'Tito' AND id = 1", $a->to_s($this->connection, substitute: true));
}

public function testSubstituteQuotesScalarsButNotOthers(): void
Expand Down Expand Up @@ -199,7 +184,7 @@ public function testNullValue(): void
public function testHashWithDefaultGlue(): void
{
$a = new WhereClause(['id' => 1, 'name' => 'Tito']);
$this->assertEquals('`id` = ? AND `name` = ?', $a->to_s($this->connection));
$this->assertEquals('id = ? AND name = ?', $a->to_s($this->connection));
}

public function testHashWithGlue(): void
Expand All @@ -208,7 +193,7 @@ public function testHashWithGlue(): void
'id' => 1,
'name' => 'Tito'
]);
$this->assertEquals('`id` = ?, `name` = ?', $a->to_s($this->connection, glue: ', '));
$this->assertEquals('id = ?, name = ?', $a->to_s($this->connection, glue: ', '));
}

public function testHashWithArray(): void
Expand All @@ -217,6 +202,6 @@ public function testHashWithArray(): void
'id' => 1,
'name' => ['Tito', 'Mexican']
]);
$this->assertEquals('`id` = ? AND `name` IN(?,?)', $a->to_s($this->connection));
$this->assertEquals('id = ? AND name IN(?,?)', $a->to_s($this->connection));
}
}