Addressed review comments

This commit is contained in:
David Puglielli 2019-04-22 16:54:43 -07:00
parent 13fe59d510
commit ea17b72c83

View file

@ -3,25 +3,44 @@ Test a batch query with different cursor types
--DESCRIPTION--
Verifies that batch queries don't work with dynamic, static, and keyset
server-side cursors, and checks that correct column and row counts are
returned otherwise
returned otherwise. For information on the expected behaviour of cursors
with batch queries, see
https://docs.microsoft.com/en-us/previous-versions/visualstudio/aa266531(v=vs.60)
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
// All supported cursor types
$cursors = array('forward', 'dynamic', 'static', 'keyset', 'buffered');
$expectedCols = 1;
$expectedRows = 5;
$expectedResultSets = 5;
//expected errors
$noCursor = array(array('01000', 16954, '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Executing SQL directly; no cursor.'),
array('01S02', 0, '[Microsoft][ODBC Driver 17 for SQL Server]Cursor type changed'));
// Expected error messages
$noCursor = array(array('01000', 16954, 'Executing SQL directly; no cursor.'),
array('01S02', 0, 'Cursor type changed'));
$wrongCursor = array(array('IMSSP', -50, 'This function only works with statements that have static or keyset scrollable cursors.'));
$noNextResult = array(array('IMSSP', -22, 'There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved.'));
// Data for testing, all integer types
$data = array(array(86, -217483648, 0, -432987563, 7, 217483647),
array(0, 31, 127, 255, 1, 10),
array(4534, -212, 32767, 0, 7, -32768),
array(-1, 546098342985600, 9223372000000000000, 5115115115115, 7, -7),
array(0, 1, 0, 0, 1, 1),
);
// Column names
$colName = array('c1_int', 'c2_tinyint', 'c3_smallint', 'c4_bigint', 'c5_bit');
// Fetch one column at a time
$expectedCols = 1;
// Number of table rows
$expectedRows = sizeof($data[0]);
// Expected result sets = number of columns, since the batch fetches each column sequentially
$expectedResultSets = 5;
function checkErrors($expectedError)
{
$actualError = sqlsrv_errors();
@ -39,8 +58,8 @@ function checkErrors($expectedError)
// Make sure the SQLSTATE, code, and message are identical
$e = 0;
foreach ($actualError AS $error) {
if ($error[0] != $expectedError[$e][0] or $error[1] != $expectedError[$e][1] or $error[2] != $expectedError[$e][2]) {
fatalError("Wrong error message, expected ".$error[2].", got ".$expectedError[$e][2]."\n");
if ($error[0] != $expectedError[$e][0] or $error[1] != $expectedError[$e][1] or !fnmatch('*'.$expectedError[$e][2], $error[2])) {
fatalError("Wrong error message, expected ".$expectedError[$e][2].", got $error[2]\n");
}
++$e;
}
@ -53,18 +72,18 @@ function checkColumnsAndRows($stmt, $cursor, $error)
$cols = sqlsrv_num_fields($stmt);
if ($cols != $expectedCols) {
fatalError("Incorrect number of columns returned with ".$cursor." cursor. Expected ".$expectedCols." columns, got ".$cols." columns\n");
fatalError("Incorrect number of columns returned with $cursor cursor. Expected $expectedCols columns, got $cols columns\n");
}
$rows = sqlsrv_num_rows($stmt);
if ($cursor == 'buffered') {
if ($rows != $expectedRows) {
fatalError("Incorrect number of columns returned with buffered cursor. Expected ".$expectedRows." rows, got ".$rows." rows\n");
fatalError("Incorrect number of columns returned with buffered cursor. Expected $expectedRows rows, got $rows rows\n");
}
} else {
if ($rows !== false) {
fatalError("Expected sqlsrv_num_rows to return false with ".$cursor." cursor, instead returned ".$rows." rows\n");
fatalError("Expected sqlsrv_num_rows to return false with $cursor cursor, instead returned $rows rows\n");
} else {
checkErrors($error);
}
@ -77,11 +96,11 @@ $conn = AE\connect();
// Create and populate a table of integer types
$tableName = 'batch_query_test';
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
new AE\ColumnMeta('tinyint', 'c2_tinyint'),
new AE\ColumnMeta('smallint', 'c3_smallint'),
new AE\ColumnMeta('bigint', 'c4_bigint'),
new AE\ColumnMeta('bit', 'c5_bit'));
$columns = array(new AE\ColumnMeta('int', $colName[0]),
new AE\ColumnMeta('tinyint', $colName[1]),
new AE\ColumnMeta('smallint',$colName[2]),
new AE\ColumnMeta('bigint', $colName[3]),
new AE\ColumnMeta('bit', $colName[4]));
$stmt = AE\createTable($conn, $tableName, $columns);
if (!$stmt) {
@ -89,66 +108,72 @@ if (!$stmt) {
}
sqlsrv_free_stmt($stmt);
$inputs = array(array('c1_int'=>86, 'c2_tinyint'=>0, 'c3_smallint'=>4534, 'c4_bigint'=>-1, 'c5_bit'=>0),
array('c1_int'=>-217483648, 'c2_tinyint'=>31, 'c3_smallint'=>-212, 'c4_bigint'=>546098342985694, 'c5_bit'=>1),
array('c1_int'=>0, 'c2_tinyint'=>127, 'c3_smallint'=>32767, 'c4_bigint'=>9223372000000000000, 'c5_bit'=>0),
array('c1_int'=>-432987563, 'c2_tinyint'=>255, 'c3_smallint'=>0, 'c4_bigint'=>5115115115115115115, 'c5_bit'=>0),
array('c1_int'=>7, 'c2_tinyint'=>1, 'c3_smallint'=>7, 'c4_bigint'=>7, 'c5_bit'=>1),
);
$inputs = array();
// Generate the inputs for insertRow()
for ($i = 0; $i < $expectedRows; ++$i)
{
$inputs[] = array();
for ($j = 0; $j < $expectedResultSets; ++$j)
{
$inputs[$i][$colName[$j]] = $data[$j][$i];
}
}
for ($i=0; $i < sizeof($inputs); ++$i) {
$stmt = AE\insertRow($conn, $tableName, $inputs[$i]);
sqlsrv_free_stmt($stmt);
}
$query = "SELECT c1_int from batch_query_test;
SELECT c2_tinyint from batch_query_test;
SELECT c3_smallint from batch_query_test;
SELECT c4_bigint from batch_query_test;
SELECT c5_bit from batch_query_test;";
$query = "SELECT c1_int from $tableName;
SELECT c2_tinyint from $tableName;
SELECT c3_smallint from $tableName;
SELECT c4_bigint from $tableName;
SELECT c5_bit from $tableName;";
// Test the batch query with different cursor types
for ($i = 0; $i < sizeof($cursors); ++$i)
{
$cursor = $cursors[$i];
echo "Testing with ".$cursor." cursor...\n";
echo "Testing with $cursor cursor...\n";
$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>$cursor));
if (!$stmt) {
fatalError("Error preparing statement with ".$cursor." cursor\n");
fatalError("Error preparing statement with $cursor cursor\n");
}
if (!sqlsrv_execute($stmt)) {
if ($cursor == 'forward' or $cursor == 'buffered') {
fatalError("Statement execution failed unexpectedly with a ".$cursor." cursor\n");
fatalError("Statement execution failed unexpectedly with a $cursor cursor\n");
} else {
checkErrors($noCursor);
continue;
}
}
$numResultSets = 0;
// Check the column and row count before and after running through
// all results
checkColumnsAndRows($stmt, $cursor, $wrongCursor);
while ($res = sqlsrv_fetch($stmt))
{ }
checkColumnsAndRows($stmt, $cursor, $wrongCursor);
$numResultSets = 1;
while ($next = sqlsrv_next_result($stmt)) {
// each result set, because some cursor types may return the number
// of rows only after fetching all rows in the result set
do {
checkColumnsAndRows($stmt, $cursor, $wrongCursor);
while ($res = sqlsrv_fetch($stmt))
{ }
$row = 0;
while ($res = sqlsrv_fetch_array($stmt)) {
if ($res[0] != $data[$numResultSets][$row]) {
fatalError("Wrong result, expected ".$data[$numResultSets][$row].", got $res[0]\n");
}
++$row;
}
checkColumnsAndRows($stmt, $cursor, $wrongCursor);
++$numResultSets;
}
} while ($next = sqlsrv_next_result($stmt));
if ($numResultSets != $expectedResultSets) {
fatalError("Unexpected number of result sets, expected ".$expectedResultedSets.", got ".$numResultSets."\n");
fatalError("Unexpected number of result sets, expected $expectedResultedSets, got $numResultSets\n");
}
// We expect an error if sqlsrv_next_result returns false,
@ -158,7 +183,7 @@ for ($i = 0; $i < sizeof($cursors); ++$i)
if ($cursor == 'forward') {
checkErrors($noNextResult);
} else {
fatalError("sqlsrv_next_result failed with a ".$cursor." cursor\n");
fatalError("sqlsrv_next_result failed with a $cursor cursor\n");
}
}