Passed
Pull Request — master (#377)
by Wilmer
02:58
created

QueryBuilderProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 149
c 2
b 0
f 1
dl 0
loc 269
rs 10
wmc 1

1 Method

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