Skip to content

Commit aa3f07d

Browse files
shmaxipundit
andauthored
Fix mixedcase (#89)
Co-authored-by: Michael Hu <michael@eazypaper.com>
1 parent b9affaf commit aa3f07d

File tree

9 files changed

+52
-30
lines changed

9 files changed

+52
-30
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Simply run:
6464
docker-compose up -d
6565
```
6666

67-
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 ).
67+
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).
6868

6969
When you're done, you can take it down with:
7070
```sh

lib/Adapter/PgsqlAdapter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,23 @@ public function set_encoding(string $charset): void
135135
{
136136
$this->query("SET NAMES '$charset'");
137137
}
138+
139+
/**
140+
* @see Connection::escapeColumns()
141+
*
142+
* @param string $expression The where clause to be escaped
143+
* @param list<string> $columns The columns of the table
144+
*/
145+
public function escapeColumns(string $expression, array $columns): string
146+
{
147+
static $quotedNames = [];
148+
foreach ($columns as $column) {
149+
if ($column !== strtolower($column)) {
150+
$quotedNames[$column] ??= $this->quote_name($column);
151+
$expression = str_replace($column, $this->quote_name($column), $expression);
152+
}
153+
}
154+
155+
return $expression;
156+
}
138157
}

lib/Connection.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,24 @@ public function next_sequence_value(string $sequence_name): ?string
485485
* Quote a name like table names and field names.
486486
*
487487
* @param string $string string to quote
488-
*
489-
* @return string
490488
*/
491-
public function quote_name($string)
489+
public function quote_name($string): string
492490
{
493491
return $string[0] === static::$QUOTE_CHARACTER || $string[strlen($string) - 1] === static::$QUOTE_CHARACTER ?
494492
$string : static::$QUOTE_CHARACTER . $string . static::$QUOTE_CHARACTER;
495493
}
496494

495+
/**
496+
* Escape the column names in the where phrases
497+
*
498+
* @param string $expression The where clause to be escaped
499+
* @param list<string> $columns The columns of the table
500+
*/
501+
public function escapeColumns(string $expression, array $columns): string
502+
{
503+
return $expression;
504+
}
505+
497506
public function date_string(\DateTimeInterface $datetime): string
498507
{
499508
return $datetime->format(static::$date_format);

lib/SQLBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,20 @@ public function get_where_values(): array
118118
/**
119119
* @param list<WhereClause> $clauses
120120
* @param array<string,string> $mappedNames
121+
* @param array<string> $columns Table column names
121122
*
122123
* @throws Exception\ExpressionsException
123124
*
124125
* @return $this
125126
*/
126-
public function where(array $clauses=[], array $mappedNames=[]): static
127+
public function where(array $clauses=[], array $mappedNames=[], array $columns=[]): static
127128
{
128129
$values = [];
129130
$sql = '';
130131
$glue = ' AND ';
131132
foreach ($clauses as $idx => $clause) {
132133
$expression = $clause->to_s($this->connection, !empty($this->joins) ? $this->table : '', $mappedNames);
134+
$expression = $this->connection->escapeColumns($expression, $columns);
133135
$values = array_merge($values, array_flatten($clause->values()));
134136
$inverse = $clause->negated() ? '!' : '';
135137
$wrappedExpression = $inverse || count($clauses) > 1 ? '(' . $expression . ')' : $expression;

lib/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function options_to_sql(array $options): SQLBuilder
229229
);
230230
}
231231

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

234234
if (array_key_exists('order', $options)) {
235235
$sql->order($options['order']);

lib/WhereClause.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ private function build_sql_from_hash(Connection $connection, array $hash, string
218218
$table = !empty($prependTableName) ? $connection->quote_name($prependTableName) : '';
219219

220220
foreach ($hash as $name => $value) {
221-
$name = $connection->quote_name($name);
222-
223221
if (!empty($prependTableName)) {
224222
$name = $table . '.' . $name;
225223
}

test/ActiveRecordNotTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static function conditions(): array
2323
'hash' => [[
2424
'name' => 'Another Book',
2525
],
26-
'WHERE `name` = ?',
27-
'WHERE !(`name` = ?)'
26+
'WHERE name = ?',
27+
'WHERE !(name = ?)'
2828
],
2929
'in' => [[
3030
'book_id in (?)', [1, 2],

test/PgsqlAdapterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public function testInsertId()
1313
$this->assertTrue($this->connection->insert_id('authors_author_id_seq') > 0);
1414
}
1515

16+
public function testToSql(): void
17+
{
18+
$this->assertEquals(
19+
'SELECT * FROM "authors" WHERE "mixedCaseField" = ? ORDER BY name',
20+
\test\models\Author::where('mixedCaseField = ?', 'The Art of Main Tanking')
21+
->order('name')->to_sql()
22+
);
23+
}
24+
1625
public function testInsertIdWithParams()
1726
{
1827
$x = ['name'];

test/WhereClauseTest.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
<?php
22

3-
use ActiveRecord\ConnectionManager;
4-
use ActiveRecord\DatabaseException;
53
use ActiveRecord\Exception\ExpressionsException;
64
use ActiveRecord\WhereClause;
7-
use PHPUnit\Framework\TestCase;
85

9-
class WhereClauseTest extends TestCase
6+
class WhereClauseTest extends DatabaseTestCase
107
{
11-
protected $connection;
12-
13-
public function setUp(): void
14-
{
15-
parent::setUp(); // TODO: Change the autogenerated stub
16-
try {
17-
$this->connection = ConnectionManager::get_connection();
18-
} catch (DatabaseException $e) {
19-
$this->markTestSkipped('failed to connect. ' . $e->getMessage());
20-
}
21-
}
22-
238
public function testValues()
249
{
2510
$c = new WhereClause('a=? and b=?', [1, 2]);
@@ -134,7 +119,7 @@ public function testSubstituteOnString(): void
134119
public function testSubstituteOnHash(): void
135120
{
136121
$a = new WhereClause(['name' => 'Tito', 'id'=> 1]);
137-
$this->assertEquals("`name` = 'Tito' AND `id` = 1", $a->to_s($this->connection, substitute: true));
122+
$this->assertEquals("name = 'Tito' AND id = 1", $a->to_s($this->connection, substitute: true));
138123
}
139124

140125
public function testSubstituteQuotesScalarsButNotOthers(): void
@@ -199,7 +184,7 @@ public function testNullValue(): void
199184
public function testHashWithDefaultGlue(): void
200185
{
201186
$a = new WhereClause(['id' => 1, 'name' => 'Tito']);
202-
$this->assertEquals('`id` = ? AND `name` = ?', $a->to_s($this->connection));
187+
$this->assertEquals('id = ? AND name = ?', $a->to_s($this->connection));
203188
}
204189

205190
public function testHashWithGlue(): void
@@ -208,7 +193,7 @@ public function testHashWithGlue(): void
208193
'id' => 1,
209194
'name' => 'Tito'
210195
]);
211-
$this->assertEquals('`id` = ?, `name` = ?', $a->to_s($this->connection, glue: ', '));
196+
$this->assertEquals('id = ?, name = ?', $a->to_s($this->connection, glue: ', '));
212197
}
213198

214199
public function testHashWithArray(): void
@@ -217,6 +202,6 @@ public function testHashWithArray(): void
217202
'id' => 1,
218203
'name' => ['Tito', 'Mexican']
219204
]);
220-
$this->assertEquals('`id` = ? AND `name` IN(?,?)', $a->to_s($this->connection));
205+
$this->assertEquals('id = ? AND name IN(?,?)', $a->to_s($this->connection));
221206
}
222207
}

0 commit comments

Comments
 (0)