2017-05-04 02:00:31 +03:00
|
|
|
--TEST--
|
|
|
|
Fetch Scrollabale Data Test
|
|
|
|
--DESCRIPTION--
|
|
|
|
Verifies data retrieval with scrollable result sets.
|
|
|
|
--ENV--
|
|
|
|
PHPT_EXEC=true
|
|
|
|
--SKIPIF--
|
2017-12-20 20:31:44 +03:00
|
|
|
<?php
|
|
|
|
require('skipif_versions_old.inc');
|
|
|
|
?>
|
2017-05-04 02:00:31 +03:00
|
|
|
--FILE--
|
|
|
|
<?php
|
2017-10-05 00:22:56 +03:00
|
|
|
require_once('MsCommon.inc');
|
2017-05-04 02:00:31 +03:00
|
|
|
|
2017-10-24 03:04:35 +03:00
|
|
|
function fetchRow($noRows)
|
2017-05-04 02:00:31 +03:00
|
|
|
{
|
2017-10-05 00:22:56 +03:00
|
|
|
setup();
|
2017-10-24 03:04:35 +03:00
|
|
|
$tableName = 'TC48test';
|
2017-12-20 20:31:44 +03:00
|
|
|
if (useUTF8Data()) {
|
2017-10-24 03:04:35 +03:00
|
|
|
$conn1 = AE\connect(array('CharacterSet'=>'UTF-8'));
|
2017-10-05 00:22:56 +03:00
|
|
|
} else {
|
2017-10-24 03:04:35 +03:00
|
|
|
$conn1 = AE\connect();
|
2017-10-05 00:22:56 +03:00
|
|
|
}
|
2017-10-24 03:04:35 +03:00
|
|
|
AE\createTestTable($conn1, $tableName);
|
2017-05-04 02:00:31 +03:00
|
|
|
|
2017-10-24 03:04:35 +03:00
|
|
|
$noRowsInserted = AE\insertTestRows($conn1, $tableName, $noRows);
|
2017-05-04 02:00:31 +03:00
|
|
|
|
|
|
|
$actual = null;
|
|
|
|
$expected = null;
|
|
|
|
|
|
|
|
// fetch array (to retrieve reference values)
|
2017-10-24 03:04:35 +03:00
|
|
|
$stmt1 = AE\selectFromTable($conn1, $tableName);
|
2017-05-04 02:00:31 +03:00
|
|
|
$numFields = sqlsrv_num_fields($stmt1);
|
2017-10-24 03:04:35 +03:00
|
|
|
$expected = fetchArray($stmt1, $noRowsInserted, $numFields);
|
2017-05-04 02:00:31 +03:00
|
|
|
sqlsrv_free_stmt($stmt1);
|
2017-10-24 03:04:35 +03:00
|
|
|
|
2017-05-04 02:00:31 +03:00
|
|
|
$query = "SELECT * FROM [$tableName]";
|
|
|
|
|
|
|
|
// fetch object - FORWARD cursor
|
|
|
|
$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD);
|
2017-10-24 03:04:35 +03:00
|
|
|
$stmt2 = AE\executeQueryEx($conn1, $query, $options);
|
|
|
|
$actual = fetchObject($stmt2, $noRowsInserted, $numFields, SQLSRV_SCROLL_NEXT);
|
2017-05-04 02:00:31 +03:00
|
|
|
sqlsrv_free_stmt($stmt2);
|
2017-10-24 03:04:35 +03:00
|
|
|
checkData($noRowsInserted, $numFields, $actual, $expected);
|
|
|
|
|
2018-01-19 02:35:13 +03:00
|
|
|
// fetch object - STATIC cursor
|
|
|
|
$options = array('Scrollable' => SQLSRV_CURSOR_STATIC);
|
|
|
|
$stmt2 = AE\executeQueryEx($conn1, $query, $options);
|
|
|
|
$actual = fetchObject($stmt2, $noRowsInserted, $numFields, SQLSRV_SCROLL_RELATIVE);
|
|
|
|
sqlsrv_free_stmt($stmt2);
|
|
|
|
checkData($noRowsInserted, $numFields, $actual, $expected);
|
2017-10-24 03:04:35 +03:00
|
|
|
|
2018-01-19 02:35:13 +03:00
|
|
|
// fetch object - DYNAMIC cursor
|
|
|
|
$options = array('Scrollable' => SQLSRV_CURSOR_DYNAMIC);
|
|
|
|
$stmt2 = AE\executeQueryEx($conn1, $query, $options);
|
|
|
|
$actual = fetchObject($stmt2, $noRowsInserted, $numFields, SQLSRV_SCROLL_ABSOLUTE);
|
|
|
|
sqlsrv_free_stmt($stmt2);
|
|
|
|
checkData($noRowsInserted, $numFields, $actual, $expected);
|
2017-10-24 03:04:35 +03:00
|
|
|
|
2018-01-19 02:35:13 +03:00
|
|
|
// fetch object - KEYSET cursor
|
|
|
|
$options = array('Scrollable' => SQLSRV_CURSOR_KEYSET);
|
|
|
|
$stmt2 = AE\executeQueryEx($conn1, $query, $options);
|
|
|
|
$actual = fetchObject($stmt2, $noRowsInserted, $numFields, SQLSRV_SCROLL_PRIOR, 0);
|
|
|
|
sqlsrv_free_stmt($stmt2);
|
|
|
|
checkData($noRowsInserted, $numFields, $actual, $expected);
|
2017-10-24 03:04:35 +03:00
|
|
|
|
2017-10-05 00:22:56 +03:00
|
|
|
dropTable($conn1, $tableName);
|
|
|
|
|
2017-05-04 02:00:31 +03:00
|
|
|
sqlsrv_close($conn1);
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:04:35 +03:00
|
|
|
function fetchArray($stmt, $rows, $fields)
|
2017-05-04 02:00:31 +03:00
|
|
|
{
|
|
|
|
$values = array();
|
2017-10-05 00:22:56 +03:00
|
|
|
for ($i = 0; $i < $rows; $i++) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$row = sqlsrv_fetch_array($stmt);
|
2017-10-05 00:22:56 +03:00
|
|
|
if ($row === false) {
|
|
|
|
fatalError("Row $i is missing");
|
2017-05-04 02:00:31 +03:00
|
|
|
}
|
|
|
|
$values[$i] = $row;
|
|
|
|
}
|
|
|
|
return ($values);
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:04:35 +03:00
|
|
|
function fetchObject($stmt, $rows, $fields, $dir)
|
2017-05-04 02:00:31 +03:00
|
|
|
{
|
2017-10-05 00:22:56 +03:00
|
|
|
trace("\tRetrieving $rows objects with $fields fields each ...\n");
|
2017-05-04 02:00:31 +03:00
|
|
|
$values = array();
|
2017-10-05 00:22:56 +03:00
|
|
|
for ($i = 0; $i < $rows; $i++) {
|
|
|
|
if ($dir == SQLSRV_SCROLL_NEXT) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$obj = sqlsrv_fetch_object($stmt, null, null, $dir);
|
2017-10-05 00:22:56 +03:00
|
|
|
} elseif ($dir == SQLSRV_SCROLL_PRIOR) {
|
|
|
|
if ($i == 0) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_LAST);
|
2017-10-05 00:22:56 +03:00
|
|
|
} else {
|
2017-05-04 02:00:31 +03:00
|
|
|
$obj = sqlsrv_fetch_object($stmt, null, null, $dir);
|
|
|
|
}
|
2017-10-05 00:22:56 +03:00
|
|
|
} elseif ($dir == SQLSRV_SCROLL_ABSOLUTE) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$obj = sqlsrv_fetch_object($stmt, null, null, $dir, $i);
|
2017-10-05 00:22:56 +03:00
|
|
|
} elseif ($dir == SQLSRV_SCROLL_RELATIVE) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$obj = sqlsrv_fetch_object($stmt, null, null, $dir, 1);
|
|
|
|
}
|
2017-10-05 00:22:56 +03:00
|
|
|
if ($obj === false) {
|
|
|
|
fatalError("Row $i is missing");
|
2017-05-04 02:00:31 +03:00
|
|
|
}
|
2017-10-05 00:22:56 +03:00
|
|
|
if ($dir == SQLSRV_SCROLL_PRIOR) {
|
2017-05-04 02:00:31 +03:00
|
|
|
$values[$rows - $i - 1] = $obj;
|
2017-10-05 00:22:56 +03:00
|
|
|
} else {
|
2017-05-04 02:00:31 +03:00
|
|
|
$values[$i] = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ($values);
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:04:35 +03:00
|
|
|
function checkData($rows, $fields, $actualValues, $expectedValues)
|
2017-05-04 02:00:31 +03:00
|
|
|
{
|
2017-10-05 00:22:56 +03:00
|
|
|
if (($actualValues != null) && ($expectedValues != null)) {
|
|
|
|
for ($i = 0; $i < $rows; $i++) {
|
|
|
|
for ($j = 0; $j < $fields; $j++) {
|
2017-10-24 03:04:35 +03:00
|
|
|
$colName = getColName($j + 1);
|
2017-05-04 02:00:31 +03:00
|
|
|
$actual = $actualValues[$i]->$colName;
|
|
|
|
$expected = $expectedValues[$i][$colName];
|
2017-10-05 00:22:56 +03:00
|
|
|
if ($actual != $expected) {
|
2017-05-04 02:00:31 +03:00
|
|
|
die("Data corruption on row ".($i + 1)." column ".($j + 1).": $expected => $actual");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:31:44 +03:00
|
|
|
// locale must be set before 1st connection
|
2018-01-03 01:42:08 +03:00
|
|
|
setUSAnsiLocale();
|
2017-12-20 20:31:44 +03:00
|
|
|
$testName = "Fetch - Scrollable";
|
|
|
|
|
|
|
|
// test ansi only if windows or non-UTF8 locales are supported (ODBC 17 and above)
|
|
|
|
startTest($testName);
|
|
|
|
if (isLocaleSupported()) {
|
|
|
|
try {
|
|
|
|
setUTF8Data(false);
|
|
|
|
fetchRow(10);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endTest($testName);
|
|
|
|
|
|
|
|
// test utf8
|
|
|
|
startTest($testName);
|
2017-10-24 03:04:35 +03:00
|
|
|
try {
|
2017-12-20 20:31:44 +03:00
|
|
|
setUTF8Data(true);
|
2018-01-04 19:53:20 +03:00
|
|
|
resetLocaleToDefault();
|
2017-10-24 03:04:35 +03:00
|
|
|
fetchRow(10);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
2017-05-04 02:00:31 +03:00
|
|
|
}
|
2017-12-20 20:31:44 +03:00
|
|
|
endTest($testName);
|
2017-05-04 02:00:31 +03:00
|
|
|
|
|
|
|
?>
|
|
|
|
--EXPECT--
|
|
|
|
Test "Fetch - Scrollable" completed successfully.
|
2017-12-20 20:31:44 +03:00
|
|
|
Test "Fetch - Scrollable" completed successfully.
|