Skip to content

Commit 7429471

Browse files
committed
Added missing partition definition.
1 parent 7d3b89b commit 7429471

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
/**
4+
* Parses the create definition of a partition.
5+
*
6+
* Used for parsing `CREATE TABLE` statement.
7+
*
8+
* @package SqlParser
9+
* @subpackage Components
10+
*/
11+
namespace SqlParser\Components;
12+
13+
use SqlParser\Context;
14+
use SqlParser\Component;
15+
use SqlParser\Parser;
16+
use SqlParser\Token;
17+
use SqlParser\TokensList;
18+
19+
/**
20+
* Parses the create definition of a partition.
21+
*
22+
* Used for parsing `CREATE TABLE` statement.
23+
*
24+
* @category Components
25+
* @package SqlParser
26+
* @subpackage Components
27+
* @author Dan Ungureanu <[email protected]>
28+
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License
29+
*/
30+
class PartitionDefinition extends Component
31+
{
32+
33+
/**
34+
* All field options.
35+
*
36+
* @var array
37+
*/
38+
public static $OPTIONS = array(
39+
'STORAGE' => array(1, 'var'),
40+
'STORAGE ENGINE' => array(1, 'var'),
41+
'COMMENT' => array(2, 'var'),
42+
'DATA DIRECTORY' => array(3, 'var'),
43+
'INDEX DIRECTORY' => array(4, 'var'),
44+
'MAX_ROWS' => array(5, 'var'),
45+
'MIN_ROWS' => array(6, 'var'),
46+
'TABLESPACE' => array(7, 'var'),
47+
'NODEGROUP' => array(8, 'var'),
48+
);
49+
50+
/**
51+
* Whether this entry is a subpartition or a partition.
52+
*
53+
* @var bool
54+
*/
55+
public $isSubpartition;
56+
57+
/**
58+
* The name of this partition.
59+
*
60+
* @var string
61+
*/
62+
public $name;
63+
64+
/**
65+
* The type of this partition (what follows the `VALUES` keyword).
66+
*
67+
* @var string
68+
*/
69+
public $type;
70+
71+
/**
72+
* The expression used to defined this partition.
73+
*
74+
* @var Expression
75+
*/
76+
public $expr;
77+
78+
/**
79+
* The subpartitions of this partition.
80+
*
81+
* @var PartitionDefinition[]
82+
*/
83+
public $subpartitions;
84+
85+
/**
86+
* The options of this field.
87+
*
88+
* @var OptionsArray
89+
*/
90+
public $options;
91+
92+
/**
93+
* @param Parser $parser The parser that serves as context.
94+
* @param TokensList $list The list of tokens that are being parsed.
95+
* @param array $options Parameters for parsing.
96+
*
97+
* @return PartitionDefinition
98+
*/
99+
public static function parse(Parser $parser, TokensList $list, array $options = array())
100+
{
101+
$ret = new PartitionDefinition();
102+
103+
/**
104+
* The state of the parser.
105+
*
106+
* Below are the states of the parser.
107+
*
108+
* 0 -------------[ PARTITION | SUBPARTITION ]------------> 1
109+
*
110+
* 1 -----------------------[ name ]----------------------> 2
111+
*
112+
* 2 ----------------------[ VALUES ]---------------------> 3
113+
*
114+
* 3 ---------------------[ LESS THAN ]-------------------> 4
115+
* 3 ------------------------[ IN ]-----------------------> 4
116+
*
117+
* 4 -----------------------[ expr ]----------------------> 5
118+
*
119+
* 5 ----------------------[ options ]--------------------> 6
120+
*
121+
* 6 ------------------[ subpartitions ]------------------> (END)
122+
*
123+
* @var int $state
124+
*/
125+
$state = 0;
126+
127+
for (; $list->idx < $list->count; ++$list->idx) {
128+
129+
/**
130+
* Token parsed at this moment.
131+
*
132+
* @var Token $token
133+
*/
134+
$token = $list->tokens[$list->idx];
135+
136+
// End of statement.
137+
if ($token->type === Token::TYPE_DELIMITER) {
138+
break;
139+
}
140+
141+
// Skipping whitespaces and comments.
142+
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
143+
continue;
144+
}
145+
146+
if ($state === 0) {
147+
$ret->isSubpartition = ($token->type === Token::TYPE_KEYWORD) && ($token->value === 'SUBPARTITION');
148+
$state = 1;
149+
} elseif ($state === 1) {
150+
$ret->name = $token->value;
151+
$state = $ret->isSubpartition ? 5 : 2;
152+
} elseif ($state === 2) {
153+
$state = 3;
154+
} elseif ($state === 3) {
155+
$ret->type = $token->value;
156+
$state = 4;
157+
} elseif ($state === 4) {
158+
$ret->expr = Expression::parse($parser, $list, array('noAlias' => true, 'bracketsDelimited' => true));
159+
$state = 5;
160+
} elseif ($state === 5) {
161+
$ret->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
162+
$state = 6;
163+
} elseif ($state === 6) {
164+
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
165+
$ret->subpartitions = ArrayObj::parse(
166+
$parser,
167+
$list,
168+
array(
169+
'type' => 'SqlParser\Components\PartitionDefinition'
170+
)
171+
);
172+
} else {
173+
break;
174+
}
175+
}
176+
}
177+
178+
--$list->idx;
179+
return $ret;
180+
}
181+
}

0 commit comments

Comments
 (0)