Skip to content

Commit

Permalink
Migrating ModelReadTest to PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 5, 2010
1 parent 2e7d2d5 commit c23fe5d
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 86 deletions.
37 changes: 30 additions & 7 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -381,10 +381,8 @@ public function fetchRow($sql = null) {
* @return array Array of resultset rows, or false if no rows matched
*/
public function fetchAll($sql, $cache = true, $modelName = null) {
if ($cache && isset($this->_queryCache[$sql])) {
if (preg_match('/^\s*select/i', $sql)) {
return $this->_queryCache[$sql];
}
if ($cache && ($cached = $this->getQueryCache($sql)) !== false) {
return $cached;
}

if ($this->execute($sql)) {
Expand All @@ -400,9 +398,7 @@ public function fetchAll($sql, $cache = true, $modelName = null) {
}

if ($cache) {
if (strpos(trim(strtolower($sql)), 'select') !== false) {
$this->_queryCache[$sql] = $out;
}
$this->_writeQueryCache($sql, $out);
}
if (empty($out) && is_bool($this->_result)) {
return $this->_result;
Expand Down Expand Up @@ -2844,4 +2840,31 @@ public function introspectType($value) {
}
return 'string';
}

/**
* Writes a new key for the in memory sql query cache
*
* @param string $sql SQL query
* @param mixed $data result of $sql query
* @return void
*/
protected function _writeQueryCache($sql, $data) {
if (strpos(trim(strtolower($sql)), 'select') !== false) {
$this->_queryCache[$sql] = $data;
}
}

/**
* Returns the result for a sql query if it is already cached
*
* @param string $sql SQL query
* @return mixed results for query if it is cached, false otherwise
*/
public function getQueryCache($sql = null) {
if (isset($this->_queryCache[$sql]) && preg_match('/^\s*select/i', $sql)) {
return $this->_queryCache[$sql];
}
return false;
}

}

0 comments on commit c23fe5d

Please sign in to comment.