Implrement create builder

This commit is contained in:
Dennis Eichhorn 2018-12-21 20:16:33 +01:00
parent 6cc59da699
commit d4ec08179e
15 changed files with 240 additions and 24 deletions

View File

@ -27,12 +27,18 @@ use phpOMS\DataStorage\Database\Query\Builder as QueryBuilder;
*/
class Builder extends QueryBuilder
{
public $createTable = '';
public $createFields = [];
public $drop = [];
public $selectTables = ['*'];
public $selectFields = [];
public $createTableSettings = true;
/**
* Constructor.
*
@ -64,21 +70,42 @@ class Builder extends QueryBuilder
public function selectFields(string $table) : self
{
$this->type = QueryType::FIELDS;
$this->type = QueryType::FIELDS;
$this->selectFields[0] = $table;
return $this;
}
public function create(string $table)
public function createTable(string $name) : self
{
$this->type = QueryType::CREATE_TABLE;
$this->createTable = $name;
return $this;
}
// todo: consider to work with flags instead of all these booleans
public function field(
string $name, string $type, $default = null,
bool $isNullable = true, bool $isPrimary = false, bool $autoincrement = false,
string $foreignTable = null, string $foreignKey = null
) : self {
$this->createFields[$name] = [
'name' => $name,
'type' => $type,
'default' => $default,
'null' => $isNullable,
'primary' => $isPrimary,
'autoincrement' => $autoincrement,
'foreignTable' => $foreignTable,
'foreignKey' => $foreignKey,
];
return $this;
}
public function alter(array $column)
{
}
/**

View File

@ -38,6 +38,18 @@ class Grammar extends QueryGrammar
'drop',
];
/**
* Select tables components.
*
* @var string[]
* @since 1.0.0
*/
protected $createTablesComponents = [
'createTable',
'createFields',
'createTableSettings'
];
/**
* Select tables components.
*
@ -45,7 +57,7 @@ class Grammar extends QueryGrammar
* @since 1.0.0
*/
protected $tablesComponents = [
'selectTables'
'selectTables',
];
/**
@ -55,7 +67,7 @@ class Grammar extends QueryGrammar
* @since 1.0.0
*/
protected $fieldsComponents = [
'selectFields'
'selectFields',
];
/**
@ -70,11 +82,43 @@ class Grammar extends QueryGrammar
return $this->tablesComponents;
case QueryType::FIELDS:
return $this->fieldsComponents;
case QueryType::CREATE_TABLE:
return $this->createTablesComponents;
default:
return parent::getComponents($type);
}
}
/**
* Compile create table query.
*
* @param BuilderAbstract $query Query
* @param string $table Tables to drop
*
* @return string
*
* @since 1.0.0
*/
protected function compileCreateTable(BuilderAbstract $query, string $table) : string
{
return 'CREATE TABLE ' . $this->expressionizeTable([$table], $query->getPrefix());
}
/**
* Compile create table settings query.
*
* @param BuilderAbstract $query Query
* @param bool $settings Has settings
*
* @return string
*
* @since 1.0.0
*/
protected function compileCreateTableSettings(BuilderAbstract $query, bool $settings) : string
{
return '';
}
/**
* Compile drop query.
*

View File

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\BuilderAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
/**
@ -74,4 +75,67 @@ class MysqlGrammar extends Grammar
return \rtrim($builder->toSql(), ';');
}
/**
* Compile create table fields query.
*
* @param Builder $query Query
* @param array $tables Tables to drop
*
* @return string
*
* @since 1.0.0
*/
protected function compileCreateFields(Builder $query, array $fields) : string
{
$fieldQuery = '';
$keys = '';
foreach ($fields as $name => $field) {
$fieldQuery .= ' ' . $this->expressionizeTableColumn([$name], '') . ' ' . $field['type'];
if (isset($field['default'])) {
$fieldQuery .= ' DEFAULT ' . $this->compileValue($query, $field['default']);
}
if (isset($field['null'])) {
$fieldQuery .= ' ' . ($field['null'] ? '' : 'NOT ') . 'NULL';
}
if (isset($field['autoincrement']) && $field['autoincrement']) {
$fieldQuery .= ' AUTO_INCREMENT';
}
$fieldQuery .= ',';
if (isset($field['primary']) && $field['primary']) {
$keys .= ' PRIMARY KEY (' . $this->expressionizeTableColumn([$name], '') . '),';
}
if (isset($field['foreignTable'], $field['foreignKey'])
&& !empty($field['foreignTable']) && !empty($field['foreignKey'])
) {
$keys .= ' FOREIGN KEY (' . $this->expressionizeTableColumn([$name], '') . ') REFERENCES '
. $this->expressionizeTable([$field['foreignTable']], $query->getPrefix())
. ' (' . $this->expressionizeTableColumn([$field['foreignKey']], '') . '),';
}
}
return '(' . \ltrim(\rtrim($fieldQuery . $keys, ','), ' ') . ')';
}
/**
* Compile create table settings query.
*
* @param BuilderAbstract $query Query
* @param bool $settings Has settings
*
* @return string
*
* @since 1.0.0
*/
protected function compileCreateTableSettings(BuilderAbstract $query, bool $settings) : string
{
return 'ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1';
}
}

View File

@ -0,0 +1,20 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package TBD
* @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\Schema\Grammar;
class OracleGrammar extends Grammar
{
}

View File

@ -16,5 +16,11 @@ namespace phpOMS\DataStorage\Database\Schema\Grammar;
class SQLiteGrammar extends Grammar
{
/**
* System identifier.
*
* @var string
* @since 1.0.0
*/
protected $systemIdentifier = '`';
}

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema\Grammar;
class SqlServerGrammar
class SqlServerGrammar extends Grammar
{
}

View File

@ -28,8 +28,9 @@ use phpOMS\DataStorage\Database\Query\QueryType as DefaultQueryType;
*/
abstract class QueryType extends DefaultQueryType
{
public const DROP = 128;
public const ALTER = 129;
public const TABLES = 130;
public const FIELDS = 131;
public const DROP = 128;
public const ALTER = 129;
public const TABLES = 130;
public const FIELDS = 131;
public const CREATE_TABLE = 132;
}

View File

@ -45,4 +45,17 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
$sql = 'SELECT * FROM `information_schema`.`columns` WHERE `information_schema`.`columns`.`table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\' AND `information_schema`.`columns`.`table_name` = \'test\';';
self::assertEquals($sql, $query->selectFields('test')->toSql());
}
public function testMysqlCreateTable()
{
$query = new Builder($this->con);
$sql = 'CREATE TABLE `user_roles` (`user_id` INT NOT NULL AUTO_INCREMENT, `role_id` VARCHAR(10) DEFAULT \'1\' NULL, PRIMARY KEY (`user_id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`ext1_id`), FOREIGN KEY (`role_id`) REFERENCES `roles` (`ext2_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;';
self::assertEquals(
$sql,
$query->createTable('user_roles')
->field('user_id', 'INT', null, false, true, true, 'users', 'ext1_id')
->field('role_id', 'VARCHAR(10)', '1', true, false, false, 'roles', 'ext2_id')
->toSql()
);
}
}

View File

@ -13,11 +13,17 @@
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\Grammar;
class GrammarTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
$grammar = new Grammar();
self::assertEquals('Y-m-d H:i:s', $grammar->getDateFormat());
self::assertEquals('', $grammar->getTablePrefix());
$grammar->setTablePrefix('oms_');
self::assertEquals('oms_', $grammar->getTablePrefix());
}
}

View File

@ -13,11 +13,14 @@
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\MysqlGrammar;
use phpOMS\Utils\TestUtils;
class MysqlGrammarTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
self::assertInstanceOf('\phpOMS\DataStorage\Database\Schema\Grammar\Grammar', new MysqlGrammar());
self::assertEquals('`', TestUtils::getMember(new MysqlGrammar(), 'systemIdentifier'));
}
}

View File

@ -0,0 +1,25 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\OracleGrammar;
use phpOMS\Utils\TestUtils;
class OracleGrammarTest extends \PHPUnit\Framework\TestCase
{
public function testDefault()
{
self::assertInstanceOf('\phpOMS\DataStorage\Database\Schema\Grammar\Grammar', new OracleGrammar());
}
}

View File

@ -13,11 +13,13 @@
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\PostgresGrammar;
use phpOMS\Utils\TestUtils;
class PostgresGrammarTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
self::assertInstanceOf('\phpOMS\DataStorage\Database\Schema\Grammar\Grammar', new PostgresGrammar());
}
}

View File

@ -10,14 +10,16 @@
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\SQLiteGrammar;
use phpOMS\Utils\TestUtils;
class SQLiteGrammarTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
self::assertInstanceOf('\phpOMS\DataStorage\Database\Schema\Grammar\Grammar', new SQLiteGrammar());
self::assertEquals('`', TestUtils::getMember(new SQLiteGrammar(), 'systemIdentifier'));
}
}

View File

@ -13,11 +13,13 @@
namespace phpOMS\tests\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\Schema\Grammar\SqlServerGrammar;
use phpOMS\Utils\TestUtils;
class SqlServerGrammarTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
self::assertInstanceOf('\phpOMS\DataStorage\Database\Schema\Grammar\Grammar', new SqlServerGrammar());
}
}

View File

@ -19,12 +19,13 @@ class QueryTypeTest extends \PHPUnit\Framework\TestCase
{
public function testEnums()
{
self::assertEquals(11, \count(QueryType::getConstants()));
self::assertEquals(12, \count(QueryType::getConstants()));
self::assertEquals(QueryType::getConstants(), array_unique(QueryType::getConstants()));
self::assertEquals(128, QueryType::DROP);
self::assertEquals(129, QueryType::ALTER);
self::assertEquals(130, QueryType::TABLES);
self::assertEquals(131, QueryType::FIELDS);
self::assertEquals(132, QueryType::CREATE_TABLE);
}
}