From 07871f81e11d227ed73dfb2f5cd08836c3b4b376 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 11 Aug 2009 08:51:05 +0000 Subject: [PATCH] ENHANCMENT: Low-level performance improvements in database access. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@84163 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Database.php | 17 +++++++++-------- core/model/MySQLDatabase.php | 17 +++-------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/core/model/Database.php b/core/model/Database.php index b8e43ea8c..041dcda7f 100755 --- a/core/model/Database.php +++ b/core/model/Database.php @@ -711,10 +711,11 @@ abstract class Query implements Iterator { public function column($column = null) { $result = array(); - foreach($this as $record) { - $result[] = ($column) ? $record[$column] : reset($record); + while($record = $this->next()) { + if($column) $result[] = $record[$column]; + else $result[] = $record[key($record)]; } - + return $result; } @@ -726,7 +727,7 @@ abstract class Query implements Iterator { public function keyedColumn() { $column = array(); foreach($this as $record) { - $val = reset($record); + $val = $record[key($record)]; $column[$val] = $val; } return $column; @@ -759,9 +760,8 @@ abstract class Query implements Iterator { * @return string */ public function value() { - foreach($this as $record) { - return reset($record); - } + $record = $this->next(); + if($record) return $record[key($record)]; } /** @@ -851,7 +851,8 @@ abstract class Query implements Iterator { * @return boolean */ public function valid() { - return $this->current() !== false; + if(!$this->currentRecord) $this->next(); + return $this->currentRecord !== false; } /** diff --git a/core/model/MySQLDatabase.php b/core/model/MySQLDatabase.php index cf25f3fa5..e7aa6fce0 100644 --- a/core/model/MySQLDatabase.php +++ b/core/model/MySQLDatabase.php @@ -893,20 +893,9 @@ class MySQLQuery extends Query { } public function nextRecord() { - // Coalesce rather than replace common fields. - if($data = mysql_fetch_row($this->handle)) { - foreach($data as $columnIdx => $value) { - $columnName = mysql_field_name($this->handle, $columnIdx); - // $value || !$ouput[$columnName] means that the *last* occurring value is shown - // !$ouput[$columnName] means that the *first* occurring value is shown - if(isset($value) || !isset($output[$columnName])) { - $output[$columnName] = $value; - } - } - return $output; - } else { - return false; - } + $data = mysql_fetch_assoc($this->handle); + if(!$data) $data = false; + return $data; }