Skip to content

Commit efc5cc6

Browse files
committed
Improve
1 parent 4d45676 commit efc5cc6

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/Command.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,44 @@ final class Command extends AbstractPdoCommand
2727
{
2828
public function insertWithReturningPks(string $table, array|QueryInterface $columns): array|false
2929
{
30+
$tableSchema = $this->db->getSchema()->getTableSchema($table);
31+
$primaryKeys = $tableSchema?->getPrimaryKey() ?? [];
32+
$tableColumns = $tableSchema?->getColumns() ?? [];
33+
34+
foreach ($primaryKeys as $name) {
35+
/** @var ColumnInterface $column */
36+
$column = $tableColumns[$name];
37+
38+
if ($column->isAutoIncrement()) {
39+
continue;
40+
}
41+
42+
if ($columns instanceof QueryInterface) {
43+
throw new NotSupportedException(
44+
__METHOD__ . '() is not supported by Sqlite for tables without auto increment when inserting sub-query.'
45+
);
46+
}
47+
48+
break;
49+
}
50+
3051
$params = [];
31-
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
32-
$this->setSql($sql)->bindValues($params);
52+
$insertSql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
53+
$this->setSql($insertSql)->bindValues($params);
3354

3455
if ($this->execute() === 0) {
3556
return false;
3657
}
3758

38-
$tableSchema = $this->db->getSchema()->getTableSchema($table);
39-
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
40-
41-
if (empty($tablePrimaryKeys)) {
59+
if (empty($primaryKeys)) {
4260
return [];
4361
}
4462

45-
if ($columns instanceof QueryInterface) {
46-
throw new NotSupportedException(__METHOD__ . '() not supported for QueryInterface by SQLite.');
47-
}
48-
4963
$result = [];
5064

51-
/** @var TableSchema $tableSchema */
52-
foreach ($tablePrimaryKeys as $name) {
65+
foreach ($primaryKeys as $name) {
5366
/** @var ColumnInterface $column */
54-
$column = $tableSchema->getColumn($name);
67+
$column = $tableColumns[$name];
5568

5669
if ($column->isAutoIncrement()) {
5770
$value = $this->db->getLastInsertId();

tests/CommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Yiisoft\Db\Exception\Exception;
1111
use Yiisoft\Db\Exception\InvalidConfigException;
1212
use Yiisoft\Db\Exception\NotSupportedException;
13+
use Yiisoft\Db\Query\Query;
1314
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
1415
use Yiisoft\Db\Tests\Common\CommonCommandTest;
1516

@@ -326,6 +327,21 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql
326327
parent::testGetRawSql($sql, $params, $expectedRawSql);
327328
}
328329

330+
public function testInsertWithReturningPksWithSubqueryAndNoAutoincrement(): void
331+
{
332+
$db = $this->getConnection(true);
333+
$command = $db->createCommand();
334+
335+
$query = (new Query($db))->select(['order_id' => 1, 'item_id' => 2, 'quantity' => 3, 'subtotal' => 4]);
336+
337+
$this->expectException(NotSupportedException::class);
338+
$this->expectExceptionMessage(
339+
'Yiisoft\Db\Sqlite\Command::insertWithReturningPks() is not supported by Sqlite for tables without auto increment when inserting sub-query.'
340+
);
341+
342+
$command->insertWithReturningPks('order_item', $query);
343+
}
344+
329345
/**
330346
* @throws Throwable
331347
* @throws InvalidConfigException

0 commit comments

Comments
 (0)