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

Refactored connection modules, added new methods to Doctrine_Connection

This commit is contained in:
zYne 2006-11-16 12:46:23 +00:00
parent 02cc9b2f6c
commit d6d7824135
5 changed files with 124 additions and 68 deletions

View File

@ -63,7 +63,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
*/ */
private $dataDict; 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( private static $availibleDrivers = array(
'Mysql', 'Mysql',
'Pgsql', 'Pgsql',
@ -98,21 +108,97 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
/** /**
* getName * 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() { public function getName() {
return $this->driverName; return $this->driverName;
} }
/** /**
* quoteIdentifier * __get
* lazy loads given module and returns it
* *
* @param string $identifier identifier to be quoted * @param string $name the name of the module to get
* @return string modified identifier * @throws Doctrine_Connection_Exception if trying to get an unknown module
* @return Doctrine_Connection_Module connection module
*/ */
public function quoteIdentifier($identifier) { public function __get($name) {
return $identifier; 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 (<kbd>`</kbd>) -- due to MySQL
* + double quote (<kbd>"</kbd>) -- due to Oracle
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- 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 * getUnitOfWork
@ -372,7 +458,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
*/ */
public function fetchColumn($statement, array $params = array()) { public function fetchColumn($statement, array $params = array()) {
$result = $this->query($statement, $params)->fetchAll(PDO::FETCH_COLUMN); $result = $this->query($statement, $params)->fetchAll(PDO::FETCH_COLUMN);
if($this->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) if($this->options['portability'] & Doctrine::PORTABILITY_FIX_CASE)
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); $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->getAttribute(Doctrine::ATTR_LISTENER)->onPreClose($this);
$this->clear(); $this->clear();
$this->state = Doctrine_Connection::STATE_CLOSED;
$this->getAttribute(Doctrine::ATTR_LISTENER)->onClose($this); $this->getAttribute(Doctrine::ATTR_LISTENER)->onClose($this);
} }
/** /**

View File

@ -18,32 +18,19 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Connection_Module');
/** /**
* Doctrine_Connection_UnitOfWork * Doctrine_Connection_UnitOfWork
* *
* @package Doctrine * @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping * @category Object Relational Mapping
* @link www.phpdoctrine.com * @link www.phpdoctrine.com
* @since 1.0 * @since 1.0
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module 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;
}
/** /**
* buildFlushTree * buildFlushTree
* builds a flush tree that is used in transactions * builds a flush tree that is used in transactions

View File

@ -33,6 +33,7 @@ Doctrine::autoload('Doctrine_Connection_Module');
class Doctrine_Export extends Doctrine_Connection_Module { class Doctrine_Export extends Doctrine_Connection_Module {
/** /**
* drop an existing database * drop an existing database
* (this method is implemented by the drivers)
* *
* @param string $name name of the database that should be dropped * @param string $name name of the database that should be dropped
* @return void * @return void
@ -77,6 +78,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
} }
/** /**
* drop existing sequence * drop existing sequence
* (this method is implemented by the drivers)
* *
* @param string $seq_name name of the sequence to be dropped * @param string $seq_name name of the sequence to be dropped
* @return void * @return void
@ -86,6 +88,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
} }
/** /**
* create a new database * create a new database
* (this method is implemented by the drivers)
* *
* @param string $name name of the database that should be created * @param string $name name of the database that should be created
* @return void * @return void
@ -121,7 +124,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
* *
* @return void * @return void
*/ */
public function createTable($name, $fields, $options = array()) { public function createTable($name, array $fields, $options = array()) {
if ( ! $name) if ( ! $name)
throw new Doctrine_Export_Exception('no valid table name specified'); throw new Doctrine_Export_Exception('no valid table name specified');
@ -140,6 +143,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
} }
/** /**
* create sequence * create sequence
* (this method is implemented by the drivers)
* *
* @param string $seq_name name of the sequence to be created * @param string $seq_name name of the sequence to be created
* @param string $start start value of the sequence; default is 1 * @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 * 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 string $name name of the table that is intended to be changed.
* @param array $changes associative array that contains the details of each type * @param array $changes associative array that contains the details of each type

View File

@ -18,29 +18,19 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Connection_Module');
/** /**
* Doctrine_Expression * Doctrine_Expression
* *
* @package Doctrine * @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping * @category Object Relational Mapping
* @link www.phpdoctrine.com * @link www.phpdoctrine.com
* @since 1.0 * @since 1.0
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Expression { class Doctrine_Expression extends Doctrine_Connection_Module {
/**
* @var Doctrine_Connection $connection
*/
protected $conn;
/**
* @param Doctrine_Connection $conn
*/
public function __construct(Doctrine_Connection $conn) {
$this->conn = $conn;
}
/** /**
* regexp * regexp
* returns the regular expression operator * returns the regular expression operator

View File

@ -18,6 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Connection_Module');
/** /**
* *
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
@ -28,7 +29,7 @@
* @since 1.0 * @since 1.0
* @version $Revision$ * @version $Revision$
*/ */
class Doctrine_Transaction { class Doctrine_Transaction extends Doctrine_Connection_Module {
/** /**
* Doctrine_Transaction is in sleep state when it has no active transactions * 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 * Doctrine_Transaction is in busy state when it has multiple active transactions
*/ */
const STATE_BUSY = 2; 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 * @var integer $transaction_level the nesting level of transactions, used by transaction methods
*/ */
protected $transactionLevel = 0; protected $transactionLevel = 0;
/**
* the constructor
*
* @param Doctrine_Connection $conn Doctrine_Connection object
*/
public function __construct(Doctrine_Connection $conn) {
$this->conn = $conn;
}
/** /**
* getState * getState
* returns the state of this connection * returns the state of this connection