Skip to content

Commit d04adf6

Browse files
committed
Boolify PDO's transaction handlers
This includes begin(), commit(), rollBack(), and inTransaction()
1 parent 6728c1b commit d04adf6

File tree

9 files changed

+80
-78
lines changed

9 files changed

+80
-78
lines changed

ext/pdo/pdo_dbh.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ PHP_METHOD(PDO, prepare)
578578
/* }}} */
579579

580580

581-
static zend_bool pdo_is_in_transaction(pdo_dbh_t *dbh) {
581+
static bool pdo_is_in_transaction(pdo_dbh_t *dbh) {
582582
if (dbh->methods->in_transaction) {
583583
return dbh->methods->in_transaction(dbh);
584584
}
@@ -606,7 +606,7 @@ PHP_METHOD(PDO, beginTransaction)
606606
}
607607

608608
if (dbh->methods->begin(dbh)) {
609-
dbh->in_txn = 1;
609+
dbh->in_txn = true;
610610
RETURN_TRUE;
611611
}
612612

@@ -630,7 +630,7 @@ PHP_METHOD(PDO, commit)
630630
}
631631

632632
if (dbh->methods->commit(dbh)) {
633-
dbh->in_txn = 0;
633+
dbh->in_txn = false;
634634
RETURN_TRUE;
635635
}
636636

@@ -654,7 +654,7 @@ PHP_METHOD(PDO, rollBack)
654654
}
655655

656656
if (dbh->methods->rollback(dbh)) {
657-
dbh->in_txn = 0;
657+
dbh->in_txn = false;
658658
RETURN_TRUE;
659659
}
660660

@@ -1462,7 +1462,7 @@ static void pdo_dbh_free_storage(zend_object *std)
14621462
pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(std);
14631463
if (dbh->in_txn && dbh->methods && dbh->methods->rollback) {
14641464
dbh->methods->rollback(dbh);
1465-
dbh->in_txn = 0;
1465+
dbh->in_txn = false;
14661466
}
14671467

14681468
if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {

ext/pdo/php_pdo_driver.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql
237237
/* quote a string */
238238
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);
239239

240-
/* transaction related */
241-
typedef int (*pdo_dbh_txn_func)(pdo_dbh_t *dbh);
240+
/* transaction related (beingTransaction(), commit, rollBack, inTransaction)
241+
* Return true if currently inside a transaction, false otherwise. */
242+
typedef bool (*pdo_dbh_txn_func)(pdo_dbh_t *dbh);
242243

243244
/* setting of attributes */
244245
typedef int (*pdo_dbh_set_attr_func)(pdo_dbh_t *dbh, zend_long attr, zval *val);
@@ -305,6 +306,7 @@ struct pdo_dbh_methods {
305306
pdo_dbh_check_liveness_func check_liveness;
306307
pdo_dbh_get_driver_methods_func get_driver_methods;
307308
pdo_dbh_request_shutdown persistent_shutdown;
309+
/* if defined to NULL, PDO will use its internal transaction tracking state */
308310
pdo_dbh_txn_func in_transaction;
309311
pdo_dbh_get_gc_func get_gc;
310312
};
@@ -446,7 +448,7 @@ struct _pdo_dbh_t {
446448
unsigned alloc_own_columns:1;
447449

448450
/* if true, commit or rollBack is allowed to be called */
449-
unsigned in_txn:1;
451+
bool in_txn:1;
450452

451453
/* max length a single character can become after correct quoting */
452454
unsigned max_escaped_char_length:3;

ext/pdo_dblib/dblib_driver.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,32 @@ static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unqu
195195
return 1;
196196
}
197197

198-
static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
198+
static bool pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
199199
{
200200
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
201201

202202
if (FAIL == dbcmd(H->link, cmd)) {
203-
return 0;
203+
return false;
204204
}
205205

206206
if (FAIL == dbsqlexec(H->link)) {
207-
return 0;
207+
return false;
208208
}
209209

210-
return 1;
210+
return true;
211211
}
212212

213-
static int dblib_handle_begin(pdo_dbh_t *dbh)
213+
static bool dblib_handle_begin(pdo_dbh_t *dbh)
214214
{
215215
return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
216216
}
217217

218-
static int dblib_handle_commit(pdo_dbh_t *dbh)
218+
static bool dblib_handle_commit(pdo_dbh_t *dbh)
219219
{
220220
return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
221221
}
222222

223-
static int dblib_handle_rollback(pdo_dbh_t *dbh)
223+
static bool dblib_handle_rollback(pdo_dbh_t *dbh)
224224
{
225225
return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
226226
}
@@ -417,7 +417,7 @@ static const struct pdo_dbh_methods dblib_methods = {
417417
NULL, /* check liveness */
418418
NULL, /* get driver methods */
419419
NULL, /* request shutdown */
420-
NULL, /* in transaction */
420+
NULL, /* in transaction, use PDO's internal tracking mechanism */
421421
NULL /* get gc */
422422
};
423423

ext/pdo_firebird/firebird_driver.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int preprocess(const char* sql, int sql_len, char* sql_out, HashTable* named_par
389389
}
390390
strncpy(pname, start, l);
391391
pname[l] = '\0';
392-
392+
393393
if (named_params) {
394394
zval tmp;
395395
ZVAL_LONG(&tmp, pindex);
@@ -691,7 +691,7 @@ static int firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t u
691691
/* }}} */
692692

693693
/* called by PDO to start a transaction */
694-
static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
694+
static bool firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
695695
{
696696
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
697697
char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1;
@@ -737,35 +737,35 @@ static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
737737
#endif
738738
if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, (unsigned short)(ptpb-tpb), tpb)) {
739739
RECORD_ERROR(dbh);
740-
return 0;
740+
return false;
741741
}
742-
return 1;
742+
return true;
743743
}
744744
/* }}} */
745745

746746
/* called by PDO to commit a transaction */
747-
static int firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
747+
static bool firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
748748
{
749749
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
750750

751751
if (isc_commit_transaction(H->isc_status, &H->tr)) {
752752
RECORD_ERROR(dbh);
753-
return 0;
753+
return false;
754754
}
755-
return 1;
755+
return true;
756756
}
757757
/* }}} */
758758

759759
/* called by PDO to rollback a transaction */
760-
static int firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
760+
static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
761761
{
762762
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
763763

764764
if (isc_rollback_transaction(H->isc_status, &H->tr)) {
765765
RECORD_ERROR(dbh);
766-
return 0;
766+
return false;
767767
}
768-
return 1;
768+
return true;
769769
}
770770
/* }}} */
771771

@@ -789,7 +789,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s
789789
if (!firebird_handle_begin(dbh)) {
790790
return 0;
791791
}
792-
dbh->in_txn = 1;
792+
dbh->in_txn = true;
793793
}
794794

795795
/* allocate the statement */
@@ -804,7 +804,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s
804804
new_sql[0] = '\0';
805805
if (!preprocess(sql, sql_len, new_sql, named_params)) {
806806
strcpy(dbh->error_code, "07000");
807-
efree(new_sql);
807+
efree(new_sql);
808808
return 0;
809809
}
810810

@@ -843,7 +843,7 @@ static int firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *v
843843
if (!firebird_handle_commit(dbh)) {
844844
break;
845845
}
846-
dbh->in_txn = 0;
846+
dbh->in_txn = false;
847847
}
848848
}
849849
dbh->auto_commit = bval;
@@ -1008,7 +1008,7 @@ static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
10081008
NULL, /* check_liveness */
10091009
NULL, /* get driver methods */
10101010
NULL, /* request shutdown */
1011-
NULL, /* in transaction */
1011+
NULL, /* in transaction, use PDO's internal tracking mechanism */
10121012
NULL /* get gc */
10131013
};
10141014
/* }}} */

ext/pdo_mysql/mysql_driver.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unqu
341341
/* }}} */
342342

343343
/* {{{ mysql_handle_begin */
344-
static int mysql_handle_begin(pdo_dbh_t *dbh)
344+
static bool mysql_handle_begin(pdo_dbh_t *dbh)
345345
{
346346
PDO_DBG_ENTER("mysql_handle_quoter");
347347
PDO_DBG_INF_FMT("dbh=%p", dbh);
@@ -350,28 +350,28 @@ static int mysql_handle_begin(pdo_dbh_t *dbh)
350350
/* }}} */
351351

352352
/* {{{ mysql_handle_commit */
353-
static int mysql_handle_commit(pdo_dbh_t *dbh)
353+
static bool mysql_handle_commit(pdo_dbh_t *dbh)
354354
{
355355
PDO_DBG_ENTER("mysql_handle_commit");
356356
PDO_DBG_INF_FMT("dbh=%p", dbh);
357357
if (mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
358358
pdo_mysql_error(dbh);
359-
PDO_DBG_RETURN(0);
359+
PDO_DBG_RETURN(false);
360360
}
361-
PDO_DBG_RETURN(1);
361+
PDO_DBG_RETURN(true);
362362
}
363363
/* }}} */
364364

365365
/* {{{ mysql_handle_rollback */
366-
static int mysql_handle_rollback(pdo_dbh_t *dbh)
366+
static bool mysql_handle_rollback(pdo_dbh_t *dbh)
367367
{
368368
PDO_DBG_ENTER("mysql_handle_rollback");
369369
PDO_DBG_INF_FMT("dbh=%p", dbh);
370370
if (mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
371371
pdo_mysql_error(dbh);
372-
PDO_DBG_RETURN(0);
372+
PDO_DBG_RETURN(false);
373373
}
374-
PDO_DBG_RETURN(1);
374+
PDO_DBG_RETURN(true);
375375
}
376376
/* }}} */
377377

@@ -556,7 +556,7 @@ static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
556556
#endif
557557

558558
/* {{{ pdo_mysql_in_transaction */
559-
static int pdo_mysql_in_transaction(pdo_dbh_t *dbh)
559+
static bool pdo_mysql_in_transaction(pdo_dbh_t *dbh)
560560
{
561561
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
562562
PDO_DBG_ENTER("pdo_mysql_in_transaction");

ext/pdo_oci/oci_driver.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -391,38 +391,38 @@ static int oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquot
391391
}
392392
/* }}} */
393393

394-
static int oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */
394+
static bool oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */
395395
{
396396
/* with Oracle, there is nothing special to be done */
397-
return 1;
397+
return true;
398398
}
399399
/* }}} */
400400

401-
static int oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */
401+
static bool oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */
402402
{
403403
pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
404404

405405
H->last_err = OCITransCommit(H->svc, H->err, 0);
406406

407407
if (H->last_err) {
408408
H->last_err = oci_drv_error("OCITransCommit");
409-
return 0;
409+
return false;
410410
}
411-
return 1;
411+
return true;
412412
}
413413
/* }}} */
414414

415-
static int oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
415+
static bool oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
416416
{
417417
pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
418418

419419
H->last_err = OCITransRollback(H->svc, H->err, 0);
420420

421421
if (H->last_err) {
422422
H->last_err = oci_drv_error("OCITransRollback");
423-
return 0;
423+
return false;
424424
}
425-
return 1;
425+
return true;
426426
}
427427
/* }}} */
428428

@@ -442,7 +442,7 @@ static int oci_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /
442442
H->last_err = oci_drv_error("OCITransCommit");
443443
return 0;
444444
}
445-
dbh->in_txn = 0;
445+
dbh->in_txn = false;
446446
}
447447

448448
dbh->auto_commit = (unsigned int)lval? 1 : 0;
@@ -705,7 +705,7 @@ static const struct pdo_dbh_methods oci_methods = {
705705
pdo_oci_check_liveness, /* check_liveness */
706706
NULL, /* get_driver_methods */
707707
NULL, /* request_shutdown */
708-
NULL, /* in_transaction */
708+
NULL, /* in transaction, use PDO's internal tracking mechanism */
709709
NULL /* get_gc */
710710
};
711711

0 commit comments

Comments
 (0)