diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index cf652c704..737e9fdbd 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -44,26 +44,31 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * keys representing Doctrine_Table component names and values as Doctrine_Table objects */ protected $tables = array(); + /** + * @var string $driverName the name of this connection driver + */ + protected $driverName; + /** + * @var array $supported an array containing all features this driver supports, + * keys representing feature names and values as + * one of the following (true, false, 'emulated') + */ + protected $supported = array(); /** * @var Doctrine_DataDict $dataDict */ private $dataDict; - private static $availibleDrivers = array( - "Mysql", - "Pgsql", - "Oracle", - "Informix", - "Mssql", - "Sqlite", - "Firebird" + private static $availibleDrivers = array( + 'Mysql', + 'Pgsql', + 'Oracle', + 'Informix', + 'Mssql', + 'Sqlite', + 'Firebird' ); - private static $driverMap = array('oracle' => 'oci8', - 'postgres' => 'pgsql', - 'oci' => 'oci8', - 'sqlite2' => 'sqlite', - 'sqlite3' => 'sqlite'); /** * the constructor @@ -84,6 +89,15 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->getAttribute(Doctrine::ATTR_LISTENER)->onOpen($this); } + /** + * getName + * + * @return string returns the name of this driver + */ + public function getName() { + return $this->driverName; + } + /** * quoteIdentifier * @@ -145,28 +159,9 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun if(isset($this->dataDict)) return $this->dataDict; - $driver = $this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME); - switch($driver) { - case "mysql": - $this->dataDict = new Doctrine_DataDict_Mysql($this->dbh); - break; - case "sqlite": - case "sqlite2": - $this->dataDict = new Doctrine_DataDict_Sqlite($this->dbh); - break; - case "pgsql": - $this->dataDict = new Doctrine_DataDict_Pgsql($this->dbh); - break; - case "oci": - case "oci8": - $this->dataDict = new Doctrine_DataDict_Oracle($this->dbh); - break; - case "mssql": - $this->dataDict = new Doctrine_DataDict_Mssql($this->dbh); - break; - default: - throw new Doctrine_Connection_Exception("No datadict driver availible for ".$driver); - } + $class = 'Doctrine_DataDict_' . $this->getName(); + $this->dataDict = new $class($this->dbh); + return $this->dataDict; } /** @@ -185,16 +180,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun public function setTransactionIsolation($isolation) { throw new Doctrine_Connection_Exception('Transaction isolation levels not supported by this database driver.'); } - /** - * getRegexpOperator - * returns the regular expression operator - * (implemented by the connection drivers) - * - * @return string - */ - public function getRegexpOperator() { - throw new Doctrine_Connection_Exception('Regular expression operator is not supported by this database driver.'); - } + /** * getTransactionIsolation * @@ -246,9 +232,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } } /** + * hasTable * whether or not this connection has table $name initialized * - * @param $mixed $name + * @param mixed $name * @return boolean */ public function hasTable($name) { diff --git a/lib/Doctrine/Connection/Firebird.php b/lib/Doctrine/Connection/Firebird.php index 5e5c0c939..aa9b311e0 100644 --- a/lib/Doctrine/Connection/Firebird.php +++ b/lib/Doctrine/Connection/Firebird.php @@ -27,6 +27,11 @@ Doctrine::autoload('Doctrine_Connection'); * @license LGPL */ class Doctrine_Connection_Firebird extends Doctrine_Connection { + /** + * @var string $driverName the name of this connection driver + */ + protected $driverName = 'Firebird'; + /** * Adds an driver-specific LIMIT clause to the query * diff --git a/lib/Doctrine/Connection/Informix.php b/lib/Doctrine/Connection/Informix.php index a5b1519f4..0ec36fd6f 100644 --- a/lib/Doctrine/Connection/Informix.php +++ b/lib/Doctrine/Connection/Informix.php @@ -26,4 +26,9 @@ Doctrine::autoload('Doctrine_Connection'); * @url www.phpdoctrine.com * @license LGPL */ -class Doctrine_Connection_Informix extends Doctrine_Connection { } +class Doctrine_Connection_Informix extends Doctrine_Connection { + /** + * @var string $driverName the name of this connection driver + */ + protected $driverName = 'Informix'; +} diff --git a/lib/Doctrine/Connection/Mssql.php b/lib/Doctrine/Connection/Mssql.php index b414b73e6..f36d4b45e 100644 --- a/lib/Doctrine/Connection/Mssql.php +++ b/lib/Doctrine/Connection/Mssql.php @@ -1,5 +1,5 @@ fetch(PDO::FETCH_NUM); return $data[0]; } - /** - * Return string to call a variable with the current timestamp inside an SQL statement - * There are three special variables for current date and time: - * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type) - * - CURRENT_DATE (date, DATE type) - * - CURRENT_TIME (time, TIME type) - * - * @return string to call a variable with the current timestamp - * @access public - */ - function now($type = 'timestamp') - { - switch ($type) { - case 'date': - case 'time': - case 'timestamp': - default: - return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')'; - } - } - /** - * substring - * - * @return string SQL substring function with given parameters - */ - function substring($value, $position = 1, $length = null) { - if($length !== null) - return "SUBSTR($value, $position, $length)"; - return "SUBSTR($value, $position)"; - } - /** - * random - * - * @return string an oracle SQL string that generates a float between 0 and 1 - */ - function random() { - return 'dbms_random.value'; - } + + } diff --git a/lib/Doctrine/Connection/Pgsql.php b/lib/Doctrine/Connection/Pgsql.php index 37fbfb3c2..d7332d47e 100644 --- a/lib/Doctrine/Connection/Pgsql.php +++ b/lib/Doctrine/Connection/Pgsql.php @@ -27,6 +27,10 @@ Doctrine::autoload("Doctrine_Connection_Common"); * @license LGPL */ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common { + /** + * @var string $driverName the name of this connection driver + */ + protected $driverName = 'Pgsql'; /** * returns the next value in the given sequence * @param string $sequence @@ -60,22 +64,6 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common { $query = 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ' . $isolation; return $this->dbh->query($query); } - /** - * getRegexpOperator - * - * @return string the regular expression operator - */ - public function getRegexpOperator() { - return 'SIMILAR TO'; - } - /** - * return string to call a function to get random value inside an SQL statement - * - * @return return string to generate float between 0 and 1 - * @access public - */ - public function random() { - return 'RANDOM()'; - } + } diff --git a/lib/Doctrine/Connection/Sqlite.php b/lib/Doctrine/Connection/Sqlite.php index b485943c2..98f0c32fd 100644 --- a/lib/Doctrine/Connection/Sqlite.php +++ b/lib/Doctrine/Connection/Sqlite.php @@ -29,22 +29,9 @@ Doctrine::autoload("Doctrine_Connection_Common"); class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common { /** - * Return string to call a variable with the current timestamp inside an SQL statement - * There are three special variables for current date and time. - * - * @return string sqlite function as string + * @var string $driverName the name of this connection driver */ - public function now($type = 'timestamp') { - switch ($type) { - case 'time': - return 'time(\'now\')'; - case 'date': - return 'date(\'now\')'; - case 'timestamp': - default: - return 'datetime(\'now\')'; - } - } + protected $driverName = 'Sqlite'; /** * Set the transacton isolation level. * @@ -72,27 +59,6 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common { $query = "PRAGMA read_uncommitted=$isolation"; return $this->_doQuery($query, true); } - /** - * return string to call a function to get a substring inside an SQL statement - * - * @return string to call a function to get a substring - * @access public - */ - public function substring($value, $position = 1, $length = null) { - if($length !== null) - return "substr($value,$position,$length)"; - return "substr($value,$position,length($value))"; - } - - /** - * return string to call a function to get random value inside an SQL statement - * - * @return string to generate float between 0 and 1 - */ - public function random() - { - return '((RANDOM() + 2147483648) / 4294967296)'; - } } diff --git a/lib/Doctrine/DB/Profiler.php b/lib/Doctrine/DB/Profiler.php index cbb63d0db..d09cc73d9 100644 --- a/lib/Doctrine/DB/Profiler.php +++ b/lib/Doctrine/DB/Profiler.php @@ -26,7 +26,11 @@ * @package Doctrine */ class Doctrine_DB_Profiler extends Doctrine_DB_EventListener { - public function onPreQuery(Doctrine_DB $dbh, array $args) { } + private $queries; + + public function onPreQuery(Doctrine_DB $dbh, array $args) { + $this->queries[] = $args[0]; + } public function onQuery(Doctrine_DB $dbh, array $args) { } public function onPrePrepare(Doctrine_DB $dbh, array $args) { } diff --git a/lib/Doctrine/Expression.php b/lib/Doctrine/Expression.php index 0201309f8..8a03db6c4 100644 --- a/lib/Doctrine/Expression.php +++ b/lib/Doctrine/Expression.php @@ -38,6 +38,15 @@ class Doctrine_Expression { public function __construct(Doctrine_Connection $conn) { $this->conn = $conn; } + /** + * regexp + * returns the regular expression operator + * + * @return string + */ + public function regexp() { + throw new Doctrine_Connection_Exception('Regular expression operator is not supported by this database driver.'); + } /** * Returns the average value of a column * @@ -160,7 +169,8 @@ class Doctrine_Expression { } /** * upper - * Returns the string $str with all characters changed to uppercase according to the current character set mapping. + * Returns the string $str with all characters changed to + * uppercase according to the current character set mapping. * * @param string $str literal string or column name * @return string @@ -170,7 +180,8 @@ class Doctrine_Expression { } /** * lower - * Returns the string $str with all characters changed to lowercase according to the current character set mapping. + * Returns the string $str with all characters changed to + * lowercase according to the current character set mapping. * * @param string $str literal string or column name * @return string @@ -198,16 +209,18 @@ class Doctrine_Expression { return 'NOW()'; } /** - * Returns part of a string. + * return string to call a function to get a substring inside an SQL statement * - * Note: Not SQL92, but common functionality. + * Note: Not SQL92, but common functionality. + * + * SQLite only supports the 2 parameter variant of this function * - * @param string $value the target $value the string or the string column. - * @param int $from extract from this characeter. - * @param int $len extract this amount of characters. - * @return string sql that extracts part of a string. + * @param string $value an sql string literal or column name/alias + * @param integer $position where to start the substring portion + * @param integer $length the substring portion length + * @return string SQL substring function with given parameters */ - public function subString($value, $from, $len = null) { + public function substring($value, $from, $len = null) { $value = $this->getIdentifier($value); if ($len === null) return 'SUBSTRING(' . $value . ' FROM ' . $from . ')'; diff --git a/lib/Doctrine/Expression/Mysql.php b/lib/Doctrine/Expression/Mysql.php index 13d7de005..419c66b2e 100644 --- a/lib/Doctrine/Expression/Mysql.php +++ b/lib/Doctrine/Expression/Mysql.php @@ -26,4 +26,13 @@ Doctrine::autoload('Doctrine_Expression'); * @url www.phpdoctrine.com * @license LGPL */ -class Doctrine_Expression_Mysql extends Doctrine_Expression { } +class Doctrine_Expression_Mysql extends Doctrine_Expression { + /** + * returns the regular expression operator + * + * @return string + */ + public function regexp() { + return 'RLIKE'; + } +} diff --git a/lib/Doctrine/Expression/Oracle.php b/lib/Doctrine/Expression/Oracle.php index 9de43f2b2..30401fa4d 100644 --- a/lib/Doctrine/Expression/Oracle.php +++ b/lib/Doctrine/Expression/Oracle.php @@ -42,4 +42,46 @@ class Doctrine_Expression_Oracle extends Doctrine_Expression { $cols = $this->getIdentifiers( $args ); return join( ' || ' , $cols ); } + /** + * return string to call a function to get a substring inside an SQL statement + * + * Note: Not SQL92, but common functionality. + * + * @param string $value an sql string literal or column name/alias + * @param integer $position where to start the substring portion + * @param integer $length the substring portion length + * @return string SQL substring function with given parameters + */ + public function substring($value, $position = 1, $length = null) { + if($length !== null) + return "SUBSTR($value, $position, $length)"; + + return "SUBSTR($value, $position)"; + } + /** + * Return string to call a variable with the current timestamp inside an SQL statement + * There are three special variables for current date and time: + * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type) + * - CURRENT_DATE (date, DATE type) + * - CURRENT_TIME (time, TIME type) + * + * @return string to call a variable with the current timestamp + */ + public function now($type = 'timestamp') { + switch ($type) { + case 'date': + case 'time': + case 'timestamp': + default: + return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')'; + } + } + /** + * random + * + * @return string an oracle SQL string that generates a float between 0 and 1 + */ + function random() { + return 'dbms_random.value'; + } } diff --git a/lib/Doctrine/Expression/Pgsql.php b/lib/Doctrine/Expression/Pgsql.php index 8e13e6326..3b2f263e0 100644 --- a/lib/Doctrine/Expression/Pgsql.php +++ b/lib/Doctrine/Expression/Pgsql.php @@ -1,4 +1,4 @@ -/* +/* * $Id$ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -88,4 +88,21 @@ class Doctrine_Expression_Pgsql extends Doctrine_Expression { return join(' || ' , $cols); } + /** + * regexp + * + * @return string the regular expression operator + */ + public function regexp() { + return 'SIMILAR TO'; + } + /** + * return string to call a function to get random value inside an SQL statement + * + * @return return string to generate float between 0 and 1 + * @access public + */ + public function random() { + return 'RANDOM()'; + } } diff --git a/lib/Doctrine/Expression/Sqlite.php b/lib/Doctrine/Expression/Sqlite.php index 76dbe987b..db94d11a4 100644 --- a/lib/Doctrine/Expression/Sqlite.php +++ b/lib/Doctrine/Expression/Sqlite.php @@ -28,22 +28,54 @@ Doctrine::autoload('Doctrine_Expression'); */ class Doctrine_Expression_Sqlite extends Doctrine_Expression { /** - * Returns part of a string. + * returns the regular expression operator * - * Note: Not SQL92, but common functionality. SQLite only supports the 3 - * parameter variant of this function, so we are using 2^30-1 as - * artificial length in that case. - * - * @param string $value the target $value the string or the string column. - * @param int $from extract from this characeter. - * @param int $len extract this amount of characters. - * @return string sql that extracts part of a string. + * @return string */ - public function subString($value, $from, $len = null) { - $value = $this->getIdentifier( $value ); - if ( $len === null ) - $len = 1073741823; + public function regexp() { + return 'RLIKE'; + } + /** + * Return string to call a variable with the current timestamp inside an SQL statement + * There are three special variables for current date and time. + * + * @return string sqlite function as string + */ + public function now($type = 'timestamp') { + switch ($type) { + case 'time': + return 'time(\'now\')'; + case 'date': + return 'date(\'now\')'; + case 'timestamp': + default: + return 'datetime(\'now\')'; + } + } + /** + * return string to call a function to get random value inside an SQL statement + * + * @return string to generate float between 0 and 1 + */ + public function random() { + return '((RANDOM() + 2147483648) / 4294967296)'; + } + /** + * return string to call a function to get a substring inside an SQL statement + * + * Note: Not SQL92, but common functionality. + * + * SQLite only supports the 2 parameter variant of this function + * + * @param string $value an sql string literal or column name/alias + * @param integer $position where to start the substring portion + * @param integer $length the substring portion length + * @return string SQL substring function with given parameters + */ + public function substring($value, $position = 1, $length = null) { + if($length !== null) + return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')'; - return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')'; + return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))'; } }