2006-05-30 12:42:10 +04:00
|
|
|
<?php
|
2006-07-27 21:51:19 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
2006-09-04 02:37:54 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @package Doctrine ORM
|
|
|
|
* @url www.phpdoctrine.com
|
|
|
|
* @license LGPL
|
|
|
|
*
|
|
|
|
* Doctrine_Manager is the base component of all doctrine based projects.
|
2006-08-22 03:19:15 +04:00
|
|
|
* It opens and keeps track of all connections (database connections).
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate {
|
|
|
|
/**
|
2006-08-18 02:53:52 +04:00
|
|
|
* @var array $connections an array containing all the opened connections
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
private $connections = array();
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @var integer $index the incremented index
|
|
|
|
*/
|
|
|
|
private $index = 0;
|
|
|
|
/**
|
2006-08-22 03:19:15 +04:00
|
|
|
* @var integer $currIndex the current connection index
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $currIndex = 0;
|
|
|
|
/**
|
|
|
|
* @var string $root root directory
|
|
|
|
*/
|
|
|
|
private $root;
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Null $null Doctrine_Null object, used for extremely fast null value checking
|
|
|
|
*/
|
|
|
|
private $null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
|
|
|
private function __construct() {
|
|
|
|
$this->root = dirname(__FILE__);
|
|
|
|
$this->null = new Doctrine_Null;
|
|
|
|
|
|
|
|
Doctrine_Record::initNullObject($this->null);
|
|
|
|
Doctrine_Collection::initNullObject($this->null);
|
2006-06-01 15:58:05 +04:00
|
|
|
Doctrine_Record_Iterator::initNullObject($this->null);
|
2006-06-07 13:07:38 +04:00
|
|
|
Doctrine_Validator::initNullObject($this->null);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return Doctrine_Null
|
|
|
|
*/
|
|
|
|
final public function getNullObject() {
|
|
|
|
return $this->null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* setDefaultAttributes
|
|
|
|
* sets default attributes
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
final public function setDefaultAttributes() {
|
|
|
|
static $init = false;
|
|
|
|
if( ! $init) {
|
|
|
|
$init = true;
|
|
|
|
$attributes = array(
|
2006-06-09 02:11:36 +04:00
|
|
|
Doctrine::ATTR_FETCHMODE => Doctrine::FETCH_IMMEDIATE,
|
2006-05-30 12:42:10 +04:00
|
|
|
Doctrine::ATTR_BATCH_SIZE => 5,
|
|
|
|
Doctrine::ATTR_COLL_LIMIT => 5,
|
2006-08-08 02:12:45 +04:00
|
|
|
Doctrine::ATTR_LISTENER => new Doctrine_EventListener_Empty(),
|
2006-05-30 12:42:10 +04:00
|
|
|
Doctrine::ATTR_LOCKMODE => 1,
|
|
|
|
Doctrine::ATTR_VLD => false,
|
2006-08-30 00:09:31 +04:00
|
|
|
Doctrine::ATTR_CREATE_TABLES => true,
|
|
|
|
Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS
|
2006-05-30 12:42:10 +04:00
|
|
|
);
|
|
|
|
foreach($attributes as $attribute => $value) {
|
|
|
|
$old = $this->getAttribute($attribute);
|
|
|
|
if($old === null)
|
|
|
|
$this->setAttribute($attribute,$value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the root directory of Doctrine
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public function getRoot() {
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getInstance
|
|
|
|
* returns an instance of this class
|
|
|
|
* (this class uses the singleton pattern)
|
|
|
|
*
|
|
|
|
* @return Doctrine_Manager
|
|
|
|
*/
|
|
|
|
final public static function getInstance() {
|
|
|
|
static $instance;
|
|
|
|
if( ! isset($instance))
|
|
|
|
$instance = new self();
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
2006-07-10 22:10:54 +04:00
|
|
|
/**
|
|
|
|
* install
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function install() {
|
|
|
|
$parent = new ReflectionClass('Doctrine_Record');
|
|
|
|
$old = $this->getAttribute(Doctrine::ATTR_CREATE_TABLES);
|
|
|
|
|
|
|
|
$this->attributes[Doctrine::ATTR_CREATE_TABLES] = true;
|
|
|
|
foreach(get_declared_classes() as $name) {
|
|
|
|
$class = new ReflectionClass($name);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-07-10 22:10:54 +04:00
|
|
|
if($class->isSubclassOf($parent))
|
|
|
|
$obj = new $class();
|
|
|
|
}
|
|
|
|
$this->attributes[Doctrine::ATTR_CREATE_TABLES] = $old;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-08-18 02:53:52 +04:00
|
|
|
* openConnection
|
2006-08-22 03:19:15 +04:00
|
|
|
* opens a new connection and saves it to Doctrine_Manager->connections
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
|
|
|
* @param PDO $pdo PDO database driver
|
2006-08-18 02:53:52 +04:00
|
|
|
* @param string $name name of the connection, if empty numeric key is used
|
2006-08-22 03:19:15 +04:00
|
|
|
* @return Doctrine_Connection
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
public function openConnection(PDO $pdo, $name = null) {
|
2006-05-30 12:42:10 +04:00
|
|
|
// initialize the default attributes
|
|
|
|
$this->setDefaultAttributes();
|
|
|
|
|
|
|
|
if($name !== null) {
|
|
|
|
$name = (string) $name;
|
2006-08-18 02:53:52 +04:00
|
|
|
if(isset($this->connections[$name]))
|
2006-08-22 03:19:15 +04:00
|
|
|
throw new Doctrine_Exception("Connection with $name already exists!");
|
2006-05-30 12:42:10 +04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$name = $this->index;
|
|
|
|
$this->index++;
|
|
|
|
}
|
|
|
|
switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)):
|
|
|
|
case "mysql":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Mysql($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "sqlite":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Sqlite($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "pgsql":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Pgsql($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "oci":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Oracle($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "mssql":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Mssql($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "firebird":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Firebird($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
case "informix":
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->connections[$name] = new Doctrine_Connection_Informix($this,$pdo);
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
|
|
|
endswitch;
|
|
|
|
|
|
|
|
|
|
|
|
$this->currIndex = $name;
|
2006-08-18 02:53:52 +04:00
|
|
|
return $this->connections[$name];
|
|
|
|
}
|
|
|
|
public function openSession(PDO $pdo, $name = null) {
|
|
|
|
return $this->openConnection($pdo, $name);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
2006-08-22 03:19:15 +04:00
|
|
|
* getConnection
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param integer $index
|
2006-08-22 03:19:15 +04:00
|
|
|
* @return object Doctrine_Connection
|
2006-05-30 12:42:10 +04:00
|
|
|
* @throws InvalidKeyException
|
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
public function getConnection($index) {
|
|
|
|
if( ! isset($this->connections[$index]))
|
2006-05-30 12:42:10 +04:00
|
|
|
throw new InvalidKeyException();
|
|
|
|
|
|
|
|
$this->currIndex = $index;
|
2006-08-18 02:53:52 +04:00
|
|
|
return $this->connections[$index];
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-08-18 02:53:52 +04:00
|
|
|
public function getSession($index) { return $this->getConnection($index); }
|
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-08-22 03:19:15 +04:00
|
|
|
* closes the connection
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
2006-08-22 03:19:15 +04:00
|
|
|
* @param Doctrine_Connection $connection
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2006-08-22 03:19:15 +04:00
|
|
|
public function closeConnection(Doctrine_Connection $connection) {
|
|
|
|
$connection->close();
|
|
|
|
unset($connection);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-08-22 03:19:15 +04:00
|
|
|
public function closeSession(Doctrine_Connection $connection) { $this->closeConnection($connection); }
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-08-18 02:53:52 +04:00
|
|
|
* getConnections
|
|
|
|
* returns all opened connections
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
public function getConnections() {
|
|
|
|
return $this->connections;
|
|
|
|
}
|
|
|
|
public function getSessions() {
|
|
|
|
return $this->connections;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
2006-08-18 02:53:52 +04:00
|
|
|
* setCurrentConnection
|
|
|
|
* sets the current connection to $key
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
2006-08-18 02:53:52 +04:00
|
|
|
* @param mixed $key the connection key
|
2006-05-30 12:42:10 +04:00
|
|
|
* @throws InvalidKeyException
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
public function setCurrentConnection($key) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$key = (string) $key;
|
2006-08-18 02:53:52 +04:00
|
|
|
if( ! isset($this->connections[$key]))
|
2006-05-30 12:42:10 +04:00
|
|
|
throw new InvalidKeyException();
|
|
|
|
|
|
|
|
$this->currIndex = $key;
|
|
|
|
}
|
2006-08-18 02:53:52 +04:00
|
|
|
public function setCurrentSession($key) {
|
|
|
|
$this->setCurrentConnection($key);
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* count
|
2006-08-22 03:19:15 +04:00
|
|
|
* returns the number of opened connections
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function count() {
|
2006-08-18 02:53:52 +04:00
|
|
|
return count($this->connections);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getIterator
|
2006-08-22 03:19:15 +04:00
|
|
|
* returns an ArrayIterator that iterates through all connections
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
2006-08-18 02:53:52 +04:00
|
|
|
return new ArrayIterator($this->connections);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
2006-08-18 02:53:52 +04:00
|
|
|
* getCurrentConnection
|
|
|
|
* returns the current connection
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
2006-08-22 03:19:15 +04:00
|
|
|
* @throws Doctrine_Connection_Exception if there are no open connections
|
|
|
|
* @return Doctrine_Connection
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-08-18 02:53:52 +04:00
|
|
|
public function getCurrentConnection() {
|
2006-05-30 12:42:10 +04:00
|
|
|
$i = $this->currIndex;
|
2006-08-18 02:53:52 +04:00
|
|
|
if( ! isset($this->connections[$i]))
|
2006-08-22 03:19:15 +04:00
|
|
|
throw new Doctrine_Connection_Exception();
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-08-18 02:53:52 +04:00
|
|
|
return $this->connections[$i];
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-08-18 02:53:52 +04:00
|
|
|
public function getCurrentSession() { return $this->getCurrentConnection(); }
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* __toString
|
|
|
|
* returns a string representation of this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
$r[] = "<pre>";
|
|
|
|
$r[] = "Doctrine_Manager";
|
2006-08-22 03:19:15 +04:00
|
|
|
$r[] = "Connections : ".count($this->connections);
|
2006-05-30 12:42:10 +04:00
|
|
|
$r[] = "</pre>";
|
|
|
|
return implode("\n",$r);
|
|
|
|
}
|
|
|
|
}
|
2006-09-04 02:46:30 +04:00
|
|
|
|