Commit d6d78241 authored by zYne's avatar zYne

Refactored connection modules, added new methods to Doctrine_Connection

parent 02cc9b2f
...@@ -63,7 +63,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -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 ...@@ -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;
} }
/**
* __get
* lazy loads given module and returns it
*
* @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 __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];
}
/** /**
* quoteIdentifier * 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
* *
* @param string $identifier identifier to be quoted * @return string quoted string
* @return string modified identifier
*/ */
public function quoteIdentifier($identifier) { public function escapePattern($text) {
return $identifier; 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
...@@ -610,7 +696,6 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -610,7 +696,6 @@ 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);
} }
......
...@@ -18,6 +18,7 @@ ...@@ -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');
/** /**
* Doctrine_Connection_UnitOfWork * Doctrine_Connection_UnitOfWork
* *
...@@ -29,21 +30,7 @@ ...@@ -29,21 +30,7 @@
* @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
......
...@@ -33,6 +33,7 @@ Doctrine::autoload('Doctrine_Connection_Module'); ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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
......
...@@ -18,6 +18,7 @@ ...@@ -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');
/** /**
* Doctrine_Expression * Doctrine_Expression
* *
...@@ -29,18 +30,7 @@ ...@@ -29,18 +30,7 @@
* @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
......
...@@ -18,6 +18,7 @@ ...@@ -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 @@ ...@@ -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 { ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment