Skip to content

Commit

Permalink
Refactor insert default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Aug 17, 2023
1 parent 7081df0 commit 1ca0b91
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
26 changes: 3 additions & 23 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function insert(): array
$insert = parent::insert();

$insert['empty columns'][3] = <<<SQL
INSERT INTO `customer` (`id`) VALUES (NULL)
INSERT INTO `customer` () VALUES ()
SQL;

return $insert;
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ public function testDefaultValues(): void
// Primary key columns should have NULL as value
$this->assertSame(
<<<SQL
INSERT INTO `null_values` (`id`) VALUES (NULL)
INSERT INTO `null_values` () VALUES ()
SQL,
$qb->insert('null_values', []),
);

// Non-primary key columns should have DEFAULT as value
$this->assertSame(
<<<SQL
INSERT INTO `negative_default_values` (`tinyint_col`) VALUES (DEFAULT)
INSERT INTO `negative_default_values` () VALUES ()
SQL,
$qb->insert('negative_default_values', []),
);
Expand Down
20 changes: 20 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1ca0b91

Please sign in to comment.