From 1ca0b9123acde8a97436d47e4214cea7167c6245 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 17 Aug 2023 08:21:37 +0700 Subject: [PATCH] Refactor insert default values --- src/DMLQueryBuilder.php | 26 +++---------------------- tests/Provider/QueryBuilderProvider.php | 2 +- tests/QueryBuilderTest.php | 4 ++-- tests/SchemaTest.php | 20 +++++++++++++++++++ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 0fb07112..6a3e3b5f 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -119,30 +119,10 @@ public function upsert( */ protected function prepareInsertValues(string $table, QueryInterface|array $columns, array $params = []): array { - /** - * @psalm-var array $names - * @psalm-var array $placeholders - */ - [$names, $placeholders, $values, $params] = parent::prepareInsertValues($table, $columns, $params); - - if (!$columns instanceof QueryInterface && empty($names)) { - $tableSchema = $this->schema->getTableSchema($table); - - if ($tableSchema !== null) { - if (!empty($tableSchema->getPrimaryKey())) { - $columns = $tableSchema->getPrimaryKey(); - $defaultValue = 'NULL'; - } else { - $columns = [current($tableSchema->getColumns())->getName()]; - $defaultValue = 'DEFAULT'; - } - foreach ($columns as $name) { - $names[] = $this->quoter->quoteColumnName($name); - $placeholders[] = $defaultValue; - } - } + if (empty($columns)) { + return [[], [], ' () VALUES ()', []]; } - return [$names, $placeholders, $values, $params]; + return parent::prepareInsertValues($table, $columns, $params); } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 97be6006..30f91a54 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -105,7 +105,7 @@ public static function insert(): array $insert = parent::insert(); $insert['empty columns'][3] = <<assertSame( <<insert('null_values', []), ); @@ -328,7 +328,7 @@ public function testDefaultValues(): void // Non-primary key columns should have DEFAULT as value $this->assertSame( <<insert('negative_default_values', []), ); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index ce095a4a..0e5daa15 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -18,6 +18,7 @@ use Yiisoft\Db\Mysql\ColumnSchema; use Yiisoft\Db\Mysql\Schema; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; +use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonSchemaTest; use Yiisoft\Db\Tests\Support\Assert; @@ -537,4 +538,23 @@ public function testNotConnectionPDO(): void $schema->refreshTableSchema('customer'); } + + public function testInsertDefaultValues() + { + $db = $this->getConnection(true); + $command = $db->createCommand(); + + $command->insert('negative_default_values', [])->execute(); + + $row = (new Query($db))->select('*')->from('negative_default_values')->one(); + + $this->assertSame([ + 'tinyint_col' => '-123', + 'smallint_col' => '-123', + 'int_col' => '-123', + 'bigint_col' => '-123', + 'float_col' => '-12345.6789', + 'numeric_col' => '-33.22', + ], $row); + } }