mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
Draft schema builder
This commit is contained in:
parent
65e35ec38e
commit
19019936bc
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
86
DataStorage/Database/SchemaMapper.php
Normal file
86
DataStorage/Database/SchemaMapper.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user