MDL-24028 cast all bools in DML params to integers 1/0; credit goes to Eloy Lafuente

This commit is contained in:
Petr Skoda 2010-09-03 15:44:11 +00:00
parent deb73728e7
commit 0e6e90516a
2 changed files with 30 additions and 0 deletions

View file

@ -632,6 +632,11 @@ abstract class moodle_database {
// convert table names // convert table names
$sql = $this->fix_table_names($sql); $sql = $this->fix_table_names($sql);
// cast booleans to 1/0 int
foreach ($params as $key => $value) {
$params[$key] = is_bool($value) ? (int)$value : $value;
}
// NICOLAS C: Fixed regexp for negative backwards lookahead of double colons. Thanks for Sam Marshall's help // NICOLAS C: Fixed regexp for negative backwards lookahead of double colons. Thanks for Sam Marshall's help
$named_count = preg_match_all('/(?<!:):[a-z][a-z0-9_]*/', $sql, $named_matches); // :: used in pgsql casts $named_count = preg_match_all('/(?<!:):[a-z][a-z0-9_]*/', $sql, $named_matches); // :: used in pgsql casts
$dollar_count = preg_match_all('/\$[1-9][0-9]*/', $sql, $dollar_matches); $dollar_count = preg_match_all('/\$[1-9][0-9]*/', $sql, $dollar_matches);

View file

@ -180,7 +180,26 @@ class dml_test extends UnitTestCase {
} catch (Exception $e) { } catch (Exception $e) {
$this->assertTrue($e instanceof moodle_exception); $this->assertTrue($e instanceof moodle_exception);
} }
// Booleans in NAMED params are casting to 1/0 int
$sql = "SELECT * FROM {".$tablename."} WHERE course = ? OR course = ?";
$params = array(true, false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
// Booleans in QM params are casting to 1/0 int
$sql = "SELECT * FROM {".$tablename."} WHERE course = :course1 OR course = :course2";
$params = array('course1' => true, 'course2' => false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
// Booleans in DOLLAR params are casting to 1/0 int
$sql = "SELECT * FROM {".$tablename."} WHERE course = \$1 OR course = \$2";
$params = array(true, false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
} }
public function testGetTables() { public function testGetTables() {
@ -732,6 +751,12 @@ class dml_test extends UnitTestCase {
$this->assertFalse(empty($records[1]->id)); $this->assertFalse(empty($records[1]->id));
$this->assertEqual(4, count($records)); $this->assertEqual(4, count($records));
// Booleans into params
$records = $DB->get_records($tablename, array('course' => true));
$this->assertEqual(0, count($records));
$records = $DB->get_records($tablename, array('course' => false));
$this->assertEqual(0, count($records));
// note: delegate limits testing to test_get_records_sql() // note: delegate limits testing to test_get_records_sql()
} }