mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-15 07:58:40 +00:00
draft alter functionality
This commit is contained in:
parent
e697b0b1bd
commit
a91ee19ff8
|
|
@ -87,6 +87,30 @@ class Builder extends QueryBuilder
|
|||
*/
|
||||
public bool $createTableSettings = true;
|
||||
|
||||
/**
|
||||
* Table to alter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $alterTable = '';
|
||||
|
||||
/**
|
||||
* Column to alter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $alterColumn = '';
|
||||
|
||||
/**
|
||||
* Data to add.
|
||||
*
|
||||
* @var array
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public array $alterAdd = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
|
@ -262,16 +286,42 @@ class Builder extends QueryBuilder
|
|||
}
|
||||
|
||||
/**
|
||||
* Alter a field.
|
||||
* Alter a table.
|
||||
*
|
||||
* @param array $column Column data
|
||||
* @param string $table Table
|
||||
*
|
||||
* @return void
|
||||
* @return self
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function alter(array $column) : void
|
||||
public function alterTable(string $table) : self
|
||||
{
|
||||
$this->type = QueryType::ALTER;
|
||||
$this->alterTable = $table;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a constraint
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $foreignTable Foreign table
|
||||
* @param string $foreignKey Foreign key
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function addConstraint(string $key, string $foreignTable, string $foreignKey, string $constraint = null) : self
|
||||
{
|
||||
$this->alterAdd['type'] = 'CONSTRAINT';
|
||||
$this->alterAdd['key'] = $key;
|
||||
$this->alterAdd['foreignTable'] = $foreignTable;
|
||||
$this->alterAdd['foreignKey'] = $foreignKey;
|
||||
$this->alterAdd['constraint'] = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -84,6 +84,20 @@ class Grammar extends QueryGrammar
|
|||
'selectFields',
|
||||
];
|
||||
|
||||
/**
|
||||
* Alter components.
|
||||
*
|
||||
* @var string[]
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected array $alterComponents = [
|
||||
'alterTable',
|
||||
'alterColumn',
|
||||
'alterAdd',
|
||||
'alterRename',
|
||||
'alterRemove',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
@ -100,11 +114,95 @@ class Grammar extends QueryGrammar
|
|||
return $this->createTablesComponents;
|
||||
case QueryType::DROP_TABLE:
|
||||
return $this->dropTableComponents;
|
||||
case QueryType::ALTER:
|
||||
return $this->alterComponents;
|
||||
default:
|
||||
return parent::getComponents($type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile alter table query.
|
||||
*
|
||||
* @param BuilderAbstract $query Query
|
||||
* @param string $table Table to alter
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function compileAlterTable(BuilderAbstract $query, string $table) : string
|
||||
{
|
||||
return 'ALTER TABLE ' . $this->expressionizeTableColumn([$table]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile alter column query.
|
||||
*
|
||||
* @param BuilderAbstract $query Query
|
||||
* @param string $column Column to alter
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function compileAlterColumn(BuilderAbstract $query, string $column) : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile alter add query.
|
||||
*
|
||||
* @param BuilderAbstract $query Query
|
||||
* @param array $add Add data
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function compileAlterAdd(BuilderAbstract $query, array $add) : string
|
||||
{
|
||||
switch ($add['type']) {
|
||||
case 'COLUMN':
|
||||
return $this->addColumn($add);
|
||||
case 'CONSTRAINT':
|
||||
return $this->addConstraint($add);
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new column.
|
||||
*
|
||||
* @param array $add Add data
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function addColumn(array $add) : string
|
||||
{
|
||||
return 'ADD ' . $this->expressionizeTableColumn([$add['name']]) . ' ' . $add['datatype'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new constraint/foreign key.
|
||||
*
|
||||
* @param array $add Add data
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function addConstraint(array $add) : string
|
||||
{
|
||||
return 'ADD' . (isset($add['constraint']) ? 'CONSTRAINT ' . $add['constraint'] : '') . ' FOREIGN KEY (' . $this->expressionizeTableColumn([$add['key']]) . ') REFERENCES '
|
||||
. $this->expressionizeTableColumn([$add['foreignTable']])
|
||||
. ' (' . $this->expressionizeTableColumn([$add['foreignKey']]) . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile create table query.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -39,6 +39,23 @@ class MysqlGrammar extends Grammar
|
|||
*/
|
||||
protected string $systemIdentifier = '`';
|
||||
|
||||
/**
|
||||
* Compile remove
|
||||
*
|
||||
* @param Builder $query Builder
|
||||
* @param array $remove Remove data
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function compileAlterRemove(BuilderAbstract $query, array $remove) : string
|
||||
{
|
||||
$keyWord = $remove['type'] === 'CONSTRAINT' ? 'FOREIGN KEY ' : 'COLUMN';
|
||||
|
||||
return 'DROP ' . $keyWord . ' ' . $remove['identifier'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile from.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user