Skip to content

Commit 6a4eeb1

Browse files
twosenikic
authored andcommitted
Improve PDO::inTransaction() support for MySQL
Closes GH-4996.
1 parent 821f6bb commit 6a4eeb1

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

ext/pdo_mysql/mysql_driver.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,21 @@ static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
532532
}
533533
/* }}} */
534534

535+
#ifdef PDO_USE_MYSQLND
536+
# define pdo_mysql_get_server_status(m) mysqlnd_get_server_status(m)
537+
#else
538+
# define pdo_mysql_get_server_status(m) (m)->server_status
539+
#endif
540+
541+
/* {{{ pdo_mysql_in_transaction */
542+
static int pdo_mysql_in_transaction(pdo_dbh_t *dbh)
543+
{
544+
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
545+
PDO_DBG_ENTER("pdo_mysql_in_transaction");
546+
PDO_DBG_RETURN((pdo_mysql_get_server_status(H->server) & SERVER_STATUS_IN_TRANS) != 0);
547+
}
548+
/* }}} */
549+
535550
/* {{{ mysql_methods */
536551
static const struct pdo_dbh_methods mysql_methods = {
537552
mysql_handle_closer,
@@ -548,7 +563,7 @@ static const struct pdo_dbh_methods mysql_methods = {
548563
pdo_mysql_check_liveness,
549564
NULL,
550565
pdo_mysql_request_shutdown,
551-
NULL
566+
pdo_mysql_in_transaction
552567
};
553568
/* }}} */
554569

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
MySQL PDO class inTransaction
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
?>
9+
--FILE--
10+
<?php
11+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
12+
13+
const BEGIN = ['BEGIN', 'START TRANSACTION'];
14+
const END = ['COMMIT', 'ROLLBACK'];
15+
16+
$db = MySQLPDOTest::factory();
17+
// $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // mysql does not support
18+
for ($b = 0; $b < count(BEGIN); $b++) {
19+
for ($e = 0; $e < count(END); $e++) {
20+
foreach (['exec', 'query', 'execute'] as $w) {
21+
foreach ([BEGIN[$b], END[$e]] as $command) {
22+
switch ($w) {
23+
case 'exec':
24+
$db->exec($command);
25+
break;
26+
case'query':
27+
$db->query($command)->execute();
28+
break;
29+
case 'execute':
30+
/* EMULATE_PREPARES = QUERY */
31+
$db->prepare($command)->execute();
32+
break;
33+
default:
34+
assert(0);
35+
}
36+
var_dump($db->inTransaction());
37+
}
38+
}
39+
}
40+
}
41+
42+
?>
43+
--EXPECT--
44+
bool(true)
45+
bool(false)
46+
bool(true)
47+
bool(false)
48+
bool(true)
49+
bool(false)
50+
bool(true)
51+
bool(false)
52+
bool(true)
53+
bool(false)
54+
bool(true)
55+
bool(false)
56+
bool(true)
57+
bool(false)
58+
bool(true)
59+
bool(false)
60+
bool(true)
61+
bool(false)
62+
bool(true)
63+
bool(false)
64+
bool(true)
65+
bool(false)
66+
bool(true)
67+
bool(false)

0 commit comments

Comments
 (0)