transactions
authorMichael Wallner <mike@php.net>
Fri, 25 Jan 2013 20:08:56 +0000 (21:08 +0100)
committerMichael Wallner <mike@php.net>
Fri, 25 Jan 2013 20:08:56 +0000 (21:08 +0100)
TODO
src/php_pq.c
src/php_pq.h
tests/trans001.phpt [new file with mode: 0644]

diff --git a/TODO b/TODO
index 8fa2e0680621007485691b2620b7ba44b3616618..f1bf12a83cba6e52445a0414637f0daa4fa1b3cb 100644 (file)
--- a/TODO
+++ b/TODO
@@ -3,11 +3,18 @@
 * LOBs
 * COPY
 * pq\Cancel
+* transaction savepoints
+* transaction snapshots
+* notice receiver (else libpq uses stderr)
+* pq\Event\Notice
 
+* pq\Event\Result\Create, 
+  pq\Event\Result\Destroy, 
+  pq\Event\Connection\Reset, 
+  pq\Event\Connection\Destroy
 * fetchInto/fetchCtor?
 * unlisten?
 * tracing?
 * userland event handler?
-* notice receiver?
 * pq\Result->__clone through PQcopyResult?
 * db/pass/user/host/port/options properties on pq\Connection?
\ No newline at end of file
index c220f2e713afa7f3bb791eeb84a7b3e914088e16..64c9ce181db466a6aa89f9a661c3a3eb7c64a272 100644 (file)
@@ -30,29 +30,6 @@ typedef int STATUS; /* SUCCESS/FAILURE */
 ZEND_DECLARE_MODULE_GLOBALS(pq)
 */
 
-const zend_function_entry pq_functions[] = {
-       {0}
-};
-
-/* {{{ pq_module_entry
- */
-zend_module_entry pq_module_entry = {
-       STANDARD_MODULE_HEADER,
-       "pq",
-       pq_functions,
-       PHP_MINIT(pq),
-       PHP_MSHUTDOWN(pq),
-       NULL,/*PHP_RINIT(pq),*/
-       NULL,/*PHP_RSHUTDOWN(pq),*/
-       PHP_MINFO(pq),
-       PHP_PQ_EXT_VERSION,
-       STANDARD_MODULE_PROPERTIES
-};
-/* }}} */
-
-#ifdef COMPILE_DL_PQ
-ZEND_GET_MODULE(pq)
-#endif
 
 /* {{{ PHP_INI
  */
@@ -78,10 +55,12 @@ static void php_pq_init_globals(zend_pq_globals *pq_globals)
 static zend_class_entry *php_pqconn_class_entry;
 static zend_class_entry *php_pqres_class_entry;
 static zend_class_entry *php_pqstm_class_entry;
+static zend_class_entry *php_pqtxn_class_entry;
 
 static zend_object_handlers php_pqconn_object_handlers;
 static zend_object_handlers php_pqres_object_handlers;
 static zend_object_handlers php_pqstm_object_handlers;
+static zend_object_handlers php_pqtxn_object_handlers;
 
 typedef struct php_pq_callback {
        zend_fcall_info fci;
@@ -133,17 +112,40 @@ typedef struct php_pqres_object {
        php_pqres_iterator_t *iter;
 } php_pqres_object_t;
 
+typedef struct php_pqstm {
+       zval *conn;
+       char *name;
+} php_pqstm_t;
+
 typedef struct php_pqstm_object {
        zend_object zo;
-       char *name;
+       php_pqstm_t *stm;
        HashTable *prophandler;
+} php_pqstm_object_t;
+
+typedef enum php_pqtxn_isolation {
+       PHP_PQTXN_READ_COMMITTED,
+       PHP_PQTXN_REPEATABLE_READ,
+       PHP_PQTXN_SERIALIZABLE,
+} php_pqtxn_isolation_t;
 
+typedef struct php_pqtxn {
        zval *conn;
-} php_pqstm_object_t;
+       php_pqtxn_isolation_t isolation;
+       unsigned readonly:1;
+       unsigned deferrable:1;
+} php_pqtxn_t;
+
+typedef struct php_pqtxn_object {
+       zend_object zo;
+       php_pqtxn_t *txn;
+       HashTable *prophandler;
+} php_pqtxn_object_t;
 
 static HashTable php_pqconn_object_prophandlers;
 static HashTable php_pqres_object_prophandlers;
 static HashTable php_pqstm_object_prophandlers;
+static HashTable php_pqtxn_object_prophandlers;
 
 typedef void (*php_pq_object_prophandler_func_t)(zval *object, void *o, zval *return_value TSRMLS_DC);
 
@@ -310,6 +312,19 @@ static zend_object_iterator_funcs php_pqres_iterator_funcs = {
        NULL
 };
 
+static STATUS php_pqres_success(PGresult *res TSRMLS_DC)
+{
+       switch (PQresultStatus(res)) {
+       case PGRES_BAD_RESPONSE:
+       case PGRES_NONFATAL_ERROR:
+       case PGRES_FATAL_ERROR:
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PQresultErrorMessage(res));
+               return FAILURE;
+       default:
+               return SUCCESS;
+       }
+}
+
 static void php_pq_callback_dtor(php_pq_callback_t *cb) {
        if (cb->fci.size > 0) {
                zend_fcall_info_args_clear(&cb->fci, 1);
@@ -373,13 +388,25 @@ static void php_pqstm_object_free(void *o TSRMLS_DC)
 {
        php_pqstm_object_t *obj = o;
 
-       if (obj->name) {
-               efree(obj->name);
-               obj->name = NULL;
+       if (obj->stm) {
+               zval_ptr_dtor(&obj->stm->conn);
+               efree(obj->stm->name);
+               efree(obj->stm);
+               obj->stm = NULL;
        }
-       if (obj->conn) {
-               zval_ptr_dtor(&obj->conn);
-               obj->conn = NULL;
+       zend_object_std_dtor((zend_object *) o TSRMLS_CC);
+       efree(obj);
+}
+
+static void php_pqtxn_object_free(void *o TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+
+       if (obj->txn) {
+               zval_ptr_dtor(&obj->txn->conn);
+               obj->txn->conn = NULL;
+               efree(obj->txn);
+               obj->txn = NULL;
        }
        zend_object_std_dtor((zend_object *) o TSRMLS_CC);
        efree(obj);
@@ -435,7 +462,7 @@ static zend_object_value php_pqres_create_object_ex(zend_class_entry *ce, PGresu
        return ov;
 }
 
-static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, zval *conn, const char *name, php_pqstm_object_t **ptr TSRMLS_DC)
+static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, php_pqstm_t *stm, php_pqstm_object_t **ptr TSRMLS_DC)
 {
        zend_object_value ov;
        php_pqstm_object_t *o;
@@ -449,13 +476,8 @@ static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, zval *
                *ptr = o;
        }
 
-       if (conn) {
-               Z_ADDREF_P(conn);
-               o->conn = conn;
-       }
-
-       if (name) {
-               o->name = estrdup(name);
+       if (stm) {
+               o->stm = stm;
        }
 
        ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqstm_object_free, NULL TSRMLS_CC);
@@ -464,6 +486,30 @@ static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, zval *
        return ov;
 }
 
+static zend_object_value php_pqtxn_create_object_ex(zend_class_entry *ce, php_pqtxn_t *txn, php_pqtxn_object_t **ptr TSRMLS_DC)
+{
+       zend_object_value ov;
+       php_pqtxn_object_t *o;
+
+       o = ecalloc(1, sizeof(*o));
+       zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
+       object_properties_init((zend_object *) o, ce);
+       o->prophandler = &php_pqtxn_object_prophandlers;
+
+       if (ptr) {
+               *ptr = o;
+       }
+
+       if (txn) {
+               o->txn = txn;
+       }
+
+       ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtxn_object_free, NULL TSRMLS_CC);
+       ov.handlers = &php_pqtxn_object_handlers;
+
+       return ov;
+}
+
 static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC)
 {
        return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
@@ -476,7 +522,12 @@ static zend_object_value php_pqres_create_object(zend_class_entry *class_type TS
 
 static zend_object_value php_pqstm_create_object(zend_class_entry *class_type TSRMLS_DC)
 {
-       return php_pqstm_create_object_ex(class_type, NULL, NULL, NULL TSRMLS_CC);
+       return php_pqstm_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
+}
+
+static zend_object_value php_pqtxn_create_object(zend_class_entry *class_type TSRMLS_DC)
+{
+       return php_pqtxn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
 }
 
 static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
@@ -487,30 +538,31 @@ static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_has
        php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
 
        if (SUCCESS == zend_hash_find(ht, key->arKey, key->nKeyLength, (void *) &return_value)) {
+
                if (ph->read) {
+                       zval_ptr_dtor(return_value);
                        MAKE_STD_ZVAL(*return_value);
                        ZVAL_NULL(*return_value);
 
                        ph->read(object, obj, *return_value TSRMLS_CC);
-               } else {
-                       zval member;
-
-                       INIT_PZVAL(&member);
-                       ZVAL_STRINGL(&member, key->arKey, key->nKeyLength-1, 0);
-                       *return_value = zend_get_std_object_handlers()->read_property(object, &member, BP_VAR_R, NULL TSRMLS_CC);
-                       Z_ADDREF_PP(return_value);
                }
        }
 
        return ZEND_HASH_APPLY_KEEP;
 }
 
-static int apply_pi_to_debug(void *p, void *arg TSRMLS_DC)
+static int apply_pi_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
 {
        zend_property_info *pi = p;
-       HashTable *ht = arg;
+       HashTable *ht = va_arg(argv, HashTable *);
+       zval *object = va_arg(argv, zval *);
+       php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
+       zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC);
 
-       zend_hash_add_empty_element(ht, pi->name, pi->name_length + 1);
+       if (!Z_REFCOUNT_P(property)) {
+               Z_ADDREF_P(property);
+       }
+       zend_hash_add(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
 
        return ZEND_HASH_APPLY_KEEP;
 }
@@ -524,7 +576,7 @@ static HashTable *php_pq_object_debug_info(zval *object, int *temp TSRMLS_DC)
        ALLOC_HASHTABLE(ht);
        ZEND_INIT_SYMTABLE(ht);
 
-       zend_hash_apply_with_argument(&obj->zo.ce->properties_info, apply_pi_to_debug, ht TSRMLS_CC);
+       zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_debug, 3, ht, object, obj);
        zend_hash_apply_with_arguments(obj->prophandler TSRMLS_CC, apply_ph_to_debug, 3, ht, object, obj);
 
        return ht;
@@ -757,14 +809,116 @@ static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value
 {
        php_pqstm_object_t *obj = o;
 
-       RETVAL_STRING(obj->name, 1);
+       RETVAL_STRING(obj->stm->name, 1);
 }
 
 static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
 {
        php_pqstm_object_t *obj = o;
 
-       RETVAL_ZVAL(obj->conn, 1, 0);
+       RETVAL_ZVAL(obj->stm->conn, 1, 0);
+}
+
+static void php_pqtxn_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+
+       RETVAL_ZVAL(obj->txn->conn, 1, 0);
+}
+
+static void php_pqtxn_object_read_isolation(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+
+       RETVAL_LONG(obj->txn->isolation);
+}
+
+static void php_pqtxn_object_read_readonly(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+
+       RETVAL_LONG(obj->txn->readonly);
+}
+
+static void php_pqtxn_object_read_deferrable(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+
+       RETVAL_LONG(obj->txn->deferrable);
+}
+
+static void php_pqtxn_object_write_isolation(zval *object, void *o, zval *value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+       php_pqtxn_isolation_t orig = obj->txn->isolation;
+       zval *zisolation = value;
+       PGresult *res;
+
+       if (Z_TYPE_P(zisolation) != IS_LONG) {
+               convert_to_long_ex(&zisolation);
+       }
+
+       switch ((obj->txn->isolation = Z_LVAL_P(zisolation))) {
+       case PHP_PQTXN_READ_COMMITTED:
+               res = PQexec(conn_obj->conn, "SET TRANSACTION READ COMMITED");
+               break;
+       case PHP_PQTXN_REPEATABLE_READ:
+               res = PQexec(conn_obj->conn, "SET TRANSACTION REPEATABLE READ");
+               break;
+       case PHP_PQTXN_SERIALIZABLE:
+               res = PQexec(conn_obj->conn, "SET TRANSACTION SERIALIZABLE");
+               break;
+       default:
+               obj->txn->isolation = orig;
+               res = NULL;
+               break;
+       }
+
+       if (zisolation != value) {
+               zval_ptr_dtor(&zisolation);
+       }
+
+       if (res) {
+               php_pqres_success(res TSRMLS_CC);
+               PQclear(res);
+       }
+}
+
+static void php_pqtxn_object_write_readonly(zval *object, void *o, zval *value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+       PGresult *res;
+
+       if ((obj->txn->readonly = zend_is_true(value))) {
+               res = PQexec(conn_obj->conn, "SET TRANSACTION READ ONLY");
+       } else {
+               res = PQexec(conn_obj->conn, "SET TRANSACTION READ WRITE");
+       }
+
+       if (res) {
+               php_pqres_success(res TSRMLS_CC);
+               PQclear(res);
+       }
+}
+
+static void php_pqtxn_object_write_deferrable(zval *object, void *o, zval *value TSRMLS_DC)
+{
+       php_pqtxn_object_t *obj = o;
+       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+       PGresult *res;
+
+       if ((obj->txn->deferrable = zend_is_true(value))) {
+               res = PQexec(conn_obj->conn, "SET TRANSACTION DEFERRABLE");
+       } else {
+               res = PQexec(conn_obj->conn, "SET TRANSACTION NOT DEFERRABLE");
+       }
+
+       if (res) {
+               php_pqres_success(res TSRMLS_CC);
+               PQclear(res);
+       }
 }
 
 static zend_class_entry *ancestor(zend_class_entry *ce) {
@@ -910,6 +1064,17 @@ static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *
        return data;
 }
 
+static void php_pqconn_notice_recv(void *o, const PGresult *res)
+{
+       php_pqconn_object_t *obj = o;
+       zval *notice;
+
+       return;
+
+       MAKE_STD_ZVAL(notice);
+       ZVAL_STRING(notice, PQerrorMessage(res), 1);
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
        ZEND_ARG_INFO(0, dsn)
        ZEND_ARG_INFO(0, async)
@@ -934,7 +1099,7 @@ static PHP_METHOD(pqconn, __construct) {
                } else {
                        obj->conn = PQconnectdb(dsn_str);
                }
-               
+               PQsetNoticeReceiver(obj->conn, php_pqconn_notice_recv, obj);
                PQregisterEventProc(obj->conn, php_pqconn_event, "ext-pq", data);
                if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed: %s", PQerrorMessage(obj->conn));
@@ -997,19 +1162,6 @@ static void php_pqconn_add_listener(php_pqconn_object_t *obj, const char *channe
        }
 }
 
-static STATUS php_pqres_success(PGresult *res TSRMLS_DC)
-{
-       switch (PQresultStatus(res)) {
-       case PGRES_BAD_RESPONSE:
-       case PGRES_NONFATAL_ERROR:
-       case PGRES_FATAL_ERROR:
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PQresultErrorMessage(res));
-               return FAILURE;
-       default:
-               return SUCCESS;
-       }
-}
-
 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
        ZEND_ARG_INFO(0, channel)
        ZEND_ARG_INFO(0, callable)
@@ -1477,8 +1629,14 @@ static PHP_METHOD(pqconn, prepare) {
 
                if (obj->conn) {
                        if (SUCCESS == php_pqconn_prepare(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
+                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
+
+                               stm->conn = getThis();
+                               Z_ADDREF_P(stm->conn);
+                               stm->name = estrdup(name_str);
+
                                return_value->type = IS_OBJECT;
-                               return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, getThis(), name_str, NULL TSRMLS_CC);
+                               return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
                        }
                        php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
                } else {
@@ -1539,8 +1697,14 @@ static PHP_METHOD(pqconn, prepareAsync) {
                if (obj->conn) {
                        obj->poller = PQconsumeInput;
                        if (SUCCESS == php_pqconn_prepare_async(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
+                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
+
+                               stm->conn = getThis();
+                               Z_ADDREF_P(stm->conn);
+                               stm->name = estrdup(name_str);
+
                                return_value->type = IS_OBJECT;
-                               return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, getThis(), name_str, NULL TSRMLS_CC);
+                               return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
                        }
                        php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
                } else {
@@ -1660,6 +1824,146 @@ static PHP_METHOD(pqconn, unescapeBytea) {
        }
 }
 
+static const char *isolation_level(long *isolation) {
+       switch (*isolation) {
+       case PHP_PQTXN_SERIALIZABLE:
+               return "SERIALIZABLE";
+       case PHP_PQTXN_REPEATABLE_READ:
+               return "REPEATABLE READ";
+       default:
+               *isolation = PHP_PQTXN_READ_COMMITTED;
+               /* no break */
+       case PHP_PQTXN_READ_COMMITTED:
+               return "READ COMMITTED";
+       }
+}
+
+static STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
+{
+       if (!conn_obj) {
+               conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+       }
+
+       if (conn_obj->conn) {
+               PGresult *res;
+               char *cmd;
+
+               spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE",
+                               isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT");
+
+               res = PQexec(conn_obj->conn, cmd);
+
+               efree(cmd);
+
+               if (res) {
+                       STATUS rv = php_pqres_success(res TSRMLS_CC);
+
+                       PQclear(res);
+                       return rv;
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction: %s", PQerrorMessage(conn_obj->conn));
+                       return FAILURE;
+               }
+       } else {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+               return FAILURE;
+       }
+}
+
+static STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
+{
+       if (!conn_obj) {
+               conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+       }
+
+       if (conn_obj->conn) {
+               char *cmd;
+
+               spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE",
+                               isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT");
+
+               if (PQsendQuery(conn_obj->conn, cmd)) {
+                       conn_obj->poller = PQconsumeInput;
+                       efree(cmd);
+                       return SUCCESS;
+               } else {
+                       efree(cmd);
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction: %s", PQerrorMessage(conn_obj->conn));
+                       return FAILURE;
+               }
+       } else {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+               return FAILURE;
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
+       ZEND_ARG_INFO(0, isolation)
+       ZEND_ARG_INFO(0, readonly)
+       ZEND_ARG_INFO(0, deferrable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, startTransaction) {
+       zend_error_handling zeh;
+       long isolation = PHP_PQTXN_READ_COMMITTED;
+       zend_bool readonly = 0, deferrable = 0;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) {
+               STATUS rv;
+               php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
+
+               if (SUCCESS == rv) {
+                       php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
+
+                       txn->conn = getThis();
+                       Z_ADDREF_P(txn->conn);
+                       txn->isolation = isolation;
+                       txn->readonly = readonly;
+                       txn->deferrable = deferrable;
+
+                       return_value->type = IS_OBJECT;
+                       return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
+       ZEND_ARG_INFO(0, isolation)
+       ZEND_ARG_INFO(0, readonly)
+       ZEND_ARG_INFO(0, deferrable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, startTransactionAsync) {
+       zend_error_handling zeh;
+       long isolation = PHP_PQTXN_READ_COMMITTED;
+       zend_bool readonly = 0, deferrable = 0;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) {
+               STATUS rv;
+               php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
+
+               if (SUCCESS == rv) {
+                       php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
+
+                       txn->conn = getThis();
+                       Z_ADDREF_P(txn->conn);
+                       txn->isolation = isolation;
+                       txn->readonly = readonly;
+                       txn->deferrable = deferrable;
+
+                       return_value->type = IS_OBJECT;
+                       return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
 static zend_function_entry php_pqconn_methods[] = {
        PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
@@ -1678,6 +1982,8 @@ static zend_function_entry php_pqconn_methods[] = {
        PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
        PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
        PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
+       PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
+       PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
        {0}
 };
 
@@ -1798,20 +2104,22 @@ static PHP_METHOD(pqstm, __construct) {
                php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
 
                if (conn_obj->conn) {
+                       STATUS rv;
                        if (async) {
                                conn_obj->poller = PQconsumeInput;
-                               if (SUCCESS == php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
-                                       Z_ADDREF_P(zconn);
-                                       obj->conn = zconn;
-                                       obj->name = estrdup(name_str);
-                               }
+                               rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
                        } else {
-                               if (SUCCESS == php_pqconn_prepare(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
-                                       Z_ADDREF_P(zconn);
-                                       obj->conn = zconn;
-                                       obj->name = estrdup(name_str);
-                               }
-                               php_pqconn_notify_listeners(obj->conn, conn_obj TSRMLS_CC);
+                               rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
+                               php_pqconn_notify_listeners(zconn, conn_obj TSRMLS_CC);
+                       }
+
+                       if (SUCCESS == rv) {
+                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
+
+                               stm->conn = zconn;
+                               Z_ADDREF_P(stm->conn);
+                               stm->name = estrdup(name_str);
+                               obj->stm = stm;
                        }
                } else {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
@@ -1831,8 +2139,8 @@ static PHP_METHOD(pqstm, exec) {
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &zparams)) {
                php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-               if (obj->conn && obj->name) {
-                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->conn TSRMLS_CC);
+               if (obj->stm) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->stm->conn TSRMLS_CC);
 
                        if (conn_obj->conn) {
                                int count = 0;
@@ -1845,7 +2153,7 @@ static PHP_METHOD(pqstm, exec) {
                                        count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
                                }
 
-                               res = PQexecPrepared(conn_obj->conn, obj->name, count, (const char *const*) params, NULL, NULL, 0);
+                               res = PQexecPrepared(conn_obj->conn, obj->stm->name, count, (const char *const*) params, NULL, NULL, 0);
 
                                if (params) {
                                        efree(params);
@@ -1854,7 +2162,7 @@ static PHP_METHOD(pqstm, exec) {
                                        zend_hash_destroy(&zdtor);
                                }
 
-                               php_pqconn_notify_listeners(obj->conn, conn_obj TSRMLS_CC);
+                               php_pqconn_notify_listeners(obj->stm->conn, conn_obj TSRMLS_CC);
 
                                if (res) {
                                        if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
@@ -1887,8 +2195,8 @@ static PHP_METHOD(pqstm, execAsync) {
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!f", &zparams, &resolver.fci, &resolver.fcc)) {
                php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-               if (obj->conn && obj->name) {
-                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->conn TSRMLS_CC);
+               if (obj->stm) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->stm->conn TSRMLS_CC);
 
                        if (conn_obj->conn) {
                                int count;
@@ -1908,8 +2216,8 @@ static PHP_METHOD(pqstm, execAsync) {
 
                                conn_obj->poller = PQconsumeInput;
 
-                               if (PQsendQueryPrepared(conn_obj->conn, obj->name, count, (const char *const*) params, NULL, NULL, 0)) {
-                                       if (zend_is_true(zend_read_property(Z_OBJCE_P(obj->conn), obj->conn, ZEND_STRL("unbuffered"), 0 TSRMLS_CC))) {
+                               if (PQsendQueryPrepared(conn_obj->conn, obj->stm->name, count, (const char *const*) params, NULL, NULL, 0)) {
+                                       if (zend_is_true(zend_read_property(Z_OBJCE_P(obj->stm->conn), obj->stm->conn, ZEND_STRL("unbuffered"), 0 TSRMLS_CC))) {
                                                if (!PQsetSingleRowMode(conn_obj->conn)) {
                                                        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(conn_obj->conn));
                                                }
@@ -1927,7 +2235,7 @@ static PHP_METHOD(pqstm, execAsync) {
                                        zend_hash_destroy(&zdtor);
                                }
 
-                               php_pqconn_notify_listeners(obj->conn, conn_obj TSRMLS_CC);
+                               php_pqconn_notify_listeners(obj->stm->conn, conn_obj TSRMLS_CC);
 
                        } else {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
@@ -1950,13 +2258,13 @@ static PHP_METHOD(pqstm, desc) {
        if (SUCCESS == zend_parse_parameters_none()) {
                php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-               if (obj->conn && obj->name) {
-                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->conn TSRMLS_CC);
+               if (obj->stm) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->stm->conn TSRMLS_CC);
 
                        if (conn_obj->conn) {
-                               PGresult *res = PQdescribePrepared(conn_obj->conn, obj->name);
+                               PGresult *res = PQdescribePrepared(conn_obj->conn, obj->stm->name);
 
-                               php_pqconn_notify_listeners(obj->conn, conn_obj TSRMLS_CC);
+                               php_pqconn_notify_listeners(obj->stm->conn, conn_obj TSRMLS_CC);
 
                                if (res) {
                                        if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
@@ -1988,17 +2296,188 @@ static zend_function_entry php_pqstm_methods[] = {
        {0}
 };
 
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_construct, 0, 0, 1)
+       ZEND_ARG_INFO(0, connection)
+       ZEND_ARG_INFO(0, async)
+       ZEND_ARG_INFO(0, isolation)
+       ZEND_ARG_INFO(0, readonly)
+       ZEND_ARG_INFO(0, deferrable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, __construct) {
+       zend_error_handling zeh;
+       zval *zconn;
+       long isolation = PHP_PQTXN_READ_COMMITTED;
+       zend_bool async = 0, readonly = 0, deferrable = 0;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|blbb", &zconn, php_pqconn_class_entry, &async, &isolation, &readonly, &deferrable)) {
+               STATUS rv;
+               php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+
+               if (conn_obj->conn) {
+                       if (async) {
+                               rv = php_pqconn_start_transaction_async(zconn, conn_obj, isolation, readonly, deferrable TSRMLS_CC);
+                       } else {
+                               rv = php_pqconn_start_transaction(zconn, conn_obj, isolation, readonly, deferrable TSRMLS_CC);
+                       }
+
+                       if (SUCCESS == rv) {
+                               if (obj->txn) {
+                                       zval_ptr_dtor(&obj->txn->conn);
+                                       efree(obj->txn);
+                               }
+                               Z_ADDREF_P(zconn);
+                               obj->txn = ecalloc(1, sizeof(*obj->txn));
+                               obj->txn->conn = zconn;
+                               obj->txn->isolation = isolation;
+                               obj->txn->readonly = readonly;
+                               obj->txn->deferrable = deferrable;
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, commit) {
+       zend_error_handling zeh;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (obj->txn) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+
+                       if (conn_obj->conn) {
+                               PGresult *res = PQexec(conn_obj->conn, "COMMIT");
+
+                               if (res) {
+                                       php_pqres_success(res TSRMLS_CC);
+                                       PQclear(res);
+                               } else {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction: %s", PQerrorMessage(conn_obj->conn));
+                               }
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, commitAsync) {
+       zend_error_handling zeh;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (obj->txn) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+
+                       if (conn_obj->conn) {
+                               conn_obj->poller = PQconsumeInput;
+
+                               if (!PQsendQuery(conn_obj->conn, "COMMIT")) {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction: %s", PQerrorMessage(conn_obj->conn));
+                               }
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, rollback) {
+       zend_error_handling zeh;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (obj->txn) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+
+                       if (conn_obj->conn) {
+                               PGresult *res = PQexec(conn_obj->conn, "ROLLBACK");
+
+                               if (res) {
+                                       php_pqres_success(res TSRMLS_CC);
+                                       PQclear(res);
+                               } else {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction: %s", PQerrorMessage(conn_obj->conn));
+                               }
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, rollbackAsync) {
+       zend_error_handling zeh;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (obj->txn) {
+                       php_pqconn_object_t *conn_obj = zend_object_store_get_object(obj->txn->conn TSRMLS_CC);
+
+                       if (conn_obj->conn) {
+                               conn_obj->poller = PQconsumeInput;
+                               if (!PQsendQuery(conn_obj->conn, "REOLLBACK")) {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction: %s", PQerrorMessage(conn_obj->conn));
+                               }
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+               }
+       }
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+static zend_function_entry php_pqtxn_methods[] = {
+       PHP_ME(pqtxn, __construct, ai_pqtxn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_ME(pqtxn, commit, ai_pqtxn_commit, ZEND_ACC_PUBLIC)
+       PHP_ME(pqtxn, rollback, ai_pqtxn_rollback, ZEND_ACC_PUBLIC)
+       PHP_ME(pqtxn, commitAsync, ai_pqtxn_commit_async, ZEND_ACC_PUBLIC)
+       PHP_ME(pqtxn, rollbackAsync, ai_pqtxn_rollback_async, ZEND_ACC_PUBLIC)
+       {0}
+};
+
 /* {{{ PHP_MINIT_FUNCTION
  */
-PHP_MINIT_FUNCTION(pq)
+static PHP_MINIT_FUNCTION(pq)
 {
        zend_class_entry ce = {0};
        php_pq_object_prophandler_t ph = {0};
 
-       zend_hash_init(&php_pqconn_object_prophandlers, 1, NULL, NULL, 1);
        INIT_NS_CLASS_ENTRY(ce, "pq", "Connection", php_pqconn_methods);
        php_pqconn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
        php_pqconn_class_entry->create_object = php_pqconn_create_object;
+
        memcpy(&php_pqconn_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_pqconn_object_handlers.read_property = php_pq_object_read_prop;
        php_pqconn_object_handlers.write_property = php_pq_object_write_prop;
@@ -2006,6 +2485,8 @@ PHP_MINIT_FUNCTION(pq)
        php_pqconn_object_handlers.get_property_ptr_ptr = NULL;
        php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
 
+       zend_hash_init(&php_pqconn_object_prophandlers, 8, NULL, NULL, 1);
+
        zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqconn_object_read_status;
        zend_hash_add(&php_pqconn_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL);
@@ -2058,13 +2539,13 @@ PHP_MINIT_FUNCTION(pq)
        zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC);
        zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC);
 
-       zend_hash_init(&php_pqres_object_prophandlers, 1, NULL, NULL, 1);
        memset(&ce, 0, sizeof(ce));
        INIT_NS_CLASS_ENTRY(ce, "pq", "Result", php_pqres_methods);
        php_pqres_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
        php_pqres_class_entry->create_object = php_pqres_create_object;
        php_pqres_class_entry->iterator_funcs.funcs = &php_pqres_iterator_funcs;
        php_pqres_class_entry->get_iterator = php_pqres_iterator_init;
+       zend_class_implements(php_pqres_class_entry TSRMLS_CC, 1, zend_ce_traversable);
 
        memcpy(&php_pqres_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_pqres_object_handlers.read_property = php_pq_object_read_prop;
@@ -2073,6 +2554,8 @@ PHP_MINIT_FUNCTION(pq)
        php_pqres_object_handlers.get_property_ptr_ptr = NULL;
        php_pqres_object_handlers.get_debug_info = php_pq_object_debug_info;
 
+       zend_hash_init(&php_pqres_object_prophandlers, 6, NULL, NULL, 1);
+
        zend_declare_property_null(php_pqres_class_entry, ZEND_STRL("status"), ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqres_object_read_status;
        zend_hash_add(&php_pqres_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL);
@@ -2114,7 +2597,6 @@ PHP_MINIT_FUNCTION(pq)
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("FETCH_ASSOC"), PHP_PQRES_FETCH_ASSOC TSRMLS_CC);
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("FETCH_OBJECT"), PHP_PQRES_FETCH_OBJECT TSRMLS_CC);
 
-       zend_hash_init(&php_pqstm_object_prophandlers, 1, NULL, NULL, 1);
        memset(&ce, 0, sizeof(ce));
        INIT_NS_CLASS_ENTRY(ce, "pq", "Statement", php_pqstm_methods);
        php_pqstm_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
@@ -2127,6 +2609,8 @@ PHP_MINIT_FUNCTION(pq)
        php_pqstm_object_handlers.get_property_ptr_ptr = NULL;
        php_pqstm_object_handlers.get_debug_info = php_pq_object_debug_info;
 
+       zend_hash_init(&php_pqstm_object_prophandlers, 2, NULL, NULL, 1);
+
        zend_declare_property_null(php_pqstm_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqstm_object_read_name;
        zend_hash_add(&php_pqstm_object_prophandlers, "name", sizeof("name"), (void *) &ph, sizeof(ph), NULL);
@@ -2135,6 +2619,44 @@ PHP_MINIT_FUNCTION(pq)
        ph.read = php_pqstm_object_read_connection;
        zend_hash_add(&php_pqstm_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
 
+       memset(&ce, 0, sizeof(ce));
+       INIT_NS_CLASS_ENTRY(ce, "pq", "Transaction", php_pqtxn_methods);
+       php_pqtxn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
+       php_pqtxn_class_entry->create_object = php_pqtxn_create_object;
+
+       memcpy(&php_pqtxn_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+       php_pqtxn_object_handlers.read_property = php_pq_object_read_prop;
+       php_pqtxn_object_handlers.write_property = php_pq_object_write_prop;
+       php_pqtxn_object_handlers.clone_obj = NULL;
+       php_pqtxn_object_handlers.get_property_ptr_ptr = NULL;
+       php_pqtxn_object_handlers.get_debug_info = php_pq_object_debug_info;
+
+       zend_hash_init(&php_pqtxn_object_prophandlers, 4, NULL, NULL, 1);
+
+       zend_declare_property_null(php_pqtxn_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqtxn_object_read_connection;
+       zend_hash_add(&php_pqtxn_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
+
+       zend_declare_property_null(php_pqtxn_class_entry, ZEND_STRL("isolation"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqtxn_object_read_isolation;
+       ph.write = php_pqtxn_object_write_isolation;
+       zend_hash_add(&php_pqtxn_object_prophandlers, "isolation", sizeof("isolation"), (void *) &ph, sizeof(ph), NULL);
+
+       zend_declare_property_null(php_pqtxn_class_entry, ZEND_STRL("readonly"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqtxn_object_read_readonly;
+       ph.write = php_pqtxn_object_write_readonly;
+       zend_hash_add(&php_pqtxn_object_prophandlers, "readonly", sizeof("readonly"), (void *) &ph, sizeof(ph), NULL);
+
+       zend_declare_property_null(php_pqtxn_class_entry, ZEND_STRL("deferrable"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqtxn_object_read_deferrable;
+       ph.write = php_pqtxn_object_write_deferrable;
+       zend_hash_add(&php_pqtxn_object_prophandlers, "deferrable", sizeof("deferrable"), (void *) &ph, sizeof(ph), NULL);
+       ph.write = NULL;
+
+       zend_declare_class_constant_long(php_pqtxn_class_entry, ZEND_STRL("READ_COMMITTED"), PHP_PQTXN_READ_COMMITTED TSRMLS_CC);
+       zend_declare_class_constant_long(php_pqtxn_class_entry, ZEND_STRL("REPEATABLE READ"), PHP_PQTXN_REPEATABLE_READ TSRMLS_CC);
+       zend_declare_class_constant_long(php_pqtxn_class_entry, ZEND_STRL("SERIALIZABLE"), PHP_PQTXN_SERIALIZABLE TSRMLS_CC);
+
        /*
        REGISTER_INI_ENTRIES();
        */
@@ -2144,7 +2666,7 @@ PHP_MINIT_FUNCTION(pq)
 
 /* {{{ PHP_MSHUTDOWN_FUNCTION
  */
-PHP_MSHUTDOWN_FUNCTION(pq)
+static PHP_MSHUTDOWN_FUNCTION(pq)
 {
        /* uncomment this line if you have INI entries
        UNREGISTER_INI_ENTRIES();
@@ -2155,7 +2677,7 @@ PHP_MSHUTDOWN_FUNCTION(pq)
 
 /* {{{ PHP_MINFO_FUNCTION
  */
-PHP_MINFO_FUNCTION(pq)
+static PHP_MINFO_FUNCTION(pq)
 {
        php_info_print_table_start();
        php_info_print_table_header(2, "pq support", "enabled");
@@ -2167,6 +2689,29 @@ PHP_MINFO_FUNCTION(pq)
 }
 /* }}} */
 
+const zend_function_entry pq_functions[] = {
+       {0}
+};
+
+/* {{{ pq_module_entry
+ */
+zend_module_entry pq_module_entry = {
+       STANDARD_MODULE_HEADER,
+       "pq",
+       pq_functions,
+       PHP_MINIT(pq),
+       PHP_MSHUTDOWN(pq),
+       NULL,/*PHP_RINIT(pq),*/
+       NULL,/*PHP_RSHUTDOWN(pq),*/
+       PHP_MINFO(pq),
+       PHP_PQ_EXT_VERSION,
+       STANDARD_MODULE_PROPERTIES
+};
+/* }}} */
+
+#ifdef COMPILE_DL_PQ
+ZEND_GET_MODULE(pq)
+#endif
 
 
 /*
index f29a6c81f94712220ccb032ee7cecbcfbfb807f1..702d0992c895c95a6caeea1bf9304fae8f7c6aea 100644 (file)
@@ -32,13 +32,6 @@ zend_module_entry pq_module_entry;
 #      include "TSRM.h"
 #endif
 
-PHP_MINIT_FUNCTION(pq);
-PHP_MSHUTDOWN_FUNCTION(pq);
-PHP_RINIT_FUNCTION(pq);
-PHP_RSHUTDOWN_FUNCTION(pq);
-PHP_MINFO_FUNCTION(pq);
-
-
 /* 
 ZEND_BEGIN_MODULE_GLOBALS(pq)
        long  global_value;
diff --git a/tests/trans001.phpt b/tests/trans001.phpt
new file mode 100644 (file)
index 0000000..02b8c17
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+transaction
+--SKIPIF--
+<?php include "_skipif.inc"; ?>
+--FILE--
+<?php
+echo "Test\n";
+
+include "_setup.inc";
+
+$c = new pq\Connection(PQ_DSN);
+$t = new pq\Transaction($c);
+$c->exec("CREATE TABLE test (id serial, data text)");
+$s = $c->prepare("test_insert", "INSERT INTO test (data) VALUES (\$1)", array($c->types->byName->text->oid));
+$s->exec(array("a"));
+$s->exec(array("b"));
+$s->exec(array("c"));
+$r = $c->exec("SELECT * FROM test");
+while ($row = $r->fetchRow(pq\Result::FETCH_OBJECT)) {
+       printf("%d => %s\n", $row->id, $row->data);
+}
+$t->rollback();
+?>
+DONE
+--EXPECT--
+Test
+1 => a
+2 => b
+3 => c
+DONE