Skip to content

Commit

Permalink
Fix restoring PDO::ATTR_ERRMODE after PDO::lastInsertId() call failed (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw authored Nov 18, 2021
1 parent 3826f1a commit f52fb48
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/pdo_sqlsrv/pdo_dbh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
Confirm that PDO::ATTR_ERRMODE value should be restored whether PDO::lastInsertId() call succeeded or not.
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsCommon_mid-refactor.inc");

try {
$conn = connect();

// create temporary tables
createTable($conn, "table1", array(new columnMeta("int", "id", "IDENTITY(100,2)"), "val" => "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");

// Should trigger exception
$tsql = "SELECT * FROM dummy";
$conn->exec($tsql);

unset($conn);
} catch (PDOException $e) {
print_r($e->getMessage());
exit;
}


?>
--EXPECTREGEX--
int\(2\)
int\(2\)
int\(2\)
.*Invalid object name \'dummy\'\.

0 comments on commit f52fb48

Please sign in to comment.