From 5405c6e1eae640dfba5021ca92279635f0dfe503 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 10 Jan 2016 14:15:00 +0100 Subject: [PATCH] Preparing for schema query suppory --- DataStorage/Database/BuilderAbstract.php | 88 ++++++++++++ .../Connection/ConnectionAbstract.php | 25 +++- .../Connection/ConnectionInterface.php | 11 ++ .../Database/Connection/MysqlConnection.php | 6 +- DataStorage/Database/Grammar.php | 39 ----- DataStorage/Database/GrammarAbstract.php | 136 ++++++++++++++++++ DataStorage/Database/Query/Builder.php | 65 +-------- .../Database/Query/Grammar/Grammar.php | 108 +------------- DataStorage/Database/Schema/Builder.php | 76 ++++++++++ .../Database/Schema/Grammar/Grammar.php | 44 ++++++ .../Schema/Grammar/GrammarInterface.php | 40 ------ .../Database/Schema/Grammar/MysqlGrammar.php | 73 ++++++++++ DataStorage/Database/Schema/QueryType.php | 7 +- 13 files changed, 466 insertions(+), 252 deletions(-) create mode 100644 DataStorage/Database/BuilderAbstract.php delete mode 100644 DataStorage/Database/Grammar.php create mode 100644 DataStorage/Database/GrammarAbstract.php create mode 100644 DataStorage/Database/Schema/Grammar/Grammar.php diff --git a/DataStorage/Database/BuilderAbstract.php b/DataStorage/Database/BuilderAbstract.php new file mode 100644 index 000000000..4bf7bc298 --- /dev/null +++ b/DataStorage/Database/BuilderAbstract.php @@ -0,0 +1,88 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database; +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; + +/** + * Database query builder. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class BuilderAbstract +{ + protected $grammar = null; + + /** + * Database connectino. + * + * @var ConnectionAbstract + * @since 1.0.0 + */ + protected $connection = null; + + /** + * Query type. + * + * @var int + * @since 1.0.0 + */ + protected $type = null; + + /** + * Prefix. + * + * @var \string + * @since 1.0.0 + */ + protected $prefix = ''; + + /** + * Set prefix. + * + * @param \string $prefix Prefix + * + * @return BuilderAbstract + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function prefix(\string $prefix) + { + $this->prefix = $prefix; + + return $this; + } + + /** + * Get prefix. + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getPrefix() : \string + { + return $this->prefix; + } +} diff --git a/DataStorage/Database/Connection/ConnectionAbstract.php b/DataStorage/Database/Connection/ConnectionAbstract.php index e2717476e..fc9adf039 100644 --- a/DataStorage/Database/Connection/ConnectionAbstract.php +++ b/DataStorage/Database/Connection/ConnectionAbstract.php @@ -17,6 +17,7 @@ namespace phpOMS\DataStorage\Database\Connection; use phpOMS\DataStorage\Database\DatabaseStatus; use phpOMS\DataStorage\Database\Query\Grammar\Grammar; +use phpOMS\DataStorage\Database\Schema\Query\Grammar\Grammar as SchemaGrammar; /** * Database handler. @@ -87,6 +88,14 @@ abstract class ConnectionAbstract implements ConnectionInterface */ protected $grammar = null; + /** + * Database grammar. + * + * @var Grammar + * @since 1.0.0 + */ + protected $schemaGrammar = null; + /** * {@inheritdoc} */ @@ -117,8 +126,8 @@ abstract class ConnectionAbstract implements ConnectionInterface } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getGrammar() : Grammar { if (!isset($this->grammar)) { @@ -128,6 +137,18 @@ abstract class ConnectionAbstract implements ConnectionInterface return $this->grammar; } + /** + * {@inheritdoc} + */ + public function getSchemaGrammar() : SchemaGrammar + { + if (!isset($this->schemaGrammar)) { + $this->schemaGrammar = new SchemaGrammar(); + } + + return $this->schemaGrammar; + } + /** * Object destructor. * diff --git a/DataStorage/Database/Connection/ConnectionInterface.php b/DataStorage/Database/Connection/ConnectionInterface.php index c7fad96d3..0ea8f96e1 100644 --- a/DataStorage/Database/Connection/ConnectionInterface.php +++ b/DataStorage/Database/Connection/ConnectionInterface.php @@ -16,6 +16,7 @@ namespace phpOMS\DataStorage\Database\Connection; use phpOMS\DataStorage\Database\Query\Grammar\Grammar; +use phpOMS\DataStorage\Database\Schema\Query\Grammar\Grammar as SchemaGrammar; /** * Database connection interface. @@ -85,4 +86,14 @@ interface ConnectionInterface */ public function getGrammar() : Grammar; + /** + * Return grammar for this connection. + * + * @return SchemaGrammar + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getSchemaGrammar() : SchemaGrammar; + } diff --git a/DataStorage/Database/Connection/MysqlConnection.php b/DataStorage/Database/Connection/MysqlConnection.php index 05896f05d..ad1cd48cd 100644 --- a/DataStorage/Database/Connection/MysqlConnection.php +++ b/DataStorage/Database/Connection/MysqlConnection.php @@ -18,6 +18,7 @@ namespace phpOMS\DataStorage\Database\Connection; use phpOMS\DataStorage\Database\DatabaseStatus; use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\Query\Grammar\MysqlGrammar; +use phpOMS\DataStorage\Database\Schema\Grammar\MysqlGrammar as MysqlSchemaGrammar; /** * Database handler. @@ -48,8 +49,9 @@ class MysqlConnection extends ConnectionAbstract */ public function __construct(array $dbdata) { - $this->type = DatabaseType::MYSQL; - $this->grammar = new MysqlGrammar(); + $this->type = DatabaseType::MYSQL; + $this->grammar = new MysqlGrammar(); + $this->schemaGrammar = new MysqlSchemaGrammar(); $this->connect($dbdata); } diff --git a/DataStorage/Database/Grammar.php b/DataStorage/Database/Grammar.php deleted file mode 100644 index 2f873b48b..000000000 --- a/DataStorage/Database/Grammar.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ - -namespace phpOMS\DataStorage\Database; - -abstract class Grammar -{ - - protected $tablePrefix = ''; - - public function getDateFormat() : \string - { - return 'Y-m-d H:i:s'; - } - - public function getTablePrefix() : \string - { - return $this->tablePrefix; - } - - public function setTablePrefix(\string $prefix) - { - $this->tablePrefix = $prefix; - } - -} diff --git a/DataStorage/Database/GrammarAbstract.php b/DataStorage/Database/GrammarAbstract.php new file mode 100644 index 000000000..6910e5794 --- /dev/null +++ b/DataStorage/Database/GrammarAbstract.php @@ -0,0 +1,136 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database; + +abstract class GrammarAbstract +{ + /** + * Comment style. + * + * @var \string + * @since 1.0.0 + */ + protected $comment = '--'; + + /** + * String quotes style. + * + * @var \string + * @since 1.0.0 + */ + protected $valueQuotes = '\''; + + /** + * System identifier. + * + * @var \string + * @since 1.0.0 + */ + public $systemIdentifier = '"'; + + /** + * And operator. + * + * @var \string + * @since 1.0.0 + */ + protected $and = 'AND'; + + /** + * Or operator. + * + * @var \string + * @since 1.0.0 + */ + protected $or = 'OR'; + + protected $tablePrefix = ''; + + /** + * Compile to query. + * + * @param Builder $query Builder + * + * @return \string + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function compileQuery($query) : \string + { + return trim( + implode(' ', + array_filter( + $this->compileComponents($query), + function ($value) { + return (string) $value !== ''; + } + ) + ) + ) . ';'; + } + + /** + * Expressionize elements. + * + * @param array $elements Elements + * @param \string $prefix Prefix for table + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function expressionizeTableColumn(array $elements, \string $prefix = '') : \string + { + $expression = ''; + + foreach ($elements as $key => $element) { + if (is_string($element) && $element !== '*') { + $expression .= $this->compileSystem($element, $prefix) . ', '; + } elseif (is_string($element) && $element === '*') { + $expression .= '*, '; + } elseif ($element instanceof \Closure) { + $expression .= $element() . ', '; + } elseif ($element instanceof BuilderAbstract) { + $expression .= $element->toSql() . ', '; + } else { + throw new \InvalidArgumentException(); + } + } + + return rtrim($expression, ', '); + } + + public function getDateFormat() : \string + { + return 'Y-m-d H:i:s'; + } + + public function getTablePrefix() : \string + { + return $this->tablePrefix; + } + + public function setTablePrefix(\string $prefix) + { + $this->tablePrefix = $prefix; + } + +} diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 1edf6e1f6..d75f61612 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -16,6 +16,7 @@ namespace phpOMS\DataStorage\Database\Query; +use phpOMS\DataStorage\Database\BuilderAbstract; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; use phpOMS\DataStorage\Database\Query; @@ -30,7 +31,7 @@ use phpOMS\DataStorage\Database\Query; * @link http://orange-management.com * @since 1.0.0 */ -class Builder +class Builder extends BuilderAbstract { /** @@ -153,44 +154,12 @@ class Builder */ public $lock = false; - /** - * Database connectino. - * - * @var ConnectionAbstract - * @since 1.0.0 - */ - protected $connection = null; - - /** - * Grammar. - * - * @var \phpOMS\DataStorage\Database\Query\Grammar\GrammarInterface - * @since 1.0.0 - */ - protected $grammar = null; - - /** - * Query type. - * - * @var QueryType - * @since 1.0.0 - */ - protected $type = QueryType::INSERT; - protected $unionLimit = null; protected $unionOffset = null; protected $unionOrders = []; - /** - * Prefix. - * - * @var \string - * @since 1.0.0 - */ - protected $prefix = ''; - /** * Comparison operators. * @@ -241,36 +210,6 @@ class Builder $this->grammar = $connection->getGrammar(); } - /** - * Set prefix. - * - * @param \string $prefix Prefix - * - * @return Builder - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function prefix(\string $prefix) - { - $this->prefix = $prefix; - - return $this; - } - - /** - * Get prefix. - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getPrefix() : \string - { - return $this->prefix; - } - /** * Select. * diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index f15ff98b0..f2132fc12 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -16,6 +16,7 @@ namespace phpOMS\DataStorage\Database\Query\Grammar; +use phpOMS\DataStorage\Database\GrammarAbstract; use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\Query\Column; use phpOMS\DataStorage\Database\Query\QueryType; @@ -31,49 +32,8 @@ use phpOMS\DataStorage\Database\Query\QueryType; * @link http://orange-management.com * @since 1.0.0 */ -class Grammar extends \phpOMS\DataStorage\Database\Grammar +class Grammar extends GrammarAbstract { - - /** - * Comment style. - * - * @var \string - * @since 1.0.0 - */ - protected $comment = '--'; - - /** - * String quotes style. - * - * @var \string - * @since 1.0.0 - */ - protected $valueQuotes = '\''; - - /** - * System identifier. - * - * @var \string - * @since 1.0.0 - */ - public $systemIdentifier = '"'; - - /** - * And operator. - * - * @var \string - * @since 1.0.0 - */ - protected $and = 'AND'; - - /** - * Or operator. - * - * @var \string - * @since 1.0.0 - */ - protected $or = 'OR'; - /** * Select components. * @@ -119,32 +79,6 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar 'wheres', ]; - /** - * Compile to query. - * - * @param Builder $query Builder - * - * @return \string - * - * @throws - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function compileQuery($query) : \string - { - return trim( - implode(' ', - array_filter( - $this->compileComponents($query), - function ($value) { - return (string) $value !== ''; - } - ) - ) - ) . ';'; - } - /** * Compile components. * @@ -203,45 +137,13 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar { $expression = $this->expressionizeTableColumn($columns, $query->getPrefix()); - if ($expression == '') { - return ''; + if ($expression === '') { + $expression = '*'; } return ($query->distinct ? 'SELECT DISTINCT ' : 'SELECT ') . $expression; } - /** - * Expressionize elements. - * - * @param array $elements Elements - * @param \string $prefix Prefix for table - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - protected function expressionizeTableColumn(array $elements, \string $prefix = '') : \string - { - $expression = ''; - - foreach ($elements as $key => $element) { - if (is_string($element) && $element !== '*') { - $expression .= $this->compileSystem($element, $prefix) . ', '; - } elseif (is_string($element) && $element === '*') { - $expression .= '*, '; - } elseif ($element instanceof \Closure) { - $expression .= $element() . ', '; - } elseif ($element instanceof Builder) { - $expression .= $element->toSql() . ', '; - } else { - throw new \InvalidArgumentException(); - } - } - - return rtrim($expression, ', '); - } - /** * Compile from. * @@ -257,7 +159,7 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar { $expression = $this->expressionizeTableColumn($table, $query->getPrefix()); - if ($expression == '') { + if ($expression === '') { return ''; } diff --git a/DataStorage/Database/Schema/Builder.php b/DataStorage/Database/Schema/Builder.php index e69de29bb..787149ed6 100644 --- a/DataStorage/Database/Schema/Builder.php +++ b/DataStorage/Database/Schema/Builder.php @@ -0,0 +1,76 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Schema\Query; + +use phpOMS\DataStorage\Database\BuilderAbstract; +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; +use phpOMS\DataStorage\Database\Query; +use phpOMS\DataStorage\Database\Schema\QueryType; + +/** + * Database query builder. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Builder extends BuilderAbstract +{ + private $type = QueryType::SELECT; + + private $table = []; + + /** + * Constructor. + * + * @param ConnectionAbstract $connection Database connection + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(ConnectionAbstract $connection) + { + $this->connection = $connection; + $this->grammar = $connection->getSchemaGrammar(); + } + + public function select(...$table) + { + $this->type = QueryType::SELECT; + $this->table += $table; + $this->table = array_unique($this->table); + } + + public function drop(\string $table) + { + + } + + public function create(\string $table) + { + + } + + public function alter(array $column) + { + + } +} diff --git a/DataStorage/Database/Schema/Grammar/Grammar.php b/DataStorage/Database/Schema/Grammar/Grammar.php new file mode 100644 index 000000000..6fc3e193b --- /dev/null +++ b/DataStorage/Database/Schema/Grammar/Grammar.php @@ -0,0 +1,44 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Schema\Grammar; + +use phpOMS\DataStorage\Database\GrammarAbstract; + +/** + * Database query grammar. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Grammar extends GrammarAbstract +{ + /** + * Select components. + * + * @var \string[] + * @since 1.0.0 + */ + protected $selectComponents = [ + 'selects', + 'from', + ]; +} diff --git a/DataStorage/Database/Schema/Grammar/GrammarInterface.php b/DataStorage/Database/Schema/Grammar/GrammarInterface.php index eb966bd3b..a0c8d4f2f 100644 --- a/DataStorage/Database/Schema/Grammar/GrammarInterface.php +++ b/DataStorage/Database/Schema/Grammar/GrammarInterface.php @@ -18,44 +18,4 @@ namespace phpOMS\DataStorage\Database\Schema; interface GrammarInterface { - public function typeTinyInt($places); - - public function typeSmallInt($places); - - public function typeMediumInt($places); - - public function typeInt($places); - - public function typeBigInt($places); - - public function typeFloat($m, $e, $b = 10); - - public function typeDouble($m, $e, $b = 10); - - public function typeDecimal($a, $b); - - public function typeBoolean(); - - public function typeJson(); - - public function typeDate(); - - public function typeTime(); - - public function typeDateTime(); - - public function typeTimestamp(); - - public function typeBinary(); - - public function typeChar(); - - public function typeString(); - - public function typeMediumText(); - - public function typeText(); - - public function typeLongText(); - } diff --git a/DataStorage/Database/Schema/Grammar/MysqlGrammar.php b/DataStorage/Database/Schema/Grammar/MysqlGrammar.php index e69de29bb..5a014bf35 100644 --- a/DataStorage/Database/Schema/Grammar/MysqlGrammar.php +++ b/DataStorage/Database/Schema/Grammar/MysqlGrammar.php @@ -0,0 +1,73 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\DataStorage\Database\Schema\Grammar; + +use phpOMS\DataStorage\Database\Query\Builder; + +/** + * Database query grammar. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class MysqlGrammar extends Grammar +{ + /** + * Compile select. + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileSelects(Builder $query, array $columns) : \string + { + $expression = $this->expressionizeTableColumn($columns); + + if ($expression === '') { + $expression = '*'; + } + + return $expression; + } + + /** + * Compile from. + * + * @param Builder $query Builder + * @param array $table Tables + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileFrom(Builder $query, array $table) : \string + { + $expression = $this->expressionizeTableColumn('information_schema.tables'); + + return 'FROM ' . $expression; + } +} diff --git a/DataStorage/Database/Schema/QueryType.php b/DataStorage/Database/Schema/QueryType.php index bd834cf07..f8af17edb 100644 --- a/DataStorage/Database/Schema/QueryType.php +++ b/DataStorage/Database/Schema/QueryType.php @@ -32,7 +32,8 @@ use phpOMS\Datatypes\Enum; */ abstract class QueryType extends Enum { - const CREATE = 0; - const DROP = 1; - const ALTER = 2; + const SELECT = 0; + const CREATE = 1; + const DROP = 2; + const ALTER = 3; }