Final Class Yiisoft\Db\Mysql\DDLQueryBuilder
| Inheritance | Yiisoft\Db\Mysql\DDLQueryBuilder » Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder |
|---|
Implements a (Data Definition Language) SQL statements for MySQL, MariaDB.
Public Methods
Method Details
| public addCheck( string $table, string $name, string $expression ): string | ||
| $table | string | |
| $name | string | |
| $expression | string | |
| throws | \Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function addCheck(string $table, string $name, string $expression): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}
| public addCommentOnColumn( string $table, string $column, string $comment ): string | ||
| $table | string | |
| $column | string | |
| $comment | string | |
| throws | Throwable | |
|---|---|---|
public function addCommentOnColumn(string $table, string $column, string $comment): string
{
/** @var string $definition Strip existing comment which may include escaped quotes */
$definition = preg_replace("/COMMENT '(?:''|[^'])*'/i", '', $this->getColumnDefinition($table, $column));
$definition = trim($definition);
$checkRegex = '/CHECK *(\(([^()]|(?-2))*\))/';
$check = preg_match($checkRegex, $definition, $checkMatches);
if ($check === 1) {
$definition = preg_replace($checkRegex, '', $definition);
}
$alterSql = 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' CHANGE '
. $this->quoter->quoteColumnName($column)
. ' '
. $this->quoter->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ' COMMENT '
. $this->quoter->quoteValue($comment);
if ($check === 1) {
$alterSql .= ' ' . $checkMatches[0];
}
return $alterSql;
}
| public addCommentOnTable( string $table, string $comment ): string | ||
| $table | string | |
| $comment | string | |
public function addCommentOnTable(string $table, string $comment): string
{
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' COMMENT '
. $this->quoter->quoteValue($comment);
}
| public addDefaultValue( string $table, string $name, string $column, mixed $value ): string | ||
| $table | string | |
| $name | string | |
| $column | string | |
| $value | mixed | |
public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}
| public checkIntegrity( string $schema = '', string $table = '', boolean $check = true ): string | ||
| $schema | string | |
| $table | string | |
| $check | boolean | |
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
{
return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);
}
| public createIndex( string $table, string $name, array|string $columns, string|null $indexType = null, string|null $indexMethod = null ): string | ||
| $table | string | |
| $name | string | |
| $columns | array|string | |
| $indexType | string|null | |
| $indexMethod | string|null | |
public function createIndex(
string $table,
string $name,
array|string $columns,
?string $indexType = null,
?string $indexMethod = null,
): string {
return 'CREATE ' . (!empty($indexType) ? $indexType . ' ' : '') . 'INDEX '
. $this->quoter->quoteTableName($name)
. (!empty($indexMethod) ? " USING $indexMethod" : '')
. ' ON ' . $this->quoter->quoteTableName($table)
. ' (' . $this->queryBuilder->buildColumns($columns) . ')';
}
| public dropCheck( string $table, string $name ): string | ||
| $table | string | |
| $name | string | |
| throws | \Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function dropCheck(string $table, string $name): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}
| public dropCommentFromColumn( string $table, string $column ): string | ||
| $table | string | |
| $column | string | |
| throws | \Yiisoft\Db\Exception\Exception | |
|---|---|---|
| throws | Throwable | |
public function dropCommentFromColumn(string $table, string $column): string
{
return $this->addCommentOnColumn($table, $column, '');
}
| public dropCommentFromTable( string $table ): string | ||
| $table | string | |
| throws | Exception | |
|---|---|---|
public function dropCommentFromTable(string $table): string
{
return $this->addCommentOnTable($table, '');
}
| public dropDefaultValue( string $table, string $name ): string | ||
| $table | string | |
| $name | string | |
public function dropDefaultValue(string $table, string $name): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}
| public dropForeignKey( string $table, string $name ): string | ||
| $table | string | |
| $name | string | |
public function dropForeignKey(string $table, string $name): string
{
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' DROP FOREIGN KEY '
. $this->quoter->quoteColumnName($name);
}
| public dropPrimaryKey( string $table, string $name ): string | ||
| $table | string | |
| $name | string | |
public function dropPrimaryKey(string $table, string $name): string
{
return 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' DROP PRIMARY KEY';
}
| public dropTable( string $table, boolean $ifExists = false, boolean $cascade = false ): string | ||
| $table | string | |
| $ifExists | boolean | |
| $cascade | boolean | |
| throws | \Yiisoft\Db\Exception\NotSupportedException |
MySQL doesn't support cascade drop table. |
|---|---|---|
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
if ($cascade) {
throw new NotSupportedException('MySQL doesn\'t support cascade drop table.');
}
return parent::dropTable($table, $ifExists, false);
}
| public dropUnique( string $table, string $name ): string | ||
| $table | string | |
| $name | string | |
public function dropUnique(string $table, string $name): string
{
return $this->dropIndex($table, $name);
}
| public renameColumn( string $table, string $oldName, string $newName ): string | ||
| $table | string | |
| $oldName | string | |
| $newName | string | |
| throws | \Yiisoft\Db\Exception\Exception | |
|---|---|---|
| throws | Throwable | |
public function renameColumn(string $table, string $oldName, string $newName): string
{
$quotedTable = $this->quoter->quoteTableName($table);
$columnDefinition = $this->getColumnDefinition($table, $oldName);
/* try to give back an SQL anyway */
return "ALTER TABLE $quotedTable CHANGE "
. $this->quoter->quoteColumnName($oldName) . ' '
. $this->quoter->quoteColumnName($newName)
. (!empty($columnDefinition) ? ' ' . $columnDefinition : '');
}
Signup or Login in order to comment.