Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/SQLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ private function _lex($sql){
$c += 2;
continue;
}
# quote doubling
if ($sql[$c] == $q && isset($sql[$c+1]) && $sql[$c+1] == $q){
$c += 2;
continue;
}
if ($sql[$c] == $q){
$slen = $c + 1 - $pos;
$source_map[] = array($pos, $slen);
Expand Down Expand Up @@ -1154,8 +1159,11 @@ function decode_value($token){
'n' => "\n",
'r' => "\r",
't' => "\t",
"'" => "'",
'"' => '"',
);
$out = '';
$q = $token[0];
for ($i=1; $i<strlen($token)-1; $i++){
if ($token[$i] == '\\'){
if ($map[$token[$i+1]]){
Expand All @@ -1164,6 +1172,10 @@ function decode_value($token){
$out .= $token[$i+1];
}
$i++;
}elseif ($token[$i] == $q && $token[$i+1] == $q){
# quote doubling
$out .= $q;
$i++;
}else{
$out .= $token[$i];
}
Expand Down
80 changes: 80 additions & 0 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,86 @@ function testSets(){

# ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]
# SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]

# a `\'` inside a string quoted with `'`
$tbl = $this->get_first_table("CREATE TABLE foo (bar ENUM('hel\'lo'))");
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array("hel'lo"),
)
));

# a `\"` inside a string quoted with `"`
$tbl = $this->get_first_table('CREATE TABLE foo (bar ENUM("hel\"lo"))');
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array('hel"lo'),
)
));

# a `\'` inside a string quoted with `"`
$tbl = $this->get_first_table("CREATE TABLE foo (bar ENUM(\"hel\'lo\"))");
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array("hel'lo"),
)
));

# a `\"` inside a string quoted with `'`
$tbl = $this->get_first_table('CREATE TABLE foo (bar ENUM(\'hel\"lo\'))');
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array('hel"lo'),
)
));

# a `''` inside a string quoted with `'`
$tbl = $this->get_first_table("CREATE TABLE foo (bar ENUM('hel''lo'))");
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array("hel'lo"),
)
));

# a `""` inside a string quoted with `"`
$tbl = $this->get_first_table('CREATE TABLE foo (bar ENUM("hel""lo"))');
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array('hel"lo'),
)
));

# a `''` inside a string quoted with `"`
$tbl = $this->get_first_table("CREATE TABLE foo (bar ENUM(\"hel''lo\"))");
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array("hel''lo"),
)
));

# a `""` inside a string quoted with `'`
$tbl = $this->get_first_table('CREATE TABLE foo (bar ENUM(\'hel""lo\'))');
$this->assertEquals($tbl['fields'], array(
array(
'name' => "bar",
'type' => "ENUM",
'values' => array('hel""lo'),
)
));
}


Expand Down
12 changes: 12 additions & 0 deletions tests/LexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,17 @@ public function testStrings(){

$this->lex_test("foo \"bar\" baz", array("foo", "\"bar\"", "baz"));
$this->lex_test("foo \"bar \\\" baz\" qux", array("foo", "\"bar \\\" baz\"", "qux"));

# a `''` inside a string quoted with `'`
$this->lex_test("'hel''lo'", array("'hel''lo'"));

# a `""` inside a string quoted with `"`
$this->lex_test('"hel""lo"', array('"hel""lo"'));

# a `''` inside a string quoted with `"`
$this->lex_test("\"hel''lo\"", array("\"hel''lo\""));

# a `""` inside a string quoted with `'`
$this->lex_test('\'hel""lo\'', array('\'hel""lo\''));
}
}