diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 0604776ba..d03bf9869 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -25,7 +25,7 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; * @link http://website.orange-management.de * @since 1.0.0 */ -final class Builder extends BuilderAbstract +class Builder extends BuilderAbstract { /** * Is read only. diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index df695b32a..3f4dbd8cd 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -144,15 +144,15 @@ class Grammar extends GrammarAbstract { switch ($type) { case QueryType::SELECT: - return $components = $this->selectComponents; + return $this->selectComponents; case QueryType::INSERT: - return $components = $this->insertComponents; + return $this->insertComponents; case QueryType::UPDATE: - return $components = $this->updateComponents; + return $this->updateComponents; case QueryType::DELETE: - return $components = $this->deleteComponents; + return $this->deleteComponents; case QueryType::RANDOM: - return $components = $this->selectComponents; + return $this->selectComponents; default: throw new \InvalidArgumentException('Unknown query type.'); } diff --git a/DataStorage/Database/Schema/Builder.php b/DataStorage/Database/Schema/Builder.php index bfd9c2a97..d84a67160 100644 --- a/DataStorage/Database/Schema/Builder.php +++ b/DataStorage/Database/Schema/Builder.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Database\Schema; -use phpOMS\DataStorage\Database\BuilderAbstract; +use phpOMS\DataStorage\Database\Query\Builder as QueryBuilder; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; /** @@ -25,10 +25,8 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; * @link http://website.orange-management.de * @since 1.0.0 */ -class Builder extends BuilderAbstract +class Builder extends QueryBuilder { - public $table = []; - public $drop = []; /** @@ -44,13 +42,6 @@ class Builder extends BuilderAbstract $this->grammar = $connection->getSchemaGrammar(); } - public function select(...$table) : void - { - $this->type = QueryType::SELECT; - $this->table += $table; - $this->table = array_unique($this->table); - } - public function drop(...$table) { $this->type = QueryType::DROP; diff --git a/DataStorage/Database/Schema/Grammar/Grammar.php b/DataStorage/Database/Schema/Grammar/Grammar.php index 09d2086ba..a415ce179 100644 --- a/DataStorage/Database/Schema/Grammar/Grammar.php +++ b/DataStorage/Database/Schema/Grammar/Grammar.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Database\Schema\Grammar; use phpOMS\DataStorage\Database\BuilderAbstract; -use phpOMS\DataStorage\Database\GrammarAbstract; +use phpOMS\DataStorage\Database\Query\Grammar\Grammar as QueryGrammar; use phpOMS\DataStorage\Database\Schema\QueryType; /** @@ -26,19 +26,8 @@ use phpOMS\DataStorage\Database\Schema\QueryType; * @link http://website.orange-management.de * @since 1.0.0 */ -class Grammar extends GrammarAbstract +class Grammar extends QueryGrammar { - /** - * Select components. - * - * @var string[] - * @since 1.0.0 - */ - protected $selectComponents = [ - 'selects', - 'from', - ]; - /** * Select components. * @@ -50,34 +39,22 @@ class Grammar extends GrammarAbstract ]; /** - * Compile components based on query type. + * Get query components based on query type. * - * @param BuilderAbstract $query Query + * @param int $type Query type * - * @return array + * @return array Array of components to build query * * @since 1.0.0 */ - public function compileComponents(BuilderAbstract $query) : array + private function getComponents(int $type) : array { - $sql = []; - - switch ($query->getType()) { + switch ($type) { case QueryType::DROP: - $components = $this->dropComponents; - break; + return $this->dropComponents; default: - throw new \InvalidArgumentException('Unknown query type.'); + return parent::getComponents($type); } - - /* Loop all possible query components and if they exist compile them. */ - foreach ($components as $component) { - if (isset($query->{$component}) && !empty($query->{$component})) { - $sql[$component] = $this->{'compile' . \ucfirst($component)}($query, $query->{$component}); - } - } - - return $sql; } /** diff --git a/DataStorage/Database/Schema/QueryType.php b/DataStorage/Database/Schema/QueryType.php index 95785a4b6..9ec09d47d 100644 --- a/DataStorage/Database/Schema/QueryType.php +++ b/DataStorage/Database/Schema/QueryType.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Database\Schema; -use phpOMS\Stdlib\Base\Enum; +use phpOMS\DataStorage\Database\Query\QueryType as DefaultQueryType; /** * Database type enum. @@ -26,10 +26,8 @@ use phpOMS\Stdlib\Base\Enum; * @link http://website.orange-management.de * @since 1.0.0 */ -abstract class QueryType extends Enum +abstract class QueryType extends DefaultQueryType { - public const SELECT = 0; - public const CREATE = 1; - public const DROP = 2; - public const ALTER = 3; + public const DROP = 128; + public const ALTER = 129; } diff --git a/DataStorage/Database/SchemaMapper.php b/DataStorage/Database/SchemaMapper.php new file mode 100644 index 000000000..e5dedff59 --- /dev/null +++ b/DataStorage/Database/SchemaMapper.php @@ -0,0 +1,86 @@ +db = $db; + } + + public function getTables() : array + { + $builder = new Builder($this->db); + $tNames = $builder->selectTables()->execute(); + + $tables = []; + foreach ($tNames as $name) + { + $tables[] = $this->getTable($name); + } + + return $tables; + } + + public function getTable(string $name) : Table + { + $table = new Table(); + + return $table; + } + + public function getFields(string $table) : array + { + $builder = new Builder($this->db); + $tNames = $builder->selectFields()->execute(); + + $fields = []; + foreach ($tNames as $name) + { + $fields[] = $this->getField($name); + } + + return $fields; + } + + public function getField(string $table, string $name) : Field + { + $field = new Field(); + + return $field; + } +} \ No newline at end of file diff --git a/Model/Message/Notify.php b/Model/Message/Notify.php index cbb3058a9..7be10d873 100644 --- a/Model/Message/Notify.php +++ b/Model/Message/Notify.php @@ -24,7 +24,7 @@ use phpOMS\Contract\ArrayableInterface; * @link http://website.orange-management.de * @since 1.0.0 */ -class Notify implements \Serializable, ArrayableInterface +class Notify implements \Serializable, ArrayableInterface, \JsonSerializable { /** diff --git a/tests/DataStorage/Database/Schema/BuilderTest.php b/tests/DataStorage/Database/Schema/BuilderTest.php index 28389028a..703e025e0 100644 --- a/tests/DataStorage/Database/Schema/BuilderTest.php +++ b/tests/DataStorage/Database/Schema/BuilderTest.php @@ -17,8 +17,31 @@ use phpOMS\DataStorage\Database\Schema\Builder; class BuilderTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + protected $con = null; + + protected function setUp() { - self::markTestIncomplete(); + $this->con = new MysqlConnection($GLOBALS['CONFIG']['db']['core']['masters']['admin']); + } + + public function testMysqlDrop() + { + $query = new Builder($this->con); + $sql = 'DROP DATABASE `test`;'; + self::assertEquals($sql, $query->drop('test')); + } + + public function testMysqlShowTables() + { + $query = new Builder($this->con); + $sql = 'SELECT `table_name` FROM `information_schema`.`tables` WHERE `table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\';'; + self::assertEquals($sql, $query->selectTables()); + } + + public function testMysqlShowFields() + { + $query = new Builder($this->con); + $sql = 'SELECT * FROM `information_schema`.`columns` WHERE `table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\' AND t`able_name` = \'test\';'; + self::assertEquals($sql, $query->selectFields('test')); } } diff --git a/tests/DataStorage/Database/Schema/QueryTypeTest.php b/tests/DataStorage/Database/Schema/QueryTypeTest.php index dc6f157a7..8748b581d 100644 --- a/tests/DataStorage/Database/Schema/QueryTypeTest.php +++ b/tests/DataStorage/Database/Schema/QueryTypeTest.php @@ -19,12 +19,10 @@ class QueryTypeTest extends \PHPUnit\Framework\TestCase { public function testEnums() { - self::assertEquals(4, \count(QueryType::getConstants())); + self::assertEquals(2, \count(QueryType::getConstants())); self::assertEquals(QueryType::getConstants(), array_unique(QueryType::getConstants())); - self::assertEquals(0, QueryType::SELECT); - self::assertEquals(1, QueryType::CREATE); - self::assertEquals(2, QueryType::DROP); - self::assertEquals(3, QueryType::ALTER); + self::assertEquals(128, QueryType::DROP); + self::assertEquals(129, QueryType::ALTER); } }