Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new tests for setting client buffer size related to issue 228 #920

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
sqlsrv_has_rows() using a forward and scrollable cursor
GitHub issue #228 - how max client buffer size affects the fetching of data
--DESCRIPTION--
A pdo_sqlsrv variation of the example in GitHub issue 228, using PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE the statement attribute.
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
Expand All @@ -21,28 +23,38 @@ try {
insertRow($conn, $tableName2, array("c1_int" => 990021574, "c2_varchar" => ">vh~Ö.bÐ*äß/ÄAabýZâOüzr£ðAß+|~|OU¢a|U<ßrv.uCB.ÐÜh_î+ãå@üðöã,U+ßvuU:/ý_Öãî/ð|bB|_Zbua©r++BA¢z£.üî¢öåäözÜ¢ßb</üöîã,ZbOhߢAåb*öî:r>:aöCrÄ~ýZ¢uªÐö.hhßð*zÜÜß*ãüåýãÄ+åýüüaߢÃÐBî@~AZöÃOßC@äoÃuCÜ,ÐÄa:îäÄÖý:h*ouªuåvUz_ArßAªãaãvÐåAUüAB:¢Äz|öub<üZvößüå:ãÄ@r/ZAÄðÄÄvzîv~C/£|ýýbüÖ~£|Öå<Üa~/v@åAz©¢£U_ßhbaÃß,zz<ã¢|<ä©>öAuövÖ>abu,zå,+ß/ü/ª_bbB:ÃC~£ü/O©O©ªAª_,|a¢~ýý/b>ßC@/böîöh>~£ð+Bßr©ÄÐÖßã:bA@:>B:UAbããîÜ~uÜ£îCöÖ£©_ÜßzÐ+ÖýZb,A:<<AA*¢ã@Uî:B<öBîÐ>z.ãîÄzC@©*ä|ã._ßZOäb¢Cßovå+uv.£B~~b£ª|ÖÄîßö>©Ãbb|©©ðA£åO~âãüîuvÄÜýUzîOÖ/oOßO*>ªßzêÖÐböÄåbîðîÐa~©ßîÄßУ<î>å<býUrAA©r£@üÄb_:ãZß_/ou@|ªåü~ACãUO<£îßÄîäbßöhßO©ZvßOBü:Zä<ĪobbO@ÃÐ_~î|a~ð©+,v+Ð/rÃzuöÖZÐOß/CCÖßý¢:<b£B,ÐߢoZbð~BüOö,Üö~ð:ß,CCî<Oä,Öãî£ÜCZ~/z~ý©vuzoöß/u©å|£ãÐv¢îhý:ÄoÐrz.ßbr_U*<hCîßÖ_+:hbü*að.Aö_Oª_öB>Bã_ý*ah¢rOĪ,ßo¢¢a|BÖäz</bUabÖðOA.Ðîý,Bhö*Cßuߣo,|ü_,ýÐu_b|ZÜh|<U@~übU¢Uð/o/Ð>U£.B£@Ü,ßAÃ>,ðßß+ßÜ©|Ðr©bCðТüãz>AßðåÃ>bÄåÄ|Z~äÃ/Cb*£bð_/Ða@~AÜãO+ý*CîîÃzÄöÃa©+@vuz>î>©.Cv>hÃý>©Bä,ö~@~@r,AðCu@Ü,@U*ÐvöÃêuã.Öa*uZªoZ/ðÖ©ßv_<ÖvåÜÐÜOÐoðßðÃUýZÐB:+ÄÃã£"));

$size = 2;
$stmt = $conn->prepare("SELECT * FROM $tableName1", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED,PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE=> $size));
$attr = $stmt->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
$stmt = $conn->prepare("SELECT * FROM $tableName1", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED,PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE=> $size));
$attr = $stmt->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
$stmt->execute();
$stmt->execute();
$numRows = 0;
while ($result = $stmt->fetch()) {
$numRows++;
}
echo ("Number of rows: $numRows\n");

$size = 3;
$stmt = $conn->prepare("SELECT * FROM $tableName2", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED,PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE=> $size));
$attr = $stmt->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
$size = 3;
$stmt = $conn->prepare("SELECT * FROM $tableName2", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED,PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE=> $size));
$attr = $stmt->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
$stmt->execute();
$stmt->execute();
$numRows = 0;
while ($result = $stmt->fetch()) {
$numRows++;
}

echo ("Number of rows: $numRows\n");

$size = 1;
$stmt = $conn->prepare("SELECT * FROM $tableName2", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED,PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE=> $size));
$attr = $stmt->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
try {
$stmt->execute();
echo "Expect this to fail!!\n";
} catch (PDOException $e) {
var_dump($e->getMessage());
}

dropTable($conn, $tableName1);
dropTable($conn, $tableName2);
unset($stmt);
Expand All @@ -58,4 +70,6 @@ Client Buffer Size in KB: 2
Number of rows: 1
Client Buffer Size in KB: 3
Number of rows: 1
Client Buffer Size in KB: 1
string(65) "SQLSTATE[IMSSP]: Memory limit of 1 KB exceeded for buffered query"
Done
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
--TEST--
GitHub issue #228 - how max client buffer size affects the fetching of data
--DESCRIPTION--
A pdo_sqlsrv variation of the example in GitHub issue 228, using PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE the connection attribute.
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsCommon_mid-refactor.inc");

function testErrors($conn, $size, $error)
{
try {
$conn->setAttribute(PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE, $size);
} catch (PDOException $e) {
if (strpos($e->getMessage(), $error) === false) {
echo $e->getMessage() . "\n";
}
}
}

try {
// Connect
$conn = connect();

$error = 'The PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE attribute is not a number or the number is not positive. Only positive numbers are valid for this attribute.';
testErrors($conn, 0, $error);
testErrors($conn, 2.99, $error);

// Create 2 tables
$tableName1 = 'pdo_228_1';
$tableName2 = 'pdo_228_2';

createTable($conn,$tableName1, array("c1_int" => "int", "c2_varchar" => "varchar(1000)"));
insertRow($conn, $tableName1, array("c1_int" => 990021574, "c2_varchar" => ">vh~Ö.bÐ*äß/ÄAabýZâOüzr£ðAß+|~|OU¢a|U<ßrv.uCB.ÐÜh_î+ãå@üðöã,U+ßvuU:/ý_Öãî/ð|bB|_Zbua©r++BA¢z£.üî¢öåäözÜ¢ßb</üöîã,ZbOhߢAåb*öî:r>:aöCrÄ~ýZ¢uªÐö.hhßð*zÜÜß*ãüåýãÄ+åýüüaߢÃÐBî@~AZöÃOßC@äoÃuCÜ,ÐÄa:îäÄÖý:h*ouªuåvUz_ArßAªãaãvÐåAUüAB:¢Äz|öub<üZvößüå:ãÄ@r/ZAÄðÄÄvzîv~C/£|ýýbüÖ~£|Öå<Üa~/v@åAz©¢£U_ßhbaÃß,zz<ã¢|<ä©>öAuövÖ>abu,zå,+ß/ü/ª_bbB:ÃC~£ü/O©O©ªAª_,|a¢~ýý/b>ßC@/böîöh>~£ð+Bßr©ÄÐÖßã:bA@:>B:UAbããîÜ~uÜ£îCöÖ£©_ÜßzÐ+ÖýZb,A:<<AA*¢ã@Uî:B<öBîÐ>z.ãîÄzC@©*ä|ã._ßZOäb¢Cßovå+uv.£B~~b£ª|ÖÄîßö>©Ãbb|©©ðA£åO~âãüîuvÄÜýUzîOÖ/oOßO*>ªßzêÖÐböÄåbîðîÐa~©ßîÄßУ<î>å<býUrAA©r£@üÄb_:ãZß_/ou@|ªåü~ACãUO<£îßÄîäbßöhßO©ZvßOBü:Zä<ĪobbO@ÃÐ_~î|a~ð©+,v+Ð/rÃzuöÖZÐOß/CCÖßý¢:<b£B,ÐߢoZbð~BüOö,Üö~ð:ß,CCî<Oä,Öãî£ÜCZ~/z~ý©vuzoöß/u©å|£ãÐv¢îhý:ÄoÐrz.ßbr_U*<hCîßÖ_+:hbü*að.Aö_Oª_öB>Bã_ý*ah¢rOĪ,ßo¢¢a|BÖäz</bUabÖðOA.Ðîý,Bhö*Cßuߣo,|ü_,ýÐu_b|ZÜh|<U@~übU¢Uð/o/Ð>U£.B£@Ü,ßAÃ>,ðßß+ßÜ©|Ðr©bCðТüãz>AßðåÃ>bÄåÄ|Z~äÃ/Cb*£bð_/Ða@~AÜãO+ý*CîîÃzÄöÃa©+@vuz>î>©.Cv>hÃý>©Bä,ö~@~@r,AðCu@Ü,@U*ÐvöÃêuã.Öa*uZªoZ/ðÖ©ßv_<ÖvåÜÐÜOÐoðßðÃUýZÐB:+ÄÃã£"));

createTable($conn,$tableName2, array("c1_int" => "int", "c2_varchar" => "varchar(max)"));
insertRow($conn, $tableName2, array("c1_int" => 990021574, "c2_varchar" => ">vh~Ö.bÐ*äß/ÄAabýZâOüzr£ðAß+|~|OU¢a|U<ßrv.uCB.ÐÜh_î+ãå@üðöã,U+ßvuU:/ý_Öãî/ð|bB|_Zbua©r++BA¢z£.üî¢öåäözÜ¢ßb</üöîã,ZbOhߢAåb*öî:r>:aöCrÄ~ýZ¢uªÐö.hhßð*zÜÜß*ãüåýãÄ+åýüüaߢÃÐBî@~AZöÃOßC@äoÃuCÜ,ÐÄa:îäÄÖý:h*ouªuåvUz_ArßAªãaãvÐåAUüAB:¢Äz|öub<üZvößüå:ãÄ@r/ZAÄðÄÄvzîv~C/£|ýýbüÖ~£|Öå<Üa~/v@åAz©¢£U_ßhbaÃß,zz<ã¢|<ä©>öAuövÖ>abu,zå,+ß/ü/ª_bbB:ÃC~£ü/O©O©ªAª_,|a¢~ýý/b>ßC@/böîöh>~£ð+Bßr©ÄÐÖßã:bA@:>B:UAbããîÜ~uÜ£îCöÖ£©_ÜßzÐ+ÖýZb,A:<<AA*¢ã@Uî:B<öBîÐ>z.ãîÄzC@©*ä|ã._ßZOäb¢Cßovå+uv.£B~~b£ª|ÖÄîßö>©Ãbb|©©ðA£åO~âãüîuvÄÜýUzîOÖ/oOßO*>ªßzêÖÐböÄåbîðîÐa~©ßîÄßУ<î>å<býUrAA©r£@üÄb_:ãZß_/ou@|ªåü~ACãUO<£îßÄîäbßöhßO©ZvßOBü:Zä<ĪobbO@ÃÐ_~î|a~ð©+,v+Ð/rÃzuöÖZÐOß/CCÖßý¢:<b£B,ÐߢoZbð~BüOö,Üö~ð:ß,CCî<Oä,Öãî£ÜCZ~/z~ý©vuzoöß/u©å|£ãÐv¢îhý:ÄoÐrz.ßbr_U*<hCîßÖ_+:hbü*að.Aö_Oª_öB>Bã_ý*ah¢rOĪ,ßo¢¢a|BÖäz</bUabÖðOA.Ðîý,Bhö*Cßuߣo,|ü_,ýÐu_b|ZÜh|<U@~übU¢Uð/o/Ð>U£.B£@Ü,ßAÃ>,ðßß+ßÜ©|Ðr©bCðТüãz>AßðåÃ>bÄåÄ|Z~äÃ/Cb*£bð_/Ða@~AÜãO+ý*CîîÃzÄöÃa©+@vuz>î>©.Cv>hÃý>©Bä,ö~@~@r,AðCu@Ü,@U*ÐvöÃêuã.Öa*uZªoZ/ðÖ©ßv_<ÖvåÜÐÜOÐoðßðÃUýZÐB:+ÄÃã£"));

$size = 2;
$conn->setAttribute(PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE, $size);
$stmt = $conn->prepare("SELECT * FROM $tableName1", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$attr = $conn->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
$stmt->execute();
$numRows = 0;
while ($result = $stmt->fetch()) {
$numRows++;
}
echo ("Number of rows: $numRows\n");

$size = 3;
$conn->setAttribute(PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE, $size);
$stmt = $conn->prepare("SELECT * FROM $tableName2", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$attr = $conn->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
$stmt->execute();
$numRows = 0;
while ($result = $stmt->fetch()) {
$numRows++;
}

echo ("Number of rows: $numRows\n");

$size = 1;
$conn->setAttribute(PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE, $size);
$stmt = $conn->prepare("SELECT * FROM $tableName2", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL,PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$attr = $conn->getAttribute(constant('PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE'));
echo("Client Buffer Size in KB: $attr\n");
try {
$stmt->execute();
echo "Expect this to fail!!\n";
} catch (PDOException $e) {
var_dump($e->getMessage());
}

dropTable($conn, $tableName1);
dropTable($conn, $tableName2);
unset($stmt);
unset($conn);
print "Done";
} catch (PDOException $e) {
var_dump($e->errorInfo);
}
?>

--EXPECT--
Client Buffer Size in KB: 2
Number of rows: 1
Client Buffer Size in KB: 3
Number of rows: 1
Client Buffer Size in KB: 1
string(65) "SQLSTATE[IMSSP]: Memory limit of 1 KB exceeded for buffered query"
Done
45 changes: 34 additions & 11 deletions test/functional/sqlsrv/srv_228_sqlsrv_clientbuffermaxkbsize.phpt
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
--TEST--
sqlsrv_has_rows() using a forward and scrollable cursor
GitHub issue #228 - how ClientBufferMaxKBSize affects sqlsrv_has_rows and sqlsrv_fetch_array
--DESCRIPTION--
Based on the example in GitHub issue 228, configuring ClientBufferMaxKBSize with sqlsrv_configure.
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php

require_once('MsCommon.inc');

function testErrors($conn)
{
// set client buffer size to 0KB returns false
$ret = sqlsrv_configure('ClientBufferMaxKBSize', 0);
if (!$ret) {
echo sqlsrv_errors()[0]['message'] . "\n";
}

$ret = sqlsrv_configure('ClientBufferMaxKBSize', -1.9);
if (!$ret) {
echo sqlsrv_errors()[0]['message'] . "\n";
}
}

function fetchData($conn, $table, $size)
{
$ret = sqlsrv_configure('ClientBufferMaxKBSize', $size);
Expand All @@ -16,10 +32,13 @@ function fetchData($conn, $table, $size)
echo("ClientBufferMaxKBSize is $attr\n");

sqlsrv_execute($stmt);
if ($size < 2) {
echo sqlsrv_errors()[0]['message'] . "\n";
}

$rows = sqlsrv_has_rows($stmt);
var_dump($rows);

sqlsrv_execute($stmt);
$numRowsFetched = 0;
while ($row = sqlsrv_fetch_array($stmt)) {
$numRowsFetched++;
Expand All @@ -40,18 +59,17 @@ $stmt = AE\createTable($conn, $tableName1, $columns);

unset($columns);
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
new AE\ColumnMeta('varchar(1036)', 'c2_varchar_1036'));
new AE\ColumnMeta('varchar(1400)', 'c2_varchar_1400'));
$stmt = AE\createTable($conn, $tableName2, $columns);

// insert > 1KB into c2_varchar_max & c2_varchar_1036 (1036 characters).
$longString = 'This is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a test';
// insert > 1KB into c2_varchar_max & c2_varchar_1400 (1400 characters).
$longString = str_repeat('This is a test', 100);

$stmt = AE\insertRow($conn, $tableName1, array('c1_int' => 1, 'c2_varchar_max' => $longString));
$stmt = AE\insertRow($conn, $tableName2, array('c1_int' => 1, 'c2_varchar_1036' => $longString));
$stmt = AE\insertRow($conn, $tableName2, array('c1_int' => 1, 'c2_varchar_1400' => $longString));
sqlsrv_free_stmt($stmt);

// set client buffer size to 0KB returns false
$ret = sqlsrv_configure('ClientBufferMaxKBSize', 0);
var_dump($ret);
testErrors($conn);

// set client buffer size to 1KB
$size = 1;
Expand All @@ -62,19 +80,24 @@ $size = 2;
fetchData($conn, $tableName1, $size); // this should return 1 row.
fetchData($conn, $tableName2, $size); // this should return 1 row.

sqlsrv_free_stmt($stmt);
dropTable($conn, $tableName1);
dropTable($conn, $tableName2);

sqlsrv_close($conn);
print "Done"
?>

--EXPECT--
bool(false)
Setting for ClientBufferMaxKBSize was non-int or non-positive.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is not the same as the one in the PDO tests above. Is this correct? It looks like it's the same error, just the message is slightly different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they are different. The error messages have always been there just no tests for checking them.

Setting for ClientBufferMaxKBSize was non-int or non-positive.
bool(true)
ClientBufferMaxKBSize is 1
Memory limit of 1 KB exceeded for buffered query
bool(false)
Number of rows fetched: 0
bool(true)
ClientBufferMaxKBSize is 1
Memory limit of 1 KB exceeded for buffered query
bool(false)
Number of rows fetched: 0
bool(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
--TEST--
GitHub issue #228 - how ClientBufferMaxKBSize affects sqlsrv_has_rows and sqlsrv_fetch_array
--DESCRIPTION--
A variation of the example in GitHub issue 228, using ClientBufferMaxKBSize the statement option.
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php

require_once('MsCommon.inc');

function testErrors($conn, $table, $error)
{
$query = "SELECT * FROM $table";

// set client buffer size to 0KB
$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>"buffered", "ClientBufferMaxKBSize" => 0));
if ($stmt !== false) {
echo("Setting client buffer size to 0KB should have failed\n");
} else {
if (strpos(sqlsrv_errors()[0]['message'], $error) === false) {
print_r(sqlsrv_errors());
}
}

// set client buffer size to 0.99KB
$stmt = sqlsrv_prepare($conn, $query, array(), array("Scrollable"=>"buffered", "ClientBufferMaxKBSize" => 0.99));
if ($stmt !== false) {
echo("Setting client buffer size to 0.99KB should have failed\n");
} else {
if (strpos(sqlsrv_errors()[0]['message'], $error) === false) {
print_r(sqlsrv_errors());
}
}
}

function fetchData($conn, $table, $size)
{
$stmt = sqlsrv_prepare($conn, "SELECT * FROM $table", array(), array("Scrollable"=>"buffered", "ClientBufferMaxKBSize" => $size));

$numRowsExpected = ($size > 1) ? 1 : 0;
$res = sqlsrv_execute($stmt);
if ($res && $size < 2) {
echo "Expect this to fail\n";
} else {
$error = 'Memory limit of 1 KB exceeded for buffered query';
if (strpos(sqlsrv_errors()[0]['message'], $error) === false) {
print_r(sqlsrv_errors());
}
}

$rows = sqlsrv_has_rows($stmt);
if ($numRowsExpected && !$rows) {
fatalError("sqlsrv_has_rows failed\n");
}

$numRowsFetched = 0;
while ($row = sqlsrv_fetch_array($stmt)) {
$numRowsFetched++;
}
if ($numRowsExpected != $numRowsFetched) {
echo("Expected $numRowsExpected but number of rows fetched is $numRowsFetched\n");
}
}

// connect
$conn = AE\connect();

$tableName1 = 'php_test_table_1';
$tableName2 = 'php_test_table_2';

// Create tables
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
new AE\ColumnMeta('varchar(max)', 'c2_varchar_max'));
$stmt = AE\createTable($conn, $tableName1, $columns);

unset($columns);
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
new AE\ColumnMeta('varchar(1050)', 'c2_varchar_1050'));
$stmt = AE\createTable($conn, $tableName2, $columns);

// insert > 1KB into c2_varchar_max & c2_varchar_1050 (1050 characters).
$longString = str_repeat('This is a test', 75);

$stmt = AE\insertRow($conn, $tableName1, array('c1_int' => 1, 'c2_varchar_max' => $longString));
$stmt = AE\insertRow($conn, $tableName2, array('c1_int' => 1, 'c2_varchar_1050' => $longString));
sqlsrv_free_stmt($stmt);

$error = 'Setting for ClientBufferMaxKBSize was non-int or non-positive';
testErrors($conn, $tableName1, $error);

// set client buffer size to 1KB
$size = 1;
fetchData($conn, $tableName1, $size); // this should return 0 rows.
fetchData($conn, $tableName2, $size); // this should return 0 rows.
// set client buffer size to 2KB
$size = 2;
fetchData($conn, $tableName1, $size); // this should return 1 row.
fetchData($conn, $tableName2, $size); // this should return 1 row.

dropTable($conn, $tableName1);
dropTable($conn, $tableName2);

sqlsrv_close($conn);
print "Done"
?>

--EXPECT--
Done