Commit 0390a998 by Qiang Xue

Use backquotes to quote column and table names for sqlite (related with #1318)

parent 0b1ddc93
...@@ -50,6 +50,28 @@ class Schema extends \yii\db\Schema ...@@ -50,6 +50,28 @@ class Schema extends \yii\db\Schema
]; ];
/** /**
* Quotes a table name for use in a query.
* A simple table name has no schema prefix.
* @param string $name table name
* @return string the properly quoted table name
*/
public function quoteSimpleTableName($name)
{
return strpos($name, "`") !== false ? $name : "`" . $name . "`";
}
/**
* Quotes a column name for use in a query.
* A simple column name has no prefix.
* @param string $name column name
* @return string the properly quoted column name
*/
public function quoteSimpleColumnName($name)
{
return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
}
/**
* Creates a query builder for the MySQL database. * Creates a query builder for the MySQL database.
* This method may be overridden by child classes to create a DBMS-specific query builder. * This method may be overridden by child classes to create a DBMS-specific query builder.
* @return QueryBuilder query builder instance * @return QueryBuilder query builder instance
......
...@@ -17,6 +17,6 @@ class SqliteCommandTest extends CommandTest ...@@ -17,6 +17,6 @@ class SqliteCommandTest extends CommandTest
$sql = 'SELECT [[id]], [[t.name]] FROM {{tbl_customer}} t'; $sql = 'SELECT [[id]], [[t.name]] FROM {{tbl_customer}} t';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals("SELECT \"id\", 't'.\"name\" FROM 'tbl_customer' t", $command->sql); $this->assertEquals("SELECT `id`, `t`.`name` FROM `tbl_customer` t", $command->sql);
} }
} }
...@@ -30,8 +30,8 @@ class SqliteConnectionTest extends ConnectionTest ...@@ -30,8 +30,8 @@ class SqliteConnectionTest extends ConnectionTest
public function testQuoteTableName() public function testQuoteTableName()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$this->assertEquals("'table'", $connection->quoteTableName('table')); $this->assertEquals("`table`", $connection->quoteTableName('table'));
$this->assertEquals("'schema'.'table'", $connection->quoteTableName('schema.table')); $this->assertEquals("`schema`.`table`", $connection->quoteTableName('schema.table'));
$this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}'));
$this->assertEquals('(table)', $connection->quoteTableName('(table)')); $this->assertEquals('(table)', $connection->quoteTableName('(table)'));
} }
...@@ -39,8 +39,8 @@ class SqliteConnectionTest extends ConnectionTest ...@@ -39,8 +39,8 @@ class SqliteConnectionTest extends ConnectionTest
public function testQuoteColumnName() public function testQuoteColumnName()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
$this->assertEquals('"column"', $connection->quoteColumnName('column')); $this->assertEquals('`column`', $connection->quoteColumnName('column'));
$this->assertEquals("'table'.\"column\"", $connection->quoteColumnName('table.column')); $this->assertEquals("`table`.`column`", $connection->quoteColumnName('table.column'));
$this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]'));
$this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}'));
$this->assertEquals('(column)', $connection->quoteColumnName('(column)')); $this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
......
...@@ -85,6 +85,6 @@ class SqliteQueryBuilderTest extends QueryBuilderTest ...@@ -85,6 +85,6 @@ class SqliteQueryBuilderTest extends QueryBuilderTest
public function testBatchInsert() public function testBatchInsert()
{ {
$sql = $this->getQueryBuilder()->batchInsert('{{tbl_customer}} t', ['t.id','t.name'], array(array(1,'a'), array(2,'b'))); $sql = $this->getQueryBuilder()->batchInsert('{{tbl_customer}} t', ['t.id','t.name'], array(array(1,'a'), array(2,'b')));
$this->assertEquals("INSERT INTO {{tbl_customer}} t ('t'.\"id\", 't'.\"name\") SELECT 1, 'a' UNION ALL 2, 'b'", $sql); $this->assertEquals("INSERT INTO {{tbl_customer}} t (`t`.`id`, `t`.`name`) SELECT 1, 'a' UNION ALL 2, 'b'", $sql);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment