Skip to content

Commit

Permalink
MDL-48856 dmllib: Adjust performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-russ committed Jul 22, 2016
1 parent 73d37b2 commit e868574
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions lib/dml/pgsql_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ public function get_columns($table, $usecache=true) {
* @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;

Expand All @@ -600,8 +602,6 @@ protected function normalise_value($column, $value) {
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;
}
Expand Down Expand Up @@ -678,11 +678,7 @@ public function execute($sql, array $params=null) {
}

$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);
Expand Down Expand Up @@ -710,19 +706,11 @@ public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limit

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);
Expand Down Expand Up @@ -757,19 +745,11 @@ public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnu

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);
Expand All @@ -787,24 +767,26 @@ public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnu
}
}

$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) {
$row[$blob] = ($row[$blob] !== null ? pg_unescape_bytea($row[$blob]) : null);
}
}
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;
}

Expand Down

0 comments on commit e868574

Please sign in to comment.