From 77abbc47f6a8bf74a4965fbde62e23e12aba3e9f Mon Sep 17 00:00:00 2001 From: stronk7 Date: Fri, 28 Aug 2009 11:17:47 +0000 Subject: [PATCH] Enforce strict mode in mysqli session in order to get consistent behaviour in various operations (casting, altering columns...) --- lib/dml/mysqli_native_moodle_database.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 1722d8358b..3a243f94ec 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -173,6 +173,19 @@ class mysqli_native_moodle_database extends moodle_database { $this->mysqli->set_charset('utf8'); $this->query_end(true); + // If available, enforce strict mode for the session. That guaranties + // standard behaviour under some situations, avoiding some MySQL nasty + // habits like truncating data or performing some transparent cast losses. + // With strict mode enforced, Moodle DB layer will be consistenly throwing + // the corresponding exceptions as expected. + $si = $this->get_server_info(); + if (version_compare($si['version'], '5.0.2', '>=')) { + $sql = "SET SESSION sql_mode = 'STRICT_ALL_TABLES'"; + $this->query_start($sql, null, SQL_QUERY_AUX); + $result = $this->mysqli->query($sql); + $this->query_end($result); + } + return true; } -- 2.39.5