Skip to content

Commit

Permalink
Simplify insertEx (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdef committed Dec 11, 2022
1 parent 3d0744a commit 5cf0be8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/CommandPDO.php
Expand Up @@ -22,6 +22,7 @@ final class CommandPDO extends AbstractCommandPDO
public function insertEx(string $table, array $columns): bool|array
{
$params = [];

$sql = $this->queryBuilder()->insertEx($table, $columns, $params);

$this->setSql($sql)->bindValues($params);
Expand Down
104 changes: 88 additions & 16 deletions tests/Provider/QueryBuilderProvider.php
Expand Up @@ -4,7 +4,9 @@

namespace Yiisoft\Db\Mssql\Tests\Provider;

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
use Yiisoft\Db\Tests\Provider\AbstractQueryBuilderProvider;
use Yiisoft\Db\Tests\Support\TraversableObject;
Expand Down Expand Up @@ -90,22 +92,92 @@ public function insert(): array

public function insertEx(): array
{
$insertEx = parent::insertEx();

$insertEx['regular-values'][3] = <<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted VALUES (:qp0, :qp1, :qp2, :qp3, :qp4);SELECT * FROM @temporary_inserted;
SQL;
$insertEx['params-and-expressions'][3] = <<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal NULL, [time] datetime , [bool_col] tinyint , [bool_col2] tinyint NULL);INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[time],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SQL;
$insertEx['carry passed params'][3] = <<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id], [col]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar));SELECT * FROM @temporary_inserted;
SQL;
$insertEx['carry passed params (query)'][3] = <<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted SELECT [email], [name], [address], [is_active], [related_id] FROM [customer] WHERE ([email]=:qp1) AND ([name]=:qp2) AND ([address]=:qp3) AND ([is_active]=:qp4) AND ([related_id] IS NULL) AND ([col]=CONCAT(:phFoo, :phBar));SELECT * FROM @temporary_inserted;
SQL;

return $insertEx;
$db = $this->getConnection();

return [
'regular-values' => [
'customer',
[
'email' => 'test@example.com',
'name' => 'silverfire',
'address' => 'Kyiv {{city}}, Ukraine',
'is_active' => false,
'related_id' => null,
],
[],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted VALUES (:qp0, :qp1, :qp2, :qp3, :qp4);SELECT * FROM @temporary_inserted;
SQL,
[
':qp0' => 'test@example.com',
':qp1' => 'silverfire',
':qp2' => 'Kyiv {{city}}, Ukraine',
':qp3' => false,
':qp4' => null,
],
],
'params-and-expressions' => [
'{{%type}}',
['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')],
[],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal NULL, [time] datetime , [bool_col] tinyint , [bool_col2] tinyint NULL);INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[time],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SQL,
[':qp0' => null],
],
'carry passed params' => [
'customer',
[
'email' => 'test@example.com',
'name' => 'sergeymakinen',
'address' => '{{city}}',
'is_active' => false,
'related_id' => null,
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
],
[':phBar' => 'bar'],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id], [col]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar));SELECT * FROM @temporary_inserted;
SQL,
[
':phBar' => 'bar',
':qp1' => 'test@example.com',
':qp2' => 'sergeymakinen',
':qp3' => '{{city}}',
':qp4' => false,
':qp5' => null,
':phFoo' => 'foo',
],
],
'carry passed params (query)' => [
'customer',
(new Query($db))
->select(['email', 'name', 'address', 'is_active', 'related_id'])
->from('customer')
->where(
[
'email' => 'test@example.com',
'name' => 'sergeymakinen',
'address' => '{{city}}',
'is_active' => false,
'related_id' => null,
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
],
),
[':phBar' => 'bar'],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([id] int , [email] varchar(128) , [name] varchar(128) NULL, [address] text NULL, [status] int NULL, [profile_id] int NULL);INSERT INTO [customer] ([email], [name], [address], [is_active], [related_id]) OUTPUT INSERTED.[id],INSERTED.[email],INSERTED.[name],INSERTED.[address],INSERTED.[status],INSERTED.[profile_id] INTO @temporary_inserted SELECT [email], [name], [address], [is_active], [related_id] FROM [customer] WHERE ([email]=:qp1) AND ([name]=:qp2) AND ([address]=:qp3) AND ([is_active]=:qp4) AND ([related_id] IS NULL) AND ([col]=CONCAT(:phFoo, :phBar));SELECT * FROM @temporary_inserted;
SQL,
[
':phBar' => 'bar',
':qp1' => 'test@example.com',
':qp2' => 'sergeymakinen',
':qp3' => '{{city}}',
':qp4' => false,
':phFoo' => 'foo',
],
],
];
}

public function selectExist(): array
Expand Down

0 comments on commit 5cf0be8

Please sign in to comment.