Skip to content

Commit 754176f

Browse files
committed
fix: support default_value and last_insert_id
1 parent 4baa186 commit 754176f

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/Processor/InsertProcessor.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
final class InsertProcessor extends Processor
1010
{
11+
/**
12+
* @throws SQLFakeUniqueKeyViolation
13+
* @throws ProcessorException
14+
* @throws \Exception
15+
*/
1116
public static function process(
1217
\Vimeo\MysqlEngine\FakePdoInterface $conn,
1318
Scope $scope,
@@ -115,19 +120,45 @@ public static function process(
115120
$stmt->selectQuery
116121
)->rows;
117122

118-
$row = [];
119-
foreach ($selectResult as $value) {
123+
foreach ($selectResult as $selected_param) {
124+
$row = [];
120125
foreach ($stmt->selectQuery->selectExpressions as $index => $expr) {
121126
if (key_exists($index, $stmt->insertColumns)
122-
&& (is_string($value[$expr->name]) || is_int($value[$expr->name]))) {
123-
$row[$stmt->insertColumns[$index]] = $value[$expr->name];
127+
&& (is_string($selected_param[$expr->name]) || is_int($selected_param[$expr->name]))) {
128+
$row[$stmt->insertColumns[$index]] = $selected_param[$expr->name];
124129
}
125130
}
126-
}
127131

128-
$table[] = $row;
132+
$row = DataIntegrity::coerceToSchema($conn, $row, $table_definition);
133+
134+
$result = DataIntegrity::checkUniqueConstraints($table, $row, $table_definition);
135+
136+
if ($result !== null) {
137+
throw new SQLFakeUniqueKeyViolation($result[0]);
138+
}
139+
140+
/**
141+
* @psalm-suppress MixedAssignment
142+
*/
143+
foreach ($row as $column_name => $value) {
144+
$column = $table_definition->columns[$column_name];
145+
146+
if ($column instanceof IntegerColumn && $column->isAutoIncrement() && is_int($value)) {
147+
$last_incremented_value = $conn->getServer()->addAutoIncrementMinValue(
148+
$database,
149+
$table_name,
150+
$column_name,
151+
$value
152+
);
153+
}
154+
}
155+
156+
$table[] = $row;
157+
}
129158

130159
$conn->getServer()->saveTable($database, $table_name, $table);
160+
$conn->setLastInsertId((string)($last_incremented_value ?? 0));
161+
131162
return count($selectResult);
132163
}
133164

tests/EndToEndTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -918,14 +918,16 @@ public function testInsertWithSelect(): void
918918

919919
$query = $pdo->prepare(
920920
"INSERT INTO `video_game_characters`
921-
(`id`, `name`, `type`, `profession`, `console`, `is_alive`, `powerups`, `skills`, `created_on`)
921+
(`name`, `type`, `profession`, `console`, `is_alive`, `powerups`, `skills`, `created_on`)
922922
SELECT
923-
19+`id`, 'wario','villain','plumber','nes','1','3','{\"magic\":0, \"speed\":0, \"strength\":0, \"weapons\":0}', NOW(),
923+
'wario','villain','plumber','nes','1','3','{\"magic\":0, \"speed\":0, \"strength\":0, \"weapons\":0}', NOW(),
924924
FROM `video_game_characters`"
925925
);
926926

927927
$query->execute();
928928

929+
self::assertEquals($pdo->lastInsertId(), 32);
930+
929931
$query = $pdo->prepare(
930932
'SELECT `id`, `name`
931933
FROM `video_game_characters`
@@ -937,7 +939,7 @@ public function testInsertWithSelect(): void
937939

938940
$this->assertSame(
939941
[
940-
['id' => 35, 'name' => 'wario'],
942+
['id' => 32, 'name' => 'wario'],
941943
],
942944
$query->fetchAll(PDO::FETCH_ASSOC)
943945
);

0 commit comments

Comments
 (0)