From e868574f0202fd75321115361ec685b1526a691b Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Fri, 1 Jul 2016 09:12:41 +1000 Subject: [PATCH] MDL-48856 dmllib: Adjust performance improvements. --- lib/dml/pgsql_native_moodle_database.php | 60 +++++++++--------------- 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 86a10bb7ea9..97ba197d220 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -586,6 +586,8 @@ class pgsql_native_moodle_database extends moodle_database { * @return mixed the normalised value */ protected function normalise_value($column, $value) { + $this->detect_objects($value); + if (is_bool($value)) { // Always, convert boolean to int $value = (int)$value; @@ -600,8 +602,6 @@ class pgsql_native_moodle_database extends moodle_database { if ($column->meta_type === 'I' or $column->meta_type === 'F' or $column->meta_type === 'N') { $value = 0; // prevent '' problems in numeric fields } - } else if (is_object($value)) { - $this->detect_objects($value); } return $value; } @@ -678,11 +678,7 @@ class pgsql_native_moodle_database extends moodle_database { } $this->query_start($sql, $params, SQL_QUERY_UPDATE); - if (empty($params)) { - $result = pg_query($this->pgsql, $sql); - } else { - $result = pg_query_params($this->pgsql, $sql, $params); - } + $result = pg_query_params($this->pgsql, $sql, $params); $this->query_end($result); pg_free_result($result); @@ -710,19 +706,11 @@ class pgsql_native_moodle_database extends moodle_database { list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum); - if ($limitfrom or $limitnum) { - if ($limitnum < 1) { - $limitnum = "ALL"; - } else if (PHP_INT_MAX - $limitnum < $limitfrom) { - // this is a workaround for weird max int problem - $limitnum = "ALL"; - } - if ($limitnum != 'ALL') { - $sql .= " LIMIT $limitnum"; - } - if (!empty($limitfrom)) { - $sql .= " OFFSET $limitfrom"; - } + if ($limitnum) { + $sql .= " LIMIT $limitnum"; + } + if ($limitfrom) { + $sql .= " OFFSET $limitfrom"; } list($sql, $params, $type) = $this->fix_sql_params($sql, $params); @@ -757,19 +745,11 @@ class pgsql_native_moodle_database extends moodle_database { list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum); - if ($limitfrom or $limitnum) { - if ($limitnum < 1) { - $limitnum = "ALL"; - } else if (PHP_INT_MAX - $limitnum < $limitfrom) { - // this is a workaround for weird max int problem - $limitnum = "ALL"; - } - if ($limitnum != 'ALL') { - $sql .= " LIMIT $limitnum"; - } - if (!empty($limitfrom)) { - $sql .= " OFFSET $limitfrom"; - } + if ($limitnum) { + $sql .= " LIMIT $limitnum"; + } + if ($limitfrom) { + $sql .= " OFFSET $limitfrom"; } list($sql, $params, $type) = $this->fix_sql_params($sql, $params); @@ -787,9 +767,12 @@ class pgsql_native_moodle_database extends moodle_database { } } + $rows = pg_fetch_all($result); + pg_free_result($result); + $return = array(); - if ($result) { - while ($row = pg_fetch_object($result)) { + if ($rows) { + foreach ($rows as $row) { $id = reset($row); if ($blobs) { foreach ($blobs as $blob) { @@ -797,14 +780,13 @@ class pgsql_native_moodle_database extends moodle_database { } } if (isset($return[$id])) { - debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$id' found in column '".key($row)."'.", DEBUG_DEVELOPER); + $colname = key($row); + debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$id' found in column '$colname'.", DEBUG_DEVELOPER); } - $return[$id] = $row; + $return[$id] = (object)$row; } } - pg_free_result($result); - return $return; }