diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-07-10 21:40:24 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-07-10 21:40:24 +0300 |
commit | 30fdd07d36a48031afe04df2ce54307e3b4bc7b2 (patch) | |
tree | 1c36f8e4109b21404945877a61c4a6c7a54aa2a1 /src | |
parent | 80cc0cd6920e4266b97e915289e6d0c146086968 (diff) | |
download | sql-parser-30fdd07d36a48031afe04df2ce54307e3b4bc7b2.zip sql-parser-30fdd07d36a48031afe04df2ce54307e3b4bc7b2.tar.gz sql-parser-30fdd07d36a48031afe04df2ce54307e3b4bc7b2.tar.bz2 |
Added remove() method for OptionsArray.
Fixed undefined variables introduced by previous renaming.
Some refactoring.
Diffstat (limited to 'src')
-rw-r--r-- | src/Components/AlterOperation.php | 6 | ||||
-rw-r--r-- | src/Components/Condition.php | 2 | ||||
-rw-r--r-- | src/Components/FieldDefinition.php | 34 | ||||
-rw-r--r-- | src/Components/OptionsArray.php | 24 | ||||
-rw-r--r-- | src/Components/ParameterDefinition.php | 8 | ||||
-rw-r--r-- | src/Context.php | 14 |
6 files changed, 57 insertions, 31 deletions
diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index aa81689..7c52828 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -91,7 +91,7 @@ class AlterOperation extends Component /** * Unparsed tokens. * - * @var Token[] + * @var Token[]|string */ public $unknown = array(); @@ -194,9 +194,7 @@ class AlterOperation extends Component if (!empty($component->field)) { $ret .= Expression::build($component->field) . ' '; } - foreach ($component->unknown as $token) { - $ret .= $token->token; - } + $ret .= TokensList::build($component->unknown); return $ret; } } diff --git a/src/Components/Condition.php b/src/Components/Condition.php index 1f57e72..46cab2a 100644 --- a/src/Components/Condition.php +++ b/src/Components/Condition.php @@ -201,7 +201,7 @@ class Condition extends Component { $ret = array(); foreach ($component as $c) { - $ret[] = $f->expr; + $ret[] = $c->expr; } return implode(' ', $ret); } diff --git a/src/Components/FieldDefinition.php b/src/Components/FieldDefinition.php index 9fd494c..0c3ea4f 100644 --- a/src/Components/FieldDefinition.php +++ b/src/Components/FieldDefinition.php @@ -230,36 +230,38 @@ class FieldDefinition extends Component */ public static function build($component) { - $ret = array(); - - foreach ($component as $c) { + if (is_array($component)) { + $ret = array(); + foreach ($component as $c) { + $ret[] = static::build($c); + } + return "(\n" . implode(",\n", $ret) . "\n)"; + } else { $tmp = ''; - if ($f->isConstraint) { + if ($component->isConstraint) { $tmp .= 'CONSTRAINT '; } - if (!empty($f->name)) { - $tmp .= Context::escape($f->name) . ' '; + if (!empty($component->name)) { + $tmp .= Context::escape($component->name) . ' '; } - if (!empty($f->type)) { - $tmp .= DataType::build($f->type) . ' '; + if (!empty($component->type)) { + $tmp .= DataType::build($component->type) . ' '; } - if (!empty($f->key)) { - $tmp .= Key::build($f->key) . ' '; + if (!empty($component->key)) { + $tmp .= Key::build($component->key) . ' '; } - if (!empty($f->references)) { - $tmp .= 'REFERENCES ' . Reference::build($f->references) . ' '; + if (!empty($component->references)) { + $tmp .= 'REFERENCES ' . Reference::build($component->references) . ' '; } - $tmp .= OptionsArray::build($f->options); + $tmp .= OptionsArray::build($component->options); - $ret[] = trim($tmp); + return trim($tmp); } - - return "(\n" . implode(",\n", $ret) . "\n)"; } } diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index 7a3a588..d691e86 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -182,9 +182,29 @@ class OptionsArray extends Component public function has($key) { foreach ($this->options as $option) { - if ((is_array($option)) && ($key === $option['name'])) { + if ($key === $option) { + return true; + } elseif ((is_array($option)) && ($key === $option['name'])) { return $option['value']; - } elseif ($key === $option) { + } + } + return false; + } + + /** + * Removes the option from the array. + * + * @param string $key The key to be removed. + * + * @return bool Whether the key was found and deleted or not. + */ + public function remove($key) + { + foreach ($this->options as $idx => $option) { + if (($key === $option) + || ((is_array($option)) && ($key === $option['name'])) + ) { + unset($this->options[$idx]); return true; } } diff --git a/src/Components/ParameterDefinition.php b/src/Components/ParameterDefinition.php index 3116b24..db0f815 100644 --- a/src/Components/ParameterDefinition.php +++ b/src/Components/ParameterDefinition.php @@ -147,13 +147,13 @@ class ParameterDefinition extends Component $ret = array(); foreach ($component as $c) { $tmp = ''; - if (!empty($f->inOut)) { - $tmp .= $f->inOut . ' '; + if (!empty($c->inOut)) { + $tmp .= $c->inOut . ' '; } $ret[] = trim( - $tmp . Context::escape($f->name) . ' ' . - DataType::build($f->type) + $tmp . Context::escape($c->name) . ' ' . + DataType::build($c->type) ); } return '(' . implode(', ', $ret) . ')'; diff --git a/src/Context.php b/src/Context.php index 99137d1..b3bf4a0 100644 --- a/src/Context.php +++ b/src/Context.php @@ -123,6 +123,10 @@ abstract class Context * https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html */ + // Compatibility mode for Microsoft's SQL server. + // This is the equivalent of ANSI_QUOTES. + const COMPAT_MYSQL = 2; + // https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates const ALLOW_INVALID_DATES = 1; @@ -446,11 +450,12 @@ abstract class Context /** * Escapes the symbol by adding surrounding backticks. * - * @param array|string $str The string to be escaped. + * @param array|string $str The string to be escaped. + * @param string $quote Quote to be used when escaping. * * @return string */ - public static function escape($str) + public static function escape($str, $quote = '`') { if (is_array($str)) { foreach ($str as $key => $value) { @@ -458,10 +463,11 @@ abstract class Context } return $str; } + if (static::$MODE & Context::ANSI_QUOTES) { - return '"' . str_replace('"', '""', $str) . '"'; + $quote = '"'; } - return '`' . str_replace('`', '``', $str) . '`'; + return $quote . str_replace($quote, $quote . $quote, $str) . $quote; } } |