Skip to content

Commit a4103c9

Browse files
committed
Add PDO::checkLiveness method
Exposes the driver's underlying pdo_dbh_check_liveness_func to PHP scripts that may want to check if a connection is still open.
1 parent 517c993 commit a4103c9

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

ext/pdo/pdo_dbh.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,29 @@ PHP_METHOD(PDO, query)
11231123
}
11241124
/* }}} */
11251125

1126+
/* {{{ Performs a liveness check */
1127+
PHP_METHOD(PDO, checkLiveness)
1128+
{
1129+
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1130+
1131+
ZEND_PARSE_PARAMETERS_NONE();
1132+
1133+
PDO_CONSTRUCT_CHECK;
1134+
1135+
if (!dbh->methods->check_liveness) {
1136+
zend_throw_exception_ex(php_pdo_get_exception(), 0, "This driver doesn't support checkLiveness");
1137+
RETURN_THROWS();
1138+
}
1139+
1140+
if (dbh->methods->check_liveness(dbh) != FAILURE) {
1141+
RETURN_TRUE;
1142+
}
1143+
1144+
PDO_HANDLE_DBH_ERR();
1145+
RETURN_FALSE;
1146+
}
1147+
/* }}} */
1148+
11261149
/* {{{ quotes string for use in a query. The optional paramtype acts as a hint for drivers that have alternate quoting styles. The default value is PDO_PARAM_STR */
11271150
PHP_METHOD(PDO, quote)
11281151
{

ext/pdo/pdo_dbh.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
99
/** @return bool */
1010
public function beginTransaction() {}
1111

12+
/** @return bool */
13+
public function checkLiveness() {}
14+
1215
/** @return bool */
1316
public function commit() {}
1417

ext/pdo/pdo_dbh_arginfo.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 623f0a96bc1ae5eadcac5ba92eb73189cd3230cd */
2+
* Stub hash: 379b21e9f0e26c7095c7366fa1eac71388ba0014 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDO___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, dsn, IS_STRING, 0)
@@ -11,6 +11,8 @@ ZEND_END_ARG_INFO()
1111
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PDO_beginTransaction, 0, 0, 0)
1212
ZEND_END_ARG_INFO()
1313

14+
#define arginfo_class_PDO_checkLiveness arginfo_class_PDO_beginTransaction
15+
1416
#define arginfo_class_PDO_commit arginfo_class_PDO_beginTransaction
1517

1618
#define arginfo_class_PDO_errorCode arginfo_class_PDO_beginTransaction
@@ -59,6 +61,7 @@ ZEND_END_ARG_INFO()
5961

6062
ZEND_METHOD(PDO, __construct);
6163
ZEND_METHOD(PDO, beginTransaction);
64+
ZEND_METHOD(PDO, checkLiveness);
6265
ZEND_METHOD(PDO, commit);
6366
ZEND_METHOD(PDO, errorCode);
6467
ZEND_METHOD(PDO, errorInfo);
@@ -77,6 +80,7 @@ ZEND_METHOD(PDO, setAttribute);
7780
static const zend_function_entry class_PDO_methods[] = {
7881
ZEND_ME(PDO, __construct, arginfo_class_PDO___construct, ZEND_ACC_PUBLIC)
7982
ZEND_ME(PDO, beginTransaction, arginfo_class_PDO_beginTransaction, ZEND_ACC_PUBLIC)
83+
ZEND_ME(PDO, checkLiveness, arginfo_class_PDO_checkLiveness, ZEND_ACC_PUBLIC)
8084
ZEND_ME(PDO, commit, arginfo_class_PDO_commit, ZEND_ACC_PUBLIC)
8185
ZEND_ME(PDO, errorCode, arginfo_class_PDO_errorCode, ZEND_ACC_PUBLIC)
8286
ZEND_ME(PDO, errorInfo, arginfo_class_PDO_errorInfo, ZEND_ACC_PUBLIC)

ext/pdo_mysql/tests/pdo_mysql_interface.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if (false == MySQLPDOTest::detect_transactional_mysql_engine($db))
1818
'__construct' => true,
1919
'prepare' => true,
2020
'beginTransaction' => true,
21+
'checkLiveness' => true,
2122
'commit' => true,
2223
'rollBack' => true,
2324
'setAttribute' => true,

0 commit comments

Comments
 (0)