Merge branch 'methods_for_schema_builder' of https://github.com/vaseninm/yii2 into vaseninm-methods_for_schema_builder

This commit is contained in:
SilverFire - Dmitry Naumenko
2016-04-19 12:21:33 +03:00
22 changed files with 811 additions and 2 deletions

View File

@ -219,4 +219,112 @@ class QueryBuilder extends \yii\db\QueryBuilder
. (!empty($names) ? ' (' . implode(', ', $names) . ')' : '')
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : ' DEFAULT VALUES');
}
}
/**
* Builds a SQL command for adding comment to column
*
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
* @return $this the command object itself
*/
public function addCommentOnColumn($table, $column, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
$definition = $this->getColumnDefinition($quotedTable, $column);
$definition = trim(preg_replace("/COMMENT '(.*?)'/i", '', $definition));
return sprintf(
'ALTER TABLE %s CHANGE %s %s%s COMMENT %s',
$quotedTable,
$this->db->quoteColumnName($column),
$this->db->quoteColumnName($column),
empty($definition) ? '' : ' ' . $definition,
$this->db->quoteValue($comment)
);
}
/**
* Builds a SQL command for adding comment to table
*
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method.
* @return $this the command object itself
*/
public function addCommentOnTable($table, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
'ALTER TABLE %s COMMENT %s',
$quotedTable,
$this->db->quoteValue($comment)
);
}
/**
* Builds a SQL command for adding comment to column
*
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method.
* @return $this the command object itself
*/
public function dropCommentFromColumn($table, $column)
{
$quotedTable = $this->db->quoteTableName($table);
$definition = $this->getColumnDefinition($quotedTable, $column);
$definition = trim(preg_replace("/COMMENT '(.*?)'/i", '', $definition));
return sprintf(
'ALTER TABLE %s CHANGE %s %s%s COMMENT \'\'',
$quotedTable,
$this->db->quoteColumnName($column),
$this->db->quoteColumnName($column),
empty($definition) ? '' : ' ' . $definition
);
}
/**
* Builds a SQL command for adding comment to table
*
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method.
* @return $this the command object itself
*/
public function dropCommentFromTable($table)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
"ALTER TABLE %s COMMENT ''",
$quotedTable,
empty($definition) ? '' : ' ' . $definition
);
}
/**
* Gets column definition.
*
* @param string $table table name
* @param string $column column name
* @return null|string the column definition
* @throws Exception in case when table does not contain column
*/
private function getColumnDefinition($table, $column)
{
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $table)->queryOne();
if ($row === false) {
throw new Exception("Unable to find column '$column' in table '$table'.");
}
if (isset($row['Create Table'])) {
$sql = $row['Create Table'];
} else {
$row = array_values($row);
$sql = $row[1];
}
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
foreach ($matches[1] as $i => $c) {
if ($c === $column) {
return $matches[2][$i];
}
}
}
return null;
}
}