diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-09-25 10:52:38 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-09-25 11:00:29 +0300 |
commit | 1415469b34b1fc33189e2c801302597b5b50f704 (patch) | |
tree | 44030954915aea36401a61d3a35f5609f25d176d /src/Components/Key.php | |
parent | 9098d370993b98c93d3b0be6e737e31ea20846fe (diff) | |
download | sql-parser-1415469b34b1fc33189e2c801302597b5b50f704.zip sql-parser-1415469b34b1fc33189e2c801302597b5b50f704.tar.gz sql-parser-1415469b34b1fc33189e2c801302597b5b50f704.tar.bz2 |
Better parsing for CREATE TABLE statements.v2.0.0
Fixes #16.
Diffstat (limited to 'src/Components/Key.php')
-rw-r--r-- | src/Components/Key.php | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/Components/Key.php b/src/Components/Key.php index 89371fa..70b2b93 100644 --- a/src/Components/Key.php +++ b/src/Components/Key.php @@ -96,6 +96,13 @@ class Key extends Component $ret = new Key(); /** + * Last parsed column. + * + * @var array + */ + $lastColumn = array(); + + /** * The state of the parser. * * Below are the states of the parser. @@ -135,12 +142,31 @@ class Key extends Component $state = 1; } elseif ($state === 1) { if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { - $ret->columns = ArrayObj::parse($parser, $list)->values; $state = 2; } else { $ret->name = $token->value; } } elseif ($state === 2) { + if ($token->type === Token::TYPE_OPERATOR) { + if ($token->value === '(') { + $state = 3; + } elseif (($token->value === ',') || ($token->value === ')')) { + $state = ($token->value === ',') ? 2 : 4; + if (!empty($lastColumn)) { + $ret->columns[] = $lastColumn; + $lastColumn = array(); + } + } + } else { + $lastColumn['name'] = $token->value; + } + } elseif ($state === 3) { + if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ')')) { + $state = 2; + } else { + $lastColumn['length'] = $token->value; + } + } elseif ($state === 4) { $ret->options = OptionsArray::parse($parser, $list, static::$KEY_OPTIONS); ++$list->idx; break; @@ -163,8 +189,17 @@ class Key extends Component if (!empty($component->name)) { $ret .= Context::escape($component->name) . ' '; } - $ret .= '(' . implode(',', Context::escape($component->columns)) . ') ' - . $component->options; + + $columns = array(); + foreach ($component->columns as $column) { + $tmp = Context::escape($column['name']); + if (isset($column['length'])) { + $tmp .= '(' . $column['length'] . ')'; + } + $columns[] = $tmp; + } + + $ret .= '(' . implode(',', $columns) . ') ' . $component->options; return trim($ret); } } |