From b168ae5f4553c25a32bd9e24583bcdbf2f279b7f Mon Sep 17 00:00:00 2001 From: mpyw Date: Wed, 17 Nov 2021 18:29:13 +0900 Subject: [PATCH 1/3] Fix restoring PDO::ATTR_ERRMODE when PDO::lastInsertId() call failed --- source/pdo_sqlsrv/pdo_dbh.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index bddbd5423..ddd5ca5ea 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -1598,6 +1598,9 @@ zend_string * pdo_sqlsrv_dbh_last_id(_Inout_ pdo_dbh_t *dbh, _In_ const zend_str driver_stmt->~sqlsrv_stmt(); } catch( core::CoreException& ) { + // restore error handling to its previous mode + dbh->error_mode = prev_err_mode; + // copy any errors on the statement to the connection so that the user sees them, since the statement is released // before this method returns strcpy_s( dbh->error_code, sizeof( dbh->error_code ), From 33349c4cc786e50438fd7079b47999a927d6039e Mon Sep 17 00:00:00 2001 From: mpyw Date: Thu, 18 Nov 2021 05:47:30 +0900 Subject: [PATCH 2/3] Add new test --- ...restore_original_errmode_after_failed.phpt | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt diff --git a/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt b/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt new file mode 100644 index 000000000..4f8174f1b --- /dev/null +++ b/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt @@ -0,0 +1,46 @@ +--TEST-- +Confirm that PDO::ATTR_ERRMODE value should be restored whether PDO::lastInsertId() call succeeded or not. +--SKIPIF-- + +--FILE-- + "int")); + createTable($conn, "table2", array(new columnMeta("int", "id", "IDENTITY(200,2)"), "val" => "int")); + createTable($conn, "table3", array("id" => "int", "val" => "int")); + + insertRow($conn, "table1", array("val" => 1), "exec"); + insertRow($conn, "table2", array("val" => 2), "exec"); + $conn->lastInsertId(); + var_dump($conn->getAttribute(PDO::ATTR_ERRMODE)); + + insertRow($conn, "table2", array("val" => 3), "exec"); + insertRow($conn, "table1", array("val" => 4), "exec"); + $conn->lastInsertId(); + var_dump($conn->getAttribute(PDO::ATTR_ERRMODE)); + + // Should restore original value even if PDO::lastInsertId() failed. + insertRow($conn, "table3", array("id" => 1, "val" => 1), "exec"); + $conn->lastInsertId(); + var_dump($conn->getAttribute(PDO::ATTR_ERRMODE)); + + dropTable($conn, "table1"); + dropTable($conn, "table2"); + dropTable($conn, "table3"); + unset($conn); +} catch (PDOException $e) { + var_dump($e); + exit; +} + + +?> +--EXPECT-- +int(2) +int(2) +int(2) From d604fea2a93db41d2d868064212450f322af9a1e Mon Sep 17 00:00:00 2001 From: mpyw Date: Thu, 18 Nov 2021 09:40:57 +0900 Subject: [PATCH 3/3] Apply review suggests to confirm actual dump --- ...Id_restore_original_errmode_after_failed.phpt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt b/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt index 4f8174f1b..700c20c78 100644 --- a/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt +++ b/test/functional/pdo_sqlsrv/pdo_lastInsertId_restore_original_errmode_after_failed.phpt @@ -32,15 +32,21 @@ try { dropTable($conn, "table1"); dropTable($conn, "table2"); dropTable($conn, "table3"); + + // Should trigger exception + $tsql = "SELECT * FROM dummy"; + $conn->exec($tsql); + unset($conn); } catch (PDOException $e) { - var_dump($e); + print_r($e->getMessage()); exit; } ?> ---EXPECT-- -int(2) -int(2) -int(2) +--EXPECTREGEX-- +int\(2\) +int\(2\) +int\(2\) +.*Invalid object name \'dummy\'\.