From d6d7824135ff159835a82e59949f0c6f1bf74aa7 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 16 Nov 2006 12:46:23 +0000 Subject: [PATCH] Refactored connection modules, added new methods to Doctrine_Connection --- lib/Doctrine/Connection.php | 107 ++++++++++++++++++++++--- lib/Doctrine/Connection/UnitOfWork.php | 33 +++----- lib/Doctrine/Export.php | 7 +- lib/Doctrine/Expression.php | 30 +++---- lib/Doctrine/Transaction.php | 15 +--- 5 files changed, 124 insertions(+), 68 deletions(-) diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index 5aed66065..13c4cbe9c 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -63,7 +63,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ private $dataDict; - + protected $options = array(); + + private $modules = array('Transaction' => false, + 'Expression' => false, + 'DataDict' => false, + 'Export' => false, + 'UnitOfWork' => false, + ); + /** + * @var array $availibleDrivers an array containing all availible drivers + */ private static $availibleDrivers = array( 'Mysql', 'Pgsql', @@ -98,21 +108,97 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } /** * getName + * returns the name of this driver * - * @return string returns the name of this driver + * @return string the name of this driver */ public function getName() { return $this->driverName; } - /** - * quoteIdentifier + * __get + * lazy loads given module and returns it * - * @param string $identifier identifier to be quoted - * @return string modified identifier + * @param string $name the name of the module to get + * @throws Doctrine_Connection_Exception if trying to get an unknown module + * @return Doctrine_Connection_Module connection module */ - public function quoteIdentifier($identifier) { - return $identifier; + public function __get($name) { + if( ! isset($this->modules[$name])) + throw new Doctrine_Connection_Exception('Unknown module ' . $name); + + if($this->modules[$name] === false) { + switch($name) { + case 'UnitOfWork': + $this->modules[$name] = new Doctrine_Connection_UnitOfWork($this); + break; + default: + $class = 'Doctrine_' . $name . '_' . $this->getName(); + $this->modules[$name] = new $class($this); + } + } + + return $this->modules[$name]; + } + /** + * Quotes pattern (% and _) characters in a string) + * + * EXPERIMENTAL + * + * WARNING: this function is experimental and may change signature at + * any time until labelled as non-experimental + * + * @param string the input string to quote + * + * @return string quoted string + */ + public function escapePattern($text) { + if ($this->string_quoting['escape_pattern']) { + $text = str_replace($this->string_quoting['escape_pattern'], $this->string_quoting['escape_pattern'] . $this->string_quoting['escape_pattern'], $text); + foreach ($this->wildcards as $wildcard) { + $text = str_replace($wildcard, $this->string_quoting['escape_pattern'] . $wildcard, $text); + } + } + return $text; + } + /** + * Quote a string so it can be safely used as a table or column name + * + * Delimiting style depends on which database driver is being used. + * + * NOTE: just because you CAN use delimited identifiers doesn't mean + * you SHOULD use them. In general, they end up causing way more + * problems than they solve. + * + * Portability is broken by using the following characters inside + * delimited identifiers: + * + backtick (`) -- due to MySQL + * + double quote (") -- due to Oracle + * + brackets ([ or ]) -- due to Access + * + * Delimited identifiers are known to generally work correctly under + * the following drivers: + * + mssql + * + mysql + * + mysqli + * + oci8 + * + pgsql + * + sqlite + * + * InterBase doesn't seem to be able to use delimited identifiers + * via PHP 4. They work fine under PHP 5. + * + * @param string $str identifier name to be quoted + * @param bool $checkOption check the 'quote_identifier' option + * + * @return string quoted identifier string + */ + public function quoteIdentifier($str, $checkOption = true) { + if ($checkOption && ! $this->options['quote_identifier']) { + return $str; + } + $str = str_replace($this->identifier_quoting['end'], $this->identifier_quoting['escape'] . $this->identifier_quoting['end'], $str); + return $this->identifier_quoting['start'] . $str . $this->identifier_quoting['end']; } /** * getUnitOfWork @@ -372,7 +458,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchColumn($statement, array $params = array()) { $result = $this->query($statement, $params)->fetchAll(PDO::FETCH_COLUMN); - + if($this->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); @@ -610,8 +696,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->getAttribute(Doctrine::ATTR_LISTENER)->onPreClose($this); $this->clear(); - $this->state = Doctrine_Connection::STATE_CLOSED; - + $this->getAttribute(Doctrine::ATTR_LISTENER)->onClose($this); } /** diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index 2de511afd..0e78c9b22 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -18,32 +18,19 @@ * and is licensed under the LGPL. For more information, see * . */ +Doctrine::autoload('Doctrine_Connection_Module'); /** * Doctrine_Connection_UnitOfWork * - * @package Doctrine - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @category Object Relational Mapping - * @link www.phpdoctrine.com - * @since 1.0 - * @version $Revision$ - * @author Konsta Vesterinen - */ -class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { - /** - * @var Doctrine_Connection $conn the connection object - */ - private $connection; - - /** - * the constructor - * - * @param Doctrine_Connection $conn - */ - public function __construct(Doctrine_Connection $conn) { - $this->conn = $conn; - } - + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + * @author Konsta Vesterinen + */ +class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implements IteratorAggregate, Countable { /** * buildFlushTree * builds a flush tree that is used in transactions diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 9ad4551d7..251742d46 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -33,6 +33,7 @@ Doctrine::autoload('Doctrine_Connection_Module'); class Doctrine_Export extends Doctrine_Connection_Module { /** * drop an existing database + * (this method is implemented by the drivers) * * @param string $name name of the database that should be dropped * @return void @@ -77,6 +78,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { } /** * drop existing sequence + * (this method is implemented by the drivers) * * @param string $seq_name name of the sequence to be dropped * @return void @@ -86,6 +88,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { } /** * create a new database + * (this method is implemented by the drivers) * * @param string $name name of the database that should be created * @return void @@ -121,7 +124,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { * * @return void */ - public function createTable($name, $fields, $options = array()) { + public function createTable($name, array $fields, $options = array()) { if ( ! $name) throw new Doctrine_Export_Exception('no valid table name specified'); @@ -140,6 +143,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { } /** * create sequence + * (this method is implemented by the drivers) * * @param string $seq_name name of the sequence to be created * @param string $start start value of the sequence; default is 1 @@ -234,6 +238,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { /** * alter an existing table + * (this method is implemented by the drivers) * * @param string $name name of the table that is intended to be changed. * @param array $changes associative array that contains the details of each type diff --git a/lib/Doctrine/Expression.php b/lib/Doctrine/Expression.php index 44ac7243c..43b0bb5c9 100644 --- a/lib/Doctrine/Expression.php +++ b/lib/Doctrine/Expression.php @@ -18,29 +18,19 @@ * and is licensed under the LGPL. For more information, see * . */ +Doctrine::autoload('Doctrine_Connection_Module'); /** * Doctrine_Expression * - * @package Doctrine - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @category Object Relational Mapping - * @link www.phpdoctrine.com - * @since 1.0 - * @version $Revision$ - * @author Konsta Vesterinen - */ -class Doctrine_Expression { - - /** - * @var Doctrine_Connection $connection - */ - protected $conn; - /** - * @param Doctrine_Connection $conn - */ - public function __construct(Doctrine_Connection $conn) { - $this->conn = $conn; - } + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + * @author Konsta Vesterinen + */ +class Doctrine_Expression extends Doctrine_Connection_Module { /** * regexp * returns the regular expression operator diff --git a/lib/Doctrine/Transaction.php b/lib/Doctrine/Transaction.php index fa52cfde9..dbf08a777 100644 --- a/lib/Doctrine/Transaction.php +++ b/lib/Doctrine/Transaction.php @@ -18,6 +18,7 @@ * and is licensed under the LGPL. For more information, see * . */ +Doctrine::autoload('Doctrine_Connection_Module'); /** * * @author Konsta Vesterinen @@ -28,7 +29,7 @@ * @since 1.0 * @version $Revision$ */ -class Doctrine_Transaction { +class Doctrine_Transaction extends Doctrine_Connection_Module { /** * Doctrine_Transaction is in sleep state when it has no active transactions */ @@ -41,22 +42,10 @@ class Doctrine_Transaction { * Doctrine_Transaction is in busy state when it has multiple active transactions */ const STATE_BUSY = 2; - /** - * @var Doctrine_Connection $conn the connection object - */ - protected $conn; /** * @var integer $transaction_level the nesting level of transactions, used by transaction methods */ protected $transactionLevel = 0; - /** - * the constructor - * - * @param Doctrine_Connection $conn Doctrine_Connection object - */ - public function __construct(Doctrine_Connection $conn) { - $this->conn = $conn; - } /** * getState * returns the state of this connection