Skip to content
Permalink
Browse files

Moving more code after changing ConnectionManager's namespace

  • Loading branch information...
lorenzo committed Feb 17, 2014
1 parent 8eec058 commit dbfb3de1b54c8dadaacb71fd3b56ee3e9242f5b4
@@ -43,6 +43,15 @@ class Collection {
*/
protected $_dialect;
/**
* The name of the cache config key to use for caching table metadata,
* of false if disabled.
*
* @var string|boolean
*/
protected $_cache = false;
/**
* Constructor.
*
@@ -51,6 +60,11 @@ class Collection {
public function __construct(Connection $connection) {
$this->_connection = $connection;
$this->_dialect = $connection->driver()->schemaDialect();
$config = $this->_connection->config();
if (!empty($config['cacheMetadata'])) {
$this->cacheMetadata(true);
}
}
/**
@@ -80,17 +94,16 @@ public function listTables() {
* @throws Cake\Database\Exception when table cannot be described.
*/
public function describe($name) {
$config = $this->_connection->config();
if (!empty($config['cacheMetadata'])) {
$cacheConfig = ($config['cacheMetadata'] === true) ? '_cake_model_' : $config['cacheMetadata'];
$cacheConfig = $this->cacheMetadata();
if ($cacheConfig) {
$cacheKey = $this->_connection->configName() . '_' . $name;
$cached = Cache::read($cacheKey, $cacheConfig);
if ($cached !== false) {
return $cached;
}
}
$config = $this->_connection->config();
list($sql, $params) = $this->_dialect->describeTableSql($name, $config);
$statement = $this->_executeSql($sql, $params);
if (count($statement) === 0) {
@@ -115,12 +128,30 @@ public function describe($name) {
}
$statement->closeCursor();
if (!empty($config['cacheMetadata'])) {
if (!empty($cacheConfig)) {
Cache::write($cacheKey, $table, $cacheConfig);
}
return $table;
}
/**
* Sets the cache config name to use for caching table metadata, or
* disabels it if false is passed.
* If called with no arguments it returns the current configuration name.
*
* @param boolean $enable whether or not to enable caching
* @return string|boolean
*/
public function cacheMetadata($enable = null) {
if ($enable === null) {
return $this->_cache;
}
if ($enable === true) {
$enable = '_cake_model_';
}
return $this->_cache = $enable;
}
/**
* Helper method to run queries and convert Exceptions to the correct types.
*
@@ -19,7 +19,8 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\DataSource\ConnectionManager;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\Error;
use Cake\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\TestCase;
@@ -18,7 +18,7 @@
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -15,9 +15,9 @@
namespace Cake\Test\TestCase\Database;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Query;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -19,9 +19,9 @@
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -75,15 +75,11 @@ public function testDescribeIncorrectTable() {
* @return void
*/
public function testDescribeCache() {
$schema = $this->connection->schemaCollection();
$table = $this->connection->schemaCollection()->describe('users');
$config = $this->connection->config();
$config['cacheMetadata'] = true;
$connection = new Connection($config);
$schema = new Collection($connection);
Cache::delete('test_users', '_cake_model_');
$schema->cacheMetadata(true);
$result = $schema->describe('users');
$this->assertEquals($table, $result);
@@ -15,10 +15,10 @@
namespace Cake\Test\TestCase\Database\Schema;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\MysqlSchema;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -15,10 +15,10 @@
namespace Cake\Test\TestCase\Database\Schema;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\PostgresSchema;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -15,10 +15,10 @@
namespace Cake\Test\TestCase\Database\Schema;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\SqliteSchema;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
/**
@@ -14,10 +14,10 @@
*/
namespace Cake\Test\TestCase\ORM;
use Cake\Database\ConnectionManager;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\OrderByExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\ORM\Table;

0 comments on commit dbfb3de

Please sign in to comment.
You can’t perform that action at this time.