Skip to content

Commit

Permalink
Database: deprecated Connection::setDatabaseReflection() and setCache…
Browse files Browse the repository at this point in the history
…Storage(), methods are replaced with setSelectionFactory() (BC break)
  • Loading branch information
dg committed Nov 15, 2012
1 parent 0a879e6 commit e7fccad
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 67 deletions.
22 changes: 14 additions & 8 deletions Nette/Config/Extensions/NetteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,27 @@ private function setupDatabase(ContainerBuilder $container, array $config)
$info['options'][constant($key)] = $value;
}

if (!$info['reflection']) {
$reflection = NULL;
} elseif (is_string($info['reflection'])) {
$reflection = new Nette\DI\Statement(preg_match('#^[a-z]+\z#', $info['reflection'])
? 'Nette\Database\Reflection\\' . ucfirst($info['reflection']) . 'Reflection'
: $info['reflection'], array('@self'));
} else {
$tmp = Nette\Config\Compiler::filterArguments(array($info['reflection']));
$reflection = reset($tmp);
}

$connection = $container->addDefinition($this->prefix("database.$name"))
->setClass('Nette\Database\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))
->setAutowired($info['autowired'])
->addSetup('setCacheStorage')
->addSetup('setSelectionFactory', array(
new Nette\DI\Statement('Nette\Database\Table\SelectionFactory', array('@self', $reflection)),
))
->addSetup('Nette\Diagnostics\Debugger::$blueScreen->addPanel(?)', array(
'Nette\Database\Diagnostics\ConnectionPanel::renderException'
));

if ($info['reflection']) {
$connection->addSetup('setDatabaseReflection', is_string($info['reflection'])
? array(new Nette\DI\Statement(preg_match('#^[a-z]+\z#', $info['reflection']) ? 'Nette\Database\Reflection\\' . ucfirst($info['reflection']) . 'Reflection' : $info['reflection']))
: Nette\Config\Compiler::filterArguments(array($info['reflection']))
);
}

if ($container->parameters['debugMode'] && $info['debugger']) {
$connection->addSetup('Nette\Database\Helpers::createDebugPanel', array($connection, !empty($info['explain'])));
}
Expand Down
85 changes: 35 additions & 50 deletions Nette/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*
* @author David Grudl
*
* @property IReflection $databaseReflection
* @property-read ISupplementalDriver $supplementalDriver
* @property-read string $dsn
*/
Expand All @@ -37,11 +36,8 @@ class Connection extends PDO
/** @var SqlPreprocessor */
private $preprocessor;

/** @var IReflection */
private $databaseReflection;

/** @var Nette\Caching\Cache */
private $cache;
/** @var Table\SelectionFactory */
private $selectionFactory;

/** @var array of function(Statement $result, $params); Occurs after query is executed */
public $onQuery;
Expand Down Expand Up @@ -76,48 +72,6 @@ public function getSupplementalDriver()



/**
* Sets database reflection.
* @return Connection provides a fluent interface
*/
public function setDatabaseReflection(IReflection $databaseReflection)
{
$this->databaseReflection = $databaseReflection;
return $this;
}



/** @return IReflection */
public function getDatabaseReflection()
{
if (!$this->databaseReflection) {
$this->setDatabaseReflection(new Reflection\ConventionalReflection);
}
return $this->databaseReflection;
}



/**
* Sets cache storage engine.
* @return Connection provides a fluent interface
*/
public function setCacheStorage(Nette\Caching\IStorage $storage = NULL)
{
$this->cache = $storage ? new Nette\Caching\Cache($storage, 'Nette.Database.' . md5($this->dsn)) : NULL;
return $this;
}



public function getCache()
{
return $this->cache;
}



/**
* Generates and executes SQL query.
* @param string statement
Expand Down Expand Up @@ -221,7 +175,7 @@ public function fetchAll($args)



/********************* selector ****************d*g**/
/********************* Selection ****************d*g**/



Expand All @@ -232,7 +186,38 @@ public function fetchAll($args)
*/
public function table($table)
{
return new Table\Selection($this, $table, $this->getDatabaseReflection(), $this->cache ? $this->cache->getStorage() : NULL);
if (!$this->selectionFactory) {
$this->selectionFactory = new Table\SelectionFactory($this);
}
return $this->selectionFactory->create($table);
}



/**
* @return Connection provides a fluent interface
*/
public function setSelectionFactory(Table\SelectionFactory $selectionFactory)
{
$this->selectionFactory = $selectionFactory;
return $this;
}



/** @deprecated */
function setDatabaseReflection()
{
trigger_error(__METHOD__ . '() is deprecated; use setSelectionFactory() instead.', E_USER_DEPRECATED);
return $this;
}



/** @deprecated */
function setCacheStorage()
{
trigger_error(__METHOD__ . '() is deprecated; use setSelectionFactory() instead.', E_USER_DEPRECATED);
}


Expand Down
50 changes: 50 additions & 0 deletions Nette/Database/Table/SelectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* This file is part of the Nette Framework (http://nette.org)
*
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Nette\Database\Table;

use Nette;



/**
* Table\Selection factory.
*
* @author David Grudl
*/
class SelectionFactory extends Nette\Object
{
/** @var Nette\Database\Connection */
private $connection;

/** @var Nette\Database\IReflection */
private $reflection;

/** @var Nette\Caching\IStorage */
private $cacheStorage;


public function __construct(Nette\Database\Connection $connection, Nette\Database\IReflection $reflection = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
{
$this->connection = $connection;
$this->reflection = $reflection ?: new Nette\Database\Reflection\ConventionalReflection;
$this->cacheStorage = $cacheStorage;
}



/** @return Selection */
public function create($table)
{
return new Selection($this->connection, $table, $this->reflection, $this->cacheStorage);
}

}
5 changes: 4 additions & 1 deletion tests/Nette/Database/Table.basic2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
require __DIR__ . '/connect.inc.php'; // create $connection

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/{$driverName}-nette_test2.sql");
$connection->setDatabaseReflection(new Nette\Database\Reflection\DiscoveredReflection($connection));
$connection->setSelectionFactory(new Nette\Database\Table\SelectionFactory(
$connection,
new Nette\Database\Reflection\DiscoveredReflection($connection)
));



Expand Down
10 changes: 6 additions & 4 deletions tests/Nette/Database/Table.cache.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/{$driverName}-nett


$cacheStorage = new Nette\Caching\Storages\MemoryStorage;
$connection->setCacheStorage($cacheStorage);
$connection->setDatabaseReflection(new Nette\Database\Reflection\DiscoveredReflection($connection, $cacheStorage));
$connection->setSelectionFactory(new Nette\Database\Table\SelectionFactory(
$connection,
new Nette\Database\Reflection\DiscoveredReflection($connection, $cacheStorage),
$cacheStorage
));



Expand Down Expand Up @@ -100,8 +103,7 @@ Assert::same(array(



$cacheStorage = new Nette\Caching\Storages\MemoryStorage;
$connection->setCacheStorage($cacheStorage);
$cacheStorage->clean(array(Nette\Caching\Cache::ALL => TRUE));



Expand Down
5 changes: 4 additions & 1 deletion tests/Nette/Database/Table.discoveredReflection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
require __DIR__ . '/connect.inc.php'; // create $connection

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/{$driverName}-nette_test1.sql");
$connection->setDatabaseReflection(new Nette\Database\Reflection\DiscoveredReflection($connection));
$connection->setSelectionFactory(new Nette\Database\Table\SelectionFactory(
$connection,
new Nette\Database\Reflection\DiscoveredReflection($connection)
));



Expand Down
7 changes: 5 additions & 2 deletions tests/Nette/Database/Table.multi-primary-key.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ require __DIR__ . '/connect.inc.php'; // create $connection

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/{$driverName}-nette_test1.sql");
$cacheStorage = new Nette\Caching\Storages\MemoryStorage;
$connection->setCacheStorage($cacheStorage);
$connection->setDatabaseReflection(new Nette\Database\Reflection\DiscoveredReflection($connection, $cacheStorage));
$connection->setSelectionFactory(new Nette\Database\Table\SelectionFactory(
$connection,
new Nette\Database\Reflection\DiscoveredReflection($connection, $cacheStorage),
$cacheStorage
));



Expand Down
5 changes: 4 additions & 1 deletion tests/Nette/Database/Table.self-reference.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use Nette\Database;
require __DIR__ . '/connect.inc.php'; // create $connection

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/{$driverName}-nette_test1.sql");
$connection->setDatabaseReflection(new Database\Reflection\DiscoveredReflection($connection));
$connection->setSelectionFactory(new Nette\Database\Table\SelectionFactory(
$connection,
new Nette\Database\Reflection\DiscoveredReflection($connection)
));



Expand Down

0 comments on commit e7fccad

Please sign in to comment.