Skip to content

Commit

Permalink
MDL-79926 dml: Add explain to log_queries info field
Browse files Browse the repository at this point in the history
  • Loading branch information
AviMoto committed Oct 31, 2023
1 parent 859df15 commit b2592a8
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 143 deletions.
9 changes: 8 additions & 1 deletion lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public function query_log($error=false) {
$log->sqltext = $this->last_sql;
$log->sqlparams = var_export((array)$this->last_params, true);
$log->error = (int)$iserror;
$log->info = $iserror ? $error : null;
$log->info = $iserror ? $error : var_export($this->query_log_explain(), true);
$log->backtrace = format_backtrace($backtrace, true);
$log->exectime = $time;
$log->timelogged = time();
Expand All @@ -550,6 +550,13 @@ public function query_log($error=false) {
}
}

/**
* Get Last query explain.
*
* @return array
*/
abstract protected function query_log_explain(): array;

/**
* Disable logging temporarily.
*/
Expand Down
26 changes: 26 additions & 0 deletions lib/dml/mysqli_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,32 @@ public function is_compressed_row_format_supported($cached = true) {
return $this->compressedrowformatsupported;
}

/**
* Get Last query explain.
*
* @return array
*/
protected function query_log_explain(): array {
list($sql, $params, $type) = $this->fix_sql_params($this->last_sql, $this->last_params);

if (strpos($sql, ';') !== false) {
throw new coding_exception('moodle_database::execute() Multiple sql statements found or
bound parameters not used properly in query!');
}

$rawsql = $this->emulate_bound_params($sql, $params);
$explain = [];
$result = $this->mysqli->query('EXPLAIN ' . $rawsql);
foreach ($result as $rs) {
$explain[] = $rs;
}
if ($result !== true) {
$result->close();
}

return $explain;
}

/**
* Check the database to see if innodb_file_per_table is on.
*
Expand Down
23 changes: 23 additions & 0 deletions lib/dml/oci_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,29 @@ protected function create_recordset($stmt) {
return new oci_native_moodle_recordset($stmt);
}

/**
* Get Last query explain.
*
* @return array
*/
protected function query_log_explain(): array {
list($rawsql, $params, $type) = $this->fix_sql_params($this->last_sql, $this->last_params);

list($rawsql, $params) = $this->tweak_param_names($rawsql, $params);

$explain = [];
$stmt = $this->parse_query('EXPLAIN ' . $rawsql);
$descriptors = [];
$this->bind_params($stmt, $params, null, $descriptors);
$result = oci_execute($stmt, $this->commit_status);
$this->free_descriptors($descriptors);
foreach ($this->create_recordset($stmt) as $rs) {
$explain[] = $rs;
}

return $explain;
}

/**
* Get a number of records as an array of objects using a SQL statement.
*
Expand Down
29 changes: 29 additions & 0 deletions lib/dml/pdo_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,35 @@ public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limit
return $result;
}

/**
* Get Last query explain.
*
* @return array
* @throws coding_exception
* @throws ddl_change_structure_exception
* @throws dml_exception
* @throws dml_read_exception
* @throws dml_write_exception
*/
protected function query_log_explain(): array {
list($sql, $params, $type) = $this->fix_sql_params($this->last_sql, $this->last_params);
$sql = 'EXPLAIN ' . $sql;
try {
$sth = $this->pdb->prepare($sql);
$sth->execute($params);
$result = $this->create_recordset($sth);
} catch (PDOException $ex) {
$this->lastError = $ex->getMessage();
$result = false;
}
$explain = [];
foreach ($result as $rs) {
$explain[] = $rs;
}

return $explain;
}

/**
* Selects rows and return values of first column as array.
*
Expand Down
Loading

0 comments on commit b2592a8

Please sign in to comment.