Skip to content

Commit 8445b31

Browse files
committed
Added transaction check process
fix macro fix report flags
1 parent 4c6dbe0 commit 8445b31

6 files changed

+43
-2
lines changed

ext/mysqli/mysqli_api.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ PHP_FUNCTION(mysqli_commit)
313313
}
314314
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
315315

316+
if (!MYSQLI_IS_IN_TRANSACTION(mysql)) {
317+
php_mysqli_report_error(NULL, 0, "There is no active transaction");
318+
RETURN_FALSE;
319+
}
320+
316321
if (FAIL == mysqlnd_commit(mysql->mysql, flags, name)) {
317322
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
318323
RETURN_FALSE;
@@ -523,12 +528,12 @@ PHP_FUNCTION(mysqli_execute_query)
523528

524529
if (FAIL == mysql_stmt_prepare(stmt->stmt, query, query_len)) {
525530
MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
526-
531+
527532
close_stmt_and_copy_errors(stmt, mysql);
528533
RETURN_FALSE;
529534
}
530535

531-
/* The bit below, which is copied from mysqli_prepare, is needed for bad index exceptions */
536+
/* The bit below, which is copied from mysqli_prepare, is needed for bad index exceptions */
532537
/* don't initialize stmt->query with NULL, we ecalloc()-ed the memory */
533538
/* Get performance boost if reporting is switched off */
534539
if (query_len && (MyG(report_mode) & MYSQLI_REPORT_INDEX)) {
@@ -1420,6 +1425,10 @@ PHP_FUNCTION(mysqli_rollback)
14201425
}
14211426
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
14221427

1428+
if (!MYSQLI_IS_IN_TRANSACTION(mysql)) {
1429+
php_mysqli_report_error(NULL, 0, "There is no active transaction");
1430+
RETURN_FALSE;
1431+
}
14231432

14241433
if (FAIL == mysqlnd_rollback(mysql->mysql, flags, name)) {
14251434
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);

ext/mysqli/mysqli_nonapi.c

+5
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,11 @@ PHP_FUNCTION(mysqli_begin_transaction)
10211021
RETURN_THROWS();
10221022
}
10231023

1024+
if (MYSQLI_IS_IN_TRANSACTION(mysql)) {
1025+
php_mysqli_report_error(NULL, 0, "There is already an active transaction");
1026+
RETURN_FALSE;
1027+
}
1028+
10241029
if (FAIL == mysqlnd_begin_transaction(mysql->mysql, flags, name)) {
10251030
RETURN_FALSE;
10261031
}

ext/mysqli/php_mysqli_structs.h

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * resul
234234
intern->ptr = NULL; \
235235
}
236236

237+
#define MYSQLI_IS_IN_TRANSACTION(__mysql) \
238+
((mysqli_server_status(__mysql->mysql) & SERVER_STATUS_IN_TRANS) != 0)
237239

238240
ZEND_BEGIN_MODULE_GLOBALS(mysqli)
239241
zend_long num_links;

ext/mysqli/tests/mysqli_begin_transaction.phpt

+9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ if (!have_innodb($link))
9393
printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
9494
}
9595

96+
try {
97+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false)
98+
mysqli_begin_transaction($link);
99+
mysqli_begin_transaction($link);
100+
} catch (\mysqli_sql_exception $e) {
101+
echo $e->getMessage() . \PHP_EOL;
102+
}
103+
96104
print "done!";
97105
?>
98106
--CLEAN--
@@ -102,4 +110,5 @@ if (!have_innodb($link))
102110
--EXPECT--
103111
NULL
104112
mysqli_begin_transaction(): Argument #2 ($flags) must be one of the MYSQLI_TRANS_* constants
113+
There is already an active transaction
105114
done!

ext/mysqli/tests/mysqli_commit.phpt

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ if (!have_innodb($link))
5656
echo $exception->getMessage() . "\n";
5757
}
5858

59+
try {
60+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false)
61+
mysqli_commit($link);
62+
} catch (\mysqli_sql_exception $e) {
63+
echo $e->getMessage() . \PHP_EOL;
64+
}
65+
5966
print "done!";
6067
?>
6168
--CLEAN--
@@ -64,4 +71,5 @@ require_once 'clean_table.inc';
6471
?>
6572
--EXPECT--
6673
mysqli object is already closed
74+
There is no active transaction
6775
done!

ext/mysqli/tests/mysqli_rollback.phpt

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ if (!have_innodb($link))
5353
echo $exception->getMessage() . "\n";
5454
}
5555

56+
try {
57+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false)
58+
mysqli_commit($link);
59+
} catch (\mysqli_sql_exception $e) {
60+
echo $e->getMessage() . \PHP_EOL;
61+
}
62+
5663
print "done!\n";
5764
?>
5865
--CLEAN--
@@ -61,4 +68,5 @@ require_once 'clean_table.inc';
6168
?>
6269
--EXPECT--
6370
mysqli object is already closed
71+
There is no active transaction
6472
done!

0 commit comments

Comments
 (0)