Skip to content

Commit eed8620

Browse files
committed
refactor
1 parent 98dc072 commit eed8620

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

ext/pdo_firebird/firebird_driver.c

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
#include "php_pdo_firebird.h"
3232
#include "php_pdo_firebird_int.h"
3333

34-
static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const zend_string*, XSQLDA*, isc_stmt_handle*,
35-
HashTable*);
34+
static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const zend_string*, XSQLDA*, isc_stmt_handle*, HashTable*);
35+
static bool _firebird_commit_transaction(pdo_dbh_t *dbh, bool retain);
36+
static bool _firebird_rollback_transaction(pdo_dbh_t *dbh, bool retain);
3637

3738
const char CHR_LETTER = 1;
3839
const char CHR_DIGIT = 2;
@@ -477,13 +478,9 @@ static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
477478

478479
if (H->tr) {
479480
if (dbh->auto_commit) {
480-
if (isc_commit_transaction(H->isc_status, &H->tr)) {
481-
RECORD_ERROR(dbh);
482-
}
481+
_firebird_commit_transaction(dbh, false);
483482
} else {
484-
if (isc_rollback_transaction(H->isc_status, &H->tr)) {
485-
RECORD_ERROR(dbh);
486-
}
483+
_firebird_rollback_transaction(dbh, false);
487484
}
488485
}
489486
H->in_manually_txn = 0;
@@ -649,9 +646,7 @@ static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /*
649646

650647
/* commit if we're in auto_commit mode */
651648
if (dbh->auto_commit && !H->in_manually_txn) {
652-
if (isc_commit_retaining(H->isc_status, &H->tr)) {
653-
RECORD_ERROR(dbh);
654-
}
649+
_firebird_commit_transaction(dbh, true);
655650
}
656651

657652
free_statement:
@@ -703,8 +698,8 @@ static zend_string* firebird_handle_quoter(pdo_dbh_t *dbh, const zend_string *un
703698
}
704699
/* }}} */
705700

706-
/* firebird_begin_transaction */
707-
static bool firebird_begin_transaction(pdo_dbh_t *dbh) /* {{{ */
701+
/* _firebird_begin_transaction */
702+
static bool _firebird_begin_transaction(pdo_dbh_t *dbh) /* {{{ */
708703
{
709704
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
710705
char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1;
@@ -757,16 +752,15 @@ static bool firebird_begin_transaction(pdo_dbh_t *dbh) /* {{{ */
757752
/* }}} */
758753

759754
/* called by PDO to start a transaction */
760-
static bool firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
755+
static bool firebird_handle_manually_begin(pdo_dbh_t *dbh) /* {{{ */
761756
{
762757
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
763758

764-
if (dbh->auto_commit && H->tr && isc_commit_transaction(H->isc_status, &H->tr)) {
765-
RECORD_ERROR(dbh);
759+
if (dbh->auto_commit && H->tr && !_firebird_commit_transaction(dbh, false)) {
766760
return false;
767761
}
768762

769-
if (!firebird_begin_transaction(dbh)) {
763+
if (!_firebird_begin_transaction(dbh)) {
770764
return false;
771765
}
772766

@@ -775,12 +769,12 @@ static bool firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
775769
}
776770
/* }}} */
777771

778-
/* called by PDO to commit a transaction */
779-
static bool firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
772+
/* _firebird_commit_transaction */
773+
static bool _firebird_commit_transaction(pdo_dbh_t *dbh, bool retain) /* {{{ */
780774
{
781775
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
782776

783-
if (dbh->auto_commit) {
777+
if (retain) {
784778
if (isc_commit_retaining(H->isc_status, &H->tr)) {
785779
RECORD_ERROR(dbh);
786780
return false;
@@ -791,17 +785,31 @@ static bool firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
791785
return false;
792786
}
793787
}
788+
789+
return true;
790+
}
791+
/* }}} */
792+
793+
/* called by PDO to commit a transaction */
794+
static bool firebird_handle_manually_commit(pdo_dbh_t *dbh) /* {{{ */
795+
{
796+
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
797+
798+
if (!_firebird_commit_transaction(dbh, dbh->auto_commit)) {
799+
return false;
800+
}
801+
794802
H->in_manually_txn = 0;
795803
return true;
796804
}
797805
/* }}} */
798806

799-
/* called by PDO to rollback a transaction */
800-
static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
807+
/* _firebird_rollback_transaction */
808+
static bool _firebird_rollback_transaction(pdo_dbh_t *dbh, bool retain) /* {{{ */
801809
{
802810
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
803811

804-
if (dbh->auto_commit) {
812+
if (retain) {
805813
if (isc_rollback_retaining(H->isc_status, &H->tr)) {
806814
RECORD_ERROR(dbh);
807815
return false;
@@ -812,6 +820,20 @@ static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
812820
return false;
813821
}
814822
}
823+
824+
return true;
825+
}
826+
/* }}} */
827+
828+
/* called by PDO to rollback a transaction */
829+
static bool firebird_handle_manually_rollback(pdo_dbh_t *dbh) /* {{{ */
830+
{
831+
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
832+
833+
if (!_firebird_rollback_transaction(dbh, dbh->auto_commit)) {
834+
return false;
835+
}
836+
815837
H->in_manually_txn = 0;
816838
return true;
817839
}
@@ -879,12 +901,12 @@ static bool firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *
879901
H->last_app_error = "Cannot enable auto-commit while a transaction is already open";
880902
return false;
881903
}
882-
if (!H->tr && !firebird_begin_transaction(dbh)) {
904+
if (!H->tr && !_firebird_begin_transaction(dbh)) {
883905
return false;
884906
}
885907
} else {
886908
/* close the transaction */
887-
if (H->tr && isc_commit_transaction(H->isc_status, &H->tr)) {
909+
if (H->tr && !_firebird_commit_transaction(dbh, false)) {
888910
return false;
889911
}
890912
H->in_manually_txn = 0;
@@ -1042,8 +1064,8 @@ static void pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval
10421064
}
10431065
/* }}} */
10441066

1045-
/* {{{ firebird_in_transaction */
1046-
static bool firebird_in_transaction(pdo_dbh_t *dbh)
1067+
/* {{{ firebird_in_manually_transaction */
1068+
static bool firebird_in_manually_transaction(pdo_dbh_t *dbh)
10471069
{
10481070
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
10491071
return H->tr && H->in_manually_txn;
@@ -1055,17 +1077,17 @@ static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
10551077
firebird_handle_preparer,
10561078
firebird_handle_doer,
10571079
firebird_handle_quoter,
1058-
firebird_handle_begin,
1059-
firebird_handle_commit,
1060-
firebird_handle_rollback,
1080+
firebird_handle_manually_begin,
1081+
firebird_handle_manually_commit,
1082+
firebird_handle_manually_rollback,
10611083
firebird_handle_set_attribute,
10621084
NULL, /* last_id not supported */
10631085
pdo_firebird_fetch_error_func,
10641086
firebird_handle_get_attribute,
10651087
NULL, /* check_liveness */
10661088
NULL, /* get driver methods */
10671089
NULL, /* request shutdown */
1068-
firebird_in_transaction,
1090+
firebird_in_manually_transaction,
10691091
NULL /* get gc */
10701092
};
10711093
/* }}} */
@@ -1148,7 +1170,7 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
11481170

11491171
H->in_manually_txn = 0;
11501172
if (dbh->auto_commit && !H->tr) {
1151-
ret = firebird_begin_transaction(dbh);
1173+
ret = _firebird_begin_transaction(dbh);
11521174
}
11531175

11541176
if (!ret) {

0 commit comments

Comments
 (0)