add comment control methods

This commit is contained in:
Matvey Vasenin
2015-08-14 12:27:21 +03:00
parent ff87dd2bd7
commit ce6e92b736
8 changed files with 345 additions and 1 deletions

View File

@ -217,4 +217,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;
}
}