Skip to content

Commit

Permalink
Fixed minor style issues in generated queries.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dan Ungureanu committed Aug 20, 2015
1 parent 14c54da commit 1b2988f
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ public static function parse(
* `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.
Expand Down
3 changes: 2 additions & 1 deletion src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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 !== '')) {
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Array2d.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Components/ArrayObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
Expand Down
10 changes: 7 additions & 3 deletions src/Components/CreateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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 = '';

Expand All @@ -291,7 +292,10 @@ public static function build($component)
}

if (!empty($component->type)) {
$tmp .= $component->type . ' ';
$tmp .= DataType::build(
$component->type,
array('lowercase' => true)
) . ' ';
}

if (!empty($component->key)) {
Expand Down
12 changes: 8 additions & 4 deletions src/Components/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
}
}
3 changes: 2 additions & 1 deletion src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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, ', ');
Expand Down
3 changes: 2 additions & 1 deletion src/Components/ExpressionArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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) {
Expand Down
3 changes: 2 additions & 1 deletion src/Components/FunctionCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Components/IntoKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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) ?
Expand Down
3 changes: 2 additions & 1 deletion src/Components/JoinKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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) {
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

/**
* @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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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;
Expand Down
3 changes: 2 additions & 1 deletion src/Components/OptionsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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 '';
Expand Down
3 changes: 2 additions & 1 deletion src/Components/OrderKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
Expand Down
3 changes: 2 additions & 1 deletion src/Components/ParameterDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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) . ')';
Expand Down
3 changes: 2 additions & 1 deletion src/Components/PartitionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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)";
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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)
Expand Down
3 changes: 2 additions & 1 deletion src/Components/RenameOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
Expand Down
3 changes: 2 additions & 1 deletion src/Components/SetOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

/**
* @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);
Expand Down
3 changes: 2 additions & 1 deletion src/Components/UnionKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct(array $options = array())
*
* @var string
*/
'indentation' => " ",
'indentation' => " ",

/**
* Whether comments should be removed or not.
Expand Down
8 changes: 4 additions & 4 deletions tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function testBuilderTable()

$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()
);
Expand All @@ -68,8 +68,8 @@ public function testBuilderTable()
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"
Expand Down

0 comments on commit 1b2988f

Please sign in to comment.