Skip to content

Commit

Permalink
Fix #300: Refactor insert default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Aug 26, 2023
1 parent 77c68ba commit 6721db1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 1.0.2 under development

- Chg #297: Remove `QueryBuilder::getColumnType()` child method as legacy code (@Tigrov)
- Enh #300: Refactor insert default values (@Tigrov)

## 1.0.1 July 24, 2023

Expand Down
26 changes: 3 additions & 23 deletions src/DMLQueryBuilder.php
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
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
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
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))->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 6721db1

Please sign in to comment.