mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 14:28:27 +08:00
add comment control methods
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user