Skip to content

Instantly share code, notes, and snippets.

@ninjacato
Created May 31, 2018 09:21
Show Gist options
  • Save ninjacato/7e8617a93996fa54379f3797f76c4253 to your computer and use it in GitHub Desktop.
Save ninjacato/7e8617a93996fa54379f3797f76c4253 to your computer and use it in GitHub Desktop.
Add $pdo->close() function to kill the database connection
diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c
index 4a372a9c18..0982555b7e 100644
--- a/ext/pdo/pdo_dbh.c
+++ b/ext/pdo/pdo_dbh.c
@@ -973,6 +973,31 @@ static PHP_METHOD(PDO, lastInsertId)
}
/* }}} */
+/* {{{ proto bool PDO::close()
+ Close the database connection */
+static PHP_METHOD(PDO, close)
+{
+ pdo_dbh_t *dbh = Z_PDO_DBH_P(getThis());
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
+ PDO_CONSTRUCT_CHECK;
+
+ if (dbh->in_txn && dbh->methods && dbh->methods->rollback) {
+ dbh->methods->rollback(dbh);
+ dbh->in_txn = 0;
+ }
+
+ if (dbh->methods && dbh->methods->closer(dbh) == 0) {
+ RETURN_TRUE;
+ }
+
+ RETURN_FALSE;
+}
+/* }}} */
+
/* {{{ proto string PDO::errorCode()
Fetch the error code associated with the last operation on the database handle */
static PHP_METHOD(PDO, errorCode)
@@ -1255,6 +1280,7 @@ const zend_function_entry pdo_dbh_functions[] = /* {{{ */ {
PHP_ME(PDO, __wakeup, arginfo_pdo__void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(PDO, __sleep, arginfo_pdo__void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(PDO, getAvailableDrivers, arginfo_pdo__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+ PHP_ME(PDO, close, arginfo_pdo__void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment