@@ -27,31 +27,44 @@ final class Command extends AbstractPdoCommand
27
27
{
28
28
public function insertWithReturningPks (string $ table , array |QueryInterface $ columns ): array |false
29
29
{
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
+
30
51
$ 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 );
33
54
34
55
if ($ this ->execute () === 0 ) {
35
56
return false ;
36
57
}
37
58
38
- $ tableSchema = $ this ->db ->getSchema ()->getTableSchema ($ table );
39
- $ tablePrimaryKeys = $ tableSchema ?->getPrimaryKey() ?? [];
40
-
41
- if (empty ($ tablePrimaryKeys )) {
59
+ if (empty ($ primaryKeys )) {
42
60
return [];
43
61
}
44
62
45
- if ($ columns instanceof QueryInterface) {
46
- throw new NotSupportedException (__METHOD__ . '() not supported for QueryInterface by SQLite. ' );
47
- }
48
-
49
63
$ result = [];
50
64
51
- /** @var TableSchema $tableSchema */
52
- foreach ($ tablePrimaryKeys as $ name ) {
65
+ foreach ($ primaryKeys as $ name ) {
53
66
/** @var ColumnInterface $column */
54
- $ column = $ tableSchema -> getColumn ( $ name) ;
67
+ $ column = $ tableColumns [ $ name] ;
55
68
56
69
if ($ column ->isAutoIncrement ()) {
57
70
$ value = $ this ->db ->getLastInsertId ();
0 commit comments