php-sqlsrv/test/functional/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt

193 lines
4.1 KiB
Plaintext
Raw Normal View History

2017-04-04 18:46:24 +02:00
--TEST--
Test with static cursor and select different rows in some random order
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2017-04-04 18:46:24 +02:00
--FILE--
<?php
require_once('MsCommon.inc');
2017-04-04 18:46:24 +02:00
function fetchRowQuery($conn)
2017-04-04 18:46:24 +02:00
{
$tableName = 'testScrollCursor';
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
new AE\ColumnMeta('varchar(10)', 'c2_varchar'));
AE\createTable($conn, $tableName, $columns);
2017-04-04 18:46:24 +02:00
// insert data
$numRows = 10;
insertData($conn, $tableName, $numRows);
// select table
2017-04-04 18:46:24 +02:00
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName", array(), array('Scrollable' => 'static'));
hasRows($stmt);
2017-04-04 18:46:24 +02:00
$numRowsFetched = 0;
while ($obj = sqlsrv_fetch_object($stmt)) {
2017-04-04 18:46:24 +02:00
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
$numRowsFetched++;
}
if ($numRowsFetched != $numRows) {
2017-04-04 18:46:24 +02:00
echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n";
}
2017-04-04 18:46:24 +02:00
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);
2017-04-04 18:46:24 +02:00
}
function insertData($conn, $tableName, $numRows)
2017-04-04 18:46:24 +02:00
{
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_varchar) VALUES (?, ?)", array(&$v1, &$v2));
for ($i = 0; $i < $numRows; $i++) {
2017-04-04 18:46:24 +02:00
$v1 = $i + 1;
$v2 = "Row " . $v1;
sqlsrv_execute($stmt);
2017-04-04 18:46:24 +02:00
}
}
function getFirstRow($stmt)
2017-04-04 18:46:24 +02:00
{
echo "\nfirst row: ";
$result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
if ($result) {
$field1 = sqlsrv_get_field($stmt, 0);
$field2 = sqlsrv_get_field($stmt, 1);
echo "$field1, $field2\n";
2017-04-04 18:46:24 +02:00
}
}
function getNextRow($stmt)
2017-04-04 18:46:24 +02:00
{
echo "\nnext row: ";
$result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_NEXT);
if ($result) {
$field1 = sqlsrv_get_field($stmt, 0);
$field2 = sqlsrv_get_field($stmt, 1);
echo "$field1, $field2\n";
2017-04-04 18:46:24 +02:00
}
}
function getPriorRow($stmt)
2017-04-04 18:46:24 +02:00
{
echo "\nprior row: ";
$obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_PRIOR);
if ($obj) {
2017-04-04 18:46:24 +02:00
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
}
2017-04-04 18:46:24 +02:00
}
function getLastRow($stmt)
2017-04-04 18:46:24 +02:00
{
echo "\nlast row: ";
2017-04-04 18:46:24 +02:00
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_LAST);
if ($row) {
2017-04-04 18:46:24 +02:00
echo $row[0] . ", " . $row[1] . "\n";
}
2017-04-04 18:46:24 +02:00
}
function getRelativeRow($stmt, $offset)
2017-04-04 18:46:24 +02:00
{
echo "\nrow $offset from the current row: ";
2017-04-04 18:46:24 +02:00
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_RELATIVE, $offset);
if ($row) {
2017-04-04 18:46:24 +02:00
echo $row['c1_int'] . ", " . $row['c2_varchar'] . "\n";
}
2017-04-04 18:46:24 +02:00
}
function getAbsoluteRow($stmt, $offset)
2017-04-04 18:46:24 +02:00
{
echo "\nabsolute row with offset $offset: ";
$obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_ABSOLUTE, $offset);
if ($obj) {
2017-04-04 18:46:24 +02:00
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
}
2017-04-04 18:46:24 +02:00
}
function hasRows($stmt)
2017-04-04 18:46:24 +02:00
{
$rows = sqlsrv_has_rows($stmt);
if ($rows != true) {
echo "Should have rows!\n";
}
2017-04-04 18:46:24 +02:00
}
set_time_limit(0);
sqlsrv_configure('WarningsReturnAsErrors', 1);
2017-04-04 18:46:24 +02:00
echo "\nTest begins...\n";
2017-04-04 18:46:24 +02:00
// Connect
$conn = AE\connect();
fetchRowQuery($conn);
2017-04-04 18:46:24 +02:00
sqlsrv_close($conn);
echo "\nDone\n";
endTest("sqlsrv_fetch_cursor_static_scroll");
2017-04-04 18:46:24 +02:00
?>
--EXPECT--

Test begins...
2017-04-04 18:46:24 +02:00
1, Row 1
2, Row 2
3, Row 3
4, Row 4
5, Row 5
6, Row 6
7, Row 7
8, Row 8
9, Row 9
10, Row 10
first row: 1, Row 1
next row: 2, Row 2
last row: 10, Row 10
prior row: 9, Row 9
absolute row with offset 7: 8, Row 8
absolute row with offset 2: 3, Row 3
row 3 from the current row: 6, Row 6
prior row: 5, Row 5
row -4 from the current row: 1, Row 1
absolute row with offset 0: 1, Row 1
next row: 2, Row 2
row 5 from the current row: 7, Row 7
absolute row with offset -1:
next row: 1, Row 1
last row: 10, Row 10
row 1 from the current row:
2017-04-04 18:46:24 +02:00
Done
Test "sqlsrv_fetch_cursor_static_scroll" completed successfully.