some event hooks added
This commit is contained in:
parent
6ef092c661
commit
e49a686a3b
94
draft/DB.php
94
draft/DB.php
@ -63,6 +63,12 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
/**
|
||||
* @var PDO $dbh the database handler
|
||||
*/
|
||||
protected $dbh;
|
||||
/**
|
||||
* @var Doctrine_DB_EventListener_Interface $listener listener for listening events
|
||||
*/
|
||||
protected $listener;
|
||||
|
||||
|
||||
/**
|
||||
* constructor
|
||||
@ -75,6 +81,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
$this->dsn = $dsn;
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->listener = new Doctrine_DB_EventListener();
|
||||
}
|
||||
/**
|
||||
* getDSN
|
||||
@ -117,28 +124,28 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
/**
|
||||
* getConnection
|
||||
*
|
||||
* @param string $dsn PEAR::DB like DSN
|
||||
* format: schema://user:password@address/dbname
|
||||
* @param string $dsn PEAR::DB like DSN or PDO like DSN
|
||||
* format for PEAR::DB like DSN: schema://user:password@address/dbname
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static function getConnection($dsn = null, $username = null, $password = null) {
|
||||
$md5 = md5($dsn);
|
||||
|
||||
if( ! isset(self::$instances[$md5])) {
|
||||
if(isset($username)) {
|
||||
self::$instances[$md5] = new self($dsn, $username, $password);
|
||||
}
|
||||
|
||||
if( ! isset(self::$instances[$md5])) {
|
||||
if( ! isset($dsn)) {
|
||||
$a = self::parseDSN(self::DSN);
|
||||
} else {
|
||||
if( ! isset($dsn))
|
||||
$a = self::parseDSN(self::DSN);
|
||||
else
|
||||
$a = self::parseDSN($dsn);
|
||||
}
|
||||
|
||||
extract($a);
|
||||
|
||||
self::$instances[$md5] = new self($dsn, $user, $pass);
|
||||
}
|
||||
}
|
||||
return self::$instances[$md5];
|
||||
}
|
||||
/**
|
||||
@ -158,8 +165,6 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return integer
|
||||
*/
|
||||
public function errorCode() {
|
||||
$this->connect();
|
||||
|
||||
return $this->dbh->errorCode();
|
||||
}
|
||||
/**
|
||||
@ -169,19 +174,21 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return array
|
||||
*/
|
||||
public function errorInfo() {
|
||||
$this->connect();
|
||||
|
||||
return $this->dbh->errorInfo();
|
||||
}
|
||||
/**
|
||||
*
|
||||
* prepare
|
||||
*
|
||||
* @param string $statement
|
||||
*/
|
||||
public function prepare ($statement) {
|
||||
$this->connect();
|
||||
$this->queries[] = $query;
|
||||
return $this->dbh->prepare($statement);
|
||||
public function prepare($statement) {
|
||||
$this->listener->onPrePrepare($this, $statement);
|
||||
|
||||
$stmt = $this->dbh->prepare($statement);
|
||||
|
||||
$this->listener->onPrepare($this,$statement);
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
/**
|
||||
* query
|
||||
@ -190,14 +197,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return Doctrine_DB_Statement|boolean
|
||||
*/
|
||||
public function query($statement, $fetchMode = null, $arg = null, $arg2 = null) {
|
||||
$this->connect();
|
||||
$args = func_get_args();
|
||||
|
||||
$this->queries[] = $query;
|
||||
$time = microtime();
|
||||
$this->listener->onPreQuery($this, $args);
|
||||
|
||||
$stmt = $this->dbh->query($query, $fetchMode, $arg, $arg2);
|
||||
$stmt = $this->dbh->query($statement, $fetchMode, $arg, $arg2);
|
||||
|
||||
$this->exectimes[] = (microtime() - $time);
|
||||
$this->listener->onQuery($this, $args);
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
@ -221,9 +227,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return integer
|
||||
*/
|
||||
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
|
||||
@ -241,9 +251,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return boolean
|
||||
*/
|
||||
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
|
||||
@ -251,9 +265,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
* @return boolean
|
||||
*/
|
||||
public function commit() {
|
||||
$this->connect();
|
||||
$this->listener->onPreCommit($this);
|
||||
|
||||
return $this->dbh->commit();
|
||||
$return = $this->dbh->commit();
|
||||
|
||||
$this->listener->onCommit($this);
|
||||
|
||||
return $return;
|
||||
}
|
||||
/**
|
||||
* rollBack
|
||||
@ -296,26 +314,6 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
|
||||
|
||||
$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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user