Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor insert default values #300

Merged
merged 4 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
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))->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);
}
}
Loading