diff -u b/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test --- b/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -3457,52 +3457,6 @@ ->fetchField(); $this->assertFalse($result, 'SQL injection attempt did not result in a row being inserted in the database table.'); } - - /** - * Test if specifying the table along with the database like in "db.{table}" - * correctly applies table prefixes. - */ - public function testTableWithDatabaseQuery() { - try { - $connection = Database::getConnection()->getConnectionOptions(); - $database = $connection['database']; - - // Test for {table} pattern - db_query('SELECT * FROM {test} LIMIT 1')->fetchObject(); - - // Test for database.{table} pattern - db_query('SELECT * FROM ' . $database . '.{test} LIMIT 1')->fetchObject(); - - $this->pass('Correctly apply prefixes if the table is specified along with the database.'); - } - catch (PDOException $e) { - $this->fail('Correctly apply prefixes if the table is specified along with the database.'); - } - } - - public function testQueriesWithReservedKeywords() { - try { - $connection = Database::getConnection()->getConnectionOptions(); - $database = $connection['database']; - - // Test db_query with {table} pattern. - db_query('SELECT * FROM {system} LIMIT 1')->fetchObject(); - - // Test db_query with database.{table} pattern - db_query('SELECT * FROM ' . $database . '.{system} LIMIT 1')->fetchObject(); - - // Test db_select with automatic keyword escaping - db_select('system', 'to') // table and alias are reserved keywords. - ->fields('to', array('name', 'type')) - ->execute() - ->fetchAssoc(); - - $this->pass('Correctly executed the queries containing reserved keywords'); - } - catch (PDOException $e) { - $this->fail('Failed to execute the queries containing reserved keywords'); - } - } } /** @@ -4285,3 +4239,62 @@ } +} + + /** + * Test reserved keyword handling (introduced for MySQL 8+) + */ +class DatabaseReservedKeywordTestCase extends DatabaseTestCase { + public static function getInfo() { + return array( + 'name' => 'Reserved Keywords', + 'description' => 'Test handling of reserved keywords.', + 'group' => 'Database', + ); + } + function setUp() { + parent::setUp('database_test'); + } + + public function testSelectQueriesWithReservedKeywords() { + try { + $connection = Database::getConnection()->getConnectionOptions(); + $database = $connection['database']; + + // Test db_query with {table} pattern. + db_query('SELECT * FROM {system} LIMIT 1')->fetchObject(); + + // Test db_query with database.{table} pattern + db_query('SELECT * FROM ' . $database . '.{system} LIMIT 1')->fetchObject(); + + // Test db_select with automatic keyword escaping. + // Both table and alias are reserved keywords, but the query works without escaping in MySQL 5.x + db_select('system', 'function') + ->fields('function', array('name', 'type')) + ->execute() + ->fetchAssoc(); + + $this->pass('Correctly executed SELECT queries containing reserved keywords'); + } + catch (PDOException $e) { + $this->fail('Failed to execute SELECT queries containing reserved keywords'); + } + } + + public function testTableWithReservedKeywords() { + try { + db_insert('virtual') + ->fields(array( + 'function' => __FUNCTION__, + )) + ->execute(); + + $rows = db_select('virtual')->countQuery()->execute()->fetchField(); + $this->assertEqual($rows, 1, 'Wrote to, and read from a table with a name and column which are reserved words.'); + + $this->pass('Correctly executed queries using a table with a schema containing reserved keywords'); + } + catch (PDOException $e) { + $this->fail('Failed execute queries using a table with a schema containing reserved keywords'); + } + } } only in patch2: unchanged: --- a/modules/simpletest/tests/database_test.install +++ b/modules/simpletest/tests/database_test.install @@ -217,5 +217,24 @@ function database_test_schema() { ), ); + $schema['virtual'] = array( + 'description' => 'Basic test table with a reserved name.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'function' => array( + 'description' => "A column with a reserved name.", + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + 'default' => '', + ), + ), + 'primary key' => array('id'), + ); + return $schema; }