From 0390a998333b10755f53013999a0475dc299afea Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 28 Nov 2013 09:04:16 -0500 Subject: [PATCH] Use backquotes to quote column and table names for sqlite (related with #1318) --- framework/yii/db/sqlite/Schema.php | 22 ++++++++++++++++++++++ .../unit/framework/db/sqlite/SqliteCommandTest.php | 2 +- .../framework/db/sqlite/SqliteConnectionTest.php | 8 ++++---- .../framework/db/sqlite/SqliteQueryBuilderTest.php | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/framework/yii/db/sqlite/Schema.php b/framework/yii/db/sqlite/Schema.php index 38fbf3a..8633650 100644 --- a/framework/yii/db/sqlite/Schema.php +++ b/framework/yii/db/sqlite/Schema.php @@ -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. * This method may be overridden by child classes to create a DBMS-specific query builder. * @return QueryBuilder query builder instance diff --git a/tests/unit/framework/db/sqlite/SqliteCommandTest.php b/tests/unit/framework/db/sqlite/SqliteCommandTest.php index 1f9ddc2..c8cb35c 100644 --- a/tests/unit/framework/db/sqlite/SqliteCommandTest.php +++ b/tests/unit/framework/db/sqlite/SqliteCommandTest.php @@ -17,6 +17,6 @@ class SqliteCommandTest extends CommandTest $sql = 'SELECT [[id]], [[t.name]] FROM {{tbl_customer}} t'; $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); } } diff --git a/tests/unit/framework/db/sqlite/SqliteConnectionTest.php b/tests/unit/framework/db/sqlite/SqliteConnectionTest.php index e1a2961..ea74f81 100644 --- a/tests/unit/framework/db/sqlite/SqliteConnectionTest.php +++ b/tests/unit/framework/db/sqlite/SqliteConnectionTest.php @@ -30,8 +30,8 @@ class SqliteConnectionTest extends ConnectionTest public function testQuoteTableName() { $connection = $this->getConnection(false); - $this->assertEquals("'table'", $connection->quoteTableName('table')); - $this->assertEquals("'schema'.'table'", $connection->quoteTableName('schema.table')); + $this->assertEquals("`table`", $connection->quoteTableName('table')); + $this->assertEquals("`schema`.`table`", $connection->quoteTableName('schema.table')); $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); $this->assertEquals('(table)', $connection->quoteTableName('(table)')); } @@ -39,8 +39,8 @@ class SqliteConnectionTest extends ConnectionTest public function testQuoteColumnName() { $connection = $this->getConnection(false); - $this->assertEquals('"column"', $connection->quoteColumnName('column')); - $this->assertEquals("'table'.\"column\"", $connection->quoteColumnName('table.column')); + $this->assertEquals('`column`', $connection->quoteColumnName('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)')); diff --git a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php index 67cae28..a2408a6 100644 --- a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php +++ b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php @@ -85,6 +85,6 @@ class SqliteQueryBuilderTest extends QueryBuilderTest public function testBatchInsert() { $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); } }