diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index fb2f34be8..c0e40f869 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -325,7 +325,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun return false; } - $event = new Doctrine_Event($this, Doctrine_Event::CONNECT); + $event = new Doctrine_Event($this, Doctrine_Event::CONN_CONNECT); $this->getListener()->preConnect($event); @@ -571,7 +571,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchAll($statement, array $params = array()) { - return $this->execute($statement, $params)->fetchAll(PDO::FETCH_ASSOC); + return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_ASSOC); } /** * fetchOne @@ -594,7 +594,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchRow($statement, array $params = array()) { - return $this->execute($statement, $params)->fetch(PDO::FETCH_ASSOC); + return $this->execute($statement, $params)->fetch(Doctrine::FETCH_ASSOC); } /** * fetchArray @@ -605,7 +605,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchArray($statement, array $params = array()) { - return $this->execute($statement, $params)->fetch(PDO::FETCH_NUM); + return $this->execute($statement, $params)->fetch(Doctrine::FETCH_NUM); } /** * fetchColumn @@ -617,7 +617,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchColumn($statement, array $params = array(), $colnum = 0) { - return $this->execute($statement, $params)->fetchAll(PDO::FETCH_COLUMN, $colnum); + return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_COLUMN, $colnum); } /** * fetchAssoc @@ -628,7 +628,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchAssoc($statement, array $params = array()) { - return $this->execute($statement, $params)->fetchAll(PDO::FETCH_ASSOC); + return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_ASSOC); } /** * fetchBoth @@ -639,7 +639,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun */ public function fetchBoth($statement, array $params = array()) { - return $this->execute($statement, $params)->fetchAll(PDO::FETCH_BOTH); + return $this->execute($statement, $params)->fetchAll(Doctrine::FETCH_BOTH); } /** * query @@ -672,7 +672,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { $this->connect(); - $event = new Doctrine_Event($this, Doctrine_Event::PREPARE, $statement); + $event = new Doctrine_Event($this, Doctrine_Event::CONN_PREPARE, $statement); $this->getAttribute(Doctrine::ATTR_LISTENER)->prePrepare($event); @@ -760,7 +760,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $stmt->execute($params); return $stmt; } else { - $event = new Doctrine_Event($this, Doctrine_Event::QUERY, $query, $params); + $event = new Doctrine_Event($this, Doctrine_Event::CONN_QUERY, $query, $params); $this->getAttribute(Doctrine::ATTR_LISTENER)->preQuery($event); @@ -795,7 +795,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun return $stmt->rowCount(); } else { - $event = new Doctrine_Event($this, Doctrine_EVENT::EXEC, $query, $params); + $event = new Doctrine_Event($this, Doctrine_Event::CONN_EXEC, $query, $params); $this->getAttribute(Doctrine::ATTR_LISTENER)->preExec($event); diff --git a/lib/Doctrine/Connection/Statement.php b/lib/Doctrine/Connection/Statement.php index 6be4a3606..af6095c64 100644 --- a/lib/Doctrine/Connection/Statement.php +++ b/lib/Doctrine/Connection/Statement.php @@ -213,7 +213,7 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf */ public function execute($params = null) { - $event = new Doctrine_Event($this, Doctrine_Event::EXECUTE, $this->_stmt->queryString, $params); + $event = new Doctrine_Event($this, Doctrine_Event::STMT_EXECUTE, $this->_stmt->queryString, $params); // print $this->_stmt->queryString . print_r($params, true) . "
"; $this->_conn->getListener()->preExecute($event); @@ -257,7 +257,7 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf $cursorOrientation = Doctrine::FETCH_ORI_NEXT, $cursorOffset = null) { - $event = new Doctrine_Db_Event($this, Doctrine_Event::FETCHALL, $this->_stmt->getQuery()); + $event = new Doctrine_Db_Event($this, Doctrine_Event::STMT_FETCH, $this->_stmt->getQuery()); $event->fetchMode = $fetchMode; $event->cursorOrientation = $cursorOrientation; @@ -289,7 +289,7 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf public function fetchAll($fetchMode = Doctrine::FETCH_BOTH, $columnIndex = null) { - $event = new Doctrine_Db_Event($this, Doctrine_Event::FETCHALL, $this->_stmt->getQuery()); + $event = new Doctrine_Db_Event($this, Doctrine_Event::STMT_FETCHALL, $this->_stmt->getQuery()); $event->fetchMode = $fetchMode; $event->columnIndex = $columnIndex; diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index 098b28e3d..b348943b1 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -139,7 +139,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module */ public function save(Doctrine_Record $record) { - $event = new Doctrine_Event($this, Doctrine_Event::SAVE); + $event = new Doctrine_Event($this, Doctrine_Event::RECORD_SAVE); $record->preSave($event); @@ -176,7 +176,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module } $this->conn->beginTransaction(); - $event = new Doctrine_Event($this, Doctrine_Event::DELETE); + $event = new Doctrine_Event($this, Doctrine_Event::RECORD_DELETE); $record->preDelete($event); @@ -326,7 +326,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module */ public function update(Doctrine_Record $record) { - $event = new Doctrine_Event($this, Doctrine_Event::UPDATE); + $event = new Doctrine_Event($this, Doctrine_Event::RECORD_UPDATE); $record->preUpdate($event); @@ -381,7 +381,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module public function insert(Doctrine_Record $record) { // listen the onPreInsert event - $event = new Doctrine_Event($this, Doctrine_Event::INSERT); + $event = new Doctrine_Event($this, Doctrine_Event::RECORD_INSERT); $record->preInsert($event); diff --git a/lib/Doctrine/Event.php b/lib/Doctrine/Event.php index 7e54f6367..ad45f3da1 100644 --- a/lib/Doctrine/Event.php +++ b/lib/Doctrine/Event.php @@ -32,23 +32,33 @@ class Doctrine_Event { /** - * EVENT CODE CONSTANTS + * CONNECTION EVENT CODES */ - const QUERY = 1; - const EXEC = 2; - const EXECUTE = 3; - const PREPARE = 4; - const BEGIN = 5; - const COMMIT = 6; - const ROLLBACK = 7; - const CONNECT = 8; - const FETCH = 9; - const FETCHALL = 10; - - const DELETE = 11; - const SAVE = 12; - const UPDATE = 13; - const INSERT = 14; + const CONN_QUERY = 1; + const CONN_EXEC = 2; + const CONN_PREPARE = 3; + const CONN_CONNECT = 4; + + const STMT_EXECUTE = 10; + const STMT_FETCH = 11; + const STMT_FETCHALL = 12; + + const TX_BEGIN = 31; + const TX_COMMIT = 32; + const TX_ROLLBACK = 33; + const SAVEPOINT_CREATE = 34; + const SAVEPOINT_ROLLBACK = 35; + const SAVEPOINT_COMMIT = 36; + + /** + * RECORD EVENT CODES + */ + const RECORD_DELETE = 21; + const RECORD_SAVE = 22; + const RECORD_UPDATE = 23; + const RECORD_INSERT = 24; + const RECORD_SERIALIZE = 25; + const RECORD_UNSERIALIZE = 26; /** * @var mixed $_invoker the handler which invoked this event */ diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index e636dae32..0b111e6cd 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -275,6 +275,30 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ protected function validateOnInsert() { } + /** + * Empty template method to provide concrete Record classes with the possibility + * to hook into the serializing procedure. + */ + public function preSerialize($event) + { } + /** + * Empty template method to provide concrete Record classes with the possibility + * to hook into the serializing procedure. + */ + public function postSerialize($event) + { } + /** + * Empty template method to provide concrete Record classes with the possibility + * to hook into the serializing procedure. + */ + public function preUnserialize($event) + { } + /** + * Empty template method to provide concrete Record classes with the possibility + * to hook into the serializing procedure. + */ + public function postUnserialize($event) + { } /** * Empty template method to provide concrete Record classes with the possibility * to hook into the saving procedure. @@ -447,7 +471,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ public function serialize() { - $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this); + $event = new Doctrine_Event($this, Doctrine_Event::SERIALIZE_RECORD); + + $this->preSerialize($event); $vars = get_object_vars($this); @@ -482,7 +508,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } } - return serialize($vars); + $str = serialize($vars); + + $this->postSerialize($event); + + return $str; } /** * unseralize diff --git a/lib/Doctrine/Transaction.php b/lib/Doctrine/Transaction.php index e08caa34a..a6f32ac57 100644 --- a/lib/Doctrine/Transaction.php +++ b/lib/Doctrine/Transaction.php @@ -222,7 +222,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module $this->createSavePoint($savepoint); } else { if ($this->transactionLevel == 0) { - $event = new Doctrine_Event($this, Doctrine_Event::BEGIN); + $event = new Doctrine_Event($this, Doctrine_Event::TX_BEGIN); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionBegin($event); @@ -267,7 +267,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module $this->releaseSavePoint($savepoint); } else { if ($this->transactionLevel == 1) { - $event = new Doctrine_Event($this, Doctrine_Event::COMMIT); + $event = new Doctrine_Event($this, Doctrine_Event::TX_COMMIT); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionCommit($event); @@ -333,9 +333,15 @@ class Doctrine_Transaction extends Doctrine_Connection_Module if ( ! is_null($savepoint)) { $this->transactionLevel = $this->removeSavePoints($savepoint); + $event = new Doctrine_Event($this, Doctrine_Event::ROLLBACK_SAVEPOINT); + + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preSavepointRollback($event); + $this->rollbackSavePoint($savepoint); + + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->postSavepointRollback($event); } else { - $event = new Doctrine_Event($this, Doctrine_Event::ROLLBACK); + $event = new Doctrine_Event($this, Doctrine_Event::TX_ROLLBACK); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionRollback($event); diff --git a/tests/Connection/ProfilerTestCase.php b/tests/Connection/ProfilerTestCase.php index 0b9a0aaff..45254c800 100644 --- a/tests/Connection/ProfilerTestCase.php +++ b/tests/Connection/ProfilerTestCase.php @@ -39,7 +39,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase {} public function setUp() {} - + public function testQuery() { $this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:')); @@ -52,7 +52,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXEC); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_EXEC); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->conn->count(), 1); @@ -66,14 +66,14 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($event->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $stmt->execute(array(1)); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->conn->count(), 2); @@ -85,13 +85,13 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)'); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $stmt2 = $this->conn->prepare('INSERT INTO test (id) VALUES (?)'); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); /** TODO: strange errors here $stmt->execute(array(1)); @@ -105,6 +105,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->conn->count(), 4); */ } + public function testExecuteStatementMultipleTimes() { try { @@ -118,14 +119,15 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } + public function testTransactionRollback() { try { @@ -136,7 +138,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::BEGIN); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_BEGIN); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); try { @@ -148,9 +150,10 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::ROLLBACK); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_ROLLBACK); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } + public function testTransactionCommit() { try { @@ -161,7 +164,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::BEGIN); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_BEGIN); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); try { @@ -174,7 +177,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::COMMIT); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_COMMIT); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } }