Skip to content

Commit 35fb0af

Browse files
committed
Added transaction check process
fix macro fix report flags fix tests
1 parent 1b846f8 commit 35fb0af

6 files changed

+48
-2
lines changed

ext/mysqli/mysqli_api.c

Lines changed: 11 additions & 2 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ if (!have_innodb($link))
9393
printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
9494
}
9595

96+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false);
97+
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
98+
try {
99+
mysqli_begin_transaction($link);
100+
mysqli_begin_transaction($link);
101+
} catch (\mysqli_sql_exception $e) {
102+
echo $e->getMessage() . \PHP_EOL;
103+
}
104+
105+
mysqli_rollback($link);
106+
96107
print "done!";
97108
?>
98109
--CLEAN--
@@ -102,4 +113,5 @@ if (!have_innodb($link))
102113
--EXPECT--
103114
NULL
104115
mysqli_begin_transaction(): Argument #2 ($flags) must be one of the MYSQLI_TRANS_* constants
116+
There is already an active transaction
105117
done!

ext/mysqli/tests/mysqli_commit.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ if (!have_innodb($link))
5656
echo $exception->getMessage() . "\n";
5757
}
5858

59+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false);
60+
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
61+
try {
62+
mysqli_commit($link);
63+
} catch (\mysqli_sql_exception $e) {
64+
echo $e->getMessage() . \PHP_EOL;
65+
}
66+
5967
print "done!";
6068
?>
6169
--CLEAN--
@@ -64,4 +72,5 @@ require_once 'clean_table.inc';
6472
?>
6573
--EXPECT--
6674
mysqli object is already closed
75+
There is no active transaction
6776
done!

ext/mysqli/tests/mysqli_rollback.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ if (!have_innodb($link))
5353
echo $exception->getMessage() . "\n";
5454
}
5555

56+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false);
57+
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
58+
try {
59+
mysqli_rollback($link);
60+
} catch (\mysqli_sql_exception $e) {
61+
echo $e->getMessage() . \PHP_EOL;
62+
}
63+
5664
print "done!\n";
5765
?>
5866
--CLEAN--
@@ -61,4 +69,5 @@ require_once 'clean_table.inc';
6169
?>
6270
--EXPECT--
6371
mysqli object is already closed
72+
There is no active transaction
6473
done!

0 commit comments

Comments
 (0)