diff options
author | George Peter Banyard <girgias@php.net> | 2020-12-23 02:17:46 +0100 |
---|---|---|
committer | George Peter Banyard <girgias@php.net> | 2021-01-06 10:20:57 +0000 |
commit | d04adf60bee30a167845644c3d480583ca755420 (patch) | |
tree | 531f72ea2f7b6b5a1db76431b2bd6eef9d89268b | |
parent | 6728c1bd72f2ddb46528a0c61ac833b1036a12a2 (diff) | |
download | php-git-d04adf60bee30a167845644c3d480583ca755420.tar.gz |
Boolify PDO's transaction handlers
This includes begin(), commit(), rollBack(), and inTransaction()
-rw-r--r-- | ext/pdo/pdo_dbh.c | 10 | ||||
-rw-r--r-- | ext/pdo/php_pdo_driver.h | 8 | ||||
-rw-r--r-- | ext/pdo_dblib/dblib_driver.c | 16 | ||||
-rw-r--r-- | ext/pdo_firebird/firebird_driver.c | 28 | ||||
-rw-r--r-- | ext/pdo_mysql/mysql_driver.c | 16 | ||||
-rw-r--r-- | ext/pdo_oci/oci_driver.c | 20 | ||||
-rw-r--r-- | ext/pdo_odbc/odbc_driver.c | 24 | ||||
-rw-r--r-- | ext/pdo_pgsql/pgsql_driver.c | 16 | ||||
-rw-r--r-- | ext/pdo_sqlite/sqlite_driver.c | 20 |
9 files changed, 80 insertions, 78 deletions
diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 8f840060e5..e64c151f85 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -578,7 +578,7 @@ PHP_METHOD(PDO, prepare) /* }}} */ -static zend_bool pdo_is_in_transaction(pdo_dbh_t *dbh) { +static bool pdo_is_in_transaction(pdo_dbh_t *dbh) { if (dbh->methods->in_transaction) { return dbh->methods->in_transaction(dbh); } @@ -606,7 +606,7 @@ PHP_METHOD(PDO, beginTransaction) } if (dbh->methods->begin(dbh)) { - dbh->in_txn = 1; + dbh->in_txn = true; RETURN_TRUE; } @@ -630,7 +630,7 @@ PHP_METHOD(PDO, commit) } if (dbh->methods->commit(dbh)) { - dbh->in_txn = 0; + dbh->in_txn = false; RETURN_TRUE; } @@ -654,7 +654,7 @@ PHP_METHOD(PDO, rollBack) } if (dbh->methods->rollback(dbh)) { - dbh->in_txn = 0; + dbh->in_txn = false; RETURN_TRUE; } @@ -1462,7 +1462,7 @@ static void pdo_dbh_free_storage(zend_object *std) pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(std); if (dbh->in_txn && dbh->methods && dbh->methods->rollback) { dbh->methods->rollback(dbh); - dbh->in_txn = 0; + dbh->in_txn = false; } if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) { diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index a8478cb3a6..33d8cbef6d 100644 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -237,8 +237,9 @@ typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql /* quote a string */ typedef int (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype); -/* transaction related */ -typedef int (*pdo_dbh_txn_func)(pdo_dbh_t *dbh); +/* transaction related (beingTransaction(), commit, rollBack, inTransaction) + * Return true if currently inside a transaction, false otherwise. */ +typedef bool (*pdo_dbh_txn_func)(pdo_dbh_t *dbh); /* setting of attributes */ typedef int (*pdo_dbh_set_attr_func)(pdo_dbh_t *dbh, zend_long attr, zval *val); @@ -305,6 +306,7 @@ struct pdo_dbh_methods { pdo_dbh_check_liveness_func check_liveness; pdo_dbh_get_driver_methods_func get_driver_methods; pdo_dbh_request_shutdown persistent_shutdown; + /* if defined to NULL, PDO will use its internal transaction tracking state */ pdo_dbh_txn_func in_transaction; pdo_dbh_get_gc_func get_gc; }; @@ -446,7 +448,7 @@ struct _pdo_dbh_t { unsigned alloc_own_columns:1; /* if true, commit or rollBack is allowed to be called */ - unsigned in_txn:1; + bool in_txn:1; /* max length a single character can become after correct quoting */ unsigned max_escaped_char_length:3; diff --git a/ext/pdo_dblib/dblib_driver.c b/ext/pdo_dblib/dblib_driver.c index 085b6990d2..ecb92fe2c8 100644 --- a/ext/pdo_dblib/dblib_driver.c +++ b/ext/pdo_dblib/dblib_driver.c @@ -195,32 +195,32 @@ static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unqu return 1; } -static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh) +static bool pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh) { pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data; if (FAIL == dbcmd(H->link, cmd)) { - return 0; + return false; } if (FAIL == dbsqlexec(H->link)) { - return 0; + return false; } - return 1; + return true; } -static int dblib_handle_begin(pdo_dbh_t *dbh) +static bool dblib_handle_begin(pdo_dbh_t *dbh) { return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh); } -static int dblib_handle_commit(pdo_dbh_t *dbh) +static bool dblib_handle_commit(pdo_dbh_t *dbh) { return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh); } -static int dblib_handle_rollback(pdo_dbh_t *dbh) +static bool dblib_handle_rollback(pdo_dbh_t *dbh) { return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh); } @@ -417,7 +417,7 @@ static const struct pdo_dbh_methods dblib_methods = { NULL, /* check liveness */ NULL, /* get driver methods */ NULL, /* request shutdown */ - NULL, /* in transaction */ + NULL, /* in transaction, use PDO's internal tracking mechanism */ NULL /* get gc */ }; diff --git a/ext/pdo_firebird/firebird_driver.c b/ext/pdo_firebird/firebird_driver.c index 54aaefed6c..39c6d4f877 100644 --- a/ext/pdo_firebird/firebird_driver.c +++ b/ext/pdo_firebird/firebird_driver.c @@ -389,7 +389,7 @@ int preprocess(const char* sql, int sql_len, char* sql_out, HashTable* named_par } strncpy(pname, start, l); pname[l] = '\0'; - + if (named_params) { zval tmp; ZVAL_LONG(&tmp, pindex); @@ -691,7 +691,7 @@ static int firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t u /* }}} */ /* called by PDO to start a transaction */ -static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */ +static bool firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */ { pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data; char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1; @@ -737,35 +737,35 @@ static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */ #endif if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, (unsigned short)(ptpb-tpb), tpb)) { RECORD_ERROR(dbh); - return 0; + return false; } - return 1; + return true; } /* }}} */ /* called by PDO to commit a transaction */ -static int firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */ +static bool firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */ { pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data; if (isc_commit_transaction(H->isc_status, &H->tr)) { RECORD_ERROR(dbh); - return 0; + return false; } - return 1; + return true; } /* }}} */ /* called by PDO to rollback a transaction */ -static int firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */ +static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */ { pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data; if (isc_rollback_transaction(H->isc_status, &H->tr)) { RECORD_ERROR(dbh); - return 0; + return false; } - return 1; + return true; } /* }}} */ @@ -789,7 +789,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s if (!firebird_handle_begin(dbh)) { return 0; } - dbh->in_txn = 1; + dbh->in_txn = true; } /* allocate the statement */ @@ -804,7 +804,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s new_sql[0] = '\0'; if (!preprocess(sql, sql_len, new_sql, named_params)) { strcpy(dbh->error_code, "07000"); - efree(new_sql); + efree(new_sql); return 0; } @@ -843,7 +843,7 @@ static int firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *v if (!firebird_handle_commit(dbh)) { break; } - dbh->in_txn = 0; + dbh->in_txn = false; } } dbh->auto_commit = bval; @@ -1008,7 +1008,7 @@ static const struct pdo_dbh_methods firebird_methods = { /* {{{ */ NULL, /* check_liveness */ NULL, /* get driver methods */ NULL, /* request shutdown */ - NULL, /* in transaction */ + NULL, /* in transaction, use PDO's internal tracking mechanism */ NULL /* get gc */ }; /* }}} */ diff --git a/ext/pdo_mysql/mysql_driver.c b/ext/pdo_mysql/mysql_driver.c index 869f0ef441..54fa43700d 100644 --- a/ext/pdo_mysql/mysql_driver.c +++ b/ext/pdo_mysql/mysql_driver.c @@ -341,7 +341,7 @@ static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unqu /* }}} */ /* {{{ mysql_handle_begin */ -static int mysql_handle_begin(pdo_dbh_t *dbh) +static bool mysql_handle_begin(pdo_dbh_t *dbh) { PDO_DBG_ENTER("mysql_handle_quoter"); PDO_DBG_INF_FMT("dbh=%p", dbh); @@ -350,28 +350,28 @@ static int mysql_handle_begin(pdo_dbh_t *dbh) /* }}} */ /* {{{ mysql_handle_commit */ -static int mysql_handle_commit(pdo_dbh_t *dbh) +static bool mysql_handle_commit(pdo_dbh_t *dbh) { PDO_DBG_ENTER("mysql_handle_commit"); PDO_DBG_INF_FMT("dbh=%p", dbh); if (mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server)) { pdo_mysql_error(dbh); - PDO_DBG_RETURN(0); + PDO_DBG_RETURN(false); } - PDO_DBG_RETURN(1); + PDO_DBG_RETURN(true); } /* }}} */ /* {{{ mysql_handle_rollback */ -static int mysql_handle_rollback(pdo_dbh_t *dbh) +static bool mysql_handle_rollback(pdo_dbh_t *dbh) { PDO_DBG_ENTER("mysql_handle_rollback"); PDO_DBG_INF_FMT("dbh=%p", dbh); if (mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server)) { pdo_mysql_error(dbh); - PDO_DBG_RETURN(0); + PDO_DBG_RETURN(false); } - PDO_DBG_RETURN(1); + PDO_DBG_RETURN(true); } /* }}} */ @@ -556,7 +556,7 @@ static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh) #endif /* {{{ pdo_mysql_in_transaction */ -static int pdo_mysql_in_transaction(pdo_dbh_t *dbh) +static bool pdo_mysql_in_transaction(pdo_dbh_t *dbh) { pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data; PDO_DBG_ENTER("pdo_mysql_in_transaction"); diff --git a/ext/pdo_oci/oci_driver.c b/ext/pdo_oci/oci_driver.c index 9cf894e9f2..ac0e9d8926 100644 --- a/ext/pdo_oci/oci_driver.c +++ b/ext/pdo_oci/oci_driver.c @@ -391,14 +391,14 @@ static int oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquot } /* }}} */ -static int oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */ +static bool oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */ { /* with Oracle, there is nothing special to be done */ - return 1; + return true; } /* }}} */ -static int oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */ +static bool oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */ { pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data; @@ -406,13 +406,13 @@ static int oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */ if (H->last_err) { H->last_err = oci_drv_error("OCITransCommit"); - return 0; + return false; } - return 1; + return true; } /* }}} */ -static int oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */ +static bool oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */ { pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data; @@ -420,9 +420,9 @@ static int oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */ if (H->last_err) { H->last_err = oci_drv_error("OCITransRollback"); - return 0; + return false; } - return 1; + return true; } /* }}} */ @@ -442,7 +442,7 @@ static int oci_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) / H->last_err = oci_drv_error("OCITransCommit"); return 0; } - dbh->in_txn = 0; + dbh->in_txn = false; } dbh->auto_commit = (unsigned int)lval? 1 : 0; @@ -705,7 +705,7 @@ static const struct pdo_dbh_methods oci_methods = { pdo_oci_check_liveness, /* check_liveness */ NULL, /* get_driver_methods */ NULL, /* request_shutdown */ - NULL, /* in_transaction */ + NULL, /* in transaction, use PDO's internal tracking mechanism */ NULL /* get_gc */ }; diff --git a/ext/pdo_odbc/odbc_driver.c b/ext/pdo_odbc/odbc_driver.c index 123d86e2cf..732512eb58 100644 --- a/ext/pdo_odbc/odbc_driver.c +++ b/ext/pdo_odbc/odbc_driver.c @@ -266,7 +266,7 @@ static int odbc_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquo } */ -static int odbc_handle_begin(pdo_dbh_t *dbh) +static bool odbc_handle_begin(pdo_dbh_t *dbh) { if (dbh->auto_commit) { /* we need to disable auto-commit now, to be able to initiate a transaction */ @@ -276,13 +276,13 @@ static int odbc_handle_begin(pdo_dbh_t *dbh) rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_IS_INTEGER); if (rc != SQL_SUCCESS) { pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = OFF"); - return 0; + return false; } } - return 1; + return true; } -static int odbc_handle_commit(pdo_dbh_t *dbh) +static bool odbc_handle_commit(pdo_dbh_t *dbh) { pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data; RETCODE rc; @@ -293,7 +293,7 @@ static int odbc_handle_commit(pdo_dbh_t *dbh) pdo_odbc_drv_error("SQLEndTran: Commit"); if (rc != SQL_SUCCESS_WITH_INFO) { - return 0; + return false; } } @@ -302,13 +302,13 @@ static int odbc_handle_commit(pdo_dbh_t *dbh) rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER); if (rc != SQL_SUCCESS) { pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = ON"); - return 0; + return false; } } - return 1; + return true; } -static int odbc_handle_rollback(pdo_dbh_t *dbh) +static bool odbc_handle_rollback(pdo_dbh_t *dbh) { pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data; RETCODE rc; @@ -319,7 +319,7 @@ static int odbc_handle_rollback(pdo_dbh_t *dbh) pdo_odbc_drv_error("SQLEndTran: Rollback"); if (rc != SQL_SUCCESS_WITH_INFO) { - return 0; + return false; } } if (dbh->auto_commit && H->dbc) { @@ -327,11 +327,11 @@ static int odbc_handle_rollback(pdo_dbh_t *dbh) rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER); if (rc != SQL_SUCCESS) { pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = ON"); - return 0; + return false; } } - return 1; + return true; } static int odbc_handle_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val) @@ -386,7 +386,7 @@ static const struct pdo_dbh_methods odbc_methods = { NULL, /* check_liveness */ NULL, /* get_driver_methods */ NULL, /* request_shutdown */ - NULL, /* in_transaction */ + NULL, /* in transaction, use PDO's internal tracking mechanism */ NULL /* get_gc */ }; diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index 62ad40c256..78ed0c30c2 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -498,7 +498,7 @@ static zend_result pdo_pgsql_check_liveness(pdo_dbh_t *dbh) } /* }}} */ -static int pgsql_handle_in_transaction(pdo_dbh_t *dbh) +static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh) { pdo_pgsql_db_handle *H; @@ -507,31 +507,31 @@ static int pgsql_handle_in_transaction(pdo_dbh_t *dbh) return PQtransactionStatus(H->server) > PQTRANS_IDLE; } -static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh) +static bool pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh) { pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; PGresult *res; - int ret = 1; + bool ret = true; res = PQexec(H->server, cmd); if (PQresultStatus(res) != PGRES_COMMAND_OK) { pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res)); - ret = 0; + ret = false; } PQclear(res); return ret; } -static int pgsql_handle_begin(pdo_dbh_t *dbh) +static bool pgsql_handle_begin(pdo_dbh_t *dbh) { return pdo_pgsql_transaction_cmd("BEGIN", dbh); } -static int pgsql_handle_commit(pdo_dbh_t *dbh) +static bool pgsql_handle_commit(pdo_dbh_t *dbh) { - int ret = pdo_pgsql_transaction_cmd("COMMIT", dbh); + bool ret = pdo_pgsql_transaction_cmd("COMMIT", dbh); /* When deferred constraints are used the commit could fail, and a ROLLBACK implicitly ran. See bug #67462 */ @@ -542,7 +542,7 @@ static int pgsql_handle_commit(pdo_dbh_t *dbh) return ret; } -static int pgsql_handle_rollback(pdo_dbh_t *dbh) +static bool pgsql_handle_rollback(pdo_dbh_t *dbh) { return pdo_pgsql_transaction_cmd("ROLLBACK", dbh); } diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index fcf82c62bf..a7c9284dfc 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -238,7 +238,7 @@ static int sqlite_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unq return 1; } -static int sqlite_handle_begin(pdo_dbh_t *dbh) +static bool sqlite_handle_begin(pdo_dbh_t *dbh) { pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; char *errmsg = NULL; @@ -247,12 +247,12 @@ static int sqlite_handle_begin(pdo_dbh_t *dbh) pdo_sqlite_error(dbh); if (errmsg) sqlite3_free(errmsg); - return 0; + return false; } - return 1; + return true; } -static int sqlite_handle_commit(pdo_dbh_t *dbh) +static bool sqlite_handle_commit(pdo_dbh_t *dbh) { pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; char *errmsg = NULL; @@ -261,12 +261,12 @@ static int sqlite_handle_commit(pdo_dbh_t *dbh) pdo_sqlite_error(dbh); if (errmsg) sqlite3_free(errmsg); - return 0; + return false; } - return 1; + return true; } -static int sqlite_handle_rollback(pdo_dbh_t *dbh) +static bool sqlite_handle_rollback(pdo_dbh_t *dbh) { pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; char *errmsg = NULL; @@ -275,9 +275,9 @@ static int sqlite_handle_rollback(pdo_dbh_t *dbh) pdo_sqlite_error(dbh); if (errmsg) sqlite3_free(errmsg); - return 0; + return false; } - return 1; + return true; } static int pdo_sqlite_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value) @@ -729,7 +729,7 @@ static const struct pdo_dbh_methods sqlite_methods = { NULL, /* check_liveness: not needed */ get_driver_methods, pdo_sqlite_request_shutdown, - NULL, /* in_transaction */ + NULL, /* in transaction, use PDO's internal tracking mechanism */ pdo_sqlite_get_gc }; |