From c1b54aabebd97f7fc6a508d8f5908c01233e60e1 Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Fri, 19 Apr 2019 13:26:34 -0700 Subject: [PATCH 1/5] Added batch query test --- .../functional/sqlsrv/sqlsrv_batch_query.phpt | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 test/functional/sqlsrv/sqlsrv_batch_query.phpt diff --git a/test/functional/sqlsrv/sqlsrv_batch_query.phpt b/test/functional/sqlsrv/sqlsrv_batch_query.phpt new file mode 100644 index 00000000..b26e7b14 --- /dev/null +++ b/test/functional/sqlsrv/sqlsrv_batch_query.phpt @@ -0,0 +1,179 @@ +--TEST-- +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 +--SKIPIF-- + +--FILE-- +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'=>9223372036854775807, '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), + ); + +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;"; + +// Test the batch query with different cursor types +for ($i = 0; $i < sizeof($cursors); ++$i) +{ + $cursor = $cursors[$i]; + 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"); + } + + if (!sqlsrv_execute($stmt)) { + if ($cursor == 'forward' or $cursor == 'buffered') { + fatalError("Statement execution failed unexpectedly with a ".$cursor." cursor\n"); + } else { + checkErrors($noCursor); + continue; + } + } + + // Check the column and row count before and after running through + // all results + checkColumnsAndRows($stmt, $cursor, $wrongCursor); + + while ($res = sqlsrv_fetch_array($stmt)) + { } + + checkColumnsAndRows($stmt, $cursor, $wrongCursor); + + $numResultSets = 1; + while ($next = sqlsrv_next_result($stmt)) { + checkColumnsAndRows($stmt, $cursor, $wrongCursor); + + while ($res = sqlsrv_fetch_array($stmt)) + { } + + checkColumnsAndRows($stmt, $cursor, $wrongCursor); + ++$numResultSets; + } + + if ($numResultSets != $expectedResultSets) { + fatalError("Unexpected number of result sets, expected ".$expectedResultedSets.", got ".$numResultSets."\n"); + } + + // We expect an error if sqlsrv_next_result returns false, + // but not if it returns null (i.e. if we are genuinely at + // the end of all the result sets with a buffered cursor) + if ($next === false) { + if ($cursor == 'forward') { + checkErrors($noNextResult); + } else { + fatalError("sqlsrv_next_result failed with a ".$cursor." cursor\n"); + } + } + + sqlsrv_free_stmt($stmt); +} + +dropTable($conn, $tableName); +sqlsrv_close($conn); + +echo "Done.\n"; +?> +--EXPECT-- +Testing with forward cursor... +Testing with dynamic cursor... +Testing with static cursor... +Testing with keyset cursor... +Testing with buffered cursor... +Done. From 13fe59d510583a31611adc39754aeabdd7bb691c Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Fri, 19 Apr 2019 16:02:19 -0700 Subject: [PATCH 2/5] Fixed 32 bit test failure --- test/functional/sqlsrv/sqlsrv_batch_query.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/sqlsrv/sqlsrv_batch_query.phpt b/test/functional/sqlsrv/sqlsrv_batch_query.phpt index b26e7b14..d3f6205d 100644 --- a/test/functional/sqlsrv/sqlsrv_batch_query.phpt +++ b/test/functional/sqlsrv/sqlsrv_batch_query.phpt @@ -91,7 +91,7 @@ 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'=>9223372036854775807, 'c5_bit'=>0), + 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), ); @@ -131,7 +131,7 @@ for ($i = 0; $i < sizeof($cursors); ++$i) // all results checkColumnsAndRows($stmt, $cursor, $wrongCursor); - while ($res = sqlsrv_fetch_array($stmt)) + while ($res = sqlsrv_fetch($stmt)) { } checkColumnsAndRows($stmt, $cursor, $wrongCursor); @@ -140,7 +140,7 @@ for ($i = 0; $i < sizeof($cursors); ++$i) while ($next = sqlsrv_next_result($stmt)) { checkColumnsAndRows($stmt, $cursor, $wrongCursor); - while ($res = sqlsrv_fetch_array($stmt)) + while ($res = sqlsrv_fetch($stmt)) { } checkColumnsAndRows($stmt, $cursor, $wrongCursor); From ea17b72c83fd373562fc1a7361af65eda17f0654 Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Mon, 22 Apr 2019 16:54:43 -0700 Subject: [PATCH 3/5] Addressed review comments --- .../functional/sqlsrv/sqlsrv_batch_query.phpt | 125 +++++++++++------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/test/functional/sqlsrv/sqlsrv_batch_query.phpt b/test/functional/sqlsrv/sqlsrv_batch_query.phpt index d3f6205d..cb388b7b 100644 --- a/test/functional/sqlsrv/sqlsrv_batch_query.phpt +++ b/test/functional/sqlsrv/sqlsrv_batch_query.phpt @@ -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-- - + --FILE-- 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"); } } From 63c6bd385f7f0e80487d682359d751e213a23c84 Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Tue, 23 Apr 2019 11:28:44 -0700 Subject: [PATCH 4/5] Formatting changes --- .../functional/sqlsrv/sqlsrv_batch_query.phpt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/functional/sqlsrv/sqlsrv_batch_query.phpt b/test/functional/sqlsrv/sqlsrv_batch_query.phpt index cb388b7b..36057a57 100644 --- a/test/functional/sqlsrv/sqlsrv_batch_query.phpt +++ b/test/functional/sqlsrv/sqlsrv_batch_query.phpt @@ -39,7 +39,7 @@ $expectedCols = 1; $expectedRows = sizeof($data[0]); // Expected result sets = number of columns, since the batch fetches each column sequentially -$expectedResultSets = 5; +$expectedResultSets = sizeof($colName); function checkErrors($expectedError) { @@ -111,11 +111,9 @@ sqlsrv_free_stmt($stmt); $inputs = array(); // Generate the inputs for insertRow() -for ($i = 0; $i < $expectedRows; ++$i) -{ +for ($i = 0; $i < $expectedRows; ++$i) { $inputs[] = array(); - for ($j = 0; $j < $expectedResultSets; ++$j) - { + for ($j = 0; $j < $expectedResultSets; ++$j) { $inputs[$i][$colName[$j]] = $data[$j][$i]; } } @@ -125,15 +123,14 @@ for ($i=0; $i < sizeof($inputs); ++$i) { sqlsrv_free_stmt($stmt); } -$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;"; +$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) -{ +for ($i = 0; $i < sizeof($cursors); ++$i) { $cursor = $cursors[$i]; echo "Testing with $cursor cursor...\n"; From 0f93bbe6fed23e7114f4e75717a19474a71ee721 Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Wed, 24 Apr 2019 12:49:33 -0700 Subject: [PATCH 5/5] Simplified insert logic --- test/functional/sqlsrv/sqlsrv_batch_query.phpt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/functional/sqlsrv/sqlsrv_batch_query.phpt b/test/functional/sqlsrv/sqlsrv_batch_query.phpt index 36057a57..4cfa1445 100644 --- a/test/functional/sqlsrv/sqlsrv_batch_query.phpt +++ b/test/functional/sqlsrv/sqlsrv_batch_query.phpt @@ -110,16 +110,14 @@ sqlsrv_free_stmt($stmt); $inputs = array(); -// Generate the inputs for insertRow() +// Insert each row. Need an associative array to use insertRow() for ($i = 0; $i < $expectedRows; ++$i) { - $inputs[] = array(); + $inputs = array(); for ($j = 0; $j < $expectedResultSets; ++$j) { - $inputs[$i][$colName[$j]] = $data[$j][$i]; + $inputs[$colName[$j]] = $data[$j][$i]; } -} -for ($i=0; $i < sizeof($inputs); ++$i) { - $stmt = AE\insertRow($conn, $tableName, $inputs[$i]); + $stmt = AE\insertRow($conn, $tableName, $inputs); sqlsrv_free_stmt($stmt); }