const ATTR_STRINGIFY_FETCHES = 17;
const ATTR_MAX_COLUMN_LEN = 18;
- const ATTR_LISTENER = 100;
const ATTR_QUOTE_IDENTIFIER = 101;
const ATTR_FIELD_CASE = 102;
const ATTR_IDXNAME_FORMAT = 103;
}
switch ($attribute) {
- case IPF_ORM::ATTR_LISTENER:
- $this->setEventListener($value);
- break;
case IPF_ORM::ATTR_COLL_KEY:
if ( ! ($this instanceof IPF_ORM_Table)) {
throw new IPF_ORM_Exception("This attribute can only be set at table level.");
return true;
}
- public function setEventListener($listener)
- {
- return $this->setListener($listener);
- }
-
public function addRecordListener($listener, $name = null)
{
if ( ! isset($this->attributes[IPF_ORM::ATTR_RECORD_LISTENER]) ||
return $this;
}
- public function addListener($listener, $name = null)
- {
- if ( ! isset($this->attributes[IPF_ORM::ATTR_LISTENER]) ||
- ! ($this->attributes[IPF_ORM::ATTR_LISTENER] instanceof IPF_ORM_EventListener_Chain)) {
-
- $this->attributes[IPF_ORM::ATTR_LISTENER] = new IPF_ORM_EventListener_Chain();
- }
- $this->attributes[IPF_ORM::ATTR_LISTENER]->add($listener, $name);
-
- return $this;
- }
-
- public function getListener()
- {
- if ( ! isset($this->attributes[IPF_ORM::ATTR_LISTENER])) {
- if (isset($this->parent)) {
- return $this->parent->getListener();
- }
- return null;
- }
- return $this->attributes[IPF_ORM::ATTR_LISTENER];
- }
-
- public function setListener($listener)
- {
- if ( ! ($listener instanceof IPF_ORM_EventListener_Interface)
- && ! ($listener instanceof IPF_ORM_Overloadable)
- ) {
- throw new IPF_ORM_Exception("Couldn't set eventlistener. EventListeners should implement either IPF_ORM_EventListener_Interface or IPF_ORM_Overloadable");
- }
- $this->attributes[IPF_ORM::ATTR_LISTENER] = $listener;
-
- return $this;
- }
-
public function getAttribute($attribute)
{
if (is_string($attribute)) {
);
protected $_count = 0;
+ public $dbListeners = array();
+
public function __construct(IPF_ORM_Manager $manager, $adapter, $user = null, $pass = null)
{
if (is_object($adapter)) {
}
$this->setParent($manager);
+ $this->dbListeners = $manager->dbListeners;
$this->setAttribute(IPF_ORM::ATTR_CASE, IPF_ORM::CASE_NATURAL);
$this->setAttribute(IPF_ORM::ATTR_ERRMODE, IPF_ORM::ERRMODE_EXCEPTION);
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->onOpen($this);
+ $this->notifyDBListeners('onOpen', $this);
}
public function getOptions()
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_CONNECT);
- $this->getListener()->preConnect($event);
+ $this->notifyDBListeners('preConnect', $event);
$e = explode(':', $this->options['dsn']);
$found = false;
if (extension_loaded('pdo')) {
if (in_array($e[0], PDO::getAvailableDrivers())) {
- try {
+ try {
$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']), $this->options['other']);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- throw new IPF_ORM_Exception('PDO Connection Error: ' . $e->getMessage());
- }
+ } catch (PDOException $e) {
+ throw new IPF_ORM_Exception('PDO Connection Error: ' . $e->getMessage());
+ }
$found = true;
}
}
$this->isConnected = true;
- $this->getListener()->postConnect($event);
+ $this->notifyDBListeners('postConnect', $event);
return true;
}
foreach ($arr as $k => $v) {
$arr[$k] = $this->quoteIdentifier($v, $checkOption);
}
-
- return $arr;
+ return $arr;
}
public function convertBooleans($item)
try {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_PREPARE, $statement);
-
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->prePrepare($event);
+
+ $this->notifyDBListeners('prePrepare', $event);
$stmt = false;
-
+
if ( ! $event->skipOperation) {
$stmt = $this->dbh->prepare($statement);
}
-
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->postPrepare($event);
-
+
+ $this->notifyDBListeners('postPrepare', $event);
+
return new IPF_ORM_Connection_Statement($this, $stmt);
- } catch(IPF_ORM_Exception_Adapter $e) {
- } catch(PDOException $e) { }
+ } catch (IPF_ORM_Exception_Adapter $e) {
+ } catch (PDOException $e) {
+ }
$this->rethrowException($e, $this);
}
} else {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_QUERY, $query, $params);
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->preQuery($event);
+ $this->notifyDBListeners('preQuery', $event);
if ( ! $event->skipOperation) {
$stmt = $this->dbh->query($query);
$this->_count++;
}
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->postQuery($event);
+ $this->notifyDBListeners('postQuery', $event);
return $stmt;
}
} catch (IPF_ORM_Exception_Adapter $e) {
- } catch (PDOException $e) { }
+ } catch (PDOException $e) {
+ }
$this->rethrowException($e, $this);
}
} else {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_EXEC, $query, $params);
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->preExec($event);
+ $this->notifyDBListeners('preExec', $event);
if ( ! $event->skipOperation) {
$count = $this->dbh->exec($query);
$this->_count++;
}
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->postExec($event);
+ $this->notifyDBListeners('postExec', $event);
return $count;
}
{
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_ERROR);
- $this->getListener()->preError($event);
+ $this->notifyDBListeners('preError', $event);
$name = 'IPF_ORM_Exception_' . $this->driverName;
throw $exc;
}
- $this->getListener()->postError($event);
+ $this->notifyDBListeners('postError', $event);
}
public function hasTable($name)
{
$event = new IPF_ORM_Event($this, IPF_ORM_Event::CONN_CLOSE);
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->preClose($event);
+ $this->notifyDBListeners('preClose', $event);
$this->clear();
unset($this->dbh);
$this->isConnected = false;
- $this->getAttribute(IPF_ORM::ATTR_LISTENER)->postClose($event);
+ $this->notifyDBListeners('postClose', $event);
}
public function getTransactionLevel()
if ($info['unix_socket']) {
$pdoDsn = $info['scheme'] . ':unix_socket=' . $info['unix_socket'];
} else {
- $pdoDsn = $info['scheme'] . ':host=' . $info['host'];
+ $pdoDsn = $info['scheme'] . ':host=' . $info['host'];
}
if (isset($this->export->tmpConnectionDatabase) && $this->export->tmpConnectionDatabase) {
{
return IPF_ORM_Utils::getConnectionAsString($this);
}
+
+ public function notifyDBListeners($method, $event)
+ {
+ foreach ($this->dbListeners as $listener)
+ if (method_exists($listener, $method))
+ $listener->$method($event);
+ }
}
+
{
try {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::STMT_EXECUTE, $this->getQuery(), $params);
- $this->_conn->getListener()->preStmtExecute($event);
+ $this->_conn->notifyDBListeners('preStmtExecute', $event);
$result = true;
if ( ! $event->skipOperation) {
$this->_conn->incrementQueryCount();
}
- $this->_conn->getListener()->postStmtExecute($event);
+ $this->_conn->notifyDBListeners('postStmtExecute', $event);
return $result;
} catch (PDOException $e) {
$event->cursorOrientation = $cursorOrientation;
$event->cursorOffset = $cursorOffset;
- $data = $this->_conn->getListener()->preFetch($event);
+ $data = $this->_conn->notifyDBListeners('preFetch', $event);
if ( ! $event->skipOperation) {
$data = $this->_stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
}
- $this->_conn->getListener()->postFetch($event);
+ $this->_conn->notifyDBListeners('postFetch', $event);
return $data;
}
$event->fetchMode = $fetchMode;
$event->columnIndex = $columnIndex;
- $this->_conn->getListener()->preFetchAll($event);
+ $this->_conn->notifyDBListeners('preFetchAll', $event);
if ( ! $event->skipOperation) {
if ($columnIndex !== null) {
$event->data = $data;
}
- $this->_conn->getListener()->postFetchAll($event);
+ $this->_conn->notifyDBListeners('postFetchAll', $event);
return $data;
}
return $this->_stmt->setFetchMode($mode, $arg1, $arg2);
}
}
+
+++ /dev/null
-<?php
-
-class IPF_ORM_EventListener implements IPF_ORM_EventListener_Interface
-{
- public function preClose(IPF_ORM_Event $event){}
- public function postClose(IPF_ORM_Event $event){}
- public function onCollectionDelete(IPF_ORM_Collection $collection){}
- public function onPreCollectionDelete(IPF_ORM_Collection $collection){}
- public function onOpen(IPF_ORM_Connection $connection){}
- public function preTransactionCommit(IPF_ORM_Event $event){}
- public function postTransactionCommit(IPF_ORM_Event $event){}
- public function preTransactionRollback(IPF_ORM_Event $event){}
- public function postTransactionRollback(IPF_ORM_Event $event){}
- public function preTransactionBegin(IPF_ORM_Event $event){}
- public function postTransactionBegin(IPF_ORM_Event $event){}
- public function preSavepointCommit(IPF_ORM_Event $event){}
- public function postSavepointCommit(IPF_ORM_Event $event){}
- public function preSavepointRollback(IPF_ORM_Event $event){}
- public function postSavepointRollback(IPF_ORM_Event $event){}
- public function preSavepointCreate(IPF_ORM_Event $event){}
- public function postSavepointCreate(IPF_ORM_Event $event){}
- public function postConnect(IPF_ORM_Event $event){}
- public function preConnect(IPF_ORM_Event $event){}
- public function preQuery(IPF_ORM_Event $event){}
- public function postQuery(IPF_ORM_Event $event){}
- public function prePrepare(IPF_ORM_Event $event){}
- public function postPrepare(IPF_ORM_Event $event){}
- public function preExec(IPF_ORM_Event $event){}
- public function postExec(IPF_ORM_Event $event){}
- public function preError(IPF_ORM_Event $event){}
- public function postError(IPF_ORM_Event $event){}
- public function preFetch(IPF_ORM_Event $event){}
- public function postFetch(IPF_ORM_Event $event){}
- public function preFetchAll(IPF_ORM_Event $event){}
- public function postFetchAll(IPF_ORM_Event $event){}
- public function preStmtExecute(IPF_ORM_Event $event){}
- public function postStmtExecute(IPF_ORM_Event $event){}
-}
+++ /dev/null
-<?php
-
-interface IPF_ORM_EventListener_Interface
-{
- public function preTransactionCommit(IPF_ORM_Event $event);
- public function postTransactionCommit(IPF_ORM_Event $event);
- public function preTransactionRollback(IPF_ORM_Event $event);
- public function postTransactionRollback(IPF_ORM_Event $event);
- public function preTransactionBegin(IPF_ORM_Event $event);
- public function postTransactionBegin(IPF_ORM_Event $event);
- public function postConnect(IPF_ORM_Event $event);
- public function preConnect(IPF_ORM_Event $event);
- public function preQuery(IPF_ORM_Event $event);
- public function postQuery(IPF_ORM_Event $event);
- public function prePrepare(IPF_ORM_Event $event);
- public function postPrepare(IPF_ORM_Event $event);
- public function preExec(IPF_ORM_Event $event);
- public function postExec(IPF_ORM_Event $event);
- public function preError(IPF_ORM_Event $event);
- public function postError(IPF_ORM_Event $event);
- public function preFetch(IPF_ORM_Event $event);
- public function postFetch(IPF_ORM_Event $event);
- public function preFetchAll(IPF_ORM_Event $event);
- public function postFetchAll(IPF_ORM_Event $event);
- public function preStmtExecute(IPF_ORM_Event $event);
- public function postStmtExecute(IPF_ORM_Event $event);
-}
protected $_index = 0;
protected $_currIndex = 0;
protected $_queryRegistry;
+ public $dbListeners = array();
private function __construct()
{
IPF_ORM::ATTR_RESULT_CACHE => null,
IPF_ORM::ATTR_QUERY_CACHE => null,
IPF_ORM::ATTR_LOAD_REFERENCES => true,
- IPF_ORM::ATTR_LISTENER => new IPF_ORM_EventListener(),
IPF_ORM::ATTR_RECORD_LISTENER => new IPF_ORM_Record_Listener(),
IPF_ORM::ATTR_THROW_EXCEPTIONS => true,
IPF_ORM::ATTR_QUERY_LIMIT => IPF_ORM::LIMIT_RECORDS,
public function beginTransaction($savepoint = null)
{
$this->conn->connect();
- $listener = $this->conn->getAttribute(IPF_ORM::ATTR_LISTENER);
-
if ( ! is_null($savepoint)) {
$this->savePoints[] = $savepoint;
$event = new IPF_ORM_Event($this, IPF_ORM_Event::SAVEPOINT_CREATE);
- $listener->preSavepointCreate($event);
+ $this->conn->notifyDBListeners('preSavepointCreate', $event);
if ( ! $event->skipOperation) {
$this->createSavePoint($savepoint);
}
- $listener->postSavepointCreate($event);
+ $this->conn->notifyDBListeners('postSavepointCreate', $event);
} else {
if ($this->_nestingLevel == 0) {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::TX_BEGIN);
- $listener->preTransactionBegin($event);
+ $this->conn->notifyDBListeners('preTransactionBegin', $event);
if ( ! $event->skipOperation) {
try {
throw new IPF_ORM_Exception($e->getMessage());
}
}
- $listener->postTransactionBegin($event);
+ $this->conn->notifyDBListeners('postTransactionBegin', $event);
}
}
$this->conn->connect();
- $listener = $this->conn->getAttribute(IPF_ORM::ATTR_LISTENER);
-
if ( ! is_null($savepoint)) {
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
$event = new IPF_ORM_Event($this, IPF_ORM_Event::SAVEPOINT_COMMIT);
- $listener->preSavepointCommit($event);
+ $this->conn->notifyDBListeners('preSavepointCommit', $event);
if ( ! $event->skipOperation) {
$this->releaseSavePoint($savepoint);
}
- $listener->postSavepointCommit($event);
+ $this->conn->notifyDBListeners('postSavepointCommit', $event);
} else {
if ($this->_nestingLevel == 1 || $this->_internalNestingLevel == 1) {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::TX_COMMIT);
- $listener->preTransactionCommit($event);
+ $this->conn->notifyDBListeners('preTransactionCommit', $event);
if ( ! $event->skipOperation) {
$this->_doCommit();
}
- $listener->postTransactionCommit($event);
+ $this->conn->notifyDBListeners('postTransactionCommit', $event);
}
}
return false;
}
- $listener = $this->conn->getAttribute(IPF_ORM::ATTR_LISTENER);
-
if ( ! is_null($savepoint)) {
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
$event = new IPF_ORM_Event($this, IPF_ORM_Event::SAVEPOINT_ROLLBACK);
- $listener->preSavepointRollback($event);
+ $this->conn->notifyDBListeners('preSavepointRollback', $event);
if ( ! $event->skipOperation) {
$this->rollbackSavePoint($savepoint);
}
- $listener->postSavepointRollback($event);
+ $this->conn->notifyDBListeners('postSavepointRollback', $event);
} else {
$event = new IPF_ORM_Event($this, IPF_ORM_Event::TX_ROLLBACK);
- $listener->preTransactionRollback($event);
+ $this->conn->notifyDBListeners('preTransactionRollback', $event);
if ( ! $event->skipOperation) {
$this->_nestingLevel = 0;
}
}
- $listener->postTransactionRollback($event);
+ $this->conn->notifyDBListeners('postTransactionRollback', $event);
}
return true;
$cli->run();
}
- public function run(){
+ public function run() {
$dsn = IPF::get('dsn');
if ($dsn=='')
throw new IPF_Exception_Panic('Specify dsn in config file');
- IPF_ORM_Manager::getInstance()->openConnection($dsn, null, true, IPF::get('db_persistent', false));
-
- if (IPF::get('debug')){
+ if (IPF::get('debug')) {
$this->sqlProfiler = new IPF_ORM_Connection_Profiler();
- IPF_ORM_Manager::getInstance()->getCurrentConnection()->setListener($this->sqlProfiler);
+ IPF_ORM_Manager::getInstance()->dbListeners[] = $this->sqlProfiler;
}
-
+
+ IPF_ORM_Manager::getInstance()->openConnection($dsn, null, true, IPF::get('db_persistent', false));
+
if (php_sapi_name() == 'cli'){
$this->cli();
return true;