Skip to content

Commit be25445

Browse files
committed
replace some/most of the array shifting with a cursor instead.
still needs some more work
1 parent 841e09a commit be25445

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

lib_sql_parser.php

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,14 @@ function walk($tokens, $sql, $source_map){
190190

191191
if (StrToUpper($s[0]) == 'CREATE TABLE'){
192192

193-
array_shift($s);
194-
195-
$table = $this->parse_create_table($s);
193+
$table = $this->parse_create_table($s, 1, count($s));
196194
$table['sql'] = $stmt['sql'];
197195
$tables[$table['name']] = $table;
198196
}
199197

200198
if (StrToUpper($s[0]) == 'CREATE TEMPORARY TABLE'){
201199

202-
array_shift($s);
203-
204-
$table = $this->parse_create_table($s);
200+
$table = $this->parse_create_table($s, 1, count($s));
205201
$table['props']['temp'] = true;
206202
$tables[$table['name']] = $table;
207203
$table['sql'] = $stmt['sql'];
@@ -218,27 +214,27 @@ function walk($tokens, $sql, $source_map){
218214
}
219215

220216

221-
function parse_create_table($tokens){
217+
function parse_create_table($tokens, $i, $num){
222218

223-
if ($tokens[0] == 'IF NOT EXISTS'){
224-
array_shift($tokens);
219+
if ($tokens[$i] == 'IF NOT EXISTS'){
220+
$i++;
225221
}
226222

227223

228224
#
229225
# name
230226
#
231227

232-
$name = $this->decode_identifier(array_shift($tokens));
228+
$name = $this->decode_identifier($tokens[$i++]);
233229

234230

235231
#
236232
# CREATE TABLE x LIKE y
237233
#
238234

239-
if ($this->next_tokens($tokens, 'LIKE')){
240-
array_shift($tokens);
241-
$old_name = $this->decode_identifier(array_shift($tokens));
235+
if ($this->next_tokens($tokens, $i, 'LIKE')){
236+
$i++;
237+
$old_name = $this->decode_identifier($tokens[$i++]);
242238

243239
return array(
244240
'name' => $name,
@@ -252,15 +248,16 @@ function parse_create_table($tokens){
252248
#
253249

254250
$fields = array();
251+
$indexes = array();
255252

256-
if ($this->next_tokens($tokens, '(')){
257-
array_shift($tokens);
258-
$ret = $this->parse_create_definition($tokens);
253+
if ($this->next_tokens($tokens, $i, '(')){
254+
$i++;
255+
$ret = $this->parse_create_definition($tokens, $i);
259256
$fields = $ret['fields'];
260257
$indexes = $ret['indexes'];
261258
}
262259

263-
$props = $this->parse_table_props($tokens);
260+
$props = $this->parse_table_props($tokens, $i);
264261

265262
$table = array(
266263
'name' => $name,
@@ -269,38 +266,38 @@ function parse_create_table($tokens){
269266
'props' => $props,
270267
);
271268

272-
if (count($tokens)) $table['more'] = $tokens;
269+
if ($i <= count($tokens)) $table['more'] = array_slice($tokens, $i);
273270

274271
return $table;
275272
}
276273

277274

278-
function next_tokens($tokens){
275+
function next_tokens($tokens, $i){
279276

280277
$args = func_get_args();
281278
array_shift($args);
279+
array_shift($args);
282280

283-
$i = 0;
284281
foreach ($args as $v){
285-
if (StrToUpper($tokens[$i]) != $v)return false;
282+
if (StrToUpper($tokens[$i]) != $v) return false;
286283
$i++;
287284
}
288285
return true;
289286
}
290287

291-
function parse_create_definition(&$tokens){
288+
function parse_create_definition($tokens, &$i){
292289

293290
$fields = array();
294291
$indexes = array();
295292

296-
while ($tokens[0] != ')'){
293+
while ($tokens[$i] != ')'){
297294

298-
$these_tokens = $this->slice_until_next_field($tokens);
295+
$these_tokens = $this->slice_until_next_field($tokens, $i);
299296

300297
$this->parse_field_or_key($these_tokens, $fields, $indexes);
301298
}
302299

303-
array_shift($tokens); # closing paren
300+
$i++;
304301

305302
return array(
306303
'fields' => $fields,
@@ -453,32 +450,32 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){
453450
$fields[] = $this->parse_field($tokens);
454451
}
455452

456-
function slice_until_next_field(&$tokens){
453+
function slice_until_next_field($tokens, &$i){
457454

458455
$out = array();
459456
$stack = 0;
460457

461-
while (count($tokens)){
462-
$next = $tokens[0];
458+
while ($i <= count($tokens)){
459+
$next = $tokens[$i];
463460
if ($next == '('){
464461
$stack++;
465-
$out[] = array_shift($tokens);
462+
$out[] = $tokens[$i++];
466463
}elseif ($next == ')'){
467464
if ($stack){
468465
$stack--;
469-
$out[] = array_shift($tokens);
466+
$out[] = $tokens[$i++];
470467
}else{
471468
return $out;
472469
}
473470
}elseif ($next == ','){
474471
if ($stack){
475-
$out[] = array_shift($tokens);
472+
$out[] = $tokens[$i++];
476473
}else{
477-
array_shift($tokens);
474+
$i++;
478475
return $out;
479476
}
480477
}else{
481-
$out[] = array_shift($tokens);
478+
$out[] = $tokens[$i++];
482479
}
483480
}
484481

@@ -639,7 +636,7 @@ function parse_field($tokens){
639636
return $f;
640637
}
641638

642-
function parse_table_props(&$tokens){
639+
function parse_table_props($tokens, &$i){
643640

644641
$alt_names = array(
645642
'CHARACTER SET' => 'CHARSET',
@@ -650,9 +647,9 @@ function parse_table_props(&$tokens){
650647

651648
$props = array();
652649

653-
while (count($tokens)){
650+
while ($i < count($tokens)){
654651

655-
switch (StrToUpper($tokens[0])){
652+
switch (StrToUpper($tokens[$i])){
656653
case 'ENGINE':
657654
case 'AUTO_INCREMENT':
658655
case 'AVG_ROW_LENGTH':
@@ -671,20 +668,20 @@ function parse_table_props(&$tokens){
671668
case 'CHARSET':
672669
case 'DATA DIRECTORY':
673670
case 'INDEX DIRECTORY':
674-
$prop = StrToUpper(array_shift($tokens));
675-
if (isset($tokens[0]) && $tokens[0] == '=') array_shift($tokens);
676-
$props[$prop] = array_shift($tokens);
677-
if (isset($tokens[0]) && $tokens[0] == ',') array_shift($tokens);
671+
$prop = StrToUpper($tokens[$i++]);
672+
if (isset($tokens[$i]) && $tokens[$i] == '=') $i++;
673+
$props[$prop] = $tokens[$i++];
674+
if (isset($tokens[$i]) && $tokens[$i] == ',') $i++;
678675
break;
679676

680677
case 'CHARACTER SET':
681678
case 'DEFAULT COLLATE':
682679
case 'DEFAULT CHARACTER SET':
683680
case 'DEFAULT CHARSET':
684-
$prop = $alt_names[StrToUpper(array_shift($tokens))];
685-
if (isset($tokens[0]) && $tokens[0] == '=') array_shift($tokens);
686-
$props[$prop] = array_shift($tokens);
687-
if (isset($tokens[0]) && $tokens[0] == ',') array_shift($tokens);
681+
$prop = $alt_names[StrToUpper($tokens[$i++])];
682+
if (isset($tokens[$i]) && $tokens[$i] == '=') $i++;
683+
$props[$prop] = $tokens[$i++];
684+
if (isset($tokens[$i]) && $tokens[$i] == ',') $i++;
688685
break;
689686

690687
default:

run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
if (1){
3535

36-
$GLOBALS['_find_single_table'] = 1;
36+
$GLOBALS['_find_single_table'] = 0;
3737

3838
$s = microtime(true);
3939
$obj->parse($sql);

tests/04_table_props.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
function table_props_test($tokens, $props_expect){
66

77
$obj = new SQLParser();
8-
$props = $obj->parse_table_props($tokens);
8+
$i = 0;
9+
$props = $obj->parse_table_props($tokens, $i);
910

1011
is_deeply($props, $props_expect);
1112
}

0 commit comments

Comments
 (0)