Draft schema builder

This commit is contained in:
Dennis Eichhorn 2018-11-11 19:53:54 +01:00
parent 65e35ec38e
commit 19019936bc
9 changed files with 136 additions and 63 deletions

View File

@ -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.

View File

@ -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.');
}

View File

@ -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;

View File

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

View File

@ -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;
}

View File

@ -0,0 +1,86 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package phpOMS\DataStorage\Database
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Schema\Builder;
/**
* Database type enum.
*
* Database types that are supported by the application
*
* @package phpOMS\DataStorage\Database
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class SchemaMapper
{
/**
* Database connection.
*
* @var ConnectionAbstract
* @since 1.0.0
*/
protected $db = null;
public function __construct(ConnectionAbstract $db)
{
$this->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;
}
}

View File

@ -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
{
/**

View File

@ -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'));
}
}

View File

@ -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);
}
}