-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug in reading varbinary max fields (#1209)
- Loading branch information
Showing
5 changed files
with
495 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
--TEST-- | ||
Test fetching varbinary, varchar, nvarchar max fields with client buffer | ||
--DESCRIPTION-- | ||
Similar to sqlsrv_fetch_large_stream test but fetching varbinary, varchar, nvarchar max fields as strings with or without client buffer | ||
--SKIPIF-- | ||
<?php require_once('skipif_mid-refactor.inc'); ?> | ||
--ENV-- | ||
PHPT_EXEC=true | ||
--FILE-- | ||
<?php | ||
require_once("MsCommon_mid-refactor.inc"); | ||
|
||
$tableName = 'pdoFetchLobTest'; | ||
$binaryColumn = 'varbinary_max'; | ||
$strColumn = 'varchar_max'; | ||
$nstrColumn = 'nvarchar_max'; | ||
|
||
$bin = 'abcdefghijklmnopqrstuvwxyz'; | ||
$binaryValue = str_repeat($bin, 100); | ||
$hexValue = str_repeat(strtoupper(bin2hex($bin)), 100); | ||
$strValue = str_repeat("stuvwxyz", 400); | ||
$nstrValue = str_repeat("ÃÜðßZZýA©", 200); | ||
|
||
function checkData($actual, $expected) | ||
{ | ||
trace("Actual:\n$actual\n"); | ||
|
||
$success = true; | ||
$pos = strpos($actual, $expected); | ||
if (($pos === false) || ($pos > 1)) { | ||
$success = false; | ||
} | ||
|
||
return ($success); | ||
} | ||
|
||
function fetchBinary($conn, $buffered) | ||
{ | ||
global $tableName, $binaryColumn, $binaryValue, $hexValue; | ||
|
||
try { | ||
$query = "SELECT $binaryColumn FROM $tableName"; | ||
if ($buffered) { | ||
$stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE=>PDO::SQLSRV_CURSOR_BUFFERED)); | ||
} else { | ||
$stmt = $conn->prepare($query); | ||
} | ||
$stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); | ||
$stmt->execute(); | ||
|
||
$row = $stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
if (!checkData($value, $binaryValue)) { | ||
echo "Fetched binary value unexpected ($buffered): $value\n"; | ||
} | ||
|
||
$stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_SYSTEM); | ||
$stmt->execute(); | ||
|
||
$row = $stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
if (!checkData($value, $hexValue)) { | ||
echo "Fetched binary value a char string ($buffered): $value\n"; | ||
} | ||
|
||
$stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_UTF8); | ||
$stmt->execute(); | ||
|
||
$row = $stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
if (!checkData($value, $hexValue)) { | ||
echo "Fetched binary value as UTF-8 string ($buffered): $value\n"; | ||
} | ||
} catch (PdoException $e) { | ||
echo "Caught exception in fetchBinary ($buffered):\n"; | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
} | ||
|
||
function fetchAsString($conn, $buffered) | ||
{ | ||
global $tableName, $strColumn, $strValue; | ||
global $nstrColumn, $nstrValue; | ||
|
||
try { | ||
$query = "SELECT $strColumn, $nstrColumn FROM $tableName"; | ||
if ($buffered) { | ||
$stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE=>PDO::SQLSRV_CURSOR_BUFFERED)); | ||
} else { | ||
$stmt = $conn->prepare($query); | ||
} | ||
$stmt->execute(); | ||
|
||
$stmt->bindColumn($strColumn, $value1, PDO::PARAM_STR); | ||
$stmt->bindColumn($nstrColumn, $value2, PDO::PARAM_STR); | ||
$row = $stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
if (!checkData($value1, $strValue)) { | ||
echo "Fetched string value ($buffered): $value1\n"; | ||
} | ||
|
||
if (!checkData($value2, $nstrValue)) { | ||
echo "Fetched string value ($buffered): $value2\n"; | ||
} | ||
$stmt->execute(); | ||
|
||
$stmt->bindColumn($strColumn, $value, PDO::PARAM_STR, 0, PDO::SQLSRV_ENCODING_SYSTEM); | ||
$row = $stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
if (!checkData($value, $strValue)) { | ||
echo "Fetched string value: $value\n"; | ||
} | ||
} catch (PdoException $e) { | ||
echo "Caught exception in fetchBinary ($buffered):\n"; | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
} | ||
|
||
try { | ||
$conn = connect(); | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
// Create table of one max column | ||
$colMeta = array(new ColumnMeta('varbinary(max)', $binaryColumn), | ||
new ColumnMeta('varchar(max)', $strColumn), | ||
new ColumnMeta('nvarchar(max)', $nstrColumn)); | ||
createTable($conn, $tableName, $colMeta); | ||
|
||
// Insert one row | ||
$query = "INSERT INTO $tableName ($binaryColumn, $strColumn, $nstrColumn) VALUES (?, ?, ?)"; | ||
$stmt = $conn->prepare($query); | ||
$stmt->bindParam(1, $binaryValue, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); | ||
$stmt->bindParam(2, $strValue, PDO::PARAM_STR, 0, PDO::SQLSRV_ENCODING_SYSTEM); | ||
$stmt->bindParam(3, $nstrValue, PDO::PARAM_STR); | ||
$stmt->execute(); | ||
unset($stmt); | ||
|
||
// Starting fetching with or without client buffer | ||
fetchBinary($conn, false); | ||
fetchBinary($conn, true); | ||
|
||
fetchAsString($conn, false); | ||
fetchAsString($conn, true); | ||
|
||
dropTable($conn, $tableName); | ||
echo "Done\n"; | ||
unset($conn); | ||
} catch (PdoException $e) { | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
?> | ||
--EXPECT-- | ||
Done |
Oops, something went wrong.