1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Moved connection driver functionality to expression drivers

This commit is contained in:
zYne 2006-10-30 23:27:26 +00:00
parent 7ef869ee40
commit 53e9b06f6c
14 changed files with 210 additions and 175 deletions

View File

@ -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) {

View File

@ -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
*

View File

@ -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';
}

View File

@ -27,6 +27,10 @@ Doctrine::autoload('Doctrine_Connection');
* @license LGPL
*/
class Doctrine_Connection_Mssql extends Doctrine_Connection {
/**
* @var string $driverName the name of this connection driver
*/
protected $driverName = 'Mssql';
/**
* returns the next value in the given sequence
* @param string $sequence

View File

@ -27,7 +27,10 @@ Doctrine::autoload('Doctrine_Connection_Common');
* @license LGPL
*/
class Doctrine_Connection_Mysql extends Doctrine_Connection_Common {
/**
* @var string $driverName the name of this connection driver
*/
protected $driverName = 'Mysql';
/**
* the constructor
* @param PDO $pdo -- database handle
@ -59,15 +62,7 @@ class Doctrine_Connection_Mysql extends Doctrine_Connection_Common {
parent::__construct($manager,$pdo);
}
/**
* returns the regular expression operator
* (implemented by the connection drivers)
*
* @return string
*/
public function getRegexpOperator() {
return 'RLIKE';
}
/**
* getTransactionIsolation
*

View File

@ -27,6 +27,10 @@ Doctrine::autoload('Doctrine_Connection');
* @license LGPL
*/
class Doctrine_Connection_Oracle extends Doctrine_Connection {
/**
* @var string $driverName the name of this connection driver
*/
protected $driverName = 'Oracle';
/**
* Adds an driver-specific LIMIT clause to the query
*
@ -78,44 +82,8 @@ class Doctrine_Connection_Oracle extends Doctrine_Connection {
$data = $stmt->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';
}
}

View File

@ -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()';
}
}

View File

@ -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)';
}
}

View File

@ -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) { }

View File

@ -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.
*
* @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.
* 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, $from, $len = null) {
public function substring($value, $from, $len = null) {
$value = $this->getIdentifier($value);
if ($len === null)
return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';

View File

@ -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';
}
}

View File

@ -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';
}
}

View File

@ -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()';
}
}

View File

@ -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 . '))';
}
}