From 7313fa0c8be51833a1ef32cddf60372d6a012eb9 Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Mon, 17 May 2021 17:37:19 -0700 Subject: [PATCH] Updated pdo_sqlsrv_get_driver_methods as per documentation (#1259) --- source/pdo_sqlsrv/pdo_dbh.cpp | 11 ++-- .../pdo_1258_is_callable_error.phpt | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 test/functional/pdo_sqlsrv/pdo_1258_is_callable_error.phpt diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index bef831358..f88b2f53b 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -1574,12 +1574,13 @@ pdo_sqlsrv_function_entry *pdo_sqlsrv_get_driver_methods( _Inout_ pdo_dbh_t *dbh PDO_LOG_DBH_ENTRY; sqlsrv_conn* driver_conn = reinterpret_cast( dbh->driver_data ); - SQLSRV_ASSERT( driver_conn != NULL, "pdo_sqlsrv_get_driver_methods: driver_data object was NULL." ); - CHECK_CUSTOM_ERROR( true, driver_conn, PDO_SQLSRV_ERROR_FUNCTION_NOT_IMPLEMENTED ) { - return NULL; - } - return NULL; // to avoid a compiler warning + // As per documentation, simply return false if the method does not exist + // https://www.php.net/manual/en/function.is-callable.php + // But user can call PDO::errorInfo() to check the error message if necessary + CHECK_CUSTOM_WARNING_AS_ERROR(true, driver_conn, PDO_SQLSRV_ERROR_FUNCTION_NOT_IMPLEMENTED); + + return NULL; // return NULL for PDO to take care of the rest } namespace { diff --git a/test/functional/pdo_sqlsrv/pdo_1258_is_callable_error.phpt b/test/functional/pdo_sqlsrv/pdo_1258_is_callable_error.phpt new file mode 100644 index 000000000..0f0f91983 --- /dev/null +++ b/test/functional/pdo_sqlsrv/pdo_1258_is_callable_error.phpt @@ -0,0 +1,50 @@ +--TEST-- +GitHub issue 1258 - is_callable() throws an exception if PDOStatement method does not exist +--DESCRIPTION-- +The test shows is_callable() will return false if PDOStatement method does not exist instead of throwing an exception. The user can still check errorInfo() for the error message. See documentation https://www.php.net/manual/en/function.is-callable.php +--ENV-- +PHPT_EXEC=true +--SKIPIF-- + +--FILE-- +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $stmt = $conn->prepare("SELECT @@Version"); + $functionExists = is_callable([$stmt, 'bindParam'], false, $callable); + var_dump($functionExists); + var_dump($callable); + + $functionExists = is_callable([$stmt, 'boo']); + var_dump($functionExists); + + echo PHP_EOL . "Error INFO:" . PHP_EOL; + var_dump($conn->errorInfo()); + + echo "Done\n"; +} catch (PdoException $e) { + echo $e->getMessage(); +} + +?> +--EXPECT-- +bool(true) +string(23) "PDOStatement::bindParam" +bool(false) + +Error INFO: +array(3) { + [0]=> + string(5) "IMSSP" + [1]=> + int(-58) + [2]=> + string(48) "This function is not implemented by this driver." +} +Done +