From 65d259ef555ecc1d5326da3c46bb6522495f64e1 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 1 Sep 2022 00:21:51 +1000 Subject: [PATCH] MDL-75614 dml: Fixed debugsqltrace with anon classes bug --- lib/dml/moodle_database.php | 6 +++--- lib/dml/tests/dml_test.php | 24 ++++++++++++++++++++++++ lib/setuplib.php | 19 ++++++++++++------- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 97a41bc1bed..53e7e144f4d 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -889,9 +889,6 @@ abstract class moodle_database { // convert table names $sql = $this->fix_table_names($sql); - // Optionally add debug trace to sql as a comment. - $sql = $this->add_sql_debugging($sql); - // cast booleans to 1/0 int and detect forbidden objects foreach ($params as $key => $value) { $this->detect_objects($value); @@ -903,6 +900,9 @@ abstract class moodle_database { $dollar_count = preg_match_all('/\$[1-9][0-9]*/', $sql, $dollar_matches); $q_count = substr_count($sql, '?'); + // Optionally add debug trace to sql as a comment. + $sql = $this->add_sql_debugging($sql); + $count = 0; if ($named_count) { diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 23f9b7a3bfa..c7a16f605cd 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -508,6 +508,30 @@ EOD; $CFG->debugsqltrace = 0; } + /** + * Test the database debugging as SQL comment in anon class + * + * @covers ::add_sql_debugging + */ + public function test_sql_debugging_anon_class() { + global $CFG; + $CFG->debugsqltrace = 100; + + // A anon class. + $another = new class { + /** + * Just a test log function + */ + public function log() { + global $DB; + $DB->get_records_sql('SELECT firstname FROM {user} WHERE firstname = :firstname', ['firstname' => 'JohnDoe']); + } + }; + $another->log(); + $CFG->debugsqltrace = 0; + // No assertions just it should not error. + } + public function test_strtok() { // Strtok was previously used by bound emulation, make sure it is not used any more. $DB = $this->tdb; diff --git a/lib/setuplib.php b/lib/setuplib.php index aacdb699948..d1065dc8150 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -714,18 +714,23 @@ function format_backtrace($callers, $plaintext = false) { if (!isset($caller['file'])) { $caller['file'] = 'unknownfile'; // probably call_user_func() } - $from .= $plaintext ? '* ' : '
  • '; - $from .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']); + $line = $plaintext ? '* ' : '
  • '; + $line .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']); if (isset($caller['function'])) { - $from .= ': call to '; + $line .= ': call to '; if (isset($caller['class'])) { - $from .= $caller['class'] . $caller['type']; + $line .= $caller['class'] . $caller['type']; } - $from .= $caller['function'] . '()'; + $line .= $caller['function'] . '()'; } else if (isset($caller['exception'])) { - $from .= ': '.$caller['exception'].' thrown'; + $line .= ': '.$caller['exception'].' thrown'; } - $from .= $plaintext ? "\n" : '
  • '; + + // Remove any non printable chars. + $line = preg_replace('/[[:^print:]]/', '', $line); + + $line .= $plaintext ? "\n" : ''; + $from .= $line; } $from .= $plaintext ? '' : '';