Skip to content

Commit

Permalink
Moving more code after changing ConnectionManager's namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 17, 2014
1 parent 8eec058 commit dbfb3de
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
41 changes: 36 additions & 5 deletions src/Database/Schema/Collection.php
Expand Up @@ -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.
*
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -18,7 +18,7 @@

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/QueryTest.php
Expand Up @@ -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;

/**
Expand Down
10 changes: 3 additions & 7 deletions tests/TestCase/Database/Schema/CollectionTest.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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;
Expand Down

0 comments on commit dbfb3de

Please sign in to comment.