Skip to content

Commit 04117a0

Browse files
committed
added support for default field values.
added decoding for values. refactored decoding for identifiers.
1 parent a93a220 commit 04117a0

1 file changed

Lines changed: 48 additions & 11 deletions

File tree

lib_sql_parser.php

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function parse_create_table($tokens){
207207
# name
208208
#
209209

210-
$name = $this->shift_field_name($tokens);
210+
$name = $this->decode_identifier(array_shift($tokens));
211211

212212

213213
#
@@ -216,7 +216,7 @@ function parse_create_table($tokens){
216216

217217
if ($this->next_tokens($tokens, 'LIKE')){
218218
array_shift($tokens);
219-
$old_name = $this->shift_field_name($tokens);
219+
$old_name = $this->decode_identifier(array_shift($tokens));
220220

221221
return array(
222222
'name' => $name,
@@ -267,14 +267,6 @@ function next_tokens($tokens){
267267
return true;
268268
}
269269

270-
function shift_field_name(&$tokens){
271-
$name = array_shift($tokens);
272-
if ($name{0} == '`'){
273-
$name = substr($name, 1, -1);
274-
}
275-
return $name;
276-
}
277-
278270
function parse_create_definition(&$tokens){
279271

280272
$fields = array();
@@ -479,7 +471,7 @@ function slice_until_next_field(&$tokens){
479471
function parse_field($tokens){
480472

481473
$f = array(
482-
'name' => $this->shift_field_name($tokens),
474+
'name' => $this->decode_identifier(array_shift($tokens)),
483475
'type' => StrToUpper(array_shift($tokens)),
484476
);
485477

@@ -607,6 +599,12 @@ function parse_field($tokens){
607599
}
608600

609601
# [DEFAULT default_value]
602+
if (StrToUpper($tokens[0]) == 'DEFAULT'){
603+
$f['default'] = $this->decode_value($tokens[1]);
604+
array_shift($tokens);
605+
array_shift($tokens);
606+
}
607+
610608
# [AUTO_INCREMENT]
611609
# [UNIQUE [KEY] | [PRIMARY] KEY]
612610
# [COMMENT 'string']
@@ -889,6 +887,45 @@ function parse_field_collate(&$tokens, &$f){
889887
array_shift($tokens);
890888
}
891889
}
890+
891+
892+
function decode_identifier($token){
893+
if ($token[0] == '`'){
894+
return substr($token, 1, -1);
895+
}
896+
return $token;
897+
}
898+
899+
function decode_value($token){
900+
901+
#
902+
# decode strings
903+
#
904+
905+
if ($token[0] == "'" || $token[0] == '"'){
906+
$map = array(
907+
'n' => "\n",
908+
'r' => "\r",
909+
't' => "\t",
910+
);
911+
$out = '';
912+
for ($i=1; $i<strlen($token)-1; $i++){
913+
if ($token[$i] == '\\'){
914+
if ($map[$token[$i+1]]){
915+
$out .= $map[$token[$i+1]];
916+
}else{
917+
$out .= $token[$i+1];
918+
}
919+
$i++;
920+
}else{
921+
$out .= $token[$i];
922+
}
923+
}
924+
return $out;
925+
}
926+
927+
return $token;
928+
}
892929
}
893930

894931

0 commit comments

Comments
 (0)