Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
- Chg #980: Add constructor with DB connection to `AbstractCommand` (@vjik)
- Enh #979: Allow `ExpressionInterface` for column definitions when create table (@Tigrov)
- Enh #981, #1004: Refactor constraints (@Tigrov)
- Enh #982, #995: Reduce binding parameters, add `QueryBuilderInterface::buildValue()` method (@Tigrov)
- Enh #982, #995, #1044: Reduce binding parameters, add `QueryBuilderInterface::buildValue()` method (@Tigrov, @vjik)
- New #984: Add `createQuery()` and `select()` methods to `ConnectionInterface` (@Tigrov)
- Chg #985: Rename `insertWithReturningPks()` to `insertReturningPks()` in `CommandInterface` and `DMLQueryBuilderInterface` (@Tigrov)
- Enh #992: Add optional type casting to `DataReaderInterface` using columns (@Tigrov)
Expand Down
7 changes: 1 addition & 6 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
if (isset($tableColumns[$name])) {
$value = $tableColumns[$name]->dbTypecast($value);
}

if ($value instanceof ExpressionInterface) {
$placeholders[] = $this->queryBuilder->buildExpression($value, $params);
} else {
$placeholders[] = $this->queryBuilder->bindParam($value, $params);
}
$placeholders[] = $this->queryBuilder->buildValue($value, $params);
}

return [array_keys($columns), $placeholders, '', $params];
Expand Down
12 changes: 6 additions & 6 deletions tests/Common/CommonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,12 +1289,12 @@ public function testInsertWithoutTypecasting(): void

$command->insert('{{type}}', $values);

$this->assertSame([
':qp0' => 1,
':qp1' => 'test',
':qp2' => 3.14,
':qp3' => $db->getDriverName() === 'oci' ? '1' : true,
], $command->getParams());
$this->assertSame(
$db->getDriverName() === 'oci'
? [':qp0' => 'test', ':qp1' => '1']
: [':qp0' => 'test'],
$command->getParams(),
);

$command = $command->withDbTypecasting(false);
$command->insert('{{type}}', $values);
Expand Down
22 changes: 11 additions & 11 deletions tests/Common/CommonQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ public function testInsertWithoutTypecasting(): void
$params = [];
$qb->insert('{{type}}', $values, $params);

$this->assertSame([
':qp0' => 1,
':qp1' => 'test',
':qp2' => 3.14,
':qp3' => $db->getDriverName() === 'oci' ? '1' : true,
], $params);
$this->assertEquals(
$db->getDriverName() === 'oci'
? [':qp0' => new Param('test', DataType::STRING), ':qp1' => new Param('1', DataType::STRING)]
: [':qp0' => new Param('test', DataType::STRING)],
$params
);

$params = [];
$qb->withTypecasting(false)->insert('{{type}}', $values, $params);

$this->assertSame([
':qp0' => '1',
':qp1' => 'test',
':qp2' => '3.14',
':qp3' => '1',
$this->assertEquals([
':qp0' => new Param('1', DataType::STRING),
':qp1' => new Param('test', DataType::STRING),
':qp2' => new Param('3.14', DataType::STRING),
':qp3' => new Param('1', DataType::STRING),
], $params);

$db->close();
Expand Down
59 changes: 31 additions & 28 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,15 +1052,13 @@ public static function insert(): array
[],
static::replaceQuotes(
<<<SQL
INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)
INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) VALUES (:qp0, :qp1, :qp2, FALSE, NULL)
SQL
),
[
':qp0' => 'test@example.com',
':qp1' => 'silverfire',
':qp2' => 'Kyiv {{city}}, Ukraine',
':qp3' => false,
':qp4' => null,
':qp0' => new Param('test@example.com', DataType::STRING),
':qp1' => new Param('silverfire', DataType::STRING),
':qp2' => new Param('Kyiv {{city}}, Ukraine', DataType::STRING),
],
],
'params-and-expressions' => [
Expand All @@ -1069,10 +1067,10 @@ public static function insert(): array
[],
static::replaceQuotes(
<<<SQL
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (:qp0, now())
INSERT INTO {{%type}} ([[related_id]], [[time]]) VALUES (NULL, now())
SQL
),
[':qp0' => null],
[],
],
'carry passed params' => [
'customer',
Expand All @@ -1087,16 +1085,14 @@ public static function insert(): array
[':phBar' => 'bar'],
static::replaceQuotes(
<<<SQL
INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]], [[col]]) VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar))
INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]], [[col]]) VALUES (:qp1, :qp2, :qp3, FALSE, NULL, CONCAT(:phFoo, :phBar))
SQL
),
[
':phBar' => 'bar',
':qp1' => 'test@example.com',
':qp2' => 'sergeymakinen',
':qp3' => '{{city}}',
':qp4' => false,
':qp5' => null,
':qp1' => new Param('test@example.com', DataType::STRING),
':qp2' => new Param('sergeymakinen', DataType::STRING),
':qp3' => new Param('{{city}}', DataType::STRING),
':phFoo' => 'foo',
],
],
Expand Down Expand Up @@ -1437,34 +1433,41 @@ public static function upsert(): array
['email' => 'test@example.com', 'address' => 'bar {{city}}', 'status' => 1, 'profile_id' => null],
true,
'',
[':qp0' => 'test@example.com', ':qp1' => 'bar {{city}}', ':qp2' => 1, ':qp3' => null],
[
':qp0' => new Param('test@example.com', DataType::STRING),
':qp1' => new Param('bar {{city}}', DataType::STRING),
],
],
'regular values with unique at not the first position' => [
'T_upsert',
['address' => 'bar {{city}}', 'email' => 'test@example.com', 'status' => 1, 'profile_id' => null],
true,
'',
[':qp0' => 'bar {{city}}', ':qp1' => 'test@example.com', ':qp2' => 1, ':qp3' => null],
[
':qp0' => new Param('bar {{city}}', DataType::STRING),
':qp1' => new Param('test@example.com', DataType::STRING),
],
],
'regular values with update part' => [
'T_upsert',
['email' => 'test@example.com', 'address' => 'bar {{city}}', 'status' => 1, 'profile_id' => null],
['address' => 'foo {{city}}', 'status' => 2, 'orders' => new Expression('T_upsert.orders + 1')],
'',
[
':qp0' => 'test@example.com',
':qp1' => 'bar {{city}}',
':qp2' => 1,
':qp3' => null,
':qp4' => new Param('foo {{city}}', DataType::STRING),
':qp0' => new Param('test@example.com', DataType::STRING),
':qp1' => new Param('bar {{city}}', DataType::STRING),
':qp2' => new Param('foo {{city}}', DataType::STRING),
],
],
'regular values without update part' => [
'T_upsert',
['email' => 'test@example.com', 'address' => 'bar {{city}}', 'status' => 1, 'profile_id' => null],
false,
'',
[':qp0' => 'test@example.com', ':qp1' => 'bar {{city}}', ':qp2' => 1, ':qp3' => null],
[
':qp0' => new Param('test@example.com', DataType::STRING),
':qp1' => new Param('bar {{city}}', DataType::STRING),
],
],
'query' => [
'T_upsert',
Expand Down Expand Up @@ -1507,21 +1510,21 @@ public static function upsert(): array
['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('CURRENT_TIMESTAMP')],
true,
'',
[':qp0' => 'dynamic@example.com'],
[':qp0' => new Param('dynamic@example.com', DataType::STRING)],
],
'values and expressions with update part' => [
'{{%T_upsert}}',
['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('CURRENT_TIMESTAMP')],
['[[orders]]' => new Expression('T_upsert.orders + 1')],
'',
[':qp0' => 'dynamic@example.com'],
[':qp0' => new Param('dynamic@example.com', DataType::STRING)],
],
'values and expressions without update part' => [
'T_upsert',
['{{%T_upsert}}.[[email]]' => 'dynamic@example.com', '[[ts]]' => new Expression('CURRENT_TIMESTAMP')],
false,
'',
[':qp0' => 'dynamic@example.com'],
[':qp0' => new Param('dynamic@example.com', DataType::STRING)],
],
'query, values and expressions with update part' => [
'{{%T_upsert}}',
Expand Down Expand Up @@ -1554,21 +1557,21 @@ public static function upsert(): array
['a' => 1],
false,
'',
[':qp0' => 1],
[],
],
'no columns to update with unique' => [
'T_upsert',
['email' => 'email'],
true,
'',
[':qp0' => 'email'],
[':qp0' => new Param('email', DataType::STRING)],
],
'no unique columns in table - simple insert' => [
'{{%animal}}',
['type' => 'test'],
false,
'',
[':qp0' => 'test'],
[':qp0' => new Param('test', DataType::STRING)],
],
];
}
Expand Down