Merge pull request #281 from v-dareck/github267_closeCursor

Github #267 close cursor error
This commit is contained in:
Jenny Tam 2017-02-10 08:04:48 -08:00 committed by GitHub
commit e363a44cf5
2 changed files with 79 additions and 6 deletions

View file

@ -393,17 +393,20 @@ int pdo_sqlsrv_stmt_close_cursor(pdo_stmt_t *stmt TSRMLS_DC)
try {
SQLSRV_ASSERT( stmt != NULL, "pdo_sqlsrv_stmt_next_rowset: pdo_stmt object was null" );
SQLSRV_ASSERT( stmt != NULL, "pdo_sqlsrv_stmt_close_cursor: pdo_stmt object was null" );
sqlsrv_stmt* driver_stmt = reinterpret_cast<sqlsrv_stmt*>( stmt->driver_data );
SQLSRV_ASSERT( driver_stmt != NULL, "pdo_sqlsrv_stmt_next_rowset: driver_data object was null" );
SQLSRV_ASSERT( driver_stmt != NULL, "pdo_sqlsrv_stmt_close_cursor: driver_data object was null" );
// to "close the cursor" means we make the statement ready for execution again. To do this, we
// skip all the result sets on the current statement.
while( driver_stmt->past_next_result_end == false ) {
core_sqlsrv_next_result( driver_stmt TSRMLS_CC );
// If the statement has not been executed there are no next results to iterate over.
if ( driver_stmt->executed == true )
{
while( driver_stmt->past_next_result_end == false ) {
core_sqlsrv_next_result( driver_stmt TSRMLS_CC );
}
}
}
catch( core::CoreException& ) {
@ -412,7 +415,7 @@ int pdo_sqlsrv_stmt_close_cursor(pdo_stmt_t *stmt TSRMLS_DC)
}
catch( ... ) {
DIE( "pdo_sqlsrv_stmt_next_rowset: Unknown exception occurred while advanding to the next result set." );
DIE( "pdo_sqlsrv_stmt_close_cursor: Unknown exception occurred while advancing to the next result set." );
}
return 1;

View file

@ -0,0 +1,70 @@
--TEST--
Test closeCursor with a stmt before/after execute and fetch.
--SKIPIF--
--FILE--
<?php
require_once("autonomous_setup.php");
try
{
/* Connect */
$conn = new PDO("sqlsrv:server=$serverName", $username, $password);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// prepare a stmt but don't execute, then closeCursor.
$stmt = $conn->prepare("select 123 as 'IntCol'");
$ret = $stmt->closeCursor();
var_dump($ret);
$ret = $stmt->closeCursor();
var_dump($ret);
// prepare a stmt and execute, then closeCursor.
$stmt = $conn->prepare("select 123 as 'IntCol'");
$stmt->execute();
$ret = $stmt->closeCursor();
var_dump($ret);
$ret = $stmt->closeCursor();
var_dump($ret);
// use two stmt, execute, and fetch, then closeCursor.
// use one with client side buffering.
$stmt1 = $conn->query("select 123 as 'IntCol'");
$stmt2 = $conn->prepare("select 'abc' as 'Charcol'", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$result = $stmt1->fetch(PDO::FETCH_NUM);
print_r($result[0]);
echo "\n";
$ret = $stmt1->closeCursor();
var_dump($ret);
$stmt2->execute();
$result = $stmt2->fetch(PDO::FETCH_NUM);
print_r($result[0]);
echo "\n";
$ret = $stmt2->closeCursor();
var_dump($ret);
$stmt1 = null;
$stmt2 = null;
$stmt = null;
$conn = null;
}
catch( PDOException $e ) {
var_dump($e);
exit;
}
print "Done";
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
123
bool(true)
abc
bool(true)
Done