Enabled caching table_columns() and results

This commit is contained in:
Deon George 2015-10-08 15:57:30 +11:00
parent 9edf5b81b3
commit 8de1dfc484
2 changed files with 41 additions and 4 deletions

View File

@ -75,6 +75,10 @@ class Kohana_Database_DB2 extends Database {
$e->getCode()); $e->getCode());
} }
if (! $this->_connection) {
throw new Database_Exception('Connection to DB2 failed? (:error)', array(':error'=>db2_conn_errormsg()));
}
// \xFF is a better delimiter, but the PHP driver uses underscore // \xFF is a better delimiter, but the PHP driver uses underscore
$this->_connection_id = sha1($hostname.'_'.$username.'_'.$password); $this->_connection_id = sha1($hostname.'_'.$username.'_'.$password);
@ -152,6 +156,17 @@ class Kohana_Database_DB2 extends Database {
} }
public function list_columns($table, $like = NULL, $add_prefix = FALSE) { public function list_columns($table, $like = NULL, $add_prefix = FALSE) {
// If caching is on, cache the tables
if ($this->_config['caching']) {
$cache_key = __METHOD__.':'.$table.':'.$like.':'.$add_prefix;
$columns = Kohana::cache($cache_key);
if ($columns) {
return $columns;
}
}
// Make sure the database is connected // Make sure the database is connected
$this->_connection or $this->connect(); $this->_connection or $this->connect();
@ -166,7 +181,12 @@ class Kohana_Database_DB2 extends Database {
else else
{ {
// Find all column names // Find all column names
$result = db2_columns($this->_connection,NULL,'%',$table); try {
$result = db2_columns($this->_connection,NULL,'%',$table);
} catch (Exception $e) {
throw new Kohana_Exception('Unable to show DB Columns :table (:connection)',array(':table'=>$table,':connection'=>$this->_connection));
}
} }
$count = 0; $count = 0;
@ -224,6 +244,11 @@ class Kohana_Database_DB2 extends Database {
$columns[$row['COLUMN_NAME']] = $column; $columns[$row['COLUMN_NAME']] = $column;
} }
// Save our cache data for next call
if ($this->_config['caching']) {
Kohana::cache($cache_key,$columns);
}
return $columns; return $columns;
} }

View File

@ -12,6 +12,9 @@ class Kohana_Database_DB2_Result extends Database_Result {
protected $_internal_row = 0; protected $_internal_row = 0;
// if we cache queries, we need to preserve our data, otherwise it is lost
protected $_internal_data = array();
public function __construct($result, $sql, $as_object = FALSE, array $params = NULL) public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
{ {
parent::__construct($result, $sql, $as_object, $params); parent::__construct($result, $sql, $as_object, $params);
@ -39,7 +42,7 @@ class Kohana_Database_DB2_Result extends Database_Result {
} }
else else
{ {
return FALSE; return isset($this->_internal_data[$offset+1]);
} }
} }
@ -53,11 +56,13 @@ class Kohana_Database_DB2_Result extends Database_Result {
if ($this->_as_object === TRUE) if ($this->_as_object === TRUE)
{ {
throw new Kohana_Exception('This configuration is not tested for caching');
// Return an stdClass // Return an stdClass
return db2_fetch_object($this->_result); return db2_fetch_object($this->_result);
} }
elseif (is_string($this->_as_object)) elseif (is_string($this->_as_object))
{ {
throw new Kohana_Exception('This configuration is not tested for caching');
$o = new $this->_as_object; $o = new $this->_as_object;
// Return an object of given class name // Return an object of given class name
@ -67,8 +72,15 @@ class Kohana_Database_DB2_Result extends Database_Result {
else else
{ {
// Return an array of the row // Return an array of the row
return db2_fetch_assoc($this->_result); return isset($this->_internal_data[$this->_internal_row])
? $this->_internal_data[$this->_internal_row]
: ($this->_internal_data[$this->_internal_row] = db2_fetch_assoc($this->_result));
} }
} }
} // End Database_DB2L_Result public function rewind() {
$this->_internal_row = 0;
return parent::rewind();
}
} // End Database_DB2_Result