From 849725d742f8a11988f3031a919515da155cc724 Mon Sep 17 00:00:00 2001 From: yitam Date: Tue, 4 Apr 2017 11:32:18 -0700 Subject: [PATCH] add new test plus fix some --- .../pdo_fetch_complex_transactions.phpt | 8 +- .../pdo_fetch_cursor_scroll_random.phpt | 219 ++++++++++++++++++ .../sqlsrv_fetch_cursor_static_scroll.phpt | 1 - 3 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt diff --git a/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt b/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt index f0fe65fd..3a29ffa6 100644 --- a/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt +++ b/test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt @@ -101,7 +101,7 @@ function FetchData($stmt, $tableName, $numRows, $fetchMode = false) //-------------------------------------------------------------------- function RunTest() { - StartTest("pdo_fetch_complex_transactions.phpt"); + StartTest("pdo_fetch_complex_transactions"); try { require_once("autonomous_setup.php"); @@ -134,7 +134,7 @@ function RunTest() echo $e->getMessage(); } echo "\nDone\n"; - EndTest("pdo_fetch_complex_transactions.phpt"); + EndTest("pdo_fetch_complex_transactions"); } RunTest(); @@ -142,7 +142,7 @@ RunTest(); ?> --EXPECT--  -...Starting 'pdo_fetch_complex_transactions.phpt' test... +...Starting 'pdo_fetch_complex_transactions' test... Number of rows fetched: 10 Committed deleting 3 rows Number of rows fetched: 7 @@ -156,4 +156,4 @@ Deletion aborted Number of rows fetched: 4 Done -...Test 'pdo_fetch_complex_transactions.phpt' completed successfully. \ No newline at end of file +...Test 'pdo_fetch_complex_transactions' completed successfully. \ No newline at end of file diff --git a/test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt b/test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt new file mode 100644 index 00000000..a38d3343 --- /dev/null +++ b/test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt @@ -0,0 +1,219 @@ +--TEST-- +Test with cursor scroll and select different rows in some random order +--FILE-- +exec("CREATE TABLE $tableName ([c1_int] int, [c2_tinyint] tinyint, [c3_smallint] smallint, [c4_bigint] bigint, [c5_bit] bit)"); + + // insert data + $numRows = InsertData($conn, $tableName); + + // select table + $stmt = $conn->prepare("SELECT * FROM $tableName ORDER BY c1_int", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_FWDONLY)); + $stmt->execute(); + + $numRowsFetched = 0; + while ($row = $stmt->fetch(PDO::FETCH_NUM)) + { + echo "$row[0]\n"; + $numRowsFetched++; + } + + if ($numRowsFetched != $numRows) + echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n"; +} + +function Cursor_Scroll_FetchRows($conn, $tableName) +{ + $stmt = $conn->prepare("SELECT * FROM $tableName ORDER BY c1_int", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL)); + $stmt->execute(); + + GetFirstRow($stmt); + GetNextRow($stmt); + GetLastRow($stmt); + GetPriorRow($stmt); + GetAbsoluteRow($stmt, 7); + GetAbsoluteRow($stmt, 2); + GetRelativeRow($stmt, 3); + GetPriorRow($stmt); + GetRelativeRow($stmt, -4); + GetAbsoluteRow($stmt, 0); + GetNextRow($stmt); + GetRelativeRow($stmt, 5); + GetAbsoluteRow($stmt, -1); + GetNextRow($stmt); + GetLastRow($stmt); + GetRelativeRow($stmt, 1); +} + +function InsertData($conn, $tableName) +{ + $numRows = 0; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((1), (0), (null), (9223372036854775807), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((2), (null), (-32768), (9223372036854775807), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((3), (255), (-32768), (1035941737), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((4), (null), (4762), (804325764), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((5), (57), (32767), (-9223372036854775808), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((6), (201), (-32768), (450619355), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((7), (244), (-21244), (981345728), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((8), (143), (0), (-1330405117), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((9), (null), (null), (209123628), (0))"); + $numRows += $count; + $count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((10), (147), (21133), (-1), (0))"); + $numRows += $count; + + return $numRows; +} + +function GetFirstRow($stmt) +{ + echo "\nfirst row: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_FIRST, 0); + if ($row) + { + echo "$row[0]\n"; + } +} + +function GetNextRow($stmt) +{ + echo "\nnext row: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT, 0); + if ($row) + { + echo "$row[0]\n"; + } +} + +function GetPriorRow($stmt) +{ + echo "\nprior row: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR, 0); + if ($row) + { + echo "$row[0]\n"; + } +} + +function GetLastRow($stmt) +{ + echo "\nlast row: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST, 0); + if ($row) + { + echo "$row[0]\n"; + } +} + +function GetRelativeRow($stmt, $offset) +{ + echo "\nrow $offset from the current row: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_REL, $offset); + if ($row) + { + echo "$row[0]\n"; + } +} + +function GetAbsoluteRow($stmt, $offset) +{ + echo "\nabsolute row with offset $offset: "; + $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $offset); + if ($row) + { + echo "$row[0]\n"; + } +} + + +//-------------------------------------------------------------------- +// RunTest +// +//-------------------------------------------------------------------- +function RunTest() +{ + StartTest("pdo_fetch_cursor_scroll_random"); + try + { + require_once("autonomous_setup.php"); + $database = "tempdb"; + + // Connect + $conn = new PDO( "sqlsrv:server=$serverName;Database=$database", $username, $password); + $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + + $tableName = GetTempTableName(); + + Cursor_ForwardOnly($conn, $tableName); + Cursor_Scroll_FetchRows($conn, $tableName); + + $conn = null; + } + catch (Exception $e) + { + echo $e->getMessage(); + } + echo "\nDone\n"; + EndTest("pdo_fetch_cursor_scroll_random"); +} + +RunTest(); + +?> +--EXPECT-- + +...Starting 'pdo_fetch_cursor_scroll_random' test... +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 + +first row: 1 + +next row: 2 + +last row: 10 + +prior row: 9 + +absolute row with offset 7: 8 + +absolute row with offset 2: 3 + +row 3 from the current row: 6 + +prior row: 5 + +row -4 from the current row: 1 + +absolute row with offset 0: 1 + +next row: 2 + +row 5 from the current row: 7 + +absolute row with offset -1: +next row: 1 + +last row: 10 + +row 1 from the current row: +Done +...Test 'pdo_fetch_cursor_scroll_random' completed successfully. \ No newline at end of file diff --git a/test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt b/test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt index a8eb3ab8..0db32c32 100644 --- a/test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt +++ b/test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt @@ -143,7 +143,6 @@ function RunTest() $conn = sqlsrv_connect($serverName, $connectionInfo); if( !$conn ) { FatalError("Could not connect.\n"); } - //echo "\nUsing SQLSRV_CURSOR_FORWARD: "; FetchRow_Query($conn); sqlsrv_close($conn);