Skip to content

Commit 4fbf0cd

Browse files
committed
More fixes
1 parent 168fe60 commit 4fbf0cd

6 files changed

+96
-28
lines changed

src/FakePdo.php

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class FakePdo extends \PDO
2323
*/
2424
public $stringifyResult = true;
2525

26+
/**
27+
* @var bool
28+
*/
29+
public $lowercaseResultKeys = false;
30+
2631
/**
2732
* @var ?string
2833
* @readonly
@@ -49,6 +54,10 @@ public function setAttribute($key, $value)
4954
$this->stringifyResult = (bool) $value;
5055
}
5156

57+
if ($key === \PDO::ATTR_CASE && $value === \PDO::CASE_LOWER) {
58+
$this->lowercaseResultKeys = true;
59+
}
60+
5261
if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) {
5362
$this->real->setAttribute($key, $value);
5463
}
@@ -69,6 +78,13 @@ public function prepare($statement, $options = null)
6978

7079
public function lastInsertId($seqname = null)
7180
{
81+
if ($this->real) {
82+
if ($this->lastInsertId != $this->real->lastInsertId($seqname)) {
83+
var_dump($this->real->lastInsertId($seqname), $this->lastInsertId);
84+
throw new \UnexpectedValueException('different last insert id');
85+
}
86+
}
87+
7288
return $this->lastInsertId;
7389
}
7490

src/FakePdoStatement.php

+57-17
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,29 @@ public function execute()
137137
$fake_result = $this->result;
138138
$real_result = $this->realStatement->fetchAll(\PDO::FETCH_ASSOC);
139139

140-
if ($this->conn->stringifyResult && $fake_result) {
141-
$fake_result = array_map(
142-
function ($row) {
143-
return self::stringify($row);
144-
},
145-
$fake_result
146-
);
140+
if ($fake_result) {
141+
if ($this->conn->stringifyResult) {
142+
$fake_result = array_map(
143+
function ($row) {
144+
return self::stringify($row);
145+
},
146+
$fake_result
147+
);
148+
}
149+
150+
if ($this->conn->lowercaseResultKeys) {
151+
$fake_result = array_map(
152+
function ($row) {
153+
return self::lowercaseKeys($row);
154+
},
155+
$fake_result
156+
);
157+
}
147158
}
148159

149160
if ($real_result !== $fake_result) {
150-
//var_dump($real_result, $fake_result);
151-
//throw new \UnexpectedValueException('different');
161+
var_dump($real_result, $fake_result);
162+
throw new \UnexpectedValueException('different');
152163
}
153164
}
154165

@@ -213,6 +224,10 @@ public function fetch(
213224
$row = self::stringify($row);
214225
}
215226

227+
if ($this->conn->lowercaseResultKeys) {
228+
$row = self::lowercaseKeys($row);
229+
}
230+
216231
if ($fetch_style === \PDO::FETCH_ASSOC) {
217232
$this->resultCursor++;
218233

@@ -249,14 +264,20 @@ public function fetchAll(int $fetch_style = -123, $fetch_argument = null, array
249264
}
250265

251266
if ($fetch_style === \PDO::FETCH_ASSOC) {
252-
if ($this->conn->stringifyResult) {
253-
return array_map(
254-
function ($row) {
255-
return self::stringify($row);
256-
},
257-
$this->result ?: []
258-
);
259-
}
267+
return array_map(
268+
function ($row) {
269+
if ($this->conn->stringifyResult) {
270+
$row = self::stringify($row);
271+
}
272+
273+
if ($this->conn->lowercaseResultKeys) {
274+
$row = self::lowercaseKeys($row);
275+
}
276+
277+
return self::stringify($row);
278+
},
279+
$this->result ?: []
280+
);
260281

261282
return $this->result ?: [];
262283
}
@@ -281,6 +302,10 @@ function ($row) {
281302
$row = self::stringify($row);
282303
}
283304

305+
if ($this->conn->lowercaseResultKeys) {
306+
$row = self::lowercaseKeys($row);
307+
}
308+
284309
return array_merge($row, \array_values($row));
285310
},
286311
$this->result ?: []
@@ -314,6 +339,10 @@ function ($row) use ($fetch_argument, $ctor_args) {
314339
$row = self::stringify($row);
315340
}
316341

342+
if ($this->conn->lowercaseResultKeys) {
343+
$row = self::lowercaseKeys($row);
344+
}
345+
317346
return self::convertRowToObject($row, $fetch_argument, $ctor_args);
318347
},
319348
$this->result
@@ -368,6 +397,17 @@ function ($value) {
368397
);
369398
}
370399

400+
private static function lowercaseKeys(array $row)
401+
{
402+
$lowercased_row = [];
403+
404+
foreach ($row as $col => $value) {
405+
$lowercased_row[\strtolower($col)] = $value;
406+
}
407+
408+
return $lowercased_row;
409+
}
410+
371411
/**
372412
* @psalm-taint-sink callable $class
373413
*

src/Processor/CreateProcessor.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,23 @@ function ($column) {
6464
}
6565
}
6666

67+
6768
$default_character_set = null;
6869
$default_collation = null;
6970

71+
$auto_increment_offsets = [];
72+
7073
foreach ($stmt->entityOptions->options as $option) {
7174
if ($option['name'] === 'DEFAULT CHARSET') {
7275
$default_character_set = $option['value'];
7376
} elseif ($option['name'] === 'COLLATE') {
7477
$default_collation = $option['value'];
78+
} elseif ($option['name'] === 'AUTO_INCREMENT') {
79+
foreach ($definition_columns as $name => $column) {
80+
if ($column instanceof Column\IntegerColumn && $column->isAutoIncrement()) {
81+
$auto_increment_offsets[$name] = $option['value'];
82+
}
83+
}
7584
}
7685
}
7786

@@ -86,7 +95,8 @@ function ($column) {
8695
$default_character_set,
8796
$default_collation,
8897
$primary_key_columns,
89-
$indexes
98+
$indexes,
99+
$auto_increment_offsets
90100
);
91101

92102
$conn->getServer()->addTableDefinition(

src/Processor/Expression/ExistsOperatorEvaluator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public static function evaluate(ExistsOperatorExpression $expr, array $row, \Vim
2020
$ret = Evaluator::evaluate($expr->exists, $row, $conn);
2121

2222
if ($expr->negated) {
23-
return !$ret;
23+
return $ret ? 0 : 1;
2424
}
2525

26-
return (bool) $ret;
26+
return $ret ? 1 : 0;
2727
}
2828
}

src/Processor/SelectProcessor.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ protected static function applySelect(FakePdo $conn, SelectQuery $stmt, array $d
156156
$val = Expression\Evaluator::evaluate($expr, [], $conn);
157157
$name = $expr->name;
158158

159-
$formatted_row[$name] = $val;
159+
$formatted_row[\substr($name, 0, 255)] = $val;
160160
}
161161

162162
return [$formatted_row];
163163
}
164164

165165
$order_by_expressions = $stmt->orderBy ?? [];
166-
166+
167167
foreach ($data as $row) {
168168
$formatted_row = [];
169169

@@ -189,7 +189,7 @@ protected static function applySelect(FakePdo $conn, SelectQuery $stmt, array $d
189189
$col_name = \end($parts);
190190
$val = $formatted_row[$col_name] ?? $val;
191191

192-
$formatted_row[$col_name] = $val;
192+
$formatted_row[\substr($col_name, 0, 255)] = $val;
193193
}
194194
}
195195

@@ -216,7 +216,7 @@ protected static function applySelect(FakePdo $conn, SelectQuery $stmt, array $d
216216
}
217217
}
218218

219-
$formatted_row[$name] = $val;
219+
$formatted_row[\substr($name, 0, 255)] = $val;
220220
}
221221

222222
foreach ($order_by_expressions as $order_by) {
@@ -225,7 +225,7 @@ protected static function applySelect(FakePdo $conn, SelectQuery $stmt, array $d
225225
})();
226226
$val = Expression\Evaluator::evaluate($order_by['expression'], $row, $conn);
227227
$name = $order_by['expression']->name;
228-
$formatted_row[$name] = $formatted_row[$name] ?? $val;
228+
$formatted_row[\substr($name, 0, 255)] = $formatted_row[$name] ?? $val;
229229
}
230230

231231
$out[] = $formatted_row;
@@ -246,7 +246,7 @@ function ($col) {
246246
);
247247

248248
if (!array_key_exists($key, $new_out)) {
249-
$new_out[$key] = $row;
249+
$new_out[\substr($key, 0, 255)] = $row;
250250
}
251251
}
252252

src/Schema/TableDefinition.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TableDefinition
4141
/**
4242
* @var array<string, int>
4343
*/
44-
public array $autoIncrementOffsets = [];
44+
public $autoIncrementOffsets = [];
4545

4646
public function __construct(
4747
string $name,
@@ -50,7 +50,8 @@ public function __construct(
5050
string $characterSet = '',
5151
string $collation = '',
5252
array $primaryKeyColumns = [],
53-
array $indexes = []
53+
array $indexes = [],
54+
array $autoIncrementOffsets = []
5455
) {
5556
$this->name = $name;
5657
$this->databaseName = $databaseName;
@@ -59,5 +60,6 @@ public function __construct(
5960
$this->defaultCollation = $collation;
6061
$this->primaryKeyColumns = $primaryKeyColumns;
6162
$this->indexes = $indexes;
63+
$this->autoIncrementOffsets = $autoIncrementOffsets;
6264
}
6365
}

0 commit comments

Comments
 (0)