diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-08-20 22:27:52 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-08-20 22:33:08 +0300 |
commit | 1b2988f296611f7294593fd1fff07c70ac514e2a (patch) | |
tree | ed618f577fe3acd835a3c801f1f8ec30d2956f8f | |
parent | 14c54da98c3b4f18c78ff44c22fbdd20e8ef6e18 (diff) | |
download | sql-parser-1b2988f296611f7294593fd1fff07c70ac514e2a.zip sql-parser-1b2988f296611f7294593fd1fff07c70ac514e2a.tar.gz sql-parser-1b2988f296611f7294593fd1fff07c70ac514e2a.tar.bz2 |
Fixed minor style issues in generated queries.v1.0.0
Introduced options for builders.
Data types are lower case in CREATE TABLE statements, but in any other case
they continue to be upper case.
Formatter uses 2 spaces instead of 4 for indentation as specified in the MySQL
Coding Guidelines.
https://dev.mysql.com/doc/internals/en/indentation-spacing.html.
24 files changed, 62 insertions, 34 deletions
diff --git a/src/Component.php b/src/Component.php index 1678706..eff88e6 100644 --- a/src/Component.php +++ b/src/Component.php @@ -70,12 +70,13 @@ namespace SqlParser { * `static::parse`. * * @param mixed $component The component to be built. + * @param array $options Parameters for building. * * @throws \Exception Not implemented yet. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { // This method should be abstract, but it can't be both static and // abstract. diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index 348f81f..ee9302c 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -210,10 +210,11 @@ class AlterOperation extends Component /** * @param AlterOperation $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { $ret = $component->options . ' '; if ((isset($component->field)) && ($component->field !== '')) { diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index 268dcea..e4c0ed7 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -124,10 +124,11 @@ class Array2d extends Component /** * @param ArrayObj[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { return ArrayObj::build($component); } diff --git a/src/Components/ArrayObj.php b/src/Components/ArrayObj.php index d3017fa..9f4e972 100644 --- a/src/Components/ArrayObj.php +++ b/src/Components/ArrayObj.php @@ -143,10 +143,11 @@ class ArrayObj extends Component /** * @param ArrayObj|ArrayObj[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode(', ', $component); diff --git a/src/Components/Condition.php b/src/Components/Condition.php index c8b4d1e..43347a8 100644 --- a/src/Components/Condition.php +++ b/src/Components/Condition.php @@ -197,10 +197,11 @@ class Condition extends Component /** * @param Condition[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode(' ', $component); diff --git a/src/Components/CreateDefinition.php b/src/Components/CreateDefinition.php index 54c2475..0ef7c4a 100644 --- a/src/Components/CreateDefinition.php +++ b/src/Components/CreateDefinition.php @@ -272,13 +272,14 @@ class CreateDefinition extends Component /** * @param CreateDefinition|CreateDefinition[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { - return "(\n" . implode(",\n", $component) . "\n)"; + return "(\n " . implode(",\n ", $component) . "\n)"; } else { $tmp = ''; @@ -291,7 +292,10 @@ class CreateDefinition extends Component } if (!empty($component->type)) { - $tmp .= $component->type . ' '; + $tmp .= DataType::build( + $component->type, + array('lowercase' => true) + ) . ' '; } if (!empty($component->key)) { diff --git a/src/Components/DataType.php b/src/Components/DataType.php index 8d42ea1..a3aca73 100644 --- a/src/Components/DataType.php +++ b/src/Components/DataType.php @@ -151,16 +151,20 @@ class DataType extends Component /** * @param DataType $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { - $tmp = ''; + $name = (empty($options['lowercase'])) ? + $component->name : strtolower($component->name); + + $parameters = ''; if (!empty($component->parameters)) { - $tmp = '(' . implode(', ', $component->parameters) . ')'; + $parameters = '(' . implode(',', $component->parameters) . ')'; } - return trim($component->name . $tmp . ' ' . $component->options); + return trim($name . $parameters . ' ' . $component->options); } } diff --git a/src/Components/Expression.php b/src/Components/Expression.php index ad4d5e8..1f8752e 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -351,10 +351,11 @@ class Expression extends Component /** * @param Expression|Expression[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode($component, ', '); diff --git a/src/Components/ExpressionArray.php b/src/Components/ExpressionArray.php index 82e67c9..8327296 100644 --- a/src/Components/ExpressionArray.php +++ b/src/Components/ExpressionArray.php @@ -106,10 +106,11 @@ class ExpressionArray extends Component /** * @param Expression[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { $ret = array(); foreach ($component as $frag) { diff --git a/src/Components/FunctionCall.php b/src/Components/FunctionCall.php index 55d9e2c..54e95ac 100644 --- a/src/Components/FunctionCall.php +++ b/src/Components/FunctionCall.php @@ -115,10 +115,11 @@ class FunctionCall extends Component /** * @param FunctionCall $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { return $component->name . $component->parameters; } diff --git a/src/Components/IntoKeyword.php b/src/Components/IntoKeyword.php index 05540e6..b230308 100644 --- a/src/Components/IntoKeyword.php +++ b/src/Components/IntoKeyword.php @@ -133,10 +133,11 @@ class IntoKeyword extends Component /** * @param IntoKeyword $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if ($component->dest instanceof Expression) { $columns = !empty($component->columns) ? diff --git a/src/Components/JoinKeyword.php b/src/Components/JoinKeyword.php index b95912e..529763b 100644 --- a/src/Components/JoinKeyword.php +++ b/src/Components/JoinKeyword.php @@ -151,10 +151,11 @@ class JoinKeyword extends Component /** * @param JoinKeyword[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { $ret = array(); foreach ($component as $c) { diff --git a/src/Components/Key.php b/src/Components/Key.php index ef5d3a8..89371fa 100644 --- a/src/Components/Key.php +++ b/src/Components/Key.php @@ -152,17 +152,18 @@ class Key extends Component } /** - * @param Key $component The component to be built. + * @param Key $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { $ret = $component->type . ' '; if (!empty($component->name)) { $ret .= Context::escape($component->name) . ' '; } - $ret .= '(' . implode(', ', Context::escape($component->columns)) . ') ' + $ret .= '(' . implode(',', Context::escape($component->columns)) . ') ' . $component->options; return trim($ret); } diff --git a/src/Components/Limit.php b/src/Components/Limit.php index 8622c96..edf663a 100644 --- a/src/Components/Limit.php +++ b/src/Components/Limit.php @@ -122,10 +122,11 @@ class Limit extends Component /** * @param Limit $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (empty($component->offset)) { return $component->rowCount; diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index a496273..f24635a 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -251,10 +251,11 @@ class OptionsArray extends Component /** * @param OptionsArray $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (empty($component->options)) { return ''; diff --git a/src/Components/OrderKeyword.php b/src/Components/OrderKeyword.php index b6918e9..9c1ad59 100644 --- a/src/Components/OrderKeyword.php +++ b/src/Components/OrderKeyword.php @@ -127,10 +127,11 @@ class OrderKeyword extends Component /** * @param OrderKeyword|OrderKeyword[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode(', ', $component); diff --git a/src/Components/ParameterDefinition.php b/src/Components/ParameterDefinition.php index d24aefc..9ed9cfc 100644 --- a/src/Components/ParameterDefinition.php +++ b/src/Components/ParameterDefinition.php @@ -140,10 +140,11 @@ class ParameterDefinition extends Component /** * @param ParameterDefinition[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return '(' . implode(', ', $component) . ')'; diff --git a/src/Components/PartitionDefinition.php b/src/Components/PartitionDefinition.php index ad31176..34acbb8 100644 --- a/src/Components/PartitionDefinition.php +++ b/src/Components/PartitionDefinition.php @@ -192,10 +192,11 @@ class PartitionDefinition extends Component /** * @param PartitionDefinition|PartitionDefinition[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return "(\n" . implode(",\n", $component) . "\n)"; diff --git a/src/Components/Reference.php b/src/Components/Reference.php index 3515791..6826f7f 100644 --- a/src/Components/Reference.php +++ b/src/Components/Reference.php @@ -137,10 +137,11 @@ class Reference extends Component /** * @param Reference $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { return trim( Context::escape($component->table) diff --git a/src/Components/RenameOperation.php b/src/Components/RenameOperation.php index b8b1691..5f7c9a5 100644 --- a/src/Components/RenameOperation.php +++ b/src/Components/RenameOperation.php @@ -162,10 +162,11 @@ class RenameOperation extends Component /** * @param RenameOperation $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode(', ', $component); diff --git a/src/Components/SetOperation.php b/src/Components/SetOperation.php index d272cab..3d04320 100644 --- a/src/Components/SetOperation.php +++ b/src/Components/SetOperation.php @@ -122,10 +122,11 @@ class SetOperation extends Component /** * @param SetOperation|SetOperation[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { if (is_array($component)) { return implode(', ', $component); diff --git a/src/Components/UnionKeyword.php b/src/Components/UnionKeyword.php index 8cf8a9f..d1a1ef5 100644 --- a/src/Components/UnionKeyword.php +++ b/src/Components/UnionKeyword.php @@ -25,10 +25,11 @@ class UnionKeyword extends Component /** * @param SelectStatement[] $component The component to be built. + * @param array $options Parameters for building. * * @return string */ - public static function build($component) + public static function build($component, array $options = array()) { return implode(' UNION ', $component); } diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index 998d94b..ebdea42 100644 --- a/src/Utils/Formatter.php +++ b/src/Utils/Formatter.php @@ -80,7 +80,7 @@ class Formatter * * @var string */ - 'indentation' => " ", + 'indentation' => " ", /** * Whether comments should be removed or not. diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index 418a0ba..64712f9 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -58,8 +58,8 @@ class CreateStatementTest extends TestCase $this->assertEquals( "CREATE TABLE `test` (\n" . - "`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n" . - "PRIMARY KEY (`id`)\n" . + " `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n" . + " PRIMARY KEY (`id`)\n" . ") ", $stmt->build() ); @@ -68,8 +68,8 @@ class CreateStatementTest extends TestCase public function testBuilderPartitions() { $query = 'CREATE TABLE ts (' . "\n" - . '`id` INT,' . "\n" - . '`purchased` DATE' . "\n" + . ' `id` int,' . "\n" + . ' `purchased` date' . "\n" . ') ' . "\n" . 'PARTITION BY RANGE(YEAR(purchased))' . "\n" . 'PARTITIONS 3' . "\n" |