Skip to content

Commit

Permalink
Fix bug #81227: PDO::inTransaction reports false when in transaction (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed May 20, 2024
1 parent 9aa3a0d commit b7bf846
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ PHP NEWS

- PDO_SQLITE:
. Added class PdoSqlite. (danack, kocsismate)
. Fixed bug #81227 (PDO::inTransaction reports false when in transaction).
(nielsdos)

- PGSQL:
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
Expand Down
11 changes: 10 additions & 1 deletion ext/pdo_sqlite/sqlite_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ static int pdo_sqlite_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return
return 1;
}

static bool pdo_sqlite_in_transaction(pdo_dbh_t *dbh)
{
pdo_sqlite_db_handle* H = (pdo_sqlite_db_handle*) dbh->driver_data;
/* It's not possible in sqlite3 to explicitly turn autocommit off other
* than manually starting a transaction. Manual transactions always are
* the mode of operation when autocommit is off. */
return H->db && sqlite3_get_autocommit(H->db) == 0;
}

static bool pdo_sqlite_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
{
pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
Expand Down Expand Up @@ -733,7 +742,7 @@ static const struct pdo_dbh_methods sqlite_methods = {
NULL, /* check_liveness: not needed */
get_driver_methods,
pdo_sqlite_request_shutdown,
NULL, /* in transaction, use PDO's internal tracking mechanism */
pdo_sqlite_in_transaction,
pdo_sqlite_get_gc
};

Expand Down
30 changes: 30 additions & 0 deletions ext/pdo_sqlite/tests/bug81227.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Bug #81227 (PDO::inTransaction reports false when in transaction)
--EXTENSIONS--
pdo_sqlite
--FILE--
<?php
$db = new PDO("sqlite::memory:");
var_dump($db->inTransaction());

$db->exec("BEGIN EXCLUSIVE TRANSACTION");
var_dump($db->inTransaction());

try {
$db->beginTransaction();
} catch (PDOException $e) {
echo $e->getMessage(), "\n";
}

$db->commit();
var_dump($db->inTransaction());

$db->beginTransaction();
var_dump($db->inTransaction());
?>
--EXPECT--
bool(false)
bool(true)
There is already an active transaction
bool(false)
bool(true)

0 comments on commit b7bf846

Please sign in to comment.