Skip to content

Commit 8bbff36

Browse files
committed
Added UNION ALL as a type of UNION.
1 parent 7d64afa commit 8bbff36

10 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/Components/UnionKeyword.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class UnionKeyword extends Component
3131
*/
3232
public static function build($component, array $options = array())
3333
{
34-
return implode(' UNION ', $component);
34+
$tmp = array();
35+
foreach ($component as $component) {
36+
$tmp[] = $component[0] . ' ' . $component[1];
37+
}
38+
return implode(' ', $tmp);
3539
}
3640
}

src/Contexts/ContextMySql50000.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class ContextMySql50000 extends Context
146146
'GROUP BY' => 7, 'NOT NULL' => 7, 'ORDER BY' => 7, 'SET NULL' => 7,
147147
'AND CHAIN' => 7, 'FULL JOIN' => 7, 'IF EXISTS' => 7, 'LEFT JOIN' => 7,
148148
'LESS THAN' => 7, 'NO ACTION' => 7, 'ON DELETE' => 7, 'ON UPDATE' => 7,
149+
'UNION ALL' => 7,
149150
'INNER JOIN' => 7, 'LINEAR KEY' => 7, 'NO RELEASE' => 7, 'OR REPLACE' => 7,
150151
'RIGHT JOIN' => 7,
151152
'LINEAR HASH' => 7,

src/Contexts/ContextMySql50100.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class ContextMySql50100 extends Context
159159
'GROUP BY' => 7, 'NOT NULL' => 7, 'ORDER BY' => 7, 'SET NULL' => 7,
160160
'AND CHAIN' => 7, 'FULL JOIN' => 7, 'IF EXISTS' => 7, 'LEFT JOIN' => 7,
161161
'LESS THAN' => 7, 'NO ACTION' => 7, 'ON DELETE' => 7, 'ON UPDATE' => 7,
162+
'UNION ALL' => 7,
162163
'INNER JOIN' => 7, 'LINEAR KEY' => 7, 'NO RELEASE' => 7, 'OR REPLACE' => 7,
163164
'RIGHT JOIN' => 7,
164165
'LINEAR HASH' => 7,

src/Contexts/ContextMySql50500.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class ContextMySql50500 extends Context
164164
'GROUP BY' => 7, 'NOT NULL' => 7, 'ORDER BY' => 7, 'SET NULL' => 7,
165165
'AND CHAIN' => 7, 'FULL JOIN' => 7, 'IF EXISTS' => 7, 'LEFT JOIN' => 7,
166166
'LESS THAN' => 7, 'NO ACTION' => 7, 'ON DELETE' => 7, 'ON UPDATE' => 7,
167+
'UNION ALL' => 7,
167168
'INNER JOIN' => 7, 'LINEAR KEY' => 7, 'NO RELEASE' => 7, 'OR REPLACE' => 7,
168169
'RIGHT JOIN' => 7,
169170
'LINEAR HASH' => 7,

src/Contexts/ContextMySql50600.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class ContextMySql50600 extends Context
170170
'GROUP BY' => 7, 'NOT NULL' => 7, 'ORDER BY' => 7, 'SET NULL' => 7,
171171
'AND CHAIN' => 7, 'FULL JOIN' => 7, 'IF EXISTS' => 7, 'LEFT JOIN' => 7,
172172
'LESS THAN' => 7, 'NO ACTION' => 7, 'ON DELETE' => 7, 'ON UPDATE' => 7,
173+
'UNION ALL' => 7,
173174
'INNER JOIN' => 7, 'LINEAR KEY' => 7, 'NO RELEASE' => 7, 'OR REPLACE' => 7,
174175
'RIGHT JOIN' => 7,
175176
'LINEAR HASH' => 7,

src/Contexts/ContextMySql50700.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class ContextMySql50700 extends Context
177177
'GROUP BY' => 7, 'NOT NULL' => 7, 'ORDER BY' => 7, 'SET NULL' => 7,
178178
'AND CHAIN' => 7, 'FULL JOIN' => 7, 'IF EXISTS' => 7, 'LEFT JOIN' => 7,
179179
'LESS THAN' => 7, 'NO ACTION' => 7, 'ON DELETE' => 7, 'ON UPDATE' => 7,
180+
'UNION ALL' => 7,
180181
'INNER JOIN' => 7, 'LINEAR KEY' => 7, 'NO RELEASE' => 7, 'OR REPLACE' => 7,
181182
'RIGHT JOIN' => 7,
182183
'LINEAR HASH' => 7,

src/Parser.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class Parser
115115
'class' => 'SqlParser\\Components\\UnionKeyword',
116116
'field' => 'union',
117117
),
118+
'UNION ALL' => array(
119+
'class' => 'SqlParser\\Components\\UnionKeyword',
120+
'field' => 'union',
121+
),
118122

119123
// Actual clause parsers.
120124
'ALTER' => array(
@@ -356,11 +360,11 @@ public function parse()
356360
$lastStatement = null;
357361

358362
/**
359-
* Whether a union is parsed or not.
363+
* Union's type or false for no union.
360364
*
361-
* @var bool $inUnion
365+
* @var bool|string $unionType
362366
*/
363-
$inUnion = false;
367+
$unionType = false;
364368

365369
/**
366370
* The index of the last token from the last statement.
@@ -417,8 +421,8 @@ public function parse()
417421
continue;
418422
}
419423

420-
if ($token->value === 'UNION') {
421-
$inUnion = true;
424+
if (($token->value === 'UNION') || ($token->value === 'UNION ALL')) {
425+
$unionType = $token->value;
422426
continue;
423427
}
424428

@@ -465,7 +469,7 @@ public function parse()
465469
$prevLastIdx = $list->idx;
466470

467471
// Handles unions.
468-
if (($inUnion)
472+
if ((!empty($unionType))
469473
&& ($lastStatement instanceof SelectStatement)
470474
&& ($statement instanceof SelectStatement)
471475
) {
@@ -480,7 +484,7 @@ public function parse()
480484
*
481485
* @var SelectStatement $lastStatement
482486
*/
483-
$lastStatement->union[] = $statement;
487+
$lastStatement->union[] = array($unionType, $statement);
484488

485489
// if there are no no delimiting brackets, the `ORDER` and
486490
// `LIMIT` keywords actually belong to the first statement.
@@ -493,7 +497,7 @@ public function parse()
493497
// union ends.
494498
$lastStatement->last = $statement->last;
495499

496-
$inUnion = false;
500+
$unionType = false;
497501
continue;
498502
}
499503

src/Statement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function parse(Parser $parser, TokensList $list)
251251

252252
// Unions are parsed by the parser because they represent more than
253253
// one statement.
254-
if ($token->value === 'UNION') {
254+
if (($token->value === 'UNION') || ($token->value === 'UNION ALL')) {
255255
break;
256256
}
257257

src/Statements/SelectStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class SelectStatement extends Statement
106106
'LIMIT' => array('LIMIT', 3),
107107
'PROCEDURE' => array('PROCEDURE', 3),
108108
'INTO' => array('INTO', 3),
109-
'UNION' => array('UNION', 3),
109+
'UNION' => array('UNION', 1),
110110
// These are available only when `UNION` is present.
111111
// 'ORDER BY' => array('ORDER BY', 3),
112112
// 'LIMIT' => array('LIMIT', 3),

tools/contexts/_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ TIMESTAMP (D)
8484
TINYBLOB (D)
8585
TINYINT (D)
8686
TINYTEXT (D)
87+
UNION ALL
8788
UNIQUE (K)
8889
UNIQUE INDEX (K)
8990
UNIQUE KEY (K)

0 commit comments

Comments
 (0)