some event hooks added
This commit is contained in:
parent
6ef092c661
commit
e49a686a3b
116
draft/DB.php
116
draft/DB.php
@ -60,9 +60,15 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @var string $password database password
|
* @var string $password database password
|
||||||
*/
|
*/
|
||||||
protected $password;
|
protected $password;
|
||||||
/**
|
/**
|
||||||
* @var PDO $dbh the database handler
|
* @var PDO $dbh the database handler
|
||||||
*/
|
*/
|
||||||
|
protected $dbh;
|
||||||
|
/**
|
||||||
|
* @var Doctrine_DB_EventListener_Interface $listener listener for listening events
|
||||||
|
*/
|
||||||
|
protected $listener;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constructor
|
* constructor
|
||||||
@ -75,6 +81,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
$this->dsn = $dsn;
|
$this->dsn = $dsn;
|
||||||
$this->username = $username;
|
$this->username = $username;
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
|
$this->listener = new Doctrine_DB_EventListener();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* getDSN
|
* getDSN
|
||||||
@ -117,27 +124,27 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
/**
|
/**
|
||||||
* getConnection
|
* getConnection
|
||||||
*
|
*
|
||||||
* @param string $dsn PEAR::DB like DSN
|
* @param string $dsn PEAR::DB like DSN or PDO like DSN
|
||||||
* format: schema://user:password@address/dbname
|
* format for PEAR::DB like DSN: schema://user:password@address/dbname
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static function getConnection($dsn = null, $username = null, $password = null) {
|
public static function getConnection($dsn = null, $username = null, $password = null) {
|
||||||
$md5 = md5($dsn);
|
$md5 = md5($dsn);
|
||||||
|
|
||||||
if(isset($username)) {
|
|
||||||
self::$instances[$md5] = new self($dsn, $username, $password);
|
|
||||||
}
|
|
||||||
|
|
||||||
if( ! isset(self::$instances[$md5])) {
|
if( ! isset(self::$instances[$md5])) {
|
||||||
if( ! isset($dsn)) {
|
if(isset($username)) {
|
||||||
$a = self::parseDSN(self::DSN);
|
self::$instances[$md5] = new self($dsn, $username, $password);
|
||||||
} else {
|
} else {
|
||||||
$a = self::parseDSN($dsn);
|
if( ! isset($dsn))
|
||||||
}
|
$a = self::parseDSN(self::DSN);
|
||||||
extract($a);
|
else
|
||||||
|
$a = self::parseDSN($dsn);
|
||||||
|
|
||||||
self::$instances[$md5] = new self($dsn, $user, $pass);
|
extract($a);
|
||||||
|
|
||||||
|
self::$instances[$md5] = new self($dsn, $user, $pass);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return self::$instances[$md5];
|
return self::$instances[$md5];
|
||||||
}
|
}
|
||||||
@ -157,9 +164,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
*
|
*
|
||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
public function errorCode() {
|
public function errorCode() {
|
||||||
$this->connect();
|
|
||||||
|
|
||||||
return $this->dbh->errorCode();
|
return $this->dbh->errorCode();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -169,19 +174,21 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function errorInfo() {
|
public function errorInfo() {
|
||||||
$this->connect();
|
|
||||||
|
|
||||||
return $this->dbh->errorInfo();
|
return $this->dbh->errorInfo();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
* prepare
|
||||||
*
|
*
|
||||||
* @param string $statement
|
* @param string $statement
|
||||||
*/
|
*/
|
||||||
public function prepare ($statement) {
|
public function prepare($statement) {
|
||||||
$this->connect();
|
$this->listener->onPrePrepare($this, $statement);
|
||||||
$this->queries[] = $query;
|
|
||||||
return $this->dbh->prepare($statement);
|
$stmt = $this->dbh->prepare($statement);
|
||||||
|
|
||||||
|
$this->listener->onPrepare($this,$statement);
|
||||||
|
|
||||||
|
return $stmt;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* query
|
* query
|
||||||
@ -190,14 +197,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @return Doctrine_DB_Statement|boolean
|
* @return Doctrine_DB_Statement|boolean
|
||||||
*/
|
*/
|
||||||
public function query($statement, $fetchMode = null, $arg = null, $arg2 = null) {
|
public function query($statement, $fetchMode = null, $arg = null, $arg2 = null) {
|
||||||
$this->connect();
|
$args = func_get_args();
|
||||||
|
|
||||||
$this->queries[] = $query;
|
|
||||||
$time = microtime();
|
|
||||||
|
|
||||||
$stmt = $this->dbh->query($query, $fetchMode, $arg, $arg2);
|
$this->listener->onPreQuery($this, $args);
|
||||||
|
|
||||||
$this->exectimes[] = (microtime() - $time);
|
$stmt = $this->dbh->query($statement, $fetchMode, $arg, $arg2);
|
||||||
|
|
||||||
|
$this->listener->onQuery($this, $args);
|
||||||
|
|
||||||
return $stmt;
|
return $stmt;
|
||||||
}
|
}
|
||||||
@ -210,7 +216,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
*/
|
*/
|
||||||
public function quote($input) {
|
public function quote($input) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
|
|
||||||
return $this->dbh->quote($input);
|
return $this->dbh->quote($input);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -221,9 +227,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
public function exec($statement) {
|
public function exec($statement) {
|
||||||
$this->connect();
|
$this->listener->onPreExec($this, $statement);
|
||||||
|
|
||||||
return $this->dbh->exec($statement);
|
$rows = $this->dbh->exec($statement);
|
||||||
|
|
||||||
|
$this->listener->onExec($this, $statement);
|
||||||
|
|
||||||
|
return $rows;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* lastInsertId
|
* lastInsertId
|
||||||
@ -241,9 +251,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function beginTransaction() {
|
public function beginTransaction() {
|
||||||
$this->connect();
|
$this->listener->onPreBeginTransaction($this);
|
||||||
|
|
||||||
return $this->dbh->beginTransaction();
|
$return = $this->dbh->beginTransaction();
|
||||||
|
|
||||||
|
$this->listener->onBeginTransaction($this);
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* commits a transaction
|
* commits a transaction
|
||||||
@ -251,9 +265,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function commit() {
|
public function commit() {
|
||||||
$this->connect();
|
$this->listener->onPreCommit($this);
|
||||||
|
|
||||||
return $this->dbh->commit();
|
$return = $this->dbh->commit();
|
||||||
|
|
||||||
|
$this->listener->onCommit($this);
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* rollBack
|
* rollBack
|
||||||
@ -296,26 +314,6 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
|
|
||||||
$this->dbh->setAttribute($attribute, $value);
|
$this->dbh->setAttribute($attribute, $value);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @param string $time exectime of the last executed query
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addExecTime($time) {
|
|
||||||
$this->exectimes[] = $time;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getExecTimes() {
|
|
||||||
return $this->exectimes;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* getQueries
|
|
||||||
* returns an array of executed queries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getQueries() {
|
|
||||||
return $this->queries;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* getIterator
|
* getIterator
|
||||||
*
|
*
|
||||||
@ -354,7 +352,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
|||||||
|
|
||||||
$drivers = self::getAvailableDrivers();
|
$drivers = self::getAvailableDrivers();
|
||||||
|
|
||||||
if( ! in_array($parts['scheme'], $drivers))
|
if( ! in_array($parts['scheme'], $drivers))
|
||||||
throw new Doctrine_DB_Exception('Driver '.$parts['scheme'].' not availible or extension not loaded');
|
throw new Doctrine_DB_Exception('Driver '.$parts['scheme'].' not availible or extension not loaded');
|
||||||
|
|
||||||
switch($parts['scheme']) {
|
switch($parts['scheme']) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user