From 6a14325f376c866c6bb9c162e527ee3377bdba92 Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Tue, 7 Nov 2017 15:31:55 -0800 Subject: [PATCH] Refactor sqlsrv tests with buffer or cursor --- test/functional/sqlsrv/0052.phpt | 64 +-- test/functional/sqlsrv/MsHelper.inc | 44 +- test/functional/sqlsrv/sqlsrv_buffered.phpt | 173 ++++---- ...rv_buffered_result_set_extended_ascii.phpt | 29 +- .../sqlsrv_fetch_cursor_static_scroll.phpt | 101 ++--- .../sqlsrv/sqlsrv_fetch_cursor_types.phpt | 87 ++-- .../sqlsrv/sqlsrv_fetch_missing_row.phpt | 41 +- .../sqlsrv/sqlsrv_fetch_object.phpt | 30 +- .../sqlsrv/sqlsrv_fetch_object_2.phpt | 39 +- ...sqlsrv_fetch_object_unicode_col_name1.phpt | 30 +- ...sqlsrv_fetch_object_unicode_col_name2.phpt | 150 ++++--- test/functional/sqlsrv/sqlsrv_num_rows.phpt | 32 +- .../sqlsrv/srv_012_sqlsrv_fetch_array.phpt | 27 +- .../sqlsrv/srv_037_sqlsrv_has_rows.phpt | 37 +- ...srv_069_fetch_empty_nvarchar_buffered.phpt | 43 +- .../sqlsrv/srv_223_sqlsrv_fetch_absolute.phpt | 88 ++-- .../srv_228_sqlsrv_clientbuffermaxkbsize.phpt | 30 +- ...srv_230_sqlsrv_buffered_numeric_types.phpt | 67 +-- ...v_330_numrow_null_buffered_result_set.phpt | 5 +- test/functional/sqlsrv/test_scrollable.phpt | 403 ++++++++---------- 20 files changed, 730 insertions(+), 790 deletions(-) diff --git a/test/functional/sqlsrv/0052.phpt b/test/functional/sqlsrv/0052.phpt index ee195050..986e8a75 100644 --- a/test/functional/sqlsrv/0052.phpt +++ b/test/functional/sqlsrv/0052.phpt @@ -1,7 +1,9 @@ --TEST-- scrollable results with no rows. +--DESCRIPTION-- +this test is very similar to test_scrollable.phpt... might consider removing this test as a duplicate --SKIPIF-- - + --FILE-- 'static' )); + $query = "SELECT * FROM $tableName"; + if (AE\isColEncrypted()) { + $options = array('Scrollable' => SQLSRV_CURSOR_FORWARD); + } else { + $options = array('Scrollable' => 'static'); + } + + $stmt = AE\executeQueryEx($conn, $query, $options); + $rows = sqlsrv_has_rows($stmt); + if ($rows != false) { + fatalError("Should be no rows present"); + }; + + if ($stmt === false) { + die(print_r(sqlsrv_errors(), true)); + } + $row = sqlsrv_fetch_array($stmt); + print_r($row); + if ($row === false) { + print_r(sqlsrv_errors(), true); + } + + $stmt = AE\selectFromTable($conn, $tableName); $rows = sqlsrv_has_rows($stmt); if ($rows != false) { fatalError("Should be no rows present"); @@ -40,23 +56,7 @@ scrollable results with no rows. print_r(sqlsrv_errors(), true); } - $stmt = sqlsrv_query($conn, "SELECT * FROM ScrollTest"); - $rows = sqlsrv_has_rows($stmt); - if ($rows != false) { - fatalError("Should be no rows present"); - }; - - if ($stmt === false) { - die(print_r(sqlsrv_errors(), true)); - } - $row = sqlsrv_fetch_array($stmt); - print_r($row); - if ($row === false) { - print_r(sqlsrv_errors(), true); - } - - $stmt = sqlsrv_query($conn, "DROP TABLE ScrollTest"); - + dropTable($conn, $tableName); echo "Test succeeded.\n"; ?> diff --git a/test/functional/sqlsrv/MsHelper.inc b/test/functional/sqlsrv/MsHelper.inc index 7bc6533f..a726e93d 100644 --- a/test/functional/sqlsrv/MsHelper.inc +++ b/test/functional/sqlsrv/MsHelper.inc @@ -381,6 +381,36 @@ function createTable($conn, $tbname, $columnMetaArr) return sqlsrv_query($conn, $createSql); } +/** + * Create a table with options for sqlsrv_prepare() + * @param resource $conn : sqlsrv connection resource + * @param string $tbname : name of the table to be created + * @param array $columnMetaArr : array of ColumnMeta objects, which contain metadata for one column + * @param array $options : array of options for sqlsrv_prepare() + * @return resource sqlsrv statement resource + */ +function createTableEx($conn, $tbname, $columnMetaArr, $options) +{ + require_once("MsCommon.inc"); + dropTable($conn, $tbname); + $colDef = ""; + foreach ($columnMetaArr as $meta) { + $colDef = $colDef . $meta->getColDef() . ", "; + } + $colDef = rtrim($colDef, ", "); + $createSql = "CREATE TABLE $tbname ( $colDef )"; + $stmt = sqlsrv_prepare($conn, $createSql, array(), $options); + if ($stmt) { + $res = sqlsrv_execute($stmt); + if ($res == false) { + fatalError("createTableEx: failed to execute the query to create table\n"); + } + } else { + fatalError("createTableEx: failed to prepare the statement to create table\n"); + } + return $stmt; +} + /** * Insert a row into a table * @param resource $conn : sqlsrv connection resource @@ -455,9 +485,10 @@ function selectFromTable($conn, $tbname, $conds = null, $values = null) * @param string $sql : T-SQL query * @param string $conds : string of condition(s) possibly with placeholders, null by default * @param array $values : array of parameters, null by default + * @param array $options : array of query options, null by default * @return resource sqlsrv statement upon success or false otherwise */ -function executeQuery($conn, $sql, $conds = null, $values = null) +function executeQuery($conn, $sql, $conds = null, $values = null, $options = null) { if (!isColEncrypted()) { // if not encrypted replace placeholders ('?') with values, if array not empty @@ -475,16 +506,17 @@ function executeQuery($conn, $sql, $conds = null, $values = null) $sql = $sql . " WHERE $clause "; } elseif (!empty($conds)) { $sql = $sql . " WHERE $conds "; - } - $stmt = sqlsrv_query($conn, $sql); + } + + $stmt = sqlsrv_query($conn, $sql, null, $options); } else { // with AE enabled, use sqlsrv_prepare() in case there are // fields with unlimited size - if (empty($conds) || empty($values)) { - $stmt = sqlsrv_prepare($conn, $sql); + if (empty($conds) || empty($values)) { + $stmt = sqlsrv_prepare($conn, $sql, null, $options); } else { $sql = $sql . " WHERE $conds "; - $stmt = sqlsrv_prepare($conn, $sql, $values); + $stmt = sqlsrv_prepare($conn, $sql, $values, $options); } if ($stmt) { $r = sqlsrv_execute($stmt); diff --git a/test/functional/sqlsrv/sqlsrv_buffered.phpt b/test/functional/sqlsrv/sqlsrv_buffered.phpt index dfdbc662..ffcdf479 100644 --- a/test/functional/sqlsrv/sqlsrv_buffered.phpt +++ b/test/functional/sqlsrv/sqlsrv_buffered.phpt @@ -1,46 +1,82 @@ --TEST-- Read, Update, Insert from a SQLSRV stream with buffered query --SKIPIF-- - + --FILE-- "This is field 2.", + "Field3" => array("010203", null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_IMAGE), + "Field4" => "This is field 4.", + "Field5" => array("040506", null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARBINARY('max')), + "Field6" => "This is field 6.", + "Field7" => "This is field 7."); + $query = AE\getInsertSqlPlaceholders($tableName, $data); + + // form the array of parameters + $params = array(); + foreach ($data as $key => $input) { + array_push($params, $input); + } + $stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); + } else { + $query = "INSERT INTO $tableName ([Field2], [Field3], [Field4], [Field5], [Field6], [Field7]) VALUES ('This is field 2.', 0x010203, 'This is field 4.', 0x040506, 'This is field 6.', 'This is field 7.' )"; + $stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); + } + + if (!$stmt) { + fatalError("insertOneRow: query could not be prepared.\n"); + } + $result = sqlsrv_execute($stmt); + if ($result === false) { + fatalError("insertOneRow: failed to insert data!\n"); + } + return $stmt; } -$query = "IF OBJECT_ID('PhpCustomerTable', 'U') IS NOT NULL DROP TABLE [PhpCustomerTable]"; -$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +function updateRow($conn, $tableName, $updateField, $params) +{ + $condField = 'Field7'; + $condition = 'This is field 7.'; -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); + if (AE\isColEncrypted()) { + $query = "UPDATE $tableName SET $updateField=? WHERE $condField = ?"; + + array_push($params, array($condition, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_NVARCHAR('max'))); + } else { + $query = "UPDATE $tableName SET $updateField=? WHERE $condField = '$condition'"; + } + $stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); + if (!$stmt) { + fatalError("updateRow: query could not be prepared.\n"); + } + $result = sqlsrv_execute($stmt); + if ($result === false) { + fatalError("updateRow: failed to update $updateField!\n"); + } + sqlsrv_free_stmt($stmt); } -sqlsrv_execute($stmt); +$conn = AE\connect(); +$tableName = 'PhpCustomerTable'; -$query = "CREATE TABLE [PhpCustomerTable] ([Id] int NOT NULL Identity (100,2) PRIMARY KEY, [Field2] text, [Field3] image, [Field4] ntext, [Field5] varbinary(max), [Field6] varchar(max), [Field7] nvarchar(max))"; -$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +// Create the test table and insert one row +$columns = array(new AE\ColumnMeta('int', 'Id', 'NOT NULL Identity (100,2) PRIMARY KEY'), + new AE\ColumnMeta('text', 'Field2'), + new AE\ColumnMeta('image', 'Field3'), + new AE\ColumnMeta('ntext', 'Field4'), + new AE\ColumnMeta('varbinary(max)', 'Field5'), + new AE\ColumnMeta('varchar(max)', 'Field6'), + new AE\ColumnMeta('nvarchar(max)', 'Field7')); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); - -$query = "INSERT [PhpCustomerTable] ([Field2], [Field3], [Field4], [Field5], [Field6], [Field7]) VALUES ('This is field 2.', 0x010203, 'This is field 4.', 0x040506, 'This is field 6.', 'This is field 7.' )"; -$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); - -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); +AE\createTableEx($conn, $tableName, $columns, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +$stmt = insertOneRow($conn, $tableName); $f2 = fopen('php://memory', 'a'); fwrite($f2, 'Update field 2.'); @@ -61,88 +97,27 @@ $f7 = fopen('php://memory', 'a'); fwrite($f7, 'Update field 7.'); rewind($f7); - - -$query = "UPDATE [PhpCustomerTable] SET [Field2]=? WHERE [Field7]='This is field 7.'"; +// Update data in the table $params = array(array(&$f2, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_TEXT)); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field2', $params); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - -$query = "UPDATE [PhpCustomerTable] SET [Field3]=? WHERE [Field7]='This is field 7.'"; $params = array(array(&$f3, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_IMAGE)); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field3', $params); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - -$query = "UPDATE [PhpCustomerTable] SET [Field4]=? WHERE [Field7]='This is field 7.'"; $params = array(array(&$f4, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NTEXT)); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field4', $params); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - -$query = "UPDATE [PhpCustomerTable] SET [Field5]=? WHERE [Field7]='This is field 7.'"; $params = array(array(&$f5, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max'))); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field5', $params); - -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - - -$query = "UPDATE [PhpCustomerTable] SET [Field6]=? WHERE [Field7]='This is field 7.'"; $params = array(array(&$f6, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR('MAX'))); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field6', $params); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - -$query = "UPDATE [PhpCustomerTable] SET [Field7]=? WHERE [Field7]='This is field 7.'"; $params = array(array(&$f7, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NVARCHAR('MAX'))); -$stmt = sqlsrv_prepare($conn, $query, $params, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); +updateRow($conn, $tableName, 'Field7', $params); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} - -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - - -$stmt = sqlsrv_query($conn, "SELECT * FROM [PhpCustomerTable]", array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} +// Fetch data from the table +$stmt = AE\executeQueryEx($conn, "SELECT * FROM [PhpCustomerTable]", array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); sqlsrv_fetch($stmt); $field = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); @@ -195,7 +170,7 @@ if (!$field) { print("$field\n"); } -sqlsrv_query($conn, "DROP TABLE [PhpCustomerTable]"); +dropTable($conn, $tableName); sqlsrv_free_stmt($stmt); sqlsrv_close($conn); diff --git a/test/functional/sqlsrv/sqlsrv_buffered_result_set_extended_ascii.phpt b/test/functional/sqlsrv/sqlsrv_buffered_result_set_extended_ascii.phpt index 3aae5741..58e14f77 100644 --- a/test/functional/sqlsrv/sqlsrv_buffered_result_set_extended_ascii.phpt +++ b/test/functional/sqlsrv/sqlsrv_buffered_result_set_extended_ascii.phpt @@ -1,40 +1,33 @@ --TEST-- Fetch array of extended ASCII data using a scrollable buffered cursor --SKIPIF-- - + --FILE-- 'UTF-8' )); -if (!$conn) { - die(print_r(sqlsrv_errors(), true)); -} +$conn = AE\connect(array('CharacterSet'=>'UTF-8')); // Create table -$tableName = '#exAsciiTest'; -$query = "CREATE TABLE $tableName (ID CHAR(10))"; -$stmt = sqlsrv_query($conn, $query); +$tableName = 'exAsciiTest'; +$columns = array(new AE\ColumnMeta('CHAR(10)', 'ID')); +AE\createTable($conn, $tableName, $columns); // Insert data -$query = "INSERT INTO $tableName VALUES ('Aå_Ð×Æ×Ø_B')"; -$stmt = sqlsrv_query($conn, $query); -if (! $stmt) { - die(print_r(sqlsrv_errors(), true)); -} +$res = null; +$stmt = AE\insertRow($conn, $tableName, array('ID' => 'Aå_Ð×Æ×Ø_B')); // Fetch data $query = "SELECT * FROM $tableName"; -$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered")); -if (! $stmt) { - die(print_r(sqlsrv_errors(), true)); -} +$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>"buffered")); // Fetch $row = sqlsrv_fetch_array($stmt); var_dump($row); +dropTable($conn, $tableName); + // Close connection sqlsrv_free_stmt($stmt); sqlsrv_close($conn); diff --git a/test/functional/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt b/test/functional/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt index 13f20875..7f2eff92 100644 --- a/test/functional/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt +++ b/test/functional/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt @@ -1,24 +1,27 @@ --TEST-- Test with static cursor and select different rows in some random order +--SKIPIF-- + --FILE--  'static')); - HasRows($stmt); + hasRows($stmt); $numRowsFetched = 0; while ($obj = sqlsrv_fetch_object($stmt)) { echo $obj->c1_int . ", " . $obj->c2_varchar . "\n"; @@ -29,25 +32,27 @@ function FetchRow_Query($conn) echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n"; } - 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); + 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); + + dropTable($conn, $tableName); } -function InsertData($conn, $tableName, $numRows) +function insertData($conn, $tableName, $numRows) { $stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_varchar) VALUES (?, ?)", array(&$v1, &$v2)); @@ -59,7 +64,7 @@ function InsertData($conn, $tableName, $numRows) } } -function GetFirstRow($stmt) +function getFirstRow($stmt) { echo "\nfirst row: "; $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); @@ -70,7 +75,7 @@ function GetFirstRow($stmt) } } -function GetNextRow($stmt) +function getNextRow($stmt) { echo "\nnext row: "; $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_NEXT); @@ -81,7 +86,7 @@ function GetNextRow($stmt) } } -function GetPriorRow($stmt) +function getPriorRow($stmt) { echo "\nprior row: "; $obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_PRIOR); @@ -90,7 +95,7 @@ function GetPriorRow($stmt) } } -function GetLastRow($stmt) +function getLastRow($stmt) { echo "\nlast row: "; $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_LAST); @@ -99,7 +104,7 @@ function GetLastRow($stmt) } } -function GetRelativeRow($stmt, $offset) +function getRelativeRow($stmt, $offset) { echo "\nrow $offset from the current row: "; $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_RELATIVE, $offset); @@ -108,7 +113,7 @@ function GetRelativeRow($stmt, $offset) } } -function GetAbsoluteRow($stmt, $offset) +function getAbsoluteRow($stmt, $offset) { echo "\nabsolute row with offset $offset: "; $obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_ABSOLUTE, $offset); @@ -117,7 +122,7 @@ function GetAbsoluteRow($stmt, $offset) } } -function HasRows($stmt) +function hasRows($stmt) { $rows = sqlsrv_has_rows($stmt); if ($rows != true) { @@ -125,36 +130,18 @@ function HasRows($stmt) } } -//-------------------------------------------------------------------- -// RunTest -// -//-------------------------------------------------------------------- -function RunTest() -{ - startTest("sqlsrv_fetch_cursor_static_scroll"); - try { - set_time_limit(0); - sqlsrv_configure('WarningsReturnAsErrors', 1); +set_time_limit(0); +sqlsrv_configure('WarningsReturnAsErrors', 1); - echo "\nTest begins...\n"; +echo "\nTest begins...\n"; - // Connect - $conn = connect(); - if (!$conn) { - fatalError("Could not connect.\n"); - } +// Connect +$conn = AE\connect(); +fetchRowQuery($conn); - FetchRow_Query($conn); - - sqlsrv_close($conn); - } catch (Exception $e) { - echo $e->getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_fetch_cursor_static_scroll"); -} - -RunTest(); +sqlsrv_close($conn); +echo "\nDone\n"; +endTest("sqlsrv_fetch_cursor_static_scroll"); ?> --EXPECT-- diff --git a/test/functional/sqlsrv/sqlsrv_fetch_cursor_types.phpt b/test/functional/sqlsrv/sqlsrv_fetch_cursor_types.phpt index dde8b0bb..716c80d4 100644 --- a/test/functional/sqlsrv/sqlsrv_fetch_cursor_types.phpt +++ b/test/functional/sqlsrv/sqlsrv_fetch_cursor_types.phpt @@ -1,25 +1,28 @@ --TEST-- Test various cursor types and whether they reflect changes in the database +--SKIPIF-- + --FILE-- - $cursorType)); sqlsrv_execute($stmt); - GetNumRows($stmt, $cursorType); + getNumRows($stmt, $cursorType); $numRowsFetched = 0; while ($obj = sqlsrv_fetch_object($stmt)) { echo $obj->c1_int . "\n"; @@ -30,10 +33,12 @@ function Fetch_WithCursor($conn, $cursorType) echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n"; } - DeleteThenFetchLastRow($conn, $stmt, $tableName, 4); + deleteThenFetchLastRow($conn, $stmt, $tableName, 4); + + dropTable($conn, $tableName); } -function InsertData($conn, $tableName, $start, $count) +function insertData($conn, $tableName, $start, $count) { $stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_char) VALUES (?, ?)", array(&$v1, &$v2)); @@ -46,13 +51,10 @@ function InsertData($conn, $tableName, $start, $count) } } -function DeleteThenFetchLastRow($conn, $stmt, $tableName, $id) +function deleteThenFetchLastRow($conn, $stmt, $tableName, $id) { echo "\nNow delete the last row then try to fetch it...\n"; - $stmt2 = sqlsrv_query($conn, "DELETE FROM $tableName WHERE [c1_int] = 4"); - if ($stmt2 !== false) { - sqlsrv_free_stmt($stmt2); - } + $stmt2 = AE\executeQuery($conn, "DELETE FROM $tableName", "[c1_int] = ?", array(4)); $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST); if ($result) { @@ -65,7 +67,7 @@ function DeleteThenFetchLastRow($conn, $stmt, $tableName, $id) } } -function GetNumRows($stmt, $cursorType) +function getNumRows($stmt, $cursorType) { $expectedToFail = false; if ($cursorType == SQLSRV_CURSOR_FORWARD || $cursorType == SQLSRV_CURSOR_DYNAMIC) { @@ -89,48 +91,31 @@ function GetNumRows($stmt, $cursorType) } } -//-------------------------------------------------------------------- -// RunTest -// -//-------------------------------------------------------------------- -function RunTest() -{ - startTest("sqlsrv_fetch_cursor_types"); - try { - set_time_limit(0); - sqlsrv_configure('WarningsReturnAsErrors', 1); +try { + set_time_limit(0); + sqlsrv_configure('WarningsReturnAsErrors', 1); - echo "\nTest begins...\n"; + // Connect + $conn = AE\connect(); - // Connect - $conn = connect(); - if (!$conn) { - fatalError("Could not connect.\n"); - } + echo "\nUsing SQLSRV_CURSOR_FORWARD...\n"; + fetchWithCursor($conn, SQLSRV_CURSOR_FORWARD); + echo "\nUsing SQLSRV_CURSOR_DYNAMIC...\n"; + fetchWithCursor($conn, SQLSRV_CURSOR_DYNAMIC); + echo "\nUsing SQLSRV_CURSOR_KEYSET...\n"; + fetchWithCursor($conn, SQLSRV_CURSOR_KEYSET); + echo "\nUsing SQLSRV_CURSOR_STATIC...\n"; + fetchWithCursor($conn, SQLSRV_CURSOR_STATIC); - echo "\nUsing SQLSRV_CURSOR_FORWARD...\n"; - Fetch_WithCursor($conn, SQLSRV_CURSOR_FORWARD); - echo "\nUsing SQLSRV_CURSOR_DYNAMIC...\n"; - Fetch_WithCursor($conn, SQLSRV_CURSOR_DYNAMIC); - echo "\nUsing SQLSRV_CURSOR_KEYSET...\n"; - Fetch_WithCursor($conn, SQLSRV_CURSOR_KEYSET); - echo "\nUsing SQLSRV_CURSOR_STATIC...\n"; - Fetch_WithCursor($conn, SQLSRV_CURSOR_STATIC); - - sqlsrv_close($conn); - } catch (Exception $e) { - echo $e->getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_fetch_cursor_types"); + sqlsrv_close($conn); +} catch (Exception $e) { + echo $e->getMessage(); } - -RunTest(); +echo "\nDone\n"; +endTest("sqlsrv_fetch_cursor_types"); ?> --EXPECT-- - -Test begins... Using SQLSRV_CURSOR_FORWARD... Error occurred in sqlsrv_num_rows, which is expected diff --git a/test/functional/sqlsrv/sqlsrv_fetch_missing_row.phpt b/test/functional/sqlsrv/sqlsrv_fetch_missing_row.phpt index 8a7abd27..1254cd80 100644 --- a/test/functional/sqlsrv/sqlsrv_fetch_missing_row.phpt +++ b/test/functional/sqlsrv/sqlsrv_fetch_missing_row.phpt @@ -1,26 +1,23 @@ --TEST-- Fetch missing row +--SKIPIF-- + --FILE-- -getMessage(); - } - echo "\nDone\n"; - endTest("sqlsrv_fetch_missing_row"); -} - -Repro(); +missingRowFetch(); +endTest("sqlsrv_fetch_missing_row"); ?> --EXPECT-- - -Test begins... There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved. -22 IMSSP - -Done Test "sqlsrv_fetch_missing_row" completed successfully. diff --git a/test/functional/sqlsrv/sqlsrv_fetch_object.phpt b/test/functional/sqlsrv/sqlsrv_fetch_object.phpt index af157ac9..c1231745 100644 --- a/test/functional/sqlsrv/sqlsrv_fetch_object.phpt +++ b/test/functional/sqlsrv/sqlsrv_fetch_object.phpt @@ -1,7 +1,7 @@ --TEST-- Test for fetch_object --SKIPIF-- - + --FILE-- + --FILE-- + --FILE-- 'UTF-8' )); +$conn = AE\connect(array( 'CharacterSet'=>'UTF-8' )); +$tableName = 'test_params'; -$stmt = sqlsrv_prepare($conn, "IF OBJECT_ID('test_params', 'U') IS NOT NULL DROP TABLE test_params"); -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); - -$stmt = sqlsrv_prepare($conn, "CREATE TABLE test_params (id tinyint, 吉安而來 char(10), [此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é] float, stuff varchar(max))"); -sqlsrv_execute($stmt); -sqlsrv_free_stmt($stmt); +$columns = array(new AE\ColumnMeta('tinyint', 'id'), + new AE\ColumnMeta('char(10)', '吉安而來'), + new AE\ColumnMeta('float', '此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é'), + new AE\ColumnMeta('varchar(max)', 'stuff')); +AE\createTable($conn, $tableName, $columns); $f1 = 1; $f2 = "testtestte"; $f3 = 12.0; $f4 = fopen("data://text/plain,This%20is%20some%20text%20meant%20to%20test%20binding%20parameters%20to%20streams", "r"); -$stmt = sqlsrv_prepare($conn, "INSERT INTO test_params (id, 吉安而來, [此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é], stuff) VALUES (?, ?, ?, ?)", array( &$f1, "testtestte", &$f3, &$f4 )); //, - //~ array( SQLSRV_SQLTYPE_INTEGER, SQLSRV_SQLTYPE_CHAR(10), SQLSRV_SQLTYPE_此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é, SQLSRV_SQLTYPE_VARBINARY(4000))); +$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (id, 吉安而來, [此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é], stuff) VALUES (?, ?, ?, ?)", array( &$f1, "testtestte", &$f3, &$f4 )); if (!$stmt) { var_dump(sqlsrv_errors()); die("sqlsrv_prepare failed."); @@ -147,7 +142,7 @@ if (!is_null($success)) { sqlsrv_free_stmt($stmt); -$stmt = sqlsrv_prepare($conn, "SELECT id, [此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é], 吉安而來, stuff FROM test_params"); +$stmt = sqlsrv_prepare($conn, "SELECT id, [此是後話Κοντάוְאַתָּה第十四章BiałopioБунтевсемужирафиtest是أي بزمام الإنذارהნომინავიiałopioБунтевсемужирафиtest父親回衙 汗流如雨 吉安而來. 關雎 誨€¥É§é], 吉安而來, stuff FROM $tableName"); $success = sqlsrv_execute($stmt); if (!$success) { var_dump(sqlsrv_errors()); @@ -234,6 +229,11 @@ if (is_null($obj)) { print_r($obj); } +dropTable($conn, $tableName); + +sqlsrv_free_stmt( $stmt ); +sqlsrv_close( $conn ); + ?> --EXPECTREGEX-- Fetch a stdClass object \(1\) diff --git a/test/functional/sqlsrv/sqlsrv_fetch_object_unicode_col_name2.phpt b/test/functional/sqlsrv/sqlsrv_fetch_object_unicode_col_name2.phpt index e00a8c48..b11a836c 100644 --- a/test/functional/sqlsrv/sqlsrv_fetch_object_unicode_col_name2.phpt +++ b/test/functional/sqlsrv/sqlsrv_fetch_object_unicode_col_name2.phpt @@ -1,10 +1,11 @@ --TEST-- sqlsrv_fetch_object() into a class with Unicode column name --SKIPIF-- + --FILE-- $inputs[0], + 'личное_имя'=> $inputs[1], + 'SafetyStockLevel' => $inputs[2], + 'StockedQty' => $inputs[3], + 'UnitPrice' => $inputs[4], + 'DueDate' => $inputs[5], + 'Color' => $inputs[6]); +} + +function getInputData2($inputs) +{ + return array('SerialNumber' => $inputs[0], + 'Code'=> $inputs[1]); +} require_once('MsCommon.inc'); -$tableName = "UnicodeColNameTest"; - -include 'MsSetup.inc'; - -$conn = connect(array( 'CharacterSet'=>'UTF-8' )); - -$tableName = "UnicodeColNameTest"; - - +$conn = AE\connect(array('CharacterSet'=>'UTF-8')); // Create table Purchasing $tableName1 = "Purchasing"; $tableName2 = "Country"; -dropTable($conn, $tableName1); -dropTable($conn, $tableName2); -$sql = "create table $tableName1 (ID CHAR(4), личное_имя VARCHAR(128), SafetyStockLevel SMALLINT, - StockedQty INT, UnitPrice FLOAT, DueDate datetime, Color VARCHAR(20))"; -sqlsrv_query($conn, $sql) ?: die(print_r(sqlsrv_errors(), true)); + +$columns = array(new AE\ColumnMeta('CHAR(4)', 'ID'), + new AE\ColumnMeta('VARCHAR(128)', 'личное_имя'), + new AE\ColumnMeta('SMALLINT', 'SafetyStockLevel'), + new AE\ColumnMeta('INT', 'StockedQty'), + new AE\ColumnMeta('FLOAT', 'UnitPrice'), + new AE\ColumnMeta('datetime', 'DueDate'), + new AE\ColumnMeta('VARCHAR(20)', 'Color')); +AE\createTable($conn, $tableName1, $columns); // Insert data -$sql = "INSERT INTO $tableName1 VALUES - ('P001','Pencil 2B','102','24','0.24','2016-02-01','Red'), - ('P002','Notepad','102','12','3.87', '2016-02-21',Null), - ('P001','Mirror 2\"','652','3','15.99', '2016-02-01',NULL), - ('P003','USB connector','1652','31','9.99','2016-02-01',NULL)"; -sqlsrv_query($conn, $sql) ?: die(print_r(sqlsrv_errors(), true)); +$params = array('P001', 'Pencil 2B', '102', '24', '0.24', '2016-02-01', 'Red'); +$data = getInputData1($params); +AE\insertRow($conn, $tableName1, $data); + +$params = array('P002', 'Notepad', '102', '12', '3.87', '2016-02-21', null); +$data = getInputData1($params); +AE\insertRow($conn, $tableName1, $data); + +$params = array('P001', 'Mirror 2\"', '652', '3', '15.99', '2016-02-01', null); +$data = getInputData1($params); +AE\insertRow($conn, $tableName1, $data); + +$params = array('P003', 'USB connector', '1652', '31', '9.99', '2016-02-01', null); +$data = getInputData1($params); +AE\insertRow($conn, $tableName1, $data); // Create table Country -$sql = "create table $tableName2 (SerialNumber CHAR(4), Code VARCHAR(2))"; -sqlsrv_query($conn, $sql) ?: die(print_r(sqlsrv_errors(), true)); +$columns = array(new AE\ColumnMeta('CHAR(4)', 'SerialNumber'), + new AE\ColumnMeta('VARCHAR(2)', 'Code')); +AE\createTable($conn, $tableName2, $columns); // Insert data -$sql = "INSERT INTO $tableName2 VALUES ('P001','FR'),('P002','UK'),('P003','DE')"; -sqlsrv_query($conn, $sql) ?: die(print_r(sqlsrv_errors(), true)); +$params = array('P001', 'FR'); +$data = getInputData2($params); +AE\insertRow($conn, $tableName2, $data); -/* Define the query. */ -$sql = "SELECT личное_имя, SafetyStockLevel, StockedQty, UnitPrice, Color, Code - FROM $tableName1 AS Purchasing - JOIN $tableName2 AS Country - ON Purchasing.ID = Country.SerialNumber - WHERE Purchasing.StockedQty < ? - AND Purchasing.UnitPrice < ? - AND Purchasing.DueDate= ?"; +$params = array('P002', 'UK'); +$data = getInputData2($params); +AE\insertRow($conn, $tableName2, $data); -/* Set the parameter values. */ -$params = array(100, '10.5', '2016-02-01'); +$params = array('P003', 'DE'); +$data = getInputData2($params); +AE\insertRow($conn, $tableName2, $data); -/* Execute the query. */ -$stmt = sqlsrv_query($conn, $sql, $params, array("Scrollable"=>"static")); //, array("Scrollable"=>"buffered") -if (!$stmt) { - echo "Error in statement execution.\n"; - die(print_r(sqlsrv_errors(), true)); +// With AE enabled, we cannot do comparisons with encrypted columns +// Also, only forward cursor or client buffer is supported +if (AE\isColEncrypted()) { + $sql = "SELECT личное_имя, SafetyStockLevel, StockedQty, UnitPrice, Color, Code + FROM $tableName1 AS Purchasing + JOIN $tableName2 AS Country + ON Purchasing.ID = Country.SerialNumber + WHERE Purchasing.личное_имя != ? + AND Purchasing.StockedQty != ? + AND Purchasing.DueDate= ?"; + + $params = array('Notepad', 3, '2016-02-01'); + $stmt = sqlsrv_prepare($conn, $sql, $params, array("Scrollable"=>"buffered")); + if ($stmt) { + $res = sqlsrv_execute($stmt); + if (!$res) { + fatalError("Error in statement execution.\n"); + } + } else { + fatalError("Error in preparing statement.\n"); + } +} else { + $sql = "SELECT личное_имя, SafetyStockLevel, StockedQty, UnitPrice, Color, Code + FROM $tableName1 AS Purchasing + JOIN $tableName2 AS Country + ON Purchasing.ID = Country.SerialNumber + WHERE Purchasing.StockedQty < ? + AND Purchasing.UnitPrice < ? + AND Purchasing.DueDate= ?"; + + $params = array(100, '10.5', '2016-02-01'); + $stmt = sqlsrv_query($conn, $sql, $params, array("Scrollable"=>"static")); + if (!$stmt) { + fatalError("Error in statement execution.\n"); + } } -// Iterate through the result set. -// $product is an instance of the Product class. -$i=0; $hasNext = true; +// Iterate through the result set +// $product is an instance of the Product class +$i=0; +$hasNext = true; while ($hasNext) { $sample = sqlsrv_fetch_object($stmt, "Sample", array($i+1000), SQLSRV_SCROLL_ABSOLUTE, $i); - // DEBUG: uncomment to see the SQL_SERVER ERROR - // if(!$sample) die( print_r( sqlsrv_errors(), true)); - if (!$sample) { $hasNext = false; } else { @@ -130,12 +178,10 @@ while ($hasNext) { } } -// DROP database -// $stmt = sqlsrv_query($conn,"DROP DATABASE ". $dbName); - //echo $dbName; - dropTable($conn, $tableName1); - dropTable($conn, $tableName2); -// Free statement and connection resources.s +dropTable($conn, $tableName1); +dropTable($conn, $tableName2); + +// Free statement and connection resources sqlsrv_free_stmt($stmt); sqlsrv_close($conn); diff --git a/test/functional/sqlsrv/sqlsrv_num_rows.phpt b/test/functional/sqlsrv/sqlsrv_num_rows.phpt index 7956f14e..d6e8c34b 100644 --- a/test/functional/sqlsrv/sqlsrv_num_rows.phpt +++ b/test/functional/sqlsrv/sqlsrv_num_rows.phpt @@ -1,30 +1,30 @@ --TEST-- Test sqlsrv_num_rows method. --SKIPIF-- - + --FILE-- 'TEST')); + + // Always Encrypted feature only supports SQLSRV_CURSOR_FORWARD or + // SQLSRV_CURSOR_CLIENT_BUFFERED + if (AE\isColEncrypted()) { + $options = array('Scrollable' => SQLSRV_CURSOR_CLIENT_BUFFERED); + } else { + $options = array('Scrollable' => SQLSRV_CURSOR_KEYSET); } - - dropTable($conn, 'utf16invalid'); - $stmt = sqlsrv_query($conn, "CREATE TABLE utf16invalid (id int identity, c1 nvarchar(100))"); - if ($stmt === false) { - die(print_r(sqlsrv_errors(), true)); - } - - $stmt = sqlsrv_query($conn, "INSERT INTO utf16invalid (c1) VALUES ('TEST')"); - if ($stmt === false) { - die(print_r(sqlsrv_errors())); - } - $stmt = sqlsrv_query($conn, "SELECT * FROM utf16invalid", array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET )); + $stmt = AE\executeQueryEx($conn, "SELECT * FROM $tableName", $options); $row_nums = sqlsrv_num_rows($stmt); echo $row_nums; diff --git a/test/functional/sqlsrv/srv_012_sqlsrv_fetch_array.phpt b/test/functional/sqlsrv/srv_012_sqlsrv_fetch_array.phpt index 52398553..d3bb5523 100644 --- a/test/functional/sqlsrv/srv_012_sqlsrv_fetch_array.phpt +++ b/test/functional/sqlsrv/srv_012_sqlsrv_fetch_array.phpt @@ -1,30 +1,27 @@ --TEST-- sqlsrv_fetch_array() using a scrollable cursor --SKIPIF-- + --FILE-- '1998.1')); +AE\insertRow($conn, $tableName, array("ID" => '-2004')); +AE\insertRow($conn, $tableName, array("ID" => '2016')); +AE\insertRow($conn, $tableName, array("ID" => '4.2EUR')); // Fetch data $query = "SELECT ID FROM $tableName"; -$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered")) - ?: die(print_r(sqlsrv_errors(), true)); +$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>"buffered")); // Fetch first row $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_NEXT); @@ -38,6 +35,8 @@ echo $row['ID']."\n"; $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_LAST); echo $row['ID']."\n"; +dropTable($conn, $tableName); + sqlsrv_free_stmt($stmt); sqlsrv_close($conn); print "Done" diff --git a/test/functional/sqlsrv/srv_037_sqlsrv_has_rows.phpt b/test/functional/sqlsrv/srv_037_sqlsrv_has_rows.phpt index 192472c4..dab853f6 100644 --- a/test/functional/sqlsrv/srv_037_sqlsrv_has_rows.phpt +++ b/test/functional/sqlsrv/srv_037_sqlsrv_has_rows.phpt @@ -5,30 +5,27 @@ This test calls sqlsrv_has_rows multiple times. Previously, multiple calls with a forward cursor would advance the cursor. Subsequent fetch calls would then fail. --SKIPIF-- + --FILE-- '1998.1')); +AE\insertRow($conn, $tableName, array("ID" => '-2004')); +AE\insertRow($conn, $tableName, array("ID" => '2016')); +AE\insertRow($conn, $tableName, array("ID" => '4.2EUR')); // Fetch data using forward only cursor $query = "SELECT ID FROM $tableName"; -$stmt = sqlsrv_query($conn, $query) - ?: die(print_r(sqlsrv_errors(), true)); +$stmt = AE\executeQuery($conn, $query); // repeated calls should return true and fetch should work. echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; @@ -43,8 +40,7 @@ if (sqlsrv_has_rows($stmt)) { } // Fetch data using a scrollable cursor -$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered")) - ?: die(print_r(sqlsrv_errors(), true)); +$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>"buffered")); echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; @@ -57,22 +53,21 @@ if (sqlsrv_has_rows($stmt)) { } } -$query = "SELECT ID FROM $tableName where ID='nomatch'"; -$stmt = sqlsrv_query($conn, $query) - ?: die(print_r(sqlsrv_errors(), true)); +// $query = "SELECT ID FROM $tableName where ID='nomatch'"; +$stmt = AE\executeQuery($conn, $query, "ID = ?", array('nomatch')); // repeated calls should return false if there are no rows. echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; // Fetch data using a scrollable cursor -$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered")) - ?: die(print_r(sqlsrv_errors(), true)); +$stmt = AE\executeQuery($conn, $query, "ID = ?", array('nomatch'), array("Scrollable"=>"buffered")); // repeated calls should return false if there are no rows. echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; +dropTable($conn, $tableName); sqlsrv_free_stmt($stmt); sqlsrv_close($conn); diff --git a/test/functional/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt b/test/functional/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt index 08335ebd..53183eb1 100644 --- a/test/functional/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt +++ b/test/functional/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt @@ -1,38 +1,27 @@ --TEST-- GitHub issue #69 - fetching an empty nvarchar using client buffer --SKIPIF-- + --FILE-- 'buffered']); - if (! $stmt) { print_errors(); } +$sql = "EXEC dbo.sp_executesql +N'DECLARE @x nvarchar(max) +SET @x = '''' -- empty string +SELECT @x AS [Empty_Nvarchar_Max]'"; - $return = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC); - print_r($return); - - // Free the statement and connection resources. - sqlsrv_free_stmt( $stmt); - sqlsrv_close( $conn); -} - -test(); +$stmt = AE\executeQueryEx($conn, $sql, ["Scrollable" => 'buffered']); + +$return = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC); +print_r($return); + +// Free the statement and connection resources. +sqlsrv_free_stmt( $stmt); +sqlsrv_close( $conn); print "Done"; ?> diff --git a/test/functional/sqlsrv/srv_223_sqlsrv_fetch_absolute.phpt b/test/functional/sqlsrv/srv_223_sqlsrv_fetch_absolute.phpt index 3b5376e3..76dffa1e 100644 --- a/test/functional/sqlsrv/srv_223_sqlsrv_fetch_absolute.phpt +++ b/test/functional/sqlsrv/srv_223_sqlsrv_fetch_absolute.phpt @@ -1,61 +1,53 @@ --TEST-- sqlsrv_fetch() with SQLSRV_SCROLL_ABSOLUTE using out of range offset --SKIPIF-- + --FILE-- SQLSRV_CURSOR_CLIENT_BUFFERED)); +if ($stmt === false) { + printErrors(); +} +sqlsrv_execute($stmt); - // Prepare the statement - $sql = "select * from cd_info"; - $stmt = sqlsrv_prepare($conn, $sql, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); - if ($stmt === false) { - printErrors(); - } - sqlsrv_execute($stmt); - - // Get row count - $row_count = sqlsrv_num_rows($stmt); - if ($row_count == 0) { - printErrors("There should be at least one row!\n"); - } - - sqlsrv_execute($stmt); - $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); - $field = sqlsrv_get_field($stmt, 0); - if (! $field) { - printErrors(); - } - - $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST); - $field = sqlsrv_get_field($stmt, 0); - if (! $field) { - printErrors(); - } - - // this should return false - $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, $row_count); - if ($row) { - printErrors("This should return false!"); - } - $field = sqlsrv_get_field($stmt, 0); - if ($field !== false) { - printErrors("This should have resulted in error!"); - } - - sqlsrv_free_stmt($stmt); - sqlsrv_close($conn); +// Get row count +$row_count = sqlsrv_num_rows($stmt); +if ($row_count == 0) { + printErrors("There should be at least one row!\n"); } -test(); +sqlsrv_execute($stmt); +$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); +$field = sqlsrv_get_field($stmt, 0); +if (! $field) { + printErrors(); +} + +$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST); +$field = sqlsrv_get_field($stmt, 0); +if (! $field) { + printErrors(); +} + +// this should return false +$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, $row_count); +if ($row) { + printErrors("This should return false!"); +} +$field = sqlsrv_get_field($stmt, 0); +if ($field !== false) { + printErrors("This should have resulted in error!"); +} + +sqlsrv_free_stmt($stmt); +sqlsrv_close($conn); print "Done"; ?> diff --git a/test/functional/sqlsrv/srv_228_sqlsrv_clientbuffermaxkbsize.phpt b/test/functional/sqlsrv/srv_228_sqlsrv_clientbuffermaxkbsize.phpt index 330e639e..6b4d1100 100644 --- a/test/functional/sqlsrv/srv_228_sqlsrv_clientbuffermaxkbsize.phpt +++ b/test/functional/sqlsrv/srv_228_sqlsrv_clientbuffermaxkbsize.phpt @@ -1,10 +1,11 @@ --TEST-- sqlsrv_has_rows() using a forward and scrollable cursor --SKIPIF-- + --FILE-- 1KB into c2_varchar_max & c2_varchar_1036 (1036 characters). -$stmt = sqlsrv_query($conn, "INSERT INTO $tableName1 (c1_int, c2_varchar_max) VALUES (1, 'This is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a test')"); -$stmt = sqlsrv_query($conn, "INSERT INTO $tableName2 (c1_int, c2_varchar_1036) VALUES (1, 'This is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a test')"); +$longString = 'This is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a test'; + +$stmt = AE\insertRow($conn, $tableName1, array('c1_int' => 1, 'c2_varchar_max' => $longString)); +$stmt = AE\insertRow($conn, $tableName2, array('c1_int' => 1, 'c2_varchar_1036' => $longString)); // set client buffer size to 0KB returns false $ret = sqlsrv_configure('ClientBufferMaxKBSize', 0); diff --git a/test/functional/sqlsrv/srv_230_sqlsrv_buffered_numeric_types.phpt b/test/functional/sqlsrv/srv_230_sqlsrv_buffered_numeric_types.phpt index eea58d43..f1550024 100644 --- a/test/functional/sqlsrv/srv_230_sqlsrv_buffered_numeric_types.phpt +++ b/test/functional/sqlsrv/srv_230_sqlsrv_buffered_numeric_types.phpt @@ -3,16 +3,28 @@ Read numeric types from SQLSRV with buffered query. --DESCRIPTION-- Test numeric conversion (number to string, string to number) functionality for buffered queries with SQLSRV. --SKIPIF-- + --FILE-- "utf-8")); -if (!$conn) { - printErrors("Connection could not be established.\n"); +function getInputData($inputs) +{ + return array('a' => $inputs[0], + 'neg_a'=> $inputs[1], + 'b' => $inputs[2], + 'neg_b' => $inputs[3], + 'c' => $inputs[4], + 'neg_c' => $inputs[5], + 'zero' => $inputs[6], + 'zerof' => $inputs[7], + 'zerod' => $inputs[8]); } +require_once('MsCommon.inc'); + +$conn = AE\connect(array("CharacterSet"=>"utf-8")); +$tableName = 'test230'; + $sample = 1234567890.1234; $sample1 = -1234567890.1234; $sample2 = 1; @@ -20,34 +32,29 @@ $sample3 = -1; $sample4 = 0.5; $sample5 = -0.55; -$query = 'CREATE TABLE #TESTTABLE (a float(53), neg_a float(53), b int, neg_b int, c decimal(16, 6), neg_c decimal(16, 6), zero int, zerof float(53), zerod decimal(16,6))'; - // Create table -$stmt = sqlsrv_query($conn, $query); -if ($stmt === false) { - die(print_r(sqlsrv_errors(), true)); -} +$columns = array(new AE\ColumnMeta('float(53)', 'a'), + new AE\ColumnMeta('float(53)', 'neg_a'), + new AE\ColumnMeta('int', 'b'), + new AE\ColumnMeta('int', 'neg_b'), + new AE\ColumnMeta('decimal(16, 6)', 'c'), + new AE\ColumnMeta('decimal(16, 6)', 'neg_c'), + new AE\ColumnMeta('int', 'zero'), + new AE\ColumnMeta('float(53)', 'zerof'), + new AE\ColumnMeta('decimal(16, 6)', 'zerod')); +AE\createTable($conn, $tableName, $columns); -$query = 'INSERT INTO #TESTTABLE (a, neg_a, b, neg_b, c, neg_c, zero, zerof, zerod) VALUES(?, ?, ?, ?, ?, ?, 0, 0, 0)'; -$params = array($sample, $sample1, $sample2, $sample3, $sample4, $sample5); +$res = null; +$params = array($sample, $sample1, $sample2, $sample3, $sample4, $sample5, 0, 0, 0); +$data = getInputData($params); +AE\insertRow($conn, $tableName, $data, $res, AE\INSERT_QUERY_PARAMS); -$stmt = sqlsrv_query($conn, $query, $params); -if ($stmt === false) { - die(print_r(sqlsrv_errors(), true)); -} -$params = array($sample4, $sample5, 100000, -1234567, $sample, $sample1); -$stmt = sqlsrv_query($conn, $query, $params); -if ($stmt === false) { - die(print_r(sqlsrv_errors(), true)); -} +$params = array($sample4, $sample5, 100000, -1234567, $sample, $sample1, 0, 0, 0); +$data = getInputData($params); +AE\insertRow($conn, $tableName, $data, $res, AE\INSERT_QUERY_PARAMS); -$query = 'SELECT TOP 2 * FROM #TESTTABLE'; -$stmt = sqlsrv_query($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); -if (!$stmt) { - echo "Statement could not be prepared.\n"; - die(print_r(sqlsrv_errors(), true)); -} -sqlsrv_execute($stmt); +$query = "SELECT TOP 2 * FROM $tableName"; +$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); $array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC); var_dump($array); @@ -73,6 +80,8 @@ for ($i = 0; $i < $rowcount; $i++) { } } +dropTable($conn, $tableName); + sqlsrv_free_stmt($stmt); sqlsrv_close($conn); diff --git a/test/functional/sqlsrv/srv_330_numrow_null_buffered_result_set.phpt b/test/functional/sqlsrv/srv_330_numrow_null_buffered_result_set.phpt index b3a03fd3..4589156d 100644 --- a/test/functional/sqlsrv/srv_330_numrow_null_buffered_result_set.phpt +++ b/test/functional/sqlsrv/srv_330_numrow_null_buffered_result_set.phpt @@ -3,12 +3,13 @@ GitHub issue #330 - get numrow of null buffered result set --DESCRIPTION-- A variation of the example in GitHub issue 330. A -1 value returned as numrow of a null buffered result set. --SKIPIF-- + --FILE-- + --FILE-- 'static' )); - -$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_ABSOLUTE, 4 ); -if( $result !== null ) { - die( "Should have failed with an invalid row number" ); -} -$rows = sqlsrv_has_rows( $stmt ); -if( $rows != true ) { - die( "Should have rows" ); -} -print_r( sqlsrv_errors() ); -$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_ABSOLUTE, -1 ); -if( $result !== null ) { - die( "Should have failed with an invalid row number" ); -} -print_r( sqlsrv_errors() ); - -$rows = sqlsrv_rows_affected( $stmt ); -print_r( sqlsrv_errors() ); - -$rows = sqlsrv_num_rows( $stmt ); -echo "Query returned $rows rows\n"; - -$row = 3; -$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_ABSOLUTE, $row ); -do { - $result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_ABSOLUTE, $row ); - if( $result === false ) { - die( print_r( sqlsrv_errors(), true )); +function hasRows($stmt, $expectedFail) +{ + $rows = sqlsrv_has_rows($stmt); + if ($expectedFail) { + if ($rows == true) { + die("Shouldn't have rows"); + } + } else { + if ($rows != true) { + die("Should have rows"); + } } - $field1 = sqlsrv_get_field( $stmt, 0 ); - $field2 = sqlsrv_get_field( $stmt, 1 ); - echo "$field1 $field2\n"; - $row = $row - 1; -} while( $row >= 0 ); - -sqlsrv_free_stmt( $stmt ); - -$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => SQLSRV_CURSOR_FORWARD )); -$rows = sqlsrv_has_rows( $stmt ); -if( $rows != true ) { - die( "Should have rows" ); } -$row_count = 0; -while( $row = sqlsrv_fetch( $stmt )) { + +function countRows($stmt, $numRows, $cursorType, $initialCount = 0) +{ + $row_count = $initialCount; + while ($row = sqlsrv_fetch($stmt)) { ++$row_count; -} -if( $row === false ) { - die( print_r( sqlsrv_errors(), true )); -} -echo "$row_count rows retrieved on the forward only cursor\n"; -sqlsrv_free_stmt( $stmt ); - -$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'static' )); -$rows = sqlsrv_has_rows( $stmt ); -if( $rows != true ) { - die( "Should have rows" ); -} -$row_count = 0; -while( $row = sqlsrv_fetch( $stmt )) { - ++$row_count; -} -if( $row === false ) { - die( print_r( sqlsrv_errors(), true )); -} -echo "$row_count rows retrieved on the static cursor\n"; -sqlsrv_free_stmt( $stmt ); - -$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'dynamic' )); - -sqlsrv_fetch( $stmt ); -sqlsrv_fetch( $stmt ); - -$stmt2 = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 5, "Row 5" )); -if( $stmt2 === false ) { - die( print_r( sqlsrv_errors(), true )); -} -$stmt2 = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 6, "Row 6" )); -if( $stmt2 === false ) { - die( print_r( sqlsrv_errors(), true )); + } + if($row === false) { + die(print_r(sqlsrv_errors(), true)); + } + if ($row_count != $numRows) { + echo "ERROR: $row_count rows retrieved on the $cursorType cursor\n"; + } } -$row_count = 2; // for the two fetches above -while( sqlsrv_fetch( $stmt )) { - ++$row_count; -} -echo "$row_count rows retrieved on the dynamic cursor\n"; -sqlsrv_free_stmt( $stmt ); +function insertOneRow($conn, $tableName, $idx, $expectedFail) +{ + $res = null; + $stmt = AE\insertRow($conn, $tableName, array('id' => $idx, 'value' => 'Row ' . $idx), $res, AE\INSERT_QUERY_PARAMS); - -$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => SQLSRV_CURSOR_STATIC )); -$row_count = sqlsrv_num_rows( $stmt ); -if( $row_count != 6 ) { - die( "sqlsrv_num_rows should have returned 6 rows in the static cursor\n" ); -} -$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, -1 ); -if( $row !== null ) { - die( "sqlsrv_fetch_array should have returned null\n" ); + if (!$stmt || $res === false) { + fatalError("failed to insert row $idx!\n"); + } + hasRows($stmt, $expectedFail); + sqlsrv_free_stmt($stmt); } -$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, 6 ); -if( $row !== null ) { - die( "sqlsrv_fetch_array should have returned null\n" ); +$conn = AE\connect(); +$tableName = 'ScrollTest'; +$numRows = 4; + +$columns = array(new AE\ColumnMeta('int', 'id'), + new AE\ColumnMeta('char(10)', 'value')); +$stmt = AE\createTable($conn, $tableName, $columns); + +$rows = sqlsrv_has_rows($stmt); +if($rows == true) { + die("Shouldn't have rows"); +} +sqlsrv_free_stmt($stmt); + +for ($i = 1; $i <= $numRows; $i++) { + insertOneRow($conn, $tableName, $i, true); } -$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => SQLSRV_CURSOR_DYNAMIC )); +// Always Encrypted feature only supports SQLSRV_CURSOR_FORWARD, so skip the rest of the test +// when AE is enabled +// https://github.com/Microsoft/msphpsql/wiki/Features#aelimitation +$query = "SELECT * FROM $tableName"; +$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD); +$stmt = AE\executeQueryEx($conn, $query, $options); +hasRows($stmt, false); +countRows($stmt, $numRows, 'forward only'); +sqlsrv_free_stmt($stmt); -$result = sqlsrv_num_rows( $stmt ); -if( $result !== false ) { - die( "sqlsrv_num_rows should have failed for a dynamic cursor." ); -} -sqlsrv_fetch( $stmt ); -sqlsrv_fetch( $stmt ); +if (! AE\isColEncrypted()) { + $options = array('Scrollable' => 'static'); + $stmt = AE\executeQueryEx($conn, $query, $options); -$stmt2 = sqlsrv_query( $conn, "DELETE FROM ScrollTest WHERE id = 2" ); -if( $stmt2 === false ) { - die( print_r( sqlsrv_errors(), true )); -} + $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, 4); + if($result !== null) { + die("Should have failed with an invalid row number"); + } + hasRows($stmt, false); + // this is empty + print_r(sqlsrv_errors()); + $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, -1); + if($result !== null) { + die("Should have failed with an invalid row number"); + } + // this is empty + print_r(sqlsrv_errors()); -$row = sqlsrv_get_field( $stmt, 0 ); -if( $row !== false ) { - die( "Should have returned false retrieving a field deleted by another query\n" ); -} -echo "sqlsrv_get_field returned false when retrieving a field deleted by another query\n"; -print_r( sqlsrv_errors() ); + // expected an error here + $rows = sqlsrv_rows_affected($stmt); + $message = !empty(sqlsrv_errors()) ? sqlsrv_errors()[0]['message'] : ''; + $expected = 'This function only works with statements that are not scrollable.'; + if (strcmp($message, $expected)) { + echo "Expected this error message: \'$expected\'\nbut it is: \'$message\'\n"; + } -// verify the sqlsrv_fetch_object is working -$obj = sqlsrv_fetch_object( $stmt, null, array(null), SQLSRV_SCROLL_LAST, 1 ); - -if( $obj === false ) { + $rows = sqlsrv_num_rows($stmt); + if ($rows != $numRows) { + echo "Error: Query returned $rows rows\n"; + } - print_r( sqlsrv_errors() ); + $row = 3; + $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, $row); + do { + $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, $row); + if($result === false) { + die(print_r(sqlsrv_errors(), true)); + } + $field1 = sqlsrv_get_field($stmt, 0); + $field2 = sqlsrv_get_field($stmt, 1); + $idx = $row + 1; + + if ($field1 != $idx || trim($field2) != "Row $idx") + echo "Field values unexpected $field1 $field2!\n"; + + $row = $row - 1; + } while($row >= 0); + sqlsrv_free_stmt($stmt); + + $options = array('Scrollable' => 'static'); + $stmt = AE\executeQueryEx($conn, $query, $options); + hasRows($stmt, false); + countRows($stmt, $numRows, 'static'); + sqlsrv_free_stmt($stmt); + + $options = array('Scrollable' => 'dynamic'); + $stmt = AE\executeQueryEx($conn, $query, $options); + + sqlsrv_fetch($stmt); + sqlsrv_fetch($stmt); + + insertOneRow($conn, $tableName, 5, true); + insertOneRow($conn, $tableName, 6, true); + $numRows = 6; + + // to account for the two fetches above + countRows($stmt, $numRows, 'dynamic', 2); + sqlsrv_free_stmt($stmt); + + $options = array('Scrollable' => SQLSRV_CURSOR_STATIC); + $stmt = AE\executeQueryEx($conn, $query, $options); + $row_count = sqlsrv_num_rows($stmt); + if($row_count != $numRows) { + die("sqlsrv_num_rows should have returned 6 rows in the static cursor\n"); + } + $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, -1); + if($row !== null) { + die("sqlsrv_fetch_array should have returned null\n"); + } + + $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, 6); + if($row !== null) { + die("sqlsrv_fetch_array should have returned null\n"); + } + + $options = array('Scrollable' => SQLSRV_CURSOR_DYNAMIC); + $stmt = AE\executeQueryEx($conn, $query, $options); + + $result = sqlsrv_num_rows($stmt); + if($result !== false) { + die("sqlsrv_num_rows should have failed for a dynamic cursor."); + } + sqlsrv_fetch($stmt); + sqlsrv_fetch($stmt); + + $stmt2 = sqlsrv_query($conn, "DELETE FROM ScrollTest WHERE id = 2"); + if($stmt2 === false) { + die(print_r(sqlsrv_errors(), true)); + } + + $row = sqlsrv_get_field($stmt, 0); + if($row !== false) { + die("sqlsrv_get_field should have returned false retrieving a field deleted by another query"); + } + $error = sqlsrv_errors()[0]; + $message = $error['message']; + $sqlstate = $error['SQLSTATE']; + if (strcmp($sqlstate, 'HY109') || strpos($message, 'Invalid cursor position') === false) { + die("Unexpected SQL state $sqlstate or error \'$message\'"); + } + + // verify the sqlsrv_fetch_object is working + $obj = sqlsrv_fetch_object($stmt, null, array(null), SQLSRV_SCROLL_LAST, 1); + if($obj === false) { + print_r(sqlsrv_errors()); + } else { + if ($obj->id != $numRows || trim($obj->value) != "Row $numRows") + echo "Field values unexpected $obj->id $obj->value!\n"; + } + sqlsrv_free_stmt($stmt); } -print_r( $obj ); - -sqlsrv_query( $conn, "DROP TABLE ScrollTest" ); - -sqlsrv_free_stmt( $stmt ); - -sqlsrv_close( $conn ); + +dropTable($conn, $tableName); +sqlsrv_close($conn); echo "Test succeeded.\n"; ?> ---EXPECTREGEX-- -Array -\( - \[0\] => Array - \( - \[0\] => IMSSP - \[SQLSTATE\] => IMSSP - \[1\] => \-51 - \[code\] => \-51 - \[2\] => This function only works with statements that are not scrollable\. - \[message\] => This function only works with statements that are not scrollable\. - \) - -\) -Query returned 4 rows -4 Row 4 -3 Row 3 -2 Row 2 -1 Row 1 -4 rows retrieved on the forward only cursor -4 rows retrieved on the static cursor -6 rows retrieved on the dynamic cursor -sqlsrv_get_field returned false when retrieving a field deleted by another query -Array -\( - \[0\] => Array - \( - \[0\] => HY109 - \[SQLSTATE\] => HY109 - \[1\] => 0 - \[code\] => 0 - \[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]Invalid cursor position - \[message\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]Invalid cursor position - \) - -\) -stdClass Object -\( - \[id\] => 6 - \[value\] => Row 6 -\) -Test succeeded\. +--EXPECT-- +Test succeeded.