2017-04-04 18:46:24 +02:00
|
|
|
|
--TEST--
|
2017-10-04 23:22:56 +02:00
|
|
|
|
Test with static cursor and select different rows in some random order
|
2017-11-08 00:31:55 +01:00
|
|
|
|
--SKIPIF--
|
|
|
|
|
<?php require('skipif_versions_old.inc'); ?>
|
2017-04-04 18:46:24 +02:00
|
|
|
|
--FILE--
|
|
|
|
|
<?php
|
2017-10-04 23:22:56 +02:00
|
|
|
|
require_once('MsCommon.inc');
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function fetchRowQuery($conn)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-11-08 00:31:55 +01:00
|
|
|
|
$tableName = 'testScrollCursor';
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
$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;
|
2017-11-08 00:31:55 +01:00
|
|
|
|
insertData($conn, $tableName, $numRows);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
// select table
|
2017-04-04 18:46:24 +02:00
|
|
|
|
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName", array(), array('Scrollable' => 'static'));
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
hasRows($stmt);
|
2017-04-04 18:46:24 +02:00
|
|
|
|
$numRowsFetched = 0;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
while ($obj = sqlsrv_fetch_object($stmt)) {
|
2017-04-04 18:46:24 +02:00
|
|
|
|
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
|
|
|
|
|
$numRowsFetched++;
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
if ($numRowsFetched != $numRows) {
|
2017-04-04 18:46:24 +02:00
|
|
|
|
echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01: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
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01: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));
|
|
|
|
|
|
2017-10-04 23:22:56 +02:00
|
|
|
|
for ($i = 0; $i < $numRows; $i++) {
|
2017-04-04 18:46:24 +02:00
|
|
|
|
$v1 = $i + 1;
|
|
|
|
|
$v2 = "Row " . $v1;
|
|
|
|
|
|
2017-10-04 23:22:56 +02:00
|
|
|
|
sqlsrv_execute($stmt);
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getFirstRow($stmt)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getNextRow($stmt)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getPriorRow($stmt)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +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-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getLastRow($stmt)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +02:00
|
|
|
|
echo "\nlast row: ";
|
2017-04-04 18:46:24 +02:00
|
|
|
|
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_LAST);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
if ($row) {
|
2017-04-04 18:46:24 +02:00
|
|
|
|
echo $row[0] . ", " . $row[1] . "\n";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getRelativeRow($stmt, $offset)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +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);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
if ($row) {
|
2017-04-04 18:46:24 +02:00
|
|
|
|
echo $row['c1_int'] . ", " . $row['c2_varchar'] . "\n";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function getAbsoluteRow($stmt, $offset)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +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-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
function hasRows($stmt)
|
2017-04-04 18:46:24 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$rows = sqlsrv_has_rows($stmt);
|
|
|
|
|
if ($rows != true) {
|
|
|
|
|
echo "Should have rows!\n";
|
|
|
|
|
}
|
2017-04-04 18:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
set_time_limit(0);
|
|
|
|
|
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
echo "\nTest begins...\n";
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
// Connect
|
|
|
|
|
$conn = AE\connect();
|
|
|
|
|
fetchRowQuery($conn);
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
2017-11-08 00:31:55 +01:00
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
|
echo "\nDone\n";
|
|
|
|
|
endTest("sqlsrv_fetch_cursor_static_scroll");
|
2017-04-04 18:46:24 +02:00
|
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
--EXPECT--
|
|
|
|
|
|
2017-05-02 02:01:30 +02:00
|
|
|
|
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:
|
2017-04-04 18:55:15 +02:00
|
|
|
|
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
|
2017-10-04 23:22:56 +02:00
|
|
|
|
Test "sqlsrv_fetch_cursor_static_scroll" completed successfully.
|