[Solar-svn] Revision 2792
pmjones at solarphp.com
pmjones at solarphp.com
Fri Sep 28 20:38:33 CDT 2007
branch: Solar_Sql_Adapter*:
* [ADD] Added 'cache' config key and $_cache property. This is currently used
to cache fetchTableList() and fetchTableCols() queries in support of the new
Model/Record/Collection system. Use will expand later to cache query
results.
* [CHG] In the abstract adapter, methods fetchTableList() and fetchTableCols()
are now public and act as facades to the new _fetchTableList() and
_fetchTableCols() methods, respectively. This is to enable transparent
caching of results from the underlying adapters.
* [CHG] Methods createTable(), dropTable(), addColumn(), dropColumn(),
createSequence(), and dropSequence() now clear the internal query cache.
Modified: branches/orm/Solar/Sql/Adapter/Mssql.php
===================================================================
--- branches/orm/Solar/Sql/Adapter/Mssql.php 2007-09-29 01:14:49 UTC (rev 2791)
+++ branches/orm/Solar/Sql/Adapter/Mssql.php 2007-09-29 01:38:33 UTC (rev 2792)
@@ -208,7 +208,7 @@
* @return array All table names in the database.
*
*/
- public function fetchTableList()
+ protected function _fetchTableList()
{
$cmd = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
$result = $this->query($cmd);
@@ -225,7 +225,7 @@
* @return array An array of table columns.
*
*/
- public function fetchTableCols($table)
+ protected function _fetchTableCols($table)
{
$cmd = "
SELECT
Modified: branches/orm/Solar/Sql/Adapter/Mysql.php
===================================================================
--- branches/orm/Solar/Sql/Adapter/Mysql.php 2007-09-29 01:14:49 UTC (rev 2791)
+++ branches/orm/Solar/Sql/Adapter/Mysql.php 2007-09-29 01:38:33 UTC (rev 2792)
@@ -111,7 +111,7 @@
* @return array All table names in the database.
*
*/
- public function fetchTableList()
+ protected function _fetchTableList()
{
return $this->fetchCol('SHOW TABLES');
}
@@ -125,7 +125,7 @@
* @return array An array of table column information.
*
*/
- public function fetchTableCols($table)
+ protected function _fetchTableCols($table)
{
// mysql> DESCRIBE table_name;
// +--------------+--------------+------+-----+---------+-------+
Modified: branches/orm/Solar/Sql/Adapter/Pgsql.php
===================================================================
--- branches/orm/Solar/Sql/Adapter/Pgsql.php 2007-09-29 01:14:49 UTC (rev 2791)
+++ branches/orm/Solar/Sql/Adapter/Pgsql.php 2007-09-29 01:38:33 UTC (rev 2792)
@@ -98,7 +98,7 @@
* @return array All table names in the database.
*
*/
- public function fetchTableList()
+ protected function _fetchTableList()
{
$cmd = "
SELECT DISTINCT table_name
@@ -118,7 +118,7 @@
* @return array
*
*/
- public function fetchTableCols($table)
+ protected function _fetchTableCols($table)
{
// name | type | require | primary | default
// ----------------------+-----------------------------+---------+---------+-------------------------------------------------------------
Modified: branches/orm/Solar/Sql/Adapter/Sqlite.php
===================================================================
--- branches/orm/Solar/Sql/Adapter/Sqlite.php 2007-09-29 01:14:49 UTC (rev 2791)
+++ branches/orm/Solar/Sql/Adapter/Sqlite.php 2007-09-29 01:38:33 UTC (rev 2792)
@@ -113,7 +113,7 @@
* @return array All table names in the database.
*
*/
- public function fetchTableList()
+ protected function _fetchTableList()
{
// copied from PEAR DB
$cmd = "SELECT name FROM sqlite_master WHERE type='table' " .
@@ -132,7 +132,7 @@
* @return array
*
*/
- public function fetchTableCols($table)
+ protected function _fetchTableCols($table)
{
// sqlite> create table areas (id INTEGER PRIMARY KEY AUTOINCREMENT,
// name VARCHAR(32) NOT NULL);
@@ -169,12 +169,17 @@
$matches
);
+ // literal default values come back with single-quotes
+ $default = is_string($val['dflt_value'])
+ ? trim($val['dflt_value'], "'")
+ : $val['dflt_value'];
+
$descr[$name] = array(
'name' => $name,
'type' => $type,
'size' => ($size ? (int) $size : null),
'scope' => ($scope ? (int) $scope : null),
- 'default' => $val['dflt_value'],
+ 'default' => $default,
'require' => (bool) ($val['notnull']),
'primary' => (bool) ($val['pk'] == 1),
'autoinc' => (bool) $autoinc,
Modified: branches/orm/Solar/Sql/Adapter.php
===================================================================
--- branches/orm/Solar/Sql/Adapter.php 2007-09-29 01:14:49 UTC (rev 2791)
+++ branches/orm/Solar/Sql/Adapter.php 2007-09-29 01:38:33 UTC (rev 2792)
@@ -81,8 +81,13 @@
'pass' => null,
'name' => null,
'profiling' => false,
+ 'cache' => array('adapter' => 'Solar_Cache_Adapter_Var'),
);
+ protected $_cache;
+
+ protected $_cache_key_prefix;
+
/**
*
* Map of Solar generic types to RDBMS native types used when creating
@@ -231,6 +236,7 @@
public function __construct($config = null)
{
parent::__construct($config);
+ $this->_cache = Solar::dependency('Solar_Cache', $this->_config['cache']);
$this->setProfiling($this->_config['profiling']);
}
@@ -312,6 +318,8 @@
*
* Creates a PDO object and connects to the database.
*
+ * Also sets the query-cache key prefix.
+ *
* @return void
*
*/
@@ -328,6 +336,9 @@
// build a DSN
$dsn = $this->_dsn();
+ // save the cache-key prefix
+ $this->_cache_key_prefix = get_class($this) . '/' . md5($dsn);
+
// attempt the connection
$this->_pdo = new PDO(
$dsn,
@@ -370,6 +381,20 @@
/**
*
+ * Gets a full cache key.
+ *
+ * @var string $key The partial cache key.
+ *
+ * @return string The full cache key.
+ *
+ */
+ protected function _getCacheKey($key)
+ {
+ return $this->_cache_key_prefix . "/$key";
+ }
+
+ /**
+ *
* Prepares and executes an SQL statement, optionally binding values
* to named parameters in the statement.
*
@@ -1277,8 +1302,19 @@
* @return array A sequential array of table names in the database.
*
*/
- abstract public function fetchTableList();
+ public function fetchTableList()
+ {
+ $key = $this->_getCacheKey(__FUNCTION__);
+ $result = $this->_cache->fetch($key);
+ if (! $result) {
+ $result = $this->_fetchTableList();
+ $this->_cache->add($key, $result);
+ }
+ return $result;
+ }
+ abstract protected function _fetchTableList();
+
/**
*
* Returns an array describing the columns in a table.
@@ -1288,8 +1324,19 @@
* @return array An array of table columns.
*
*/
- abstract public function fetchTableCols($table);
+ public function fetchTableCols($table)
+ {
+ $key = $this->_getCacheKey(__FUNCTION__ . "/$table");
+ $result = $this->_cache->fetch($key);
+ if (! $result) {
+ $result = $this->_fetchTableCols($table);
+ $this->_cache->add($key, $result);
+ }
+ return $result;
+ }
+ abstract protected function _fetchTableCols($table);
+
/**
*
* Given a column specification, parse into datatype, size, and
@@ -1381,6 +1428,7 @@
*/
public function createTable($table, $cols)
{
+ $this->_cache->deleteAll();
$stmt = $this->_sqlCreateTable($table, $cols);
$this->query($stmt);
}
@@ -1445,6 +1493,7 @@
*/
public function dropTable($table)
{
+ $this->_cache->deleteAll();
return $this->query("DROP TABLE IF EXISTS $table");
}
@@ -1477,6 +1526,7 @@
*/
public function addColumn($table, $name, $info)
{
+ $this->_cache->deleteAll();
$coldef = $this->_sqlColdef($name, $info);
$stmt = "ALTER TABLE $table ADD COLUMN $coldef";
return $this->query($stmt);
@@ -1495,6 +1545,7 @@
*/
public function dropColumn($table, $name)
{
+ $this->_cache->deleteAll();
return $this->query("ALTER TABLE $table DROP COLUMN $name");
}
@@ -1606,6 +1657,7 @@
*/
public function createSequence($name, $start = 1)
{
+ $this->_cache->deleteAll();
$name = $this->_modSequenceName($name);
$result = $this->_createSequence($name, $start);
return $result;
@@ -1635,6 +1687,7 @@
*/
public function dropSequence($name)
{
+ $this->_cache->deleteAll();
$name = $this->_modSequenceName($name);
$result = $this->_dropSequence($name);
return $result;
More information about the Solar-svn
mailing list