Passed
Pull Request — master (#377)
by Wilmer
04:35 queued 01:43
created

QueryBuilderProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 152
c 4
b 0
f 1
dl 0
loc 272
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B buildConditions() 0 270 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Provider;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\QueryBuilder\Conditions\BetweenColumnsCondition;
9
use Yiisoft\Db\QueryBuilder\Conditions\InCondition;
10
use Yiisoft\Db\Tests\Support\Mock;
11
use Yiisoft\Db\Tests\Support\TraversableObject;
12
13
abstract class QueryBuilderProvider
14
{
15
    public function buildConditions(): array
16
    {
17
        $mock = new Mock();
18
19
        return [
20
            /* empty values */
21
            [['like', 'name', []], '0=1', []],
22
            [['not like', 'name', []], '', []],
23
            [['or like', 'name', []], '0=1', []],
24
            [['or not like', 'name', []], '', []],
25
26
            /* not */
27
            [['not', 'name'], 'NOT (name)', []],
28
            [
29
                [
30
                    'not',
31
                    $mock->query()->select('exists')->from('some_table'),
32
                ],
33
                'NOT ((SELECT [[exists]] FROM [[some_table]]))', [],
34
            ],
35
36
            /* and */
37
            [['and', '', ''], '', []],
38
            [['and', '', 'id=2'], 'id=2', []],
39
            [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []],
40
            [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []],
41
            [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]],
42
            [
43
                [
44
                    'and',
45
                    ['expired' => false],
46
                    $mock->query()->select('count(*) > 1')->from('queue'),
47
                ],
48
                '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))',
49
                [':qp0' => false],
50
            ],
51
52
            /* or */
53
            [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []],
54
            [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []],
55
            [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]],
56
57
            /* between */
58
            [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]],
59
            [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]],
60
            [
61
                ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')],
62
                '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()',
63
                [],
64
            ],
65
            [
66
                ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123],
67
                '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0',
68
                [':qp0' => 123],
69
            ],
70
            [
71
                ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')],
72
                '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()',
73
                [],
74
            ],
75
            [
76
                ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123],
77
                '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0',
78
                [':qp0' => 123],
79
            ],
80
            [
81
                new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'),
82
                ':qp0 BETWEEN [[create_time]] AND [[update_time]]',
83
                [':qp0' => '2018-02-11'],
84
            ],
85
            [
86
                new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'),
87
                ':qp0 NOT BETWEEN NOW() AND [[update_time]]',
88
                [':qp0' => '2018-02-11'],
89
            ],
90
            [
91
                new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'),
92
                'NOW() BETWEEN [[create_time]] AND [[update_time]]',
93
                [],
94
            ],
95
            [
96
                new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'),
97
                'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]',
98
                [],
99
            ],
100
            [
101
                new BetweenColumnsCondition(
102
                    new Expression('NOW()'),
103
                    'NOT BETWEEN',
104
                    $mock->query()->select('min_date')->from('some_table'),
105
                    'max_date'
106
                ),
107
                'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]',
108
                [],
109
            ],
110
            [
111
                new BetweenColumnsCondition(
112
                    new Expression('NOW()'),
113
                    'NOT BETWEEN',
114
                    new Expression('min_date'),
115
                    $mock->query()->select('max_date')->from('some_table'),
116
                ),
117
                'NOW() NOT BETWEEN min_date AND (SELECT [[max_date]] FROM [[some_table]])',
118
                [],
119
            ],
120
121
            /* in */
122
            [
123
                ['in', 'id', [1, 2, $mock->query()->select('three')->from('digits')]],
124
                '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))',
125
                [':qp0' => 1, ':qp1' => 2],
126
            ],
127
            [
128
                ['not in', 'id', [1, 2, 3]],
129
                '[[id]] NOT IN (:qp0, :qp1, :qp2)',
130
                [':qp0' => 1, ':qp1' => 2, ':qp2' => 3],
131
            ],
132
            [
133
                ['in', 'id', $mock->query()->select('id')->from('users')->where(['active' => 1])],
134
                '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
135
                [':qp0' => 1],
136
            ],
137
            [
138
                ['not in', 'id', $mock->query()->select('id')->from('users')->where(['active' => 1])],
139
                '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
140
                [':qp0' => 1],
141
            ],
142
            [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]],
143
            [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]],
144
            [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]],
145
            'composite in' => [
146
                ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]],
147
                '([[id]], [[name]]) IN ((:qp0, :qp1))',
148
                [':qp0' => 1, ':qp1' => 'oy'],
149
            ],
150
            'composite in (just one column)' => [
151
                ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]],
152
                '[[id]] IN (:qp0, :qp1)',
153
                [':qp0' => 1, ':qp1' => 2],
154
            ],
155
            'composite in using array objects (just one column)' => [
156
                [
157
                    'in',
158
                    new TraversableObject(['id']),
159
                    new TraversableObject([['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]),
160
                ],
161
                '[[id]] IN (:qp0, :qp1)',
162
                [':qp0' => 1, ':qp1' => 2],
163
            ],
164
165
            /* in using array objects. */
166
            [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
167
            [
168
                ['in', 'id', new TraversableObject([1, 2, 3])],
169
                '[[id]] IN (:qp0, :qp1, :qp2)',
170
                [':qp0' => 1, ':qp1' => 2, ':qp2' => 3],
171
            ],
172
173
            /* in using array objects containing null value */
174
            [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]],
175
            [
176
                ['in', 'id', new TraversableObject([1, 2, null])],
177
                '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2],
178
            ],
179
180
            /* not in using array object containing null value */
181
            [
182
                ['not in', 'id', new TraversableObject([1, null])],
183
                '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1],
184
            ],
185
            [
186
                ['not in', 'id', new TraversableObject([1, 2, null])],
187
                '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL',
188
                [':qp0' => 1, ':qp1' => 2],
189
            ],
190
191
            /* in using array object containing only null value */
192
            [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []],
193
            [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []],
194
            'composite in using array objects' => [
195
                [
196
                    'in',
197
                    new TraversableObject(['id', 'name']),
198
                    new TraversableObject([['id' => 1, 'name' => 'oy'], ['id' => 2, 'name' => 'yo']]),
199
                ],
200
                '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))',
201
                [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'],
202
            ],
203
204
            /* in object conditions */
205
            [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]],
206
            [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]],
207
            [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]],
208
            [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]],
209
            [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
210
            [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]],
211
212
            /* exists */
213
            [
214
                [
215
                    'exists',
216
                    $mock->query()->select('id')->from('users')->where(['active' => 1]),
217
                ],
218
                'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)',
219
                [':qp0' => 1],
220
            ],
221
            [
222
                ['not exists', $mock->query()->select('id')->from('users')->where(['active' => 1])],
223
                'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1],
224
            ],
225
226
            /* simple conditions */
227
            [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']],
228
            [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]],
229
            [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']],
230
            [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]],
231
            [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']],
232
            [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]],
233
            [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']],
234
            [
235
                ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')],
236
                '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)',
237
                [],
238
            ],
239
            [
240
                ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])],
241
                '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)',
242
                [':month' => 2],
243
            ],
244
            [
245
                ['=', 'date', $mock->query()->select('max(date)')->from('test')->where(['id' => 5])],
246
                '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)',
247
                [':qp0' => 5],
248
            ],
249
250
            /* operand1 is Expression */
251
            [
252
                ['=', new Expression('date'), '2019-08-01'],
253
                'date = :qp0',
254
                [':qp0' => '2019-08-01'],
255
            ],
256
            [
257
                ['=', $mock->query()->select('COUNT(*)')->from('test')->where(['id' => 6]), 0],
258
                '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1',
259
                [':qp0' => 6, ':qp1' => 0],
260
            ],
261
262
            /* hash condition */
263
            [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]],
264
            [
265
                ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2],
266
                '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)',
267
                [':qp0' => 2],
268
            ],
269
            [['a' => null], '[[a]] IS NULL', []],
270
271
            /* direct conditions */
272
            ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []],
273
            [
274
                new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']),
275
                'a = CONCAT(col1, :param1)',
276
                ['param1' => 'value1'],
277
            ],
278
279
            /* Expression with params as operand of 'not' */
280
            [
281
                ['not', new Expression('any_expression(:a)', [':a' => 1])],
282
                'NOT (any_expression(:a))', [':a' => 1],
283
            ],
284
            [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]],
285
        ];
286
    }
287
}
288