Skip to content

Commit 561b9a9

Browse files
Remove src\TestSupport\TestQueryBuilderTrait::class from yiisoft\db. (#149)
* Remove src\TestSupport\TestQueryBuilderTrait::class from yiisoft\db.
1 parent 84076aa commit 561b9a9

4 files changed

Lines changed: 642 additions & 392 deletions

File tree

src/DMLQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function resetSequence(string $tableName, int|string $value = null): stri
4141
$table = $this->schema->getTableSchema($tableName);
4242

4343
if ($table === null) {
44-
throw new InvalidArgumentException("Table not found: $tableName");
44+
throw new InvalidArgumentException("Table not found: '$tableName'.");
4545
}
4646

4747
$sequenceName = $table->getSequenceName();

tests/Provider/QueryBuilderProvider.php

Lines changed: 106 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@
55
namespace Yiisoft\Db\Sqlite\Tests\Provider;
66

77
use Yiisoft\Db\Expression\Expression;
8-
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
9-
use Yiisoft\Db\Query\Query;
10-
use Yiisoft\Db\Sqlite\Tests\TestCase;
11-
use Yiisoft\Db\TestSupport\Provider\QueryBuilderProvider as BaseQueryBuilderProvider;
12-
use Yiisoft\Db\TestSupport\TraversableObject;
8+
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
9+
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
10+
use Yiisoft\Db\Tests\Provider\AbstractQueryBuilderProvider;
11+
use Yiisoft\Db\Tests\Support\TraversableObject;
1312

14-
final class QueryBuilderProvider extends TestCase
13+
use function array_replace;
14+
15+
final class QueryBuilderProvider extends AbstractQueryBuilderProvider
1516
{
17+
use TestTrait;
18+
1619
protected string $likeEscapeCharSql = " ESCAPE '\\'";
20+
protected array $likeParameterReplacements = [];
1721

18-
public function batchInsertProvider(): array
22+
public function buildCondition(): array
1923
{
20-
return (new BaseQueryBuilderProvider($this->getConnection()))->batchInsertProvider();
21-
}
24+
$buildCondition = parent::buildCondition();
2225

23-
public function buildConditionsProvider(): array
24-
{
25-
return array_merge((new BaseQueryBuilderProvider($this->getConnection()))->buildConditionsProvider(), [
26+
unset($buildCondition['inCondition-custom-1'], $buildCondition['inCondition-custom-2']);
27+
28+
return array_merge($buildCondition, [
2629
'composite in using array objects' => [
27-
['in', new TraversableObject(['id', 'name']), new TraversableObject([
28-
['id' => 1, 'name' => 'oy'],
29-
['id' => 2, 'name' => 'yo'],
30-
])],
30+
[
31+
'in',
32+
new TraversableObject(['id', 'name']),
33+
new TraversableObject([['id' => 1, 'name' => 'oy'], ['id' => 2, 'name' => 'yo']]),
34+
],
3135
'(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))',
3236
[':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'],
3337
],
@@ -36,222 +40,132 @@ public function buildConditionsProvider(): array
3640
'(([[id]] = :qp0 AND [[name]] = :qp1))',
3741
[':qp0' => 1, ':qp1' => 'oy'],
3842
],
39-
]);
40-
}
41-
42-
public function buildExistsParamsProvider(): array
43-
{
44-
return (new BaseQueryBuilderProvider($this->getConnection()))->buildExistsParamsProvider();
45-
}
46-
47-
public function buildFilterConditionProvider(): array
48-
{
49-
return (new BaseQueryBuilderProvider($this->getConnection()))->buildFilterConditionProvider();
50-
}
51-
52-
public function buildFromDataProvider(): array
53-
{
54-
return (new BaseQueryBuilderProvider($this->getConnection()))->buildFromDataProvider();
55-
}
56-
57-
public function buildLikeConditionsProvider(): array
58-
{
59-
return (new BaseQueryBuilderProvider(
60-
$this->getConnection(),
61-
$this->likeEscapeCharSql,
62-
$this->likeParameterReplacements,
63-
))->buildLikeConditionsProvider();
64-
}
65-
66-
public function createDropIndexesProvider(): array
67-
{
68-
$result = (new BaseQueryBuilderProvider($this->getConnection()))->createDropIndexesProvider();
69-
$result['drop'][0] = 'DROP INDEX [[CN_constraints_2_single]]';
70-
$indexName = 'myindex';
71-
$schemaName = 'myschema';
72-
$tableName = 'mytable';
73-
$result['with schema'] = [
74-
"CREATE INDEX {{{$schemaName}}}.[[$indexName]] ON {{{$tableName}}} ([[C_index_1]])",
75-
static fn (QueryBuilderInterface $qb) => $qb->createIndex($indexName, $schemaName . '.' . $tableName, 'C_index_1'),
76-
];
77-
return $result;
78-
}
79-
80-
public function deleteProvider(): array
81-
{
82-
return (new BaseQueryBuilderProvider($this->getConnection()))->deleteProvider();
83-
}
84-
85-
public function insertProvider(): array
86-
{
87-
return [
88-
'regular-values' => [
89-
'customer',
90-
[
91-
'email' => 'test@example.com',
92-
'name' => 'silverfire',
93-
'address' => 'Kyiv {{city}}, Ukraine',
94-
'is_active' => false,
95-
'related_id' => null,
96-
],
97-
[],
98-
$this->replaceQuotes(
99-
'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]])'
100-
. ' VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)'
101-
),
102-
[
103-
':qp0' => 'test@example.com',
104-
':qp1' => 'silverfire',
105-
':qp2' => 'Kyiv {{city}}, Ukraine',
106-
':qp3' => false,
107-
':qp4' => null,
108-
],
43+
'composite in array values no exist' => [
44+
['in', ['id', 'name', 'email'], [['id' => 1, 'name' => 'oy']]],
45+
'(([[id]] = :qp0 AND [[name]] = :qp1 AND [[email]] IS NULL))',
46+
[':qp0' => 1, ':qp1' => 'oy'],
10947
],
110-
'params-and-expressions' => [
111-
'{{%type}}',
112-
[
113-
'{{%type}}.[[related_id]]' => null,
114-
'[[time]]' => new Expression('now()'),
115-
],
116-
[],
117-
'INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) VALUES (:qp0, now())',
118-
[
119-
':qp0' => null,
120-
],
48+
[
49+
['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
50+
'(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))',
51+
[':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'],
12152
],
122-
'carry passed params' => [
123-
'customer',
124-
[
125-
'email' => 'test@example.com',
126-
'name' => 'sergeymakinen',
127-
'address' => '{{city}}',
128-
'is_active' => false,
129-
'related_id' => null,
130-
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
131-
],
132-
[':phBar' => 'bar'],
133-
$this->replaceQuotes(
134-
'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]], [[col]])'
135-
. ' VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar))'
136-
),
137-
[
138-
':phBar' => 'bar',
139-
':qp1' => 'test@example.com',
140-
':qp2' => 'sergeymakinen',
141-
':qp3' => '{{city}}',
142-
':qp4' => false,
143-
':qp5' => null,
144-
':phFoo' => 'foo',
145-
],
53+
[
54+
['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]],
55+
'(([[id]] != :qp0 OR [[name]] != :qp1) AND ([[id]] != :qp2 OR [[name]] != :qp3))',
56+
[':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'],
57+
],
58+
'inCondition-custom-3' => [
59+
new InCondition(['id', 'name'], 'in', [['id' => 1]]),
60+
'(([[id]] = :qp0 AND [[name]] IS NULL))',
61+
[':qp0' => 1],
62+
],
63+
'inCondition-custom-4' => [
64+
new InCondition(['id', 'name'], 'in', [['name' => 'oy']]),
65+
'(([[id]] IS NULL AND [[name]] = :qp0))',
66+
[':qp0' => 'oy'],
67+
],
68+
'inCondition-custom-5' => [
69+
new InCondition(['id', 'name'], 'in', [['id' => 1, 'name' => 'oy']]),
70+
'(([[id]] = :qp0 AND [[name]] = :qp1))',
71+
[':qp0' => 1, ':qp1' => 'oy'],
14672
],
147-
'carry passed params (query)' => [
148-
'customer',
149-
(new Query($this->getConnection()))
150-
->select([
151-
'email',
152-
'name',
153-
'address',
154-
'is_active',
155-
'related_id',
156-
])
157-
->from('customer')
158-
->where([
159-
'email' => 'test@example.com',
160-
'name' => 'sergeymakinen',
161-
'address' => '{{city}}',
162-
'is_active' => false,
163-
'related_id' => null,
164-
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
165-
]),
166-
[':phBar' => 'bar'],
167-
$this->replaceQuotes(
168-
'INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]])'
169-
. ' SELECT [[email]], [[name]], [[address]], [[is_active]], [[related_id]] FROM [[customer]]'
170-
. ' WHERE ([[email]]=:qp1) AND ([[name]]=:qp2) AND ([[address]]=:qp3) AND ([[is_active]]=:qp4)'
171-
. ' AND ([[related_id]] IS NULL) AND ([[col]]=CONCAT(:phFoo, :phBar))'
172-
),
173-
[
174-
':phBar' => 'bar',
175-
':qp1' => 'test@example.com',
176-
':qp2' => 'sergeymakinen',
177-
':qp3' => '{{city}}',
178-
':qp4' => false,
179-
':phFoo' => 'foo',
180-
],
73+
'like-custom-1' => [['like', 'a', 'b'], '[[a]] LIKE :qp0 ESCAPE \'\\\'', [':qp0' => '%b%']],
74+
'like-custom-2' => [
75+
['like', 'a', new Expression(':qp0', [':qp0' => '%b%'])],
76+
'[[a]] LIKE :qp0 ESCAPE \'\\\'',
77+
[':qp0' => '%b%'],
18178
],
182-
];
79+
'like-custom-3' => [
80+
['like', new Expression('CONCAT(col1, col2)'), 'b'],
81+
'CONCAT(col1, col2) LIKE :qp0 ESCAPE \'\\\'',
82+
[':qp0' => '%b%'],
83+
],
84+
]);
18385
}
18486

185-
public function updateProvider(): array
87+
public function insert(): array
18688
{
187-
return [
188-
[
189-
'customer',
190-
[
191-
'status' => 1,
192-
'updated_at' => new Expression('now()'),
193-
],
194-
[
195-
'id' => 100,
196-
],
197-
$this->replaceQuotes(
198-
'UPDATE [[customer]] SET [[status]]=:qp0, [[updated_at]]=now() WHERE [[id]]=:qp1'
199-
),
200-
[
201-
':qp0' => 1,
202-
':qp1' => 100,
203-
],
204-
],
205-
];
89+
$insert = parent::insert();
90+
91+
$insert['empty columns'][3] = <<<SQL
92+
INSERT INTO `customer` DEFAULT VALUES
93+
SQL;
94+
95+
return $insert;
20696
}
20797

208-
public function upsertProvider(): array
98+
public function upsert(): array
20999
{
210100
$concreteData = [
211101
'regular values' => [
212-
3 => 'WITH "EXCLUDED" (`email`, `address`, `status`, `profile_id`) AS (VALUES (:qp0, :qp1, :qp2, :qp3)) UPDATE `T_upsert` SET `address`=(SELECT `address` FROM `EXCLUDED`), `status`=(SELECT `status` FROM `EXCLUDED`), `profile_id`=(SELECT `profile_id` FROM `EXCLUDED`) WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3);',
102+
3 => <<<SQL
103+
WITH "EXCLUDED" (`email`, `address`, `status`, `profile_id`) AS (VALUES (:qp0, :qp1, :qp2, :qp3)) UPDATE `T_upsert` SET `address`=(SELECT `address` FROM `EXCLUDED`), `status`=(SELECT `status` FROM `EXCLUDED`), `profile_id`=(SELECT `profile_id` FROM `EXCLUDED`) WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3);
104+
SQL,
213105
],
214106
'regular values with update part' => [
215-
3 => 'WITH "EXCLUDED" (`email`, `address`, `status`, `profile_id`) AS (VALUES (:qp0, :qp1, :qp2, :qp3)) UPDATE `T_upsert` SET `address`=:qp4, `status`=:qp5, `orders`=T_upsert.orders + 1 WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3);',
107+
3 => <<<SQL
108+
WITH "EXCLUDED" (`email`, `address`, `status`, `profile_id`) AS (VALUES (:qp0, :qp1, :qp2, :qp3)) UPDATE `T_upsert` SET `address`=:qp4, `status`=:qp5, `orders`=T_upsert.orders + 1 WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3);
109+
SQL,
216110
],
217111
'regular values without update part' => [
218-
3 => 'INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3)',
112+
3 => <<<SQL
113+
INSERT OR IGNORE INTO `T_upsert` (`email`, `address`, `status`, `profile_id`) VALUES (:qp0, :qp1, :qp2, :qp3)
114+
SQL,
219115
],
220116
'query' => [
221-
3 => 'WITH "EXCLUDED" (`email`, `status`) AS (SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1) UPDATE `T_upsert` SET `status`=(SELECT `status` FROM `EXCLUDED`) WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1;',
117+
3 => <<<SQL
118+
WITH "EXCLUDED" (`email`, `status`) AS (SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1) UPDATE `T_upsert` SET `status`=(SELECT `status` FROM `EXCLUDED`) WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1;
119+
SQL,
222120
],
223121
'query with update part' => [
224-
3 => 'WITH "EXCLUDED" (`email`, `status`) AS (SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1) UPDATE `T_upsert` SET `address`=:qp1, `status`=:qp2, `orders`=T_upsert.orders + 1 WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1;',
122+
3 => <<<SQL
123+
WITH "EXCLUDED" (`email`, `status`) AS (SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1) UPDATE `T_upsert` SET `address`=:qp1, `status`=:qp2, `orders`=T_upsert.orders + 1 WHERE `T_upsert`.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1;
124+
SQL,
225125
],
226126
'query without update part' => [
227-
3 => 'INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1',
127+
3 => <<<SQL
128+
INSERT OR IGNORE INTO `T_upsert` (`email`, `status`) SELECT `email`, 2 AS `status` FROM `customer` WHERE `name`=:qp0 LIMIT 1
129+
SQL,
228130
],
229131
'values and expressions' => [
230-
3 => 'INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())',
132+
3 => <<<SQL
133+
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
134+
SQL,
231135
],
232136
'values and expressions with update part' => [
233-
3 => 'INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())',
137+
3 => <<<SQL
138+
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
139+
SQL,
234140
],
235141
'values and expressions without update part' => [
236-
3 => 'INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())',
142+
3 => <<<SQL
143+
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
144+
SQL,
237145
],
238146
'query, values and expressions with update part' => [
239-
3 => 'WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];',
147+
3 => <<<SQL
148+
WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];
149+
SQL,
240150
],
241151
'query, values and expressions without update part' => [
242-
3 => 'WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];',
152+
3 => <<<SQL
153+
WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];
154+
SQL,
243155
],
244156
'no columns to update' => [
245-
3 => 'INSERT OR IGNORE INTO `T_upsert_1` (`a`) VALUES (:qp0)',
157+
3 => <<<SQL
158+
INSERT OR IGNORE INTO `T_upsert_1` (`a`) VALUES (:qp0)
159+
SQL,
246160
],
247161
];
248162

249-
$newData = (new BaseQueryBuilderProvider($this->getConnection()))->upsertProvider();
163+
$upsert = parent::upsert();
250164

251165
foreach ($concreteData as $testName => $data) {
252-
$newData[$testName] = array_replace($newData[$testName], $data);
166+
$upsert[$testName] = array_replace($upsert[$testName], $data);
253167
}
254168

255-
return $newData;
169+
return $upsert;
256170
}
257171
}

0 commit comments

Comments
 (0)