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

126 lines
3.5 KiB
Plaintext
Raw Normal View History

--TEST--
2020-10-22 03:28:46 +02:00
Test fetching varchar and nvarchar max fields
--DESCRIPTION--
2020-10-22 03:28:46 +02:00
Test fetching varchar and nvarchar max fields as streams or strings with or without client buffer
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2020-10-22 03:28:46 +02:00
--ENV--
PHPT_EXEC=true
--FILE--
<?php
require_once('MsCommon.inc');
$conn = AE\connect();
2020-10-22 03:28:46 +02:00
$tableName = "char_max_fields";
2020-10-21 02:33:40 +02:00
$columns = array(new AE\ColumnMeta("varchar(max)", "varchar_max_col"),
2020-10-22 03:28:46 +02:00
new AE\ColumnMeta("nvarchar(max)", "nvarchar_max_col"));
2020-10-21 02:33:40 +02:00
AE\createTable($conn, $tableName, $columns);
2020-10-22 03:28:46 +02:00
$strValue = str_repeat("SimpleTest", 450);
$nstrValue = str_repeat("ÃÜðßZZýA©", 600);
2020-10-21 02:33:40 +02:00
2020-10-22 03:28:46 +02:00
$insertSql = "INSERT INTO $tableName (varchar_max_col, nvarchar_max_col) VALUES (?, ?)";
$params = array(array($strValue, null, null, SQLSRV_SQLTYPE_VARCHAR('max')),
array($nstrValue, null, SQLSRV_PHPTYPE_STRING('UTF-8'), SQLSRV_SQLTYPE_NVARCHAR('max')));
$stmt = sqlsrv_prepare($conn, $insertSql, $params);
if ($stmt) {
2020-10-21 02:33:40 +02:00
$res = sqlsrv_execute($stmt);
if (!$res) {
fatalError("Failed to insert data");
}
} else {
fatalError("Failed to prepare insert statement");
}
2020-10-22 03:28:46 +02:00
runTest($conn, false);
runTest($conn, true);
dropTable($conn, $tableName);
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
echo "Done\n";
///////////////////////////////////////////////////////////////////////////////////////////////
function runTest($conn, $buffered)
{
global $tableName, $strValue, $nstrValue;
trace("runTest ($buffered)\n");
$query = "SELECT * FROM $tableName";
if ($buffered) {
$stmt = sqlsrv_prepare($conn, $query, null, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
} else {
$stmt = sqlsrv_prepare($conn, $query);
}
if (!$stmt) {
fatalError("runTest ($buffered): failed to prepare select statement");
2020-10-21 02:33:40 +02:00
}
2020-10-22 03:28:46 +02:00
if (!sqlsrv_execute($stmt)) {
fatalError("runTest ($buffered): failed to execute select");
}
if (!sqlsrv_fetch($stmt)) {
fatalError("runTest ($buffered): failed to fetch data");
}
2020-10-22 03:28:46 +02:00
fetchAsString($stmt, 0, $strValue);
fetchAsString($stmt, 1, $nstrValue);
2020-10-22 03:28:46 +02:00
if (!sqlsrv_execute($stmt)) {
fatalError("runTest ($buffered): failed to execute select");
2018-01-17 00:47:36 +01:00
}
2020-10-22 03:28:46 +02:00
if (!sqlsrv_fetch($stmt)) {
fatalError("runTest ($buffered): failed to fetch data");
}
2020-10-22 03:28:46 +02:00
fetchAsStream($stmt, 0, $strValue);
fetchAsStream($stmt, 1, $nstrValue);
2020-10-21 02:33:40 +02:00
}
2020-10-22 03:28:46 +02:00
function fetchAsString($stmt, $index, $expected)
{
trace("fetchAsString ($index):\n");
$sqltype = ($index > 0) ? SQLSRV_PHPTYPE_STRING('UTF-8') : SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR);
$value = sqlsrv_get_field($stmt, $index, $sqltype);
if (!checkData($value, $expected)) {
echo("fetchAsString ($index) expected:\n$expected\nActual:\n$value\n");
}
}
2020-10-21 02:33:40 +02:00
2020-10-22 03:28:46 +02:00
function fetchAsStream($stmt, $index, $expected)
{
trace("fetchAsStream ($index):\n");
$sqltype = ($index > 0) ? SQLSRV_PHPTYPE_STREAM('UTF-8') : SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR);
$stream = sqlsrv_get_field($stmt, $index, $sqltype);
if ($stream !== false) {
$value = '';
while (!feof($stream)) {
$value .= fread($stream, 8192);
}
fclose($stream);
if (!checkData($value, $expected)) {
echo("fetchAsStream ($index) expected:\n$expected\nActual:\n$value\n");
}
}
}
2020-10-21 02:33:40 +02:00
function checkData($actual, $expected)
{
$success = true;
$pos = strpos($actual, $expected);
if (($pos === false) || ($pos > 1)) {
2020-10-22 03:28:46 +02:00
$success = false;
}
return ($success);
}
?>
--EXPECT--
2020-10-22 03:28:46 +02:00
Done