Passed
Pull Request — master (#399)
by
unknown
04:41 queued 01:23
created

SelectStatementTest::testBuilderOrderByNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Builder;
6
7
use PhpMyAdmin\SqlParser\Parser;
8
use PhpMyAdmin\SqlParser\Tests\TestCase;
9
10
class SelectStatementTest extends TestCase
11
{
12
    public function testBuilder(): void
13
    {
14
        $query = 'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
15
            . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)';
16
17
        $parser = new Parser($query);
18
        $stmt = $parser->statements[0];
19
20
        $this->assertEquals(
21
            'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
22
            . 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)',
23
            $stmt->build()
24
        );
25
26
        $parser = new Parser('SELECT NULL IS NULL');
27
        $stmt = $parser->statements[0];
28
29
        $this->assertEquals('SELECT NULL IS NULL', $stmt->build());
30
31
        $parser = new Parser('SELECT NOT 1');
32
        $stmt = $parser->statements[0];
33
34
        $this->assertEquals('SELECT NOT 1', $stmt->build());
35
36
        $parser = new Parser('SELECT 1 BETWEEN 0 AND 2');
37
        $stmt = $parser->statements[0];
38
39
        $this->assertEquals('SELECT 1 BETWEEN 0 AND 2', $stmt->build());
40
    }
41
42
    public function testBuilderUnion(): void
43
    {
44
        $parser = new Parser('SELECT 1 UNION SELECT 2');
45
        $stmt = $parser->statements[0];
46
47
        $this->assertEquals(
48
            'SELECT 1 UNION SELECT 2',
49
            $stmt->build()
50
        );
51
    }
52
53
    public function testBuilderWithIsNull(): void
54
    {
55
        $parser = new Parser('SELECT `test3`.`t1` is not null AS `is_not_null` FROM `test3` ;');
56
        $stmt = $parser->statements[0];
57
58
        $this->assertEquals('SELECT `test3`.`t1` is not null AS `is_not_null` FROM `test3`', $stmt->build());
59
60
        $parser = new Parser('SELECT test3.t1 is null AS `col1` FROM test3');
61
        $stmt = $parser->statements[0];
62
63
        $this->assertEquals('SELECT test3.t1 is null AS `col1` FROM test3', $stmt->build());
64
    }
65
66
    public function testBuilderOrderByNull(): void
67
    {
68
        $query = 'SELECT * FROM some_table ORDER BY some_col IS NULL DESC;';
69
        $parser = new Parser($query);
70
        $stmt = $parser->statements[0];
71
72
        $this->assertEquals('SELECT * FROM some_table ORDER BY some_col IS NULL DESC', $stmt->build());
73
74
        $query = 'SELECT * FROM some_table ORDER BY some_col IS NOT NULL;';
75
        $parser = new Parser($query);
76
        $stmt = $parser->statements[0];
77
78
        $this->assertEquals('SELECT * FROM some_table ORDER BY some_col IS NOT NULL ASC', $stmt->build());
79
    }
80
81
    public function testBuilderAlias(): void
82
    {
83
        $parser = new Parser(
84
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
85
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
86
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
87
            . 'ORDER BY scb.id DESC LIMIT 0,300'
88
        );
89
        $stmt = $parser->statements[0];
90
91
        $this->assertEquals(
92
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
93
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
94
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
95
            . 'ORDER BY scb.id DESC LIMIT 0, 300',
96
            $stmt->build()
97
        );
98
    }
99
100
    public function testBuilderAliasOrder(): void
101
    {
102
        $parser = new Parser(
103
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
104
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
105
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
106
            . 'ORDER BY scb.id LIMIT 0,300'
107
        );
108
        $stmt = $parser->statements[0];
109
110
        $this->assertEquals(
111
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
112
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
113
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
114
            . 'ORDER BY scb.id ASC LIMIT 0, 300',
115
            $stmt->build()
116
        );
117
    }
118
119
    public function testBuilderAliasOrderMultiple(): void
120
    {
121
        $parser = new Parser(
122
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
123
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
124
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
125
            . 'ORDER BY scb.id DESC, scb.order LIMIT 0,300'
126
        );
127
        $stmt = $parser->statements[0];
128
129
        $this->assertEquals(
130
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
131
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
132
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
133
            . 'ORDER BY scb.id DESC, scb.order ASC LIMIT 0, 300',
134
            $stmt->build()
135
        );
136
    }
137
138
    public function testBuilderAliasOrderMultipleFunctions(): void
139
    {
140
        $parser = new Parser(
141
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
142
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
143
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
144
            . 'ORDER BY scb.id DESC, YEAR(scb.dob) LIMIT 0,300'
145
        );
146
        $stmt = $parser->statements[0];
147
148
        $this->assertEquals(
149
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
150
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
151
            . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
152
            . 'ORDER BY scb.id DESC, YEAR(scb.dob) ASC LIMIT 0, 300',
153
            $stmt->build()
154
        );
155
    }
156
157
    public function testBuilderAliasGroupByMultipleFunctions(): void
158
    {
159
        $parser = new Parser(
160
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
161
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
162
            . 'WHERE `has_found_course` = \'1\' '
163
            . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0,300'
164
        );
165
        $stmt = $parser->statements[0];
166
167
        $this->assertEquals(
168
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
169
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
170
            . 'WHERE `has_found_course` = \'1\' '
171
            . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
172
            $stmt->build()
173
        );
174
    }
175
176
    public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved(): void
177
    {
178
        $parser = new Parser(
179
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
180
            . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
181
            . 'WHERE `has_found_course` = \'1\' '
182
            . 'GROUP BY scb.id ASC, YEAR(scb.dob) DESC LIMIT 0,300'
183
        );
184
        $stmt = $parser->statements[0];
185
186
        // The order is not kept, is this an expected behavior ?
187
        // Ref: 4af06d24b041e499fb0e75ab3a98caf9a91700ef
188
        // Issue: #154
189
        $this->assertEquals(
190
            'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
191
            . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
192
            . 'WHERE `has_found_course` = \'1\' '
193
            . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
194
            $stmt->build()
195
        );
196
    }
197
198
    public function testBuilderAliasOrderCase(): void
199
    {
200
        $parser = new Parser(
201
            'SELECT * FROM `world_borders` ORDER BY CASE '
202
            . 'WHEN REGION = 2 THEN 99 '
203
            . 'WHEN REGION > 3 THEN REGION+1 '
204
            . 'ELSE 100 END LIMIT 0,300'
205
        );
206
        $stmt = $parser->statements[0];
207
208
        $this->assertEquals(
209
            'SELECT * FROM `world_borders` ORDER BY CASE '
210
            . 'WHEN REGION = 2 THEN 99 '
211
            . 'WHEN REGION > 3 THEN REGION+1 '
212
            . 'ELSE 100 END ASC LIMIT 0, 300',
213
            $stmt->build()
214
        );
215
    }
216
217
    public function testBuilderAliasGroupByCase(): void
218
    {
219
        $parser = new Parser(
220
            'SELECT * FROM `world_borders` GROUP BY CASE '
221
            . 'WHEN REGION = 2 THEN 99 '
222
            . 'WHEN REGION > 3 THEN REGION+1 '
223
            . 'ELSE 100 END LIMIT 0,300'
224
        );
225
        $stmt = $parser->statements[0];
226
227
        $this->assertEquals(
228
            'SELECT * FROM `world_borders` GROUP BY CASE '
229
            . 'WHEN REGION = 2 THEN 99 '
230
            . 'WHEN REGION > 3 THEN REGION+1 '
231
            . 'ELSE 100 END LIMIT 0, 300',
232
            $stmt->build()
233
        );
234
    }
235
236
    public function testBuilderEndOptions(): void
237
    {
238
        /* Assertion 1 */
239
        $query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 FOR UPDATE';
240
        $parser = new Parser($query);
241
        $stmt = $parser->statements[0];
242
243
        $this->assertEquals(
244
            $query,
245
            $stmt->build()
246
        );
247
248
        /* Assertion 2 */
249
        $query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 LOCK IN SHARE MODE';
250
        $parser = new Parser($query);
251
        $stmt = $parser->statements[0];
252
253
        $this->assertEquals(
254
            $query,
255
            $stmt->build()
256
        );
257
    }
258
259
    public function testBuilderIntoOptions(): void
260
    {
261
        /* Assertion 1 */
262
        $query = 'SELECT a, b, a+b INTO OUTFILE "/tmp/result.txt"'
263
            . ' COLUMNS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\''
264
            . ' LINES TERMINATED BY \'\n\''
265
            . ' FROM test_table';
266
        $parser = new Parser($query);
267
        $stmt = $parser->statements[0];
268
269
        $this->assertEquals(
270
            $query,
271
            $stmt->build()
272
        );
273
    }
274
275
    public function testBuilderGroupBy(): void
276
    {
277
        $query = 'SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country';
278
        $parser = new Parser($query);
279
        $stmt = $parser->statements[0];
280
281
        $this->assertEquals(
282
            $query,
283
            $stmt->build()
284
        );
285
    }
286
287
    public function testBuilderIndexHint(): void
288
    {
289
        $query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0';
290
        $parser = new Parser($query);
291
        $stmt = $parser->statements[0];
292
293
        $this->assertEquals(
294
            $query,
295
            $stmt->build()
296
        );
297
    }
298
299
    public function testBuilderSurroundedByParanthesisWithLimit(): void
300
    {
301
        $query = '(SELECT first_name FROM `actor` LIMIT 1, 2)';
302
        $parser = new Parser($query);
303
        $stmt = $parser->statements[0];
304
305
        $this->assertEquals(
306
            'SELECT first_name FROM `actor` LIMIT 1, 2',
307
            $stmt->build()
308
        );
309
    }
310
}
311