summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Components/PartitionDefinition.php28
-rw-r--r--src/Parser.php299
-rw-r--r--src/Statements/CreateStatement.php47
-rw-r--r--src/Utils/Formatter.php6
4 files changed, 228 insertions, 152 deletions
diff --git a/src/Components/PartitionDefinition.php b/src/Components/PartitionDefinition.php
index b94134a..c7546b0 100644
--- a/src/Components/PartitionDefinition.php
+++ b/src/Components/PartitionDefinition.php
@@ -178,4 +178,32 @@ class PartitionDefinition extends Component
--$list->idx;
return $ret;
}
+
+ /**
+ * @param PartitionDefinition|PartitionDefinition[] $component The component to be built.
+ *
+ * @return string
+ */
+ public static function build($component)
+ {
+ if (is_array($component)) {
+ $ret = array();
+ foreach ($component as $c) {
+ $ret[] = static::build($c);
+ }
+ return "(\n" . implode(",\n", $ret) . "\n)";
+ } else {
+ if ($component->isSubpartition) {
+ return 'SUBPARTITION ' . $component->name;
+ } else {
+ if (!empty($component->subpartitions)) {
+ $subpartitions = ' ' . PartitionDefinition::build($component->subpartitions);
+ }
+ return 'PARTITION ' . $component->name
+ . ' VALUES ' . $component->type . $component->expr
+ . $subpartitions;
+ }
+
+ }
+ }
}
diff --git a/src/Parser.php b/src/Parser.php
index d3a639c..5b74f33 100644
--- a/src/Parser.php
+++ b/src/Parser.php
@@ -113,153 +113,158 @@ namespace SqlParser {
*/
public static $KEYWORD_PARSERS = array(
- // This is not a proper keyword and was added here to help the builder.
- '_OPTIONS' => array(
- 'class' => 'SqlParser\\Components\\OptionsArray',
- 'field' => 'options',
- ),
-
- // This is used only for building.
- 'UNION' => array(
- 'class' => 'SqlParser\\Components\\UnionKeyword',
- 'field' => 'union',
- ),
-
- 'ALTER' => array(
- 'class' => 'SqlParser\\Components\\Expression',
- 'field' => 'table',
- 'options' => array('skipColumn' => true),
- ),
- 'ANALYZE' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'BACKUP' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'CALL' => array(
- 'class' => 'SqlParser\\Components\\FunctionCall',
- 'field' => 'call',
- ),
- 'CHECK' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'CHECKSUM' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'DROP' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'fields',
- 'options' => array('skipColumn' => true),
- ),
- 'FROM' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'from',
- 'options' => array('skipColumn' => true),
- ),
- 'GROUP BY' => array(
- 'class' => 'SqlParser\\Components\\OrderKeyword',
- 'field' => 'group',
- ),
- 'HAVING' => array(
- 'class' => 'SqlParser\\Components\\Condition',
- 'field' => 'having',
- ),
- 'INTO' => array(
- 'class' => 'SqlParser\\Components\\IntoKeyword',
- 'field' => 'into',
- ),
- 'JOIN' => array(
- 'class' => 'SqlParser\\Components\\JoinKeyword',
- 'field' => 'join',
- ),
- 'LEFT JOIN' => array(
- 'class' => 'SqlParser\\Components\\JoinKeyword',
- 'field' => 'join',
- ),
- 'RIGHT JOIN' => array(
- 'class' => 'SqlParser\\Components\\JoinKeyword',
- 'field' => 'join',
- ),
- 'INNER JOIN' => array(
- 'class' => 'SqlParser\\Components\\JoinKeyword',
- 'field' => 'join',
- ),
- 'FULL JOIN' => array(
- 'class' => 'SqlParser\\Components\\JoinKeyword',
- 'field' => 'join',
- ),
- 'LIMIT' => array(
- 'class' => 'SqlParser\\Components\\Limit',
- 'field' => 'limit',
- ),
- 'OPTIMIZE' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'ORDER BY' => array(
- 'class' => 'SqlParser\\Components\\OrderKeyword',
- 'field' => 'order',
- ),
- 'PARTITION' => array(
- 'class' => 'SqlParser\\Components\\ArrayObj',
- 'field' => 'partition',
- ),
- 'PROCEDURE' => array(
- 'class' => 'SqlParser\\Components\\FunctionCall',
- 'field' => 'procedure',
- ),
- 'RENAME' => array(
- 'class' => 'SqlParser\\Components\\RenameOperation',
- 'field' => 'renames',
- ),
- 'REPAIR' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'RESTORE' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'SET' => array(
- 'class' => 'SqlParser\\Components\\SetOperation',
- 'field' => 'set',
- ),
- 'SELECT' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'expr',
- ),
- 'TRUNCATE' => array(
- 'class' => 'SqlParser\\Components\\Expression',
- 'field' => 'table',
- 'options' => array('skipColumn' => true),
- ),
- 'UPDATE' => array(
- 'class' => 'SqlParser\\Components\\ExpressionArray',
- 'field' => 'tables',
- 'options' => array('skipColumn' => true),
- ),
- 'VALUE' => array(
- 'class' => 'SqlParser\\Components\\Array2d',
- 'field' => 'values',
- ),
- 'VALUES' => array(
- 'class' => 'SqlParser\\Components\\Array2d',
- 'field' => 'values',
- ),
- 'WHERE' => array(
- 'class' => 'SqlParser\\Components\\Condition',
- 'field' => 'where',
+ // This is not a proper keyword and was added here to help the
+ // formatter.
+ 'PARTITION BY' => array(),
+ 'SUBPARTITION BY' => array(),
+
+ // This is not a proper keyword and was added here to help the
+ // builder.
+ '_OPTIONS' => array(
+ 'class' => 'SqlParser\\Components\\OptionsArray',
+ 'field' => 'options',
+ ),
+ 'UNION' => array(
+ 'class' => 'SqlParser\\Components\\UnionKeyword',
+ 'field' => 'union',
+ ),
+
+ // Actual clause parsers.
+ 'ALTER' => array(
+ 'class' => 'SqlParser\\Components\\Expression',
+ 'field' => 'table',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'ANALYZE' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'BACKUP' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'CALL' => array(
+ 'class' => 'SqlParser\\Components\\FunctionCall',
+ 'field' => 'call',
+ ),
+ 'CHECK' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'CHECKSUM' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'DROP' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'fields',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'FROM' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'from',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'GROUP BY' => array(
+ 'class' => 'SqlParser\\Components\\OrderKeyword',
+ 'field' => 'group',
+ ),
+ 'HAVING' => array(
+ 'class' => 'SqlParser\\Components\\Condition',
+ 'field' => 'having',
+ ),
+ 'INTO' => array(
+ 'class' => 'SqlParser\\Components\\IntoKeyword',
+ 'field' => 'into',
+ ),
+ 'JOIN' => array(
+ 'class' => 'SqlParser\\Components\\JoinKeyword',
+ 'field' => 'join',
+ ),
+ 'LEFT JOIN' => array(
+ 'class' => 'SqlParser\\Components\\JoinKeyword',
+ 'field' => 'join',
+ ),
+ 'RIGHT JOIN' => array(
+ 'class' => 'SqlParser\\Components\\JoinKeyword',
+ 'field' => 'join',
+ ),
+ 'INNER JOIN' => array(
+ 'class' => 'SqlParser\\Components\\JoinKeyword',
+ 'field' => 'join',
+ ),
+ 'FULL JOIN' => array(
+ 'class' => 'SqlParser\\Components\\JoinKeyword',
+ 'field' => 'join',
+ ),
+ 'LIMIT' => array(
+ 'class' => 'SqlParser\\Components\\Limit',
+ 'field' => 'limit',
+ ),
+ 'OPTIMIZE' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'ORDER BY' => array(
+ 'class' => 'SqlParser\\Components\\OrderKeyword',
+ 'field' => 'order',
+ ),
+ 'PARTITION' => array(
+ 'class' => 'SqlParser\\Components\\ArrayObj',
+ 'field' => 'partition',
+ ),
+ 'PROCEDURE' => array(
+ 'class' => 'SqlParser\\Components\\FunctionCall',
+ 'field' => 'procedure',
+ ),
+ 'RENAME' => array(
+ 'class' => 'SqlParser\\Components\\RenameOperation',
+ 'field' => 'renames',
+ ),
+ 'REPAIR' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'RESTORE' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'SET' => array(
+ 'class' => 'SqlParser\\Components\\SetOperation',
+ 'field' => 'set',
+ ),
+ 'SELECT' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'expr',
+ ),
+ 'TRUNCATE' => array(
+ 'class' => 'SqlParser\\Components\\Expression',
+ 'field' => 'table',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'UPDATE' => array(
+ 'class' => 'SqlParser\\Components\\ExpressionArray',
+ 'field' => 'tables',
+ 'options' => array('skipColumn' => true),
+ ),
+ 'VALUE' => array(
+ 'class' => 'SqlParser\\Components\\Array2d',
+ 'field' => 'values',
+ ),
+ 'VALUES' => array(
+ 'class' => 'SqlParser\\Components\\Array2d',
+ 'field' => 'values',
+ ),
+ 'WHERE' => array(
+ 'class' => 'SqlParser\\Components\\Condition',
+ 'field' => 'where',
),
);
diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php
index 03379ee..fad4017 100644
--- a/src/Statements/CreateStatement.php
+++ b/src/Statements/CreateStatement.php
@@ -15,6 +15,7 @@ use SqlParser\TokensList;
use SqlParser\Components\ArrayObj;
use SqlParser\Components\DataType;
use SqlParser\Components\CreateDefinition;
+use SqlParser\Components\PartitionDefinition;
use SqlParser\Components\Expression;
use SqlParser\Components\OptionsArray;
use SqlParser\Components\ParameterDefinition;
@@ -174,6 +175,41 @@ class CreateStatement extends Statement
public $fields;
/**
+ * Expression used for partitioning.
+ *
+ * @var string
+ */
+ public $partitionBy;
+
+ /**
+ * The number of partitions.
+ *
+ * @var int
+ */
+ public $partitionsNum;
+
+ /**
+ * Expression used for subpartitioning.
+ *
+ * @var string
+ */
+ public $subpartitionBy;
+
+ /**
+ * The number of subpartitions.
+ *
+ * @var int
+ */
+ public $subpartitionsNum;
+
+ /**
+ * The partition of the new table.
+ *
+ * @var PartitionDefinition[]
+ */
+ public $partitions;
+
+ /**
* If `CREATE TRIGGER` the name of the table.
*
* Used by `CREATE TRIGGER`.
@@ -232,16 +268,19 @@ class CreateStatement extends Statement
$partition = '';
if (!empty($this->partitionBy)) {
- $partition .= ' PARTITION BY ' . $this->partitionBy;
+ $partition .= "\nPARTITION BY " . $this->partitionBy;
}
if (!empty($this->partitionsNum)) {
- $partition .= ' PARTITIONS ' . $this->partitionsNum;
+ $partition .= "\nPARTITIONS " . $this->partitionsNum;
}
if (!empty($this->subpartitionBy)) {
- $partition .= ' SUBPARTITION BY ' . $this->subpartitionBy;
+ $partition .= "\nSUBPARTITION BY " . $this->subpartitionBy;
}
if (!empty($this->subpartitionsNum)) {
- $partition .= ' SUBPARTITIONS ' . $this->subpartitionsNum;
+ $partition .= "\nSUBPARTITIONS " . $this->subpartitionsNum;
+ }
+ if (!empty($this->partitions)) {
+ $partition .= "\n" . PartitionDefinition::build($this->partitions);
}
return 'CREATE '
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php
index aba215a..b9552bf 100644
--- a/src/Utils/Formatter.php
+++ b/src/Utils/Formatter.php
@@ -41,8 +41,12 @@ class Formatter
*/
public static $INLINE_CLAUSES = array(
'CREATE' => true,
- 'PROCEDURE' => true,
'LIMIT' => true,
+ 'PARTITION BY' => true,
+ 'PARTITION' => true,
+ 'PROCEDURE' => true,
+ 'SUBPARTITION BY' => true,
+ 'VALUES' => true,
);
/**