From dbd22eeac4147e4a69343a820522699f859de235 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Fri, 30 Oct 2020 16:32:34 +0100 Subject: [PATCH] [fix] Oracle lastInsertId --- src/Persistence/Sql.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Persistence/Sql.php b/src/Persistence/Sql.php index 16271f932..c3b3410e5 100644 --- a/src/Persistence/Sql.php +++ b/src/Persistence/Sql.php @@ -423,6 +423,19 @@ public function getFieldSqlExpression(Field $field, Expression $expression) public function lastInsertId(Model $model): string { + // TODO: Oracle does not support lastInsertId(), only for testing + // as this does not support concurrent inserts + if ($this->connection instanceof \atk4\dsql\Oracle\Connection) { + if ($model->id_field === false) { + return ''; // TODO code should never call lastInsertId() if id field is not defined + } + + $query = $this->connection->dsql()->table($model->table); + $query->field($query->expr('max({id_col})', ['id_col' => $model->id_field]), 'max_id'); + + return $query->getOne(); + } + return $this->connection->lastInsertId($this->getIdSequenceName($model)); }