Refactored code in PR #9441

This commit is contained in:
SilverFire - Dmitry Naumenko
2016-04-19 17:48:25 +03:00
parent c9dfc90be0
commit 0fe27b9d3b
21 changed files with 128 additions and 211 deletions

View File

@@ -103,7 +103,7 @@ class ColumnSchemaBuilder extends Object
*/
public $db;
/**
* @var mixed comment value of the column.
* @var string comment value of the column.
* @since 2.0.8
*/
public $comment;
@@ -168,7 +168,7 @@ class ColumnSchemaBuilder extends Object
}
/**
* Specify the comment for the column.
* Specifies the comment for column.
* @param string $comment the comment
* @return $this
* @since 2.0.8
@@ -243,7 +243,7 @@ class ColumnSchemaBuilder extends Object
{
switch ($this->getTypeCategory()) {
case self::CATEGORY_PK:
$format = '{type}{check}';
$format = '{type}{check}{comment}';
break;
default:
$format = '{type}{length}{notnull}{unique}{default}{check}{comment}';
@@ -365,6 +365,16 @@ class ColumnSchemaBuilder extends Object
return $this->categoryMap[$this->type];
}
/**
* Builds the comment specification for the column.
* @return string a string containing the COMMENT keyword and the comment itself
* @since 2.0.8
*/
protected function buildCommentString()
{
return $this->comment !== null ? " COMMENT " . $this->db->quoteValue($this->comment) : '';
}
/**
* Returns the complete column definition from input format
* @param string $format the format of the definition.
@@ -381,20 +391,11 @@ class ColumnSchemaBuilder extends Object
'{unique}' => $this->buildUniqueString(),
'{default}' => $this->buildDefaultString(),
'{check}' => $this->buildCheckString(),
'{comment}' => $this->buildCommentString(),
'{pos}' => ($this->isFirst) ?
$this->buildFirstString() :
$this->buildAfterString(),
'{comment}' => $this->buildCommentString(),
];
return strtr($format, $placeholderValues);
}
/**
* Builds the comment specification for the column.
* @return string with comment.
*/
protected function buildCommentString()
{
return $this->comment !== null ? " COMMENT '{$this->comment}'" : '';
}
}

View File

@@ -763,6 +763,7 @@ class Command extends Component
* @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
* @since 2.0.8
*/
public function addCommentOnColumn($table, $column, $comment)
{
@@ -777,6 +778,7 @@ class Command extends Component
* @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
* @since 2.0.8
*/
public function addCommentOnTable($table, $comment)
{
@@ -791,6 +793,7 @@ class Command extends Component
* @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
* @since 2.0.8
*/
public function dropCommentFromColumn($table, $column)
{
@@ -804,6 +807,7 @@ class Command extends Component
*
* @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
* @since 2.0.8
*/
public function dropCommentFromTable($table)
{

View File

@@ -505,7 +505,7 @@ class Migration extends Component implements MigrationInterface
{
echo " > drop comment from column $column ...";
$time = microtime(true);
$this->db->createCommand()->dropCommentFromColumn($table, $column, $comment)->execute();
$this->db->createCommand()->dropCommentFromColumn($table, $column)->execute();
echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
@@ -520,7 +520,7 @@ class Migration extends Component implements MigrationInterface
{
echo " > drop comment from table $table ...";
$time = microtime(true);
$this->db->createCommand()->dropCommentFromTable($table, $comment)->execute();
$this->db->createCommand()->dropCommentFromTable($table)->execute();
echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
}

View File

@@ -574,10 +574,12 @@ class QueryBuilder extends \yii\base\Object
* @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
* @return string the SQL statement for adding comment on column
* @since 2.0.8
*/
public function addCommentOnColumn($table, $column, $comment)
{
return 'COMMENT ON COLUMN ' . $this->db->quoteTableName($table) . '.' . $this->db->quoteColumnName($column) . ' IS ' . $this->db->quoteValue($comment);
}
@@ -586,7 +588,8 @@ class QueryBuilder extends \yii\base\Object
*
* @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
* @return string the SQL statement for adding comment on table
* @since 2.0.8
*/
public function addCommentOnTable($table, $comment)
{
@@ -598,7 +601,8 @@ class QueryBuilder extends \yii\base\Object
*
* @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
* @return string the SQL statement for adding comment on column
* @since 2.0.8
*/
public function dropCommentFromColumn($table, $column)
{
@@ -609,7 +613,8 @@ class QueryBuilder extends \yii\base\Object
* 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
* @return string the SQL statement for adding comment on column
* @since 2.0.8
*/
public function dropCommentFromTable($table)
{

View File

@@ -50,13 +50,13 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{
switch ($this->getTypeCategory()) {
case self::CATEGORY_PK:
$format = '{type}{check}{pos}';
$format = '{type}{check}{pos}{comment}';
break;
case self::CATEGORY_NUMERIC:
$format = '{type}{length}{unsigned}{notnull}{unique}{default}{check}{pos}';
$format = '{type}{length}{unsigned}{notnull}{unique}{default}{check}{pos}{comment}';
break;
default:
$format = '{type}{length}{notnull}{unique}{default}{check}{pos}';
$format = '{type}{length}{notnull}{unique}{default}{check}{pos}{comment}';
}
return $this->buildCompleteString($format);
}

View File

@@ -105,84 +105,42 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @since 2.0.8
* @inheritdoc
*/
public function addCommentOnColumn($table, $column, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
$definition = $this->getColumnDefinition($quotedTable, $column);
$definition = $this->getColumnDefinition($table, $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)
);
return 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' CHANGE ' . $this->db->quoteColumnName($column)
. ' ' . $this->db->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ' COMMENT ' . $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
* @since 2.0.8
* @inheritdoc
*/
public function addCommentOnTable($table, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
'ALTER TABLE %s COMMENT %s',
$quotedTable,
$this->db->quoteValue($comment)
);
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' COMMENT ' . $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
* @since 2.0.8
* @inheritdoc
*/
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
);
return $this->addCommentOnColumn($table, $column, '');
}
/**
* 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
* @since 2.0.8
* @inheritdoc
*/
public function dropCommentFromTable($table)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
"ALTER TABLE %s COMMENT ''",
$quotedTable,
empty($definition) ? '' : ' ' . $definition
);
return $this->addCommentOnTable($table, '');
}
@@ -197,7 +155,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
private function getColumnDefinition($table, $column)
{
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $table)->queryOne();
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->db->quoteTableName($table))->queryOne();
if ($row === false) {
throw new Exception("Unable to find column '$column' in table '$table'.");
}

View File

@@ -20,7 +20,6 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
/**
* @inheritdoc
*/
protected function buildCommentString()
{
return '';

View File

@@ -187,12 +187,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @inheritdoc
*/
public function addCommentOnColumn($table, $column, $comment)
{
@@ -200,22 +195,15 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @inheritdoc
*/
public function addCommentOnTable($table, $comment)
{
return "sp_updateextendedproperty @name = N'MS_Description', @value = {$this->db->quoteValue($comment)}, @level1type = N'Table', @level1name = {$this->db->quoteTableName($table)}";
}
/**
* 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
* @inheritdoc
*/
public function dropCommentFromColumn($table, $column)
{
@@ -223,10 +211,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @inheritdoc
*/
public function dropCommentFromTable($table)
{

View File

@@ -126,6 +126,7 @@ class Schema extends \yii\db\Schema
/**
* @inheritdoc
* @return ColumnSchemaBuilder column schema builder instance
*/
public function createColumnSchemaBuilder($type, $length = null)
{

View File

@@ -50,13 +50,13 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{
switch ($this->getTypeCategory()) {
case self::CATEGORY_PK:
$format = '{type}{length}{check}{pos}';
$format = '{type}{length}{check}{comment}{pos}';
break;
case self::CATEGORY_NUMERIC:
$format = '{type}{length}{unsigned}{notnull}{unique}{default}{check}{pos}';
$format = '{type}{length}{unsigned}{notnull}{unique}{default}{check}{comment}{pos}';
break;
default:
$format = '{type}{length}{notnull}{unique}{default}{check}{pos}';
$format = '{type}{length}{notnull}{unique}{default}{check}{comment}{pos}';
}
return $this->buildCompleteString($format);
}

View File

@@ -221,80 +221,42 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @inheritdoc
*/
public function addCommentOnColumn($table, $column, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
$definition = $this->getColumnDefinition($quotedTable, $column);
$definition = $this->getColumnDefinition($table, $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)
);
return 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' CHANGE ' . $this->db->quoteColumnName($column)
. ' ' . $this->db->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ' COMMENT ' . $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
* @inheritdoc
*/
public function addCommentOnTable($table, $comment)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
'ALTER TABLE %s COMMENT %s',
$quotedTable,
$this->db->quoteValue($comment)
);
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' COMMENT ' . $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
* @inheritdoc
*/
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
);
return $this->addCommentOnColumn($table, $column, '');
}
/**
* 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
* @inheritdoc
*/
public function dropCommentFromTable($table)
{
$quotedTable = $this->db->quoteTableName($table);
return sprintf(
"ALTER TABLE %s COMMENT ''",
$quotedTable,
empty($definition) ? '' : ' ' . $definition
);
return $this->addCommentOnTable($table, '');
}
@@ -308,7 +270,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
private function getColumnDefinition($table, $column)
{
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $table)->queryOne();
$quotedTable = $this->db->quoteTableName($table);
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne();
if ($row === false) {
throw new Exception("Unable to find column '$column' in table '$table'.");
}

View File

@@ -60,4 +60,12 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
}
return $this->buildCompleteString($format);
}
/**
* @inheritdoc
*/
protected function buildCommentString()
{
return '';
}
}

View File

@@ -269,12 +269,7 @@ EOD;
}
/**
* 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
* @since 2.0.8
* @inheritdoc
*/
public function dropCommentFromColumn($table, $column)
{
@@ -282,11 +277,7 @@ EOD;
}
/**
* 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
* @since 2.0.8
* @inheritdoc
*/
public function dropCommentFromTable($table)
{

View File

@@ -17,7 +17,6 @@ use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
*/
class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{
/**
* @inheritdoc
*/

View File

@@ -120,6 +120,7 @@ class Schema extends \yii\db\Schema
/**
* @inheritdoc
* @return ColumnSchemaBuilder column schema builder instance
*/
public function createColumnSchemaBuilder($type, $length = null)
{

View File

@@ -7,6 +7,7 @@
namespace yii\db\sqlite;
use yii\base\NotSupportedException;
use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
@@ -41,15 +42,14 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
default:
$format = '{type}{length}{notnull}{unique}{check}{default}';
}
return $this->buildCompleteString($format);
}
/**
* Specify the comment for the column
*
* @param string $comment the comment
* @throws NotSupportedException this is not supported by SQLite
* @since 2.0.8
* @inheritdoc
* @param string $comment
* @throws NotSupportedException
*/
public function comment($comment)
{

View File

@@ -291,13 +291,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @throws NotSupportedException this is not supported by SQLite
* @inheritdoc
* @throws NotSupportedException
*/
public function addCommentOnColumn($table, $column, $comment)
{
@@ -305,12 +300,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @throws NotSupportedException this is not supported by SQLite
* @inheritdoc
* @throws NotSupportedException
*/
public function addCommentOnTable($table, $comment)
{
@@ -318,12 +309,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @throws NotSupportedException this is not supported by SQLite
* @inheritdoc
* @throws NotSupportedException
*/
public function dropCommentFromColumn($table, $column)
{
@@ -331,11 +318,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
/**
* 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
* @throws NotSupportedException this is not supported by SQLite
* @inheritdoc
* @throws NotSupportedException
*/
public function dropCommentFromTable($table)
{

View File

@@ -93,6 +93,7 @@ class Schema extends \yii\db\Schema
/**
* @inheritdoc
* @return ColumnSchemaBuilder column schema builder instance
*/
public function createColumnSchemaBuilder($type, $length = null)
{
@@ -289,12 +290,4 @@ class Schema extends \yii\db\Schema
throw new NotSupportedException(get_class($this) . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.');
}
}
/**
* @inheritdoc
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length);
}
}

View File

@@ -916,7 +916,27 @@ abstract class QueryBuilderTest extends DatabaseTestCase
],
],
[
Schema::TYPE_INTEGER . " COMMENT 'test comment'",
$this->integer()->comment('test comment'),
[
'mysql' => "int(11) COMMENT 'test comment'",
'postgres' => 'integer',
'oci' => "NUMBER(10)",
'sqlsrv' => 'int',
'cubrid' => "int COMMENT 'test comment'",
],
],
[
Schema::TYPE_PK . " COMMENT 'test comment'",
$this->primaryKey()->comment('test comment'),
[
'mysql' => "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test comment'",
'postgres' => 'serial NOT NULL PRIMARY KEY',
'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY',
'sqlsrv' => 'int IDENTITY PRIMARY KEY',
'cubrid' => "int NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test comment'",
],
],
];

View File

@@ -48,6 +48,11 @@ class MysqlQueryBuilderTest extends QueryBuilderTest
$this->primaryKey(8)->first()->after('col_before'),
'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST'
],
[
Schema::TYPE_PK . " COMMENT 'test' AFTER `col_before`",
$this->primaryKey()->comment('test')->after('col_before'),
"int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test' AFTER `col_before`"
],
]);
}
}