From 2d24e9add0cc7ee86a633204642be9916b71794f Mon Sep 17 00:00:00 2001 From: lsmith Date: Tue, 29 Jan 2008 09:59:37 +0000 Subject: [PATCH] - lazy load the _tableFactory and record listener in order to reduce the dependencies for a to be created DBAL package --- lib/Doctrine/Configurable.php | 28 ++++---- lib/Doctrine/Connection.php | 127 +++++++++++++++++----------------- lib/Doctrine/Manager.php | 54 +++++++-------- 3 files changed, 106 insertions(+), 103 deletions(-) diff --git a/lib/Doctrine/Configurable.php b/lib/Doctrine/Configurable.php index 947b398bc..9ae26f12d 100644 --- a/lib/Doctrine/Configurable.php +++ b/lib/Doctrine/Configurable.php @@ -50,7 +50,7 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable * implementation classes */ protected $_impl = array(); - + /** * @var array $_params an array of user defined parameters */ @@ -175,36 +175,36 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable if ($namespace == null) { $namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE); } - + if ( ! isset($this->_params[$namespace])) { return null; } return $this->_params[$namespace]; } - + public function getParamNamespaces() { return array_keys($this->_params); } - public function setParam($name, $value, $namespace = null) + public function setParam($name, $value, $namespace = null) { if ($namespace == null) { $namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE); } - + $this->_params[$namespace][$name] = $value; - + return $this; } - - public function getParam($name, $value, $namespace) + + public function getParam($name, $value, $namespace) { if ($namespace == null) { $namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE); } - + if ( ! isset($this->_params[$name])) { if (isset($this->parent)) { return $this->parent->getParam($name); @@ -213,7 +213,7 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable } return $this->_params[$name]; } - + /** * setImpl * binds given class to given template name @@ -247,8 +247,8 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable } return $this->_impl[$template]; } - - + + public function hasImpl($template) { if ( ! isset($this->_impl[$template])) { @@ -298,7 +298,7 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable if (isset($this->parent)) { return $this->parent->getRecordListener(); } - return null; + $this->attributes[Doctrine::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener(); } return $this->attributes[Doctrine::ATTR_RECORD_LISTENER]; } @@ -390,7 +390,7 @@ abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable if (isset($this->attributes[$attribute])) { return $this->attributes[$attribute]; } - + if (isset($this->parent)) { return $this->parent->getAttribute($attribute); } diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index a34f510f5..f883811ed 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -34,14 +34,14 @@ Doctrine::autoload('Doctrine_Configurable'); * 2. Lazy-connecting * Creating an instance of Doctrine_Connection does not connect * to database. Connecting to database is only invoked when actually needed - * (for example when query() is being called) + * (for example when query() is being called) * * 3. Convenience methods * Doctrine_Connection provides many convenience methods such as fetchAll(), fetchOne() etc. * * 4. Modular structure * Higher level functionality such as schema importing, exporting, sequence handling etc. - * is divided into modules. For a full list of connection modules see + * is divided into modules. For a full list of connection modules see * Doctrine_Connection::$_modules * * @package Doctrine @@ -61,7 +61,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun protected $dbh; /** - * + * */ protected $_tableFactory; @@ -70,12 +70,12 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * keys representing component names and values as Doctrine_Table objects */ protected $tables = array(); - + /** * @var array An array of mapper objects currently maintained by this connection. */ protected $_mappers = array(); - + /** * $_name * @@ -166,7 +166,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @var array $serverInfo */ protected $serverInfo = array(); - + protected $options = array(); /** @@ -181,7 +181,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun 'Sqlite', 'Firebird' ); - + /** * The query count. Represents the number of executed database queries by the connection. * @@ -209,8 +209,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->options['dsn'] = $adapter['dsn']; $this->options['username'] = $adapter['user']; $this->options['password'] = $adapter['pass']; - - $this->options['other'] = array(); + + $this->options['other'] = array(); if (isset($adapter['other'])) { $this->options['other'] = array(Doctrine::ATTR_PERSISTENT => $adapter['persistent']); } @@ -218,8 +218,6 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } $this->setParent($manager); - - $this->_tableFactory = new Doctrine_Table_Factory($this); $this->setAttribute(Doctrine::ATTR_CASE, Doctrine::CASE_NATURAL); $this->setAttribute(Doctrine::ATTR_ERRMODE, Doctrine::ERRMODE_EXCEPTION); @@ -229,10 +227,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun /** * getOption - * + * * Retrieves option * - * @param string $option + * @param string $option * @return void */ public function getOption($option) @@ -286,7 +284,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * setAttribute * sets an attribute * - * @todo why check for >= 100? has this any special meaning when creating + * @todo why check for >= 100? has this any special meaning when creating * attributes? * * @param integer $attribute @@ -317,12 +315,12 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->_name; } - + public function setName($name) { $this->_name = $name; } - + public function getDriverName() { return $this->driverName; @@ -405,12 +403,12 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $e = explode(':', $this->options['dsn']); $found = false; - + if (extension_loaded('pdo')) { if (in_array($e[0], PDO::getAvailableDrivers())) { - $this->dbh = new PDO($this->options['dsn'], $this->options['username'], + $this->dbh = new PDO($this->options['dsn'], $this->options['username'], $this->options['password'], $this->options['other']); - + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $found = true; } @@ -422,7 +420,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun if (class_exists($class)) { $this->dbh = new $class($this->options['dsn'], $this->options['username'], $this->options['password']); } else { - throw new Doctrine_Connection_Exception("Couldn't locate driver named " . $e[0]); + throw new Doctrine_Connection_Exception("Couldn't locate driver named " . $e[0]); } } @@ -440,8 +438,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->getListener()->postConnect($event); return true; } - - public function incrementQueryCount() + + public function incrementQueryCount() { $this->_count++; } @@ -618,9 +616,9 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $a[] = '?'; } } - + // build the statement - $query = 'INSERT INTO ' . $this->quoteIdentifier($tableName) + $query = 'INSERT INTO ' . $this->quoteIdentifier($tableName) . ' (' . implode(', ', $cols) . ') ' . 'VALUES ('; @@ -628,7 +626,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun // prepare and execute the statement return $this->exec($query, array_values($fields)); - } + } /** * Set the charset on the current connection @@ -677,8 +675,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun // quick fix for the identifiers that contain a dot if (strpos($str, '.')) { $e = explode('.', $str); - - return $this->formatter->quoteIdentifier($e[0], $checkOption) . '.' + + return $this->formatter->quoteIdentifier($e[0], $checkOption) . '.' . $this->formatter->quoteIdentifier($e[1], $checkOption); } return $this->formatter->quoteIdentifier($str, $checkOption); @@ -730,7 +728,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param array $params prepared statement params * @return array */ - public function fetchAll($statement, array $params = array()) + public function fetchAll($statement, array $params = array()) { return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_ASSOC); } @@ -743,7 +741,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param int $colnum 0-indexed column number to retrieve * @return mixed */ - public function fetchOne($statement, array $params = array(), $colnum = 0) + public function fetchOne($statement, array $params = array(), $colnum = 0) { return $this->execute($statement, $params)->fetchColumn($colnum); } @@ -755,7 +753,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param array $params prepared statement params * @return array */ - public function fetchRow($statement, array $params = array()) + public function fetchRow($statement, array $params = array()) { return $this->execute($statement, $params)->fetch(Doctrine::FETCH_ASSOC); } @@ -767,7 +765,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param array $params prepared statement params * @return array */ - public function fetchArray($statement, array $params = array()) + public function fetchArray($statement, array $params = array()) { return $this->execute($statement, $params)->fetch(Doctrine::FETCH_NUM); } @@ -780,7 +778,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param int $colnum 0-indexed column number to retrieve * @return array */ - public function fetchColumn($statement, array $params = array(), $colnum = 0) + public function fetchColumn($statement, array $params = array(), $colnum = 0) { return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_COLUMN, $colnum); } @@ -792,7 +790,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param array $params prepared statement params * @return array */ - public function fetchAssoc($statement, array $params = array()) + public function fetchAssoc($statement, array $params = array()) { return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_ASSOC); } @@ -804,7 +802,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @param array $params prepared statement params * @return array */ - public function fetchBoth($statement, array $params = array()) + public function fetchBoth($statement, array $params = array()) { return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_BOTH); } @@ -844,17 +842,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun try { $event = new Doctrine_Event($this, Doctrine_Event::CONN_PREPARE, $statement); - + $this->getAttribute(Doctrine::ATTR_LISTENER)->prePrepare($event); $stmt = false; - + if ( ! $event->skipOperation) { $stmt = $this->dbh->prepare($statement); } - + $this->getAttribute(Doctrine::ATTR_LISTENER)->postPrepare($event); - + return new Doctrine_Connection_Statement($this, $stmt); } catch(Doctrine_Adapter_Exception $e) { } catch(PDOException $e) { } @@ -881,7 +879,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @return Doctrine_Record|false Doctrine_Record object on success, * boolean false on failure */ - public function queryOne($query, array $params = array()) + public function queryOne($query, array $params = array()) { $parser = new Doctrine_Query($this); @@ -951,7 +949,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun echo $query . "

"; } }*/ - + $this->_count++; } $this->getAttribute(Doctrine::ATTR_LISTENER)->postQuery($event); @@ -1009,7 +1007,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { $event = new Doctrine_Event($this, Doctrine_Event::CONN_ERROR); $this->getListener()->preError($event); - + $name = 'Doctrine_Connection_' . $this->driverName . '_Exception'; $exc = new $name($e->getMessage(), (int) $e->getCode()); @@ -1021,7 +1019,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun if ($this->getAttribute(Doctrine::ATTR_THROW_EXCEPTIONS)) { throw $exc; } - + $this->getListener()->postError($event); } @@ -1036,7 +1034,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return isset($this->tables[$name]); } - + /** * Gets the table object that represents the database table that is used to * persist the specified domain class. @@ -1049,22 +1047,27 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun if (isset($this->tables[$className])) { return $this->tables[$className]; } + + if (!($this->_tableFactory instanceOf Doctrine_Table_Factory)) { + $this->_tableFactory = new Doctrine_Table_Factory($this); + } + $this->_tableFactory->loadTables($className, $this->tables); return $this->tables[$className]; } - + /** * Gets a mapper for the specified domain class that is used to map instances of * the class between the relational database and their object representation. - * + * * @return Doctrine_Mapper_Abstract The mapper object. */ public function getMapper($className) - { + { if (isset($this->_mappers[$className])) { return $this->_mappers[$className]; } - + $customMapperClass = $className . 'Mapper'; if (class_exists($customMapperClass, $this->getAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES)) && in_array('Doctrine_Mapper_Abstract', class_parents($customMapperClass))) { @@ -1086,7 +1089,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } $this->_mappers[$className] = $mapper; - + return $mapper; } @@ -1099,7 +1102,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->tables; } - + public function getMappers() { //var_dump($this->_mappers); @@ -1163,11 +1166,11 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->getMapper($name)->create(); } - + /** * Creates a new Doctrine_Query object that operates on this connection. - * - * @return Doctrine_Query + * + * @return Doctrine_Query */ public function createQuery() { @@ -1228,7 +1231,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->getAttribute(Doctrine::ATTR_LISTENER)->preClose($event); $this->clear(); - + unset($this->dbh); $this->isConnected = false; @@ -1244,7 +1247,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->transaction->getTransactionLevel(); } - + /** * get the current internal transaction nesting level * @@ -1278,7 +1281,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->connect(); return $this->dbh->errorInfo(); } - + /** * getCacheDriver * @@ -1289,7 +1292,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->getResultCacheDriver(); } - + /** * getResultCacheDriver * @@ -1302,7 +1305,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } return $this->getAttribute(Doctrine::ATTR_RESULT_CACHE); } - + /** * getQueryCacheDriver * @@ -1322,7 +1325,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * Returns the ID of the last inserted row, or the last value from a sequence object, * depending on the underlying driver. * - * Note: This method may not return a meaningful or consistent result across different drivers, + * Note: This method may not return a meaningful or consistent result across different drivers, * because the underlying database may not even support the notion of auto-increment fields or sequences. * * @param string $table name of the table into which a new row was inserted @@ -1350,7 +1353,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->transaction->beginTransaction($savepoint); } - + /** * Initiates a transaction. * @@ -1390,7 +1393,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * this method can be listened with onPreTransactionRollback and onTransactionRollback * eventlistener methods * - * @param string $savepoint name of a savepoint to rollback to + * @param string $savepoint name of a savepoint to rollback to * @throws Doctrine_Transaction_Exception if the rollback operation fails at database level * @return boolean false if rollback couldn't be performed, true otherwise */ @@ -1455,7 +1458,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } $info = $this->getManager()->parsePdoDsn($dsn); - + $this->export->dropDatabase($info['dbname']); return 'Successfully dropped database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; @@ -1472,4 +1475,4 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return Doctrine_Lib::getConnectionAsString($this); } -} \ No newline at end of file +} diff --git a/lib/Doctrine/Manager.php b/lib/Doctrine/Manager.php index d756d4686..2b178e65c 100644 --- a/lib/Doctrine/Manager.php +++ b/lib/Doctrine/Manager.php @@ -63,7 +63,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera * @var Doctrine_Query_Registry the query registry */ protected $_queryRegistry; - + protected static $driverMap = array('oci' => 'oracle'); /** @@ -95,7 +95,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera Doctrine::ATTR_QUERY_CACHE => null, Doctrine::ATTR_LOAD_REFERENCES => true, Doctrine::ATTR_LISTENER => new Doctrine_EventListener(), - Doctrine::ATTR_RECORD_LISTENER => new Doctrine_Record_Listener(), + Doctrine::ATTR_RECORD_LISTENER => null, Doctrine::ATTR_THROW_EXCEPTIONS => true, Doctrine::ATTR_VALIDATE => Doctrine::VALIDATE_NONE, Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS, @@ -109,7 +109,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera Doctrine::ATTR_DECIMAL_PLACES => 2, Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE => 'doctrine', Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES => true, - ); + ); foreach ($attributes as $attribute => $value) { $old = $this->getAttribute($attribute); if ($old === null) { @@ -170,16 +170,16 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera public function setQueryRegistry(Doctrine_Query_Registry $registry) { $this->_queryRegistry = $registry; - + return $this; } /** * fetch - * fetches data using the provided queryKey and + * fetches data using the provided queryKey and * the associated query in the query registry * - * if no query for given queryKey is being found a + * if no query for given queryKey is being found a * Doctrine_Query_Registry exception is being thrown * * @param string $queryKey the query key @@ -196,10 +196,10 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera /** * fetchOne - * fetches data using the provided queryKey and + * fetches data using the provided queryKey and * the associated query in the query registry * - * if no query for given queryKey is being found a + * if no query for given queryKey is being found a * Doctrine_Query_Registry exception is being thrown * * @param string $queryKey the query key @@ -269,7 +269,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera $parts['scheme'] = $e[0]; $parts['user'] = (isset($adapter[1])) ? $adapter[1] : null; $parts['pass'] = (isset($adapter[2])) ? $adapter[2] : null; - + $driverName = $e[0]; $adapter = $parts; } else { @@ -305,11 +305,11 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera 'firebird' => 'Doctrine_Connection_Firebird', 'informix' => 'Doctrine_Connection_Informix', 'mock' => 'Doctrine_Connection_Mock'); - + if ( ! isset($drivers[$driverName])) { throw new Doctrine_Manager_Exception('Unknown driver ' . $driverName); } - + $className = $drivers[$driverName]; $conn = new $className($this, $adapter); $conn->setName($name); @@ -321,17 +321,17 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } return $this->_connections[$name]; } - + /** - * parsePdoDsn - * - * @param array $dsn An array of dsn information + * parsePdoDsn + * + * @param array $dsn An array of dsn information * @return array The array parsed */ public function parsePdoDsn($dsn) { $parts = array(); - + $names = array('dsn', 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment'); foreach ($names as $name) { @@ -339,11 +339,11 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera $parts[$name] = null; } } - + $e = explode(':', $dsn); $parts['scheme'] = $e[0]; $parts['dsn'] = $dsn; - + $e = explode(';', $e[1]); foreach ($e as $string) { if ($string) { @@ -370,7 +370,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera // fix sqlite dsn so that it will parse correctly $dsn = str_replace("////", "/", $dsn); $dsn = str_replace("///c:/", "//c:/", $dsn); - + // silence any warnings $parts = @parse_url($dsn); @@ -404,7 +404,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } break; - + case 'mssql': case 'dblib': if ( ! isset($parts['path']) || $parts['path'] == '/') { @@ -416,7 +416,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera if ( ! isset($parts['host'])) { throw new Doctrine_Manager_Exception('No hostname set in data source name'); } - + if (isset(self::$driverMap[$parts['scheme']])) { $parts['scheme'] = self::$driverMap[$parts['scheme']]; } @@ -424,7 +424,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera $parts['dsn'] = $parts['scheme'] . ':host=' . $parts['host'] . (isset($parts['port']) ? ':' . $parts['port']:null) . ';dbname=' . $parts['database']; - + break; case 'mysql': @@ -445,7 +445,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera if ( ! isset($parts['host'])) { throw new Doctrine_Manager_Exception('No hostname set in data source name'); } - + if (isset(self::$driverMap[$parts['scheme']])) { $parts['scheme'] = self::$driverMap[$parts['scheme']]; } @@ -453,7 +453,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera $parts['dsn'] = $parts['scheme'] . ':host=' . $parts['host'] . (isset($parts['port']) ? ';port=' . $parts['port']:null) . ';dbname=' . $parts['database']; - + break; default: throw new Doctrine_Manager_Exception('Unknown driver '.$parts['scheme']); @@ -550,7 +550,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } return $this->getCurrentConnection(); } - + /** * hasConnectionForComponent * @@ -575,7 +575,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera { return $this->getConnectionForComponent($componentName)->getTable($componentName); } - + /** * getMapper * Returns the mapper object for the given component name. @@ -770,4 +770,4 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera $r[] = ""; return implode("\n",$r); } -} \ No newline at end of file +}