From 9868dd3b9e0552489a931ebe9ba509d305815481 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Wed, 14 Oct 2009 21:28:18 +0100 Subject: [PATCH] Add test for complicated sql syntax --- lib/dml/simpletest/testdml.php | 35 +++++++++++++++++++++++++++++++++++ 1 files changed, 35 insertions(+), 0 deletions(-) diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index 5a205f3..c260a23 100755 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -2106,6 +2106,41 @@ class dml_test extends UnitTestCase { $this->assertTrue(true, 'DB Transactions not supported. Test skipped'); } } + + /** + * Test some more complex SQL syntax which moodle uses and depends on to work + * useful to determine if new database libraries can be supported. + */ + public function test_get_records_sql_complicated() { + global $CFG; + $DB = $this->tdb; + $dbman = $DB->get_manager(); + + $table = $this->get_test_table(); + $tablename = $table->getName(); + + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $dbman->create_table($table); + $this->tables[$tablename] = $table; + + $DB->insert_record($tablename, array('course' => 3)); + $DB->insert_record($tablename, array('course' => 3)); + $DB->insert_record($tablename, array('course' => 5)); + $DB->insert_record($tablename, array('course' => 2)); + + // we have sql like this in moodle, this syntax breaks on older versions of sqlite for example.. + $sql = 'SELECT a.id AS id, a.course AS course + FROM {'.$tablename.'} a + JOIN (SELECT * FROM {'.$tablename.'}) b + ON a.id = b.id + WHERE a.course = ?'; + + $this->assertTrue($records = $DB->get_records_sql($sql, array(3))); + $this->assertEqual(2, count($records)); + $this->assertEqual(1, reset($records)->id); + $this->assertEqual(2, next($records)->id); + } } /** -- 1.5.6.5