msphpsql/test/functional/sqlsrv/TC48_FetchScrollable.phpt

162 строки
4.8 KiB
Plaintext
Исходник Постоянная ссылка Обычный вид История

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--
<?php
require('skipif_versions_old.inc');
?>
2017-05-04 02:00:31 +03:00
--FILE--
<?php
require_once('MsCommon.inc');
2017-05-04 02:00:31 +03:00
function fetchRow($noRows)
2017-05-04 02:00:31 +03:00
{
setup();
$tableName = 'TC48test';
if (useUTF8Data()) {
$conn1 = AE\connect(array('CharacterSet'=>'UTF-8'));
} else {
$conn1 = AE\connect();
}
AE\createTestTable($conn1, $tableName);
2017-05-04 02:00:31 +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)
$stmt1 = AE\selectFromTable($conn1, $tableName);
2017-05-04 02:00:31 +03:00
$numFields = sqlsrv_num_fields($stmt1);
$expected = fetchArray($stmt1, $noRowsInserted, $numFields);
2017-05-04 02:00:31 +03:00
sqlsrv_free_stmt($stmt1);
2017-05-04 02:00:31 +03:00
$query = "SELECT * FROM [$tableName]";
// fetch object - FORWARD cursor
$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD);
$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);
checkData($noRowsInserted, $numFields, $actual, $expected);
// 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);
// 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);
// 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);
dropTable($conn1, $tableName);
2017-05-04 02:00:31 +03:00
sqlsrv_close($conn1);
}
function fetchArray($stmt, $rows, $fields)
2017-05-04 02:00:31 +03:00
{
$values = array();
for ($i = 0; $i < $rows; $i++) {
2017-05-04 02:00:31 +03:00
$row = sqlsrv_fetch_array($stmt);
if ($row === false) {
fatalError("Row $i is missing");
2017-05-04 02:00:31 +03:00
}
$values[$i] = $row;
}
return ($values);
}
function fetchObject($stmt, $rows, $fields, $dir)
2017-05-04 02:00:31 +03:00
{
trace("\tRetrieving $rows objects with $fields fields each ...\n");
2017-05-04 02:00:31 +03:00
$values = array();
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);
} 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);
} else {
2017-05-04 02:00:31 +03:00
$obj = sqlsrv_fetch_object($stmt, null, null, $dir);
}
} elseif ($dir == SQLSRV_SCROLL_ABSOLUTE) {
2017-05-04 02:00:31 +03:00
$obj = sqlsrv_fetch_object($stmt, null, null, $dir, $i);
} elseif ($dir == SQLSRV_SCROLL_RELATIVE) {
2017-05-04 02:00:31 +03:00
$obj = sqlsrv_fetch_object($stmt, null, null, $dir, 1);
}
if ($obj === false) {
fatalError("Row $i is missing");
2017-05-04 02:00:31 +03:00
}
if ($dir == SQLSRV_SCROLL_PRIOR) {
2017-05-04 02:00:31 +03:00
$values[$rows - $i - 1] = $obj;
} else {
2017-05-04 02:00:31 +03:00
$values[$i] = $obj;
}
}
return ($values);
}
function checkData($rows, $fields, $actualValues, $expectedValues)
2017-05-04 02:00:31 +03:00
{
if (($actualValues != null) && ($expectedValues != null)) {
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $fields; $j++) {
$colName = getColName($j + 1);
2017-05-04 02:00:31 +03:00
$actual = $actualValues[$i]->$colName;
$expected = $expectedValues[$i][$colName];
if ($actual != $expected) {
2017-05-04 02:00:31 +03:00
die("Data corruption on row ".($i + 1)." column ".($j + 1).": $expected => $actual");
}
}
}
}
}
// locale must be set before 1st connection
2018-01-03 01:42:08 +03:00
setUSAnsiLocale();
$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);
try {
setUTF8Data(true);
2018-01-04 19:53:20 +03:00
resetLocaleToDefault();
fetchRow(10);
} catch (Exception $e) {
echo $e->getMessage();
2017-05-04 02:00:31 +03:00
}
endTest($testName);
2017-05-04 02:00:31 +03:00
?>
--EXPECT--
Test "Fetch - Scrollable" completed successfully.
Test "Fetch - Scrollable" completed successfully.