Commit bcbdba0e authored by zYne's avatar zYne

New Doctrine_DB enhancements

parent a773997d
......@@ -26,20 +26,23 @@
* @package Doctrine
*/
class Doctrine_DB_EventListener implements Doctrine_DB_EventListener_Interface {
public function onPreQuery(Doctrine_DB $dbh, array $args) { }
public function onQuery(Doctrine_DB $dbh, array $args) { }
public function onPreQuery(Doctrine_DB2 $dbh, array $args) { }
public function onQuery(Doctrine_DB2 $dbh, array $args) { }
public function onPrePrepare(Doctrine_DB $dbh, array $args) { }
public function onPrepare(Doctrine_DB $dbh, array $args) { }
public function onPrePrepare(Doctrine_DB2 $dbh, array $args) { }
public function onPrepare(Doctrine_DB2 $dbh, array $args) { }
public function onPreCommit(Doctrine_DB $dbh) { }
public function onCommit(Doctrine_DB $dbh) { }
public function onPreCommit(Doctrine_DB2 $dbh) { }
public function onCommit(Doctrine_DB2 $dbh) { }
public function onPreRollBack(Doctrine_DB $dbh) { }
public function onRollBack(Doctrine_DB $dbh) { }
public function onPreExec(Doctrine_DB2 $dbh, array $args) { }
public function onExec(Doctrine_DB2 $dbh, array $args) { }
public function onPreBeginTransaction(Doctrine_DB $dbh) { }
public function onBeginTransaction(Doctrine_DB $dbh) { }
public function onPreRollBack(Doctrine_DB2 $dbh) { }
public function onRollBack(Doctrine_DB2 $dbh) { }
public function onPreBeginTransaction(Doctrine_DB2 $dbh) { }
public function onBeginTransaction(Doctrine_DB2 $dbh) { }
public function onPreExecute(Doctrine_DB_Statement $stmt, array $params) { }
public function onExecute(Doctrine_DB_Statement $stmt, array $params) { }
......
......@@ -25,60 +25,96 @@
* @license LGPL
* @package Doctrine
*/
class Doctrine_DB_EventListener_Chain implements Doctrine_DB_EventListener_Interface {
class Doctrine_DB_EventListener_Chain extends Doctrine_Access implements Doctrine_DB_EventListener_Interface {
private $listeners = array();
public function add($listener, $name = null) {
if( ! ($listener instanceof Doctrine_DB_EventListener_Interface) &&
! ($listener instanceof Doctrine_Overloadable))
throw new Doctrine_DB_Exception("Couldn't add eventlistener. EventListeners should implement either Doctrine_DB_EventListener_Interface or Doctrine_Overloadable");
public function onPreQuery(Doctrine_DB $dbh, array $args) {
if($name === null)
$this->listeners[] = $listener;
else
$this->listeners[$name] = $listener;
}
public function get($name) {
if( ! isset($this->listeners[$name]))
throw new Doctrine_DB_Exception("Unknown listener $name");
return $this->listeners[$name];
}
public function set($name, $listener) {
if( ! ($listener instanceof Doctrine_DB_EventListener_Interface) &&
! ($listener instanceof Doctrine_Overloadable))
throw new Doctrine_DB_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_DB_EventListener_Interface or Doctrine_Overloadable");
$this->listeners[$name] = $listener;
}
public function onPreQuery(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onPreQuery($dbh, $args);
}
}
public function onQuery(Doctrine_DB $dbh, array $args) {
public function onQuery(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onPreQuery($dbh, $args);
$listener->onQuery($dbh, $args);
}
}
public function onPreExec(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onPreExec($dbh, $args);
}
}
public function onExec(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onExec($dbh, $args);
}
}
public function onPrePrepare(Doctrine_DB $dbh, array $args) {
public function onPrePrepare(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onPrePrepare($dbh, $args);
}
}
public function onPrepare(Doctrine_DB $dbh, array $args) {
public function onPrepare(Doctrine_DB2 $dbh, array $args) {
foreach($this->listeners as $listener) {
$listener->onPrepare($dbh, $args);
}
}
public function onPreCommit(Doctrine_DB $dbh) {
public function onPreCommit(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onPreCommit($dbh);
}
}
public function onCommit(Doctrine_DB $dbh) {
public function onCommit(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onCommit($dbh);
}
}
public function onPreRollBack(Doctrine_DB $dbh) {
public function onPreRollBack(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onPreRollBack($dbh);
}
}
public function onRollBack(Doctrine_DB $dbh) {
public function onRollBack(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onRollBack($dbh);
}
}
public function onPreBeginTransaction(Doctrine_DB $dbh) {
public function onPreBeginTransaction(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onPreBeginTransaction($dbh);
}
}
public function onBeginTransaction(Doctrine_DB $dbh) {
public function onBeginTransaction(Doctrine_DB2 $dbh) {
foreach($this->listeners as $listener) {
$listener->onBeginTransaction($dbh);
}
......
......@@ -26,20 +26,23 @@
* @package Doctrine
*/
interface Doctrine_DB_EventListener_Interface {
public function onPreQuery(Doctrine_DB $dbh, array $args);
public function onQuery(Doctrine_DB $dbh, array $args);
public function onPreQuery(Doctrine_DB2 $dbh, array $args);
public function onQuery(Doctrine_DB2 $dbh, array $args);
public function onPrePrepare(Doctrine_DB $dbh, array $args);
public function onPrepare(Doctrine_DB $dbh, array $args);
public function onPrePrepare(Doctrine_DB2 $dbh, array $args);
public function onPrepare(Doctrine_DB2 $dbh, array $args);
public function onPreCommit(Doctrine_DB $dbh);
public function onCommit(Doctrine_DB $dbh);
public function onPreExec(Doctrine_DB2 $dbh, array $args);
public function onExec(Doctrine_DB2 $dbh, array $args);
public function onPreRollBack(Doctrine_DB $dbh);
public function onRollBack(Doctrine_DB $dbh);
public function onPreCommit(Doctrine_DB2 $dbh);
public function onCommit(Doctrine_DB2 $dbh);
public function onPreBeginTransaction(Doctrine_DB $dbh);
public function onBeginTransaction(Doctrine_DB $dbh);
public function onPreRollBack(Doctrine_DB2 $dbh);
public function onRollBack(Doctrine_DB2 $dbh);
public function onPreBeginTransaction(Doctrine_DB2 $dbh);
public function onBeginTransaction(Doctrine_DB2 $dbh);
public function onPreExecute(Doctrine_DB_Statement $stmt, array $params);
public function onExecute(Doctrine_DB_Statement $stmt, array $params);
......
......@@ -65,10 +65,14 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
*/
protected $dbh;
/**
* @var Doctrine_DB_EventListener_Interface $listener listener for listening events
* @var Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener listener for listening events
*/
protected $listener;
private static $driverMap = array("oracle" => "oci8",
"postgres" => "pgsql",
"oci" => "oci8");
/**
* constructor
......@@ -83,8 +87,15 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$this->password = $password;
$this->listener = new Doctrine_DB_EventListener();
}
/**
* getDBH
*/
public function getDBH() {
return $this->dbh;
}
/**
* getDSN
* returns the data source name
*
* @return string
*/
......@@ -103,6 +114,43 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
public function getPassword() {
return $this->password;
}
/**
* addListener
*
* @param Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_DB
*/
public function addListener($listener, $name = null) {
if( ! ($this->listener instanceof Doctrine_DB_EventListener_Chain))
$this->listener = new Doctrine_DB_EventListener_Chain();
$this->listener->add($listener, $name);
return $this;
}
/**
* getListener
*
* @return Doctrine_DB_EventListener_Interface|Doctrine_Overloadable
*/
public function getListener() {
return $this->listener;
}
/**
* setListener
*
* @param Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_DB
*/
public function setListener($listener) {
if( ! ($listener instanceof Doctrine_DB_EventListener_Interface) &&
! ($listener instanceof Doctrine_Overloadable))
throw new Doctrine_DB_Exception("Couldn't set eventlistener for database handler. EventListeners should implement either Doctrine_DB_EventListener_Interface or Doctrine_Overloadable");
$this->listener = $listener;
return $this;
}
/**
* connect
......@@ -116,7 +164,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$this->dbh = new PDO($this->dsn,$this->username,$this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("Doctrine_DBStatement",array($this)));
$this->dbh->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("Doctrine_DB_Statement", array($this)));
return true;
}
......@@ -148,6 +196,69 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
}
return self::$instances[$md5];
}
public static function driverName() {
}
/**
* parseDSN
*
* @param string $dsn
* @return array Parsed contents of DSN
*/
function parseDSN($dsn) {
$parts = @parse_url($dsn);
$names = array('scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment');
foreach($names as $name) {
if( ! isset($parts[$name]))
$parts[$name] = null;
}
if(count($parts) == 0 || ! isset($parts['scheme']))
throw new Doctrine_DB_Exception('Empty data source name');
$drivers = self::getAvailableDrivers();
if(isset(self::$driverMap[$parts['scheme']]))
$parts['scheme'] = self::$driverMap[$parts['scheme']];
if( ! in_array($parts['scheme'], $drivers))
throw new Doctrine_DB_Exception('Driver '.$parts['scheme'].' not availible or extension not loaded');
switch($parts['scheme']) {
case 'sqlite':
if(isset($parts['host']) && $parts['host'] == ':memory') {
$parts['database'] = ':memory:';
$parts['dsn'] = 'sqlite::memory:';
}
break;
case 'mysql':
case 'informix':
case 'oci8':
case 'mssql':
case 'firebird':
case 'pgsql':
case 'odbc':
if( ! isset($parts['path']) || $parts['path'] == '/')
throw new Doctrine_DB_Exception('No database availible in data source name');
if(isset($parts['path']))
$parts['database'] = substr($parts['path'], 1);
if( ! isset($parts['host']))
throw new Doctrine_DB_Exception('No hostname set in data source name');
$parts['dsn'] = $parts["scheme"].":host=".$parts["host"].";dbname=".$parts["database"];
break;
default:
throw new Doctrine_DB_Exception('Unknown driver '.$parts['scheme']);
}
return $parts;
}
/**
* clear
* clears all instances from the memory
......@@ -182,11 +293,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @param string $statement
*/
public function prepare($statement) {
$this->listener->onPrePrepare($this, $statement);
$args = func_get_args();
$this->listener->onPrePrepare($this, $args);
$stmt = $this->dbh->prepare($statement);
$this->listener->onPrepare($this,$statement);
$this->listener->onPrepare($this, $args);
return $stmt;
}
......@@ -227,11 +340,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @return integer
*/
public function exec($statement) {
$this->listener->onPreExec($this, $statement);
$args = func_get_args();
$this->listener->onPreExec($this, $args);
$rows = $this->dbh->exec($statement);
$this->listener->onExec($this, $statement);
$this->listener->onExec($this, $args);
return $rows;
}
......@@ -331,61 +446,6 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
public function count() {
return count($this->queries);
}
/**
* parseDSN
*
* @param string $dsn
* @return array Parsed contents of DSN
*/
function parseDSN($dsn) {
$parts = @parse_url($dsn);
$names = array('scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment');
foreach($names as $name) {
if( ! isset($parts[$name]))
$parts[$name] = null;
}
if(count($parts) == 0 || ! isset($parts['scheme']))
throw new Doctrine_DB_Exception('Empty data source name');
$drivers = self::getAvailableDrivers();
if( ! in_array($parts['scheme'], $drivers))
throw new Doctrine_DB_Exception('Driver '.$parts['scheme'].' not availible or extension not loaded');
switch($parts['scheme']) {
case 'sqlite':
if(isset($parts['host']) && $parts['host'] == ':memory') {
$parts['database'] = ':memory:';
$parts['dsn'] = 'sqlite::memory:';
}
break;
case 'mysql':
case 'informix':
case 'oci8':
case 'mssql':
case 'firebird':
case 'pgsql':
case 'odbc':
if( ! isset($parts['path']) || $parts['path'] == '/')
throw new Doctrine_DB_Exception('No database availible in data source name');
if(isset($parts['path']))
$parts['database'] = substr($parts['path'], 1);
if( ! isset($parts['host']))
throw new Doctrine_DB_Exception('No hostname set in data source name');
$parts['dsn'] = $parts["scheme"].":host=".$parts["host"].";dbname=".$parts["database"];
break;
default:
throw new Doctrine_DB_Exception('Unknown driver '.$parts['scheme']);
}
return $parts;
}
}
This diff is collapsed.
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