Skip to content

Commit e23b0d9

Browse files
committed
Improved support for partitions in the builder and formatter.
1 parent 7429471 commit e23b0d9

4 files changed

Lines changed: 228 additions & 152 deletions

File tree

src/Components/PartitionDefinition.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,32 @@ public static function parse(Parser $parser, TokensList $list, array $options =
178178
--$list->idx;
179179
return $ret;
180180
}
181+
182+
/**
183+
* @param PartitionDefinition|PartitionDefinition[] $component The component to be built.
184+
*
185+
* @return string
186+
*/
187+
public static function build($component)
188+
{
189+
if (is_array($component)) {
190+
$ret = array();
191+
foreach ($component as $c) {
192+
$ret[] = static::build($c);
193+
}
194+
return "(\n" . implode(",\n", $ret) . "\n)";
195+
} else {
196+
if ($component->isSubpartition) {
197+
return 'SUBPARTITION ' . $component->name;
198+
} else {
199+
if (!empty($component->subpartitions)) {
200+
$subpartitions = ' ' . PartitionDefinition::build($component->subpartitions);
201+
}
202+
return 'PARTITION ' . $component->name
203+
. ' VALUES ' . $component->type . $component->expr
204+
. $subpartitions;
205+
}
206+
207+
}
208+
}
181209
}

src/Parser.php

Lines changed: 152 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -113,153 +113,158 @@ class Parser
113113
*/
114114
public static $KEYWORD_PARSERS = array(
115115

116-
// This is not a proper keyword and was added here to help the builder.
117-
'_OPTIONS' => array(
118-
'class' => 'SqlParser\\Components\\OptionsArray',
119-
'field' => 'options',
120-
),
121-
122-
// This is used only for building.
123-
'UNION' => array(
124-
'class' => 'SqlParser\\Components\\UnionKeyword',
125-
'field' => 'union',
126-
),
127-
128-
'ALTER' => array(
129-
'class' => 'SqlParser\\Components\\Expression',
130-
'field' => 'table',
131-
'options' => array('skipColumn' => true),
132-
),
133-
'ANALYZE' => array(
134-
'class' => 'SqlParser\\Components\\ExpressionArray',
135-
'field' => 'tables',
136-
'options' => array('skipColumn' => true),
137-
),
138-
'BACKUP' => array(
139-
'class' => 'SqlParser\\Components\\ExpressionArray',
140-
'field' => 'tables',
141-
'options' => array('skipColumn' => true),
142-
),
143-
'CALL' => array(
144-
'class' => 'SqlParser\\Components\\FunctionCall',
145-
'field' => 'call',
146-
),
147-
'CHECK' => array(
148-
'class' => 'SqlParser\\Components\\ExpressionArray',
149-
'field' => 'tables',
150-
'options' => array('skipColumn' => true),
151-
),
152-
'CHECKSUM' => array(
153-
'class' => 'SqlParser\\Components\\ExpressionArray',
154-
'field' => 'tables',
155-
'options' => array('skipColumn' => true),
156-
),
157-
'DROP' => array(
158-
'class' => 'SqlParser\\Components\\ExpressionArray',
159-
'field' => 'fields',
160-
'options' => array('skipColumn' => true),
161-
),
162-
'FROM' => array(
163-
'class' => 'SqlParser\\Components\\ExpressionArray',
164-
'field' => 'from',
165-
'options' => array('skipColumn' => true),
166-
),
167-
'GROUP BY' => array(
168-
'class' => 'SqlParser\\Components\\OrderKeyword',
169-
'field' => 'group',
170-
),
171-
'HAVING' => array(
172-
'class' => 'SqlParser\\Components\\Condition',
173-
'field' => 'having',
174-
),
175-
'INTO' => array(
176-
'class' => 'SqlParser\\Components\\IntoKeyword',
177-
'field' => 'into',
178-
),
179-
'JOIN' => array(
180-
'class' => 'SqlParser\\Components\\JoinKeyword',
181-
'field' => 'join',
182-
),
183-
'LEFT JOIN' => array(
184-
'class' => 'SqlParser\\Components\\JoinKeyword',
185-
'field' => 'join',
186-
),
187-
'RIGHT JOIN' => array(
188-
'class' => 'SqlParser\\Components\\JoinKeyword',
189-
'field' => 'join',
190-
),
191-
'INNER JOIN' => array(
192-
'class' => 'SqlParser\\Components\\JoinKeyword',
193-
'field' => 'join',
194-
),
195-
'FULL JOIN' => array(
196-
'class' => 'SqlParser\\Components\\JoinKeyword',
197-
'field' => 'join',
198-
),
199-
'LIMIT' => array(
200-
'class' => 'SqlParser\\Components\\Limit',
201-
'field' => 'limit',
202-
),
203-
'OPTIMIZE' => array(
204-
'class' => 'SqlParser\\Components\\ExpressionArray',
205-
'field' => 'tables',
206-
'options' => array('skipColumn' => true),
207-
),
208-
'ORDER BY' => array(
209-
'class' => 'SqlParser\\Components\\OrderKeyword',
210-
'field' => 'order',
211-
),
212-
'PARTITION' => array(
213-
'class' => 'SqlParser\\Components\\ArrayObj',
214-
'field' => 'partition',
215-
),
216-
'PROCEDURE' => array(
217-
'class' => 'SqlParser\\Components\\FunctionCall',
218-
'field' => 'procedure',
219-
),
220-
'RENAME' => array(
221-
'class' => 'SqlParser\\Components\\RenameOperation',
222-
'field' => 'renames',
223-
),
224-
'REPAIR' => array(
225-
'class' => 'SqlParser\\Components\\ExpressionArray',
226-
'field' => 'tables',
227-
'options' => array('skipColumn' => true),
228-
),
229-
'RESTORE' => array(
230-
'class' => 'SqlParser\\Components\\ExpressionArray',
231-
'field' => 'tables',
232-
'options' => array('skipColumn' => true),
233-
),
234-
'SET' => array(
235-
'class' => 'SqlParser\\Components\\SetOperation',
236-
'field' => 'set',
237-
),
238-
'SELECT' => array(
239-
'class' => 'SqlParser\\Components\\ExpressionArray',
240-
'field' => 'expr',
241-
),
242-
'TRUNCATE' => array(
243-
'class' => 'SqlParser\\Components\\Expression',
244-
'field' => 'table',
245-
'options' => array('skipColumn' => true),
246-
),
247-
'UPDATE' => array(
248-
'class' => 'SqlParser\\Components\\ExpressionArray',
249-
'field' => 'tables',
250-
'options' => array('skipColumn' => true),
251-
),
252-
'VALUE' => array(
253-
'class' => 'SqlParser\\Components\\Array2d',
254-
'field' => 'values',
255-
),
256-
'VALUES' => array(
257-
'class' => 'SqlParser\\Components\\Array2d',
258-
'field' => 'values',
259-
),
260-
'WHERE' => array(
261-
'class' => 'SqlParser\\Components\\Condition',
262-
'field' => 'where',
116+
// This is not a proper keyword and was added here to help the
117+
// formatter.
118+
'PARTITION BY' => array(),
119+
'SUBPARTITION BY' => array(),
120+
121+
// This is not a proper keyword and was added here to help the
122+
// builder.
123+
'_OPTIONS' => array(
124+
'class' => 'SqlParser\\Components\\OptionsArray',
125+
'field' => 'options',
126+
),
127+
'UNION' => array(
128+
'class' => 'SqlParser\\Components\\UnionKeyword',
129+
'field' => 'union',
130+
),
131+
132+
// Actual clause parsers.
133+
'ALTER' => array(
134+
'class' => 'SqlParser\\Components\\Expression',
135+
'field' => 'table',
136+
'options' => array('skipColumn' => true),
137+
),
138+
'ANALYZE' => array(
139+
'class' => 'SqlParser\\Components\\ExpressionArray',
140+
'field' => 'tables',
141+
'options' => array('skipColumn' => true),
142+
),
143+
'BACKUP' => array(
144+
'class' => 'SqlParser\\Components\\ExpressionArray',
145+
'field' => 'tables',
146+
'options' => array('skipColumn' => true),
147+
),
148+
'CALL' => array(
149+
'class' => 'SqlParser\\Components\\FunctionCall',
150+
'field' => 'call',
151+
),
152+
'CHECK' => array(
153+
'class' => 'SqlParser\\Components\\ExpressionArray',
154+
'field' => 'tables',
155+
'options' => array('skipColumn' => true),
156+
),
157+
'CHECKSUM' => array(
158+
'class' => 'SqlParser\\Components\\ExpressionArray',
159+
'field' => 'tables',
160+
'options' => array('skipColumn' => true),
161+
),
162+
'DROP' => array(
163+
'class' => 'SqlParser\\Components\\ExpressionArray',
164+
'field' => 'fields',
165+
'options' => array('skipColumn' => true),
166+
),
167+
'FROM' => array(
168+
'class' => 'SqlParser\\Components\\ExpressionArray',
169+
'field' => 'from',
170+
'options' => array('skipColumn' => true),
171+
),
172+
'GROUP BY' => array(
173+
'class' => 'SqlParser\\Components\\OrderKeyword',
174+
'field' => 'group',
175+
),
176+
'HAVING' => array(
177+
'class' => 'SqlParser\\Components\\Condition',
178+
'field' => 'having',
179+
),
180+
'INTO' => array(
181+
'class' => 'SqlParser\\Components\\IntoKeyword',
182+
'field' => 'into',
183+
),
184+
'JOIN' => array(
185+
'class' => 'SqlParser\\Components\\JoinKeyword',
186+
'field' => 'join',
187+
),
188+
'LEFT JOIN' => array(
189+
'class' => 'SqlParser\\Components\\JoinKeyword',
190+
'field' => 'join',
191+
),
192+
'RIGHT JOIN' => array(
193+
'class' => 'SqlParser\\Components\\JoinKeyword',
194+
'field' => 'join',
195+
),
196+
'INNER JOIN' => array(
197+
'class' => 'SqlParser\\Components\\JoinKeyword',
198+
'field' => 'join',
199+
),
200+
'FULL JOIN' => array(
201+
'class' => 'SqlParser\\Components\\JoinKeyword',
202+
'field' => 'join',
203+
),
204+
'LIMIT' => array(
205+
'class' => 'SqlParser\\Components\\Limit',
206+
'field' => 'limit',
207+
),
208+
'OPTIMIZE' => array(
209+
'class' => 'SqlParser\\Components\\ExpressionArray',
210+
'field' => 'tables',
211+
'options' => array('skipColumn' => true),
212+
),
213+
'ORDER BY' => array(
214+
'class' => 'SqlParser\\Components\\OrderKeyword',
215+
'field' => 'order',
216+
),
217+
'PARTITION' => array(
218+
'class' => 'SqlParser\\Components\\ArrayObj',
219+
'field' => 'partition',
220+
),
221+
'PROCEDURE' => array(
222+
'class' => 'SqlParser\\Components\\FunctionCall',
223+
'field' => 'procedure',
224+
),
225+
'RENAME' => array(
226+
'class' => 'SqlParser\\Components\\RenameOperation',
227+
'field' => 'renames',
228+
),
229+
'REPAIR' => array(
230+
'class' => 'SqlParser\\Components\\ExpressionArray',
231+
'field' => 'tables',
232+
'options' => array('skipColumn' => true),
233+
),
234+
'RESTORE' => array(
235+
'class' => 'SqlParser\\Components\\ExpressionArray',
236+
'field' => 'tables',
237+
'options' => array('skipColumn' => true),
238+
),
239+
'SET' => array(
240+
'class' => 'SqlParser\\Components\\SetOperation',
241+
'field' => 'set',
242+
),
243+
'SELECT' => array(
244+
'class' => 'SqlParser\\Components\\ExpressionArray',
245+
'field' => 'expr',
246+
),
247+
'TRUNCATE' => array(
248+
'class' => 'SqlParser\\Components\\Expression',
249+
'field' => 'table',
250+
'options' => array('skipColumn' => true),
251+
),
252+
'UPDATE' => array(
253+
'class' => 'SqlParser\\Components\\ExpressionArray',
254+
'field' => 'tables',
255+
'options' => array('skipColumn' => true),
256+
),
257+
'VALUE' => array(
258+
'class' => 'SqlParser\\Components\\Array2d',
259+
'field' => 'values',
260+
),
261+
'VALUES' => array(
262+
'class' => 'SqlParser\\Components\\Array2d',
263+
'field' => 'values',
264+
),
265+
'WHERE' => array(
266+
'class' => 'SqlParser\\Components\\Condition',
267+
'field' => 'where',
263268
),
264269

265270
);

0 commit comments

Comments
 (0)