Skip to content

Commit

Permalink
Fix lastInsertId implementation
Browse files Browse the repository at this point in the history
last_insert_id returns autoincrement column values
  • Loading branch information
muglug committed Mar 29, 2021
1 parent 9afb99f commit c688105
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/Processor/InsertProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ public static function process(
$column = $table_definition->columns[$column_name];

if ($column instanceof IntegerColumn && $column->isAutoIncrement()) {
$conn->getServer()->addAutoIncrementMinValue(
$last_incremented_value = $conn->getServer()->addAutoIncrementMinValue(
$database,
$table_name,
$column_name,
$value
);
}
}

if (\count($table_definition->primaryKeyColumns) === 1 && $conn->lastInsertId() === "0") {
$conn->setLastInsertId((string) $row[$table_definition->primaryKeyColumns[0]]);
if ($conn->lastInsertId() === "0") {
$conn->setLastInsertId((string)$last_incremented_value);
}
}
}

$table[] = $row;
Expand Down
4 changes: 2 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function addAutoIncrementMinValue(
string $table_name,
string $column_name,
int $value
) : void {
) : int {
$table_definition = $this->getTableDefinition($database_name, $table_name);
$table = $this->databases[$database_name][$table_name] ?? null;

Expand All @@ -253,7 +253,7 @@ public function addAutoIncrementMinValue(
$table = $this->databases[$database_name][$table_name] = new TableData();
}

$table->autoIncrementCursors[$column_name] = max(
return $table->autoIncrementCursors[$column_name] = max(
$table->autoIncrementCursors[$column_name] ?? 0,
$value
);
Expand Down

0 comments on commit c688105

Please sign in to comment.