Skip to content

Commit

Permalink
https://github.com/yiisoft/yii2/issues/20150
Browse files Browse the repository at this point in the history
Set default identifier quotes to "
Fixed change and rename columns to allow any kind of quotes: ' " [ `
  • Loading branch information
santilin committed Apr 28, 2024
1 parent f9d362e commit 51f9ed9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/db/sqlite/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ public function dropColumn($tableName, $column)
$sql_fields_to_insert = [];
$skipping = false;
$column_found = false;
$quoted_column = $this->db->quoteColumnName($column);
$quoted_tablename = $this->db->quoteTableName($tableName);
$unquoted_tablename = $this->unquoteTableName($tableName);
$fields_definitions_tokens = $this->getFieldDefinitionsTokens($unquoted_tablename);
Expand All @@ -473,8 +472,7 @@ public function dropColumn($tableName, $column)
// These searchs could be done with another SqlTokenizer, but I don't konw how to do them, the documentation for sqltokenizer si really scarse.
if( $token->type == \yii\db\SqlToken::TYPE_IDENTIFIER ) {
$identifier = (string)$token;
if( $identifier == $column || $identifier == $quoted_column
|| $identifier == "\"$column\"" /* strangely this can happen */ ) {
if (self::columnMatch($identifier,$column)) {
// found column definition for $column, set skipping on up until the next ,
$column_found = $skipping = true;
} else {
Expand All @@ -491,7 +489,7 @@ public function dropColumn($tableName, $column)
++$other_offset;
}
$foreign_field = (string)$fields_definitions_tokens[$other_offset];
if ($foreign_field == $column || $foreign_field == $quoted_column) {
if (self::columnMatch($foreign_field,$column)) {
// Found foreign key for $column, skip it
$skipping = true;
$offset = $other_offset;
Expand Down Expand Up @@ -841,7 +839,6 @@ public function alterColumn($tableName, $column, $type)
$adding_column_type = false;
$unquoted_tablename = $this->unquoteTableName($tableName);
$quoted_tablename = $this->db->quoteTableName($tableName);
$quoted_column = $this->db->quoteColumnName($column);
$fields_definitions_tokens = $this->getFieldDefinitionsTokens($unquoted_tablename); $offset = 0;
// Traverse the tokens looking for either an identifier (field name) or a foreign key
while( $fields_definitions_tokens->offsetExists($offset)) {
Expand All @@ -850,7 +847,7 @@ public function alterColumn($tableName, $column, $type)
if( $token->type == \yii\db\SqlToken::TYPE_IDENTIFIER ) {
$identifier = (string)$token;
$sql_fields_to_insert[] = $identifier;
if( $identifier == $column || $identifier == $quoted_column) {
if (self::columnMatch($identifier,$column)) {
// found column definition for $column, set skipping on up until the next ,
$ddl_fields_def .= "$identifier $type";
$column_found = $skipping = true;
Expand Down Expand Up @@ -1241,4 +1238,16 @@ public function buildUnion($unions, &$params)

return trim($result);
}

static protected function columnMatch($c1,$c2): bool
{
if (in_array($c1[0], ['[',"'",'"','`'])) {
$c1 = substr($c1,1,-1);
}
if (in_array($c2[0], ['[',"'",'"','`'])) {
$c2 = substr($c2,1,-1);
}
return $c1 == $c2;
}

}
4 changes: 2 additions & 2 deletions src/db/sqlite/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
/**
* {@inheritdoc}
*/
protected $tableQuoteCharacter = '`';
protected $tableQuoteCharacter = '"';
/**
* {@inheritdoc}
*/
protected $columnQuoteCharacter = '`';
protected $columnQuoteCharacter = '"';


/**
Expand Down

0 comments on commit 51f9ed9

Please sign in to comment.