|
| 1 | +--TEST-- |
| 2 | +Test fetching varbinary, varchar, nvarchar max fields with client buffer |
| 3 | +--DESCRIPTION-- |
| 4 | +Similar to sqlsrv_fetch_large_stream test but fetching varbinary, varchar, nvarchar max fields as strings with or without client buffer |
| 5 | +--SKIPIF-- |
| 6 | +<?php require_once('skipif_mid-refactor.inc'); ?> |
| 7 | +--ENV-- |
| 8 | +PHPT_EXEC=true |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | +require_once("MsCommon_mid-refactor.inc"); |
| 12 | + |
| 13 | +$tableName = 'pdoFetchLobTest'; |
| 14 | +$binaryColumn = 'varbinary_max'; |
| 15 | +$strColumn = 'varchar_max'; |
| 16 | +$nstrColumn = 'nvarchar_max'; |
| 17 | + |
| 18 | +$bin = 'abcdefghijklmnopqrstuvwxyz'; |
| 19 | +$binaryValue = str_repeat($bin, 100); |
| 20 | +$hexValue = str_repeat(strtoupper(bin2hex($bin)), 100); |
| 21 | +$strValue = str_repeat("stuvwxyz", 400); |
| 22 | +$nstrValue = str_repeat("ÃÜðßZZýA©", 200); |
| 23 | + |
| 24 | +function checkData($actual, $expected) |
| 25 | +{ |
| 26 | + trace("Actual:\n$actual\n"); |
| 27 | + |
| 28 | + $success = true; |
| 29 | + $pos = strpos($actual, $expected); |
| 30 | + if (($pos === false) || ($pos > 1)) { |
| 31 | + $success = false; |
| 32 | + } |
| 33 | + |
| 34 | + return ($success); |
| 35 | +} |
| 36 | + |
| 37 | +function fetchBinary($conn, $buffered) |
| 38 | +{ |
| 39 | + global $tableName, $binaryColumn, $binaryValue, $hexValue; |
| 40 | + |
| 41 | + try { |
| 42 | + $query = "SELECT $binaryColumn FROM $tableName"; |
| 43 | + if ($buffered) { |
| 44 | + $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE=>PDO::SQLSRV_CURSOR_BUFFERED)); |
| 45 | + } else { |
| 46 | + $stmt = $conn->prepare($query); |
| 47 | + } |
| 48 | + $stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); |
| 49 | + $stmt->execute(); |
| 50 | + |
| 51 | + $row = $stmt->fetch(PDO::FETCH_BOUND); |
| 52 | + |
| 53 | + if (!checkData($value, $binaryValue)) { |
| 54 | + echo "Fetched binary value unexpected ($buffered): $value\n"; |
| 55 | + } |
| 56 | + |
| 57 | + $stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_SYSTEM); |
| 58 | + $stmt->execute(); |
| 59 | + |
| 60 | + $row = $stmt->fetch(PDO::FETCH_BOUND); |
| 61 | + |
| 62 | + if (!checkData($value, $hexValue)) { |
| 63 | + echo "Fetched binary value a char string ($buffered): $value\n"; |
| 64 | + } |
| 65 | + |
| 66 | + $stmt->bindColumn($binaryColumn, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_UTF8); |
| 67 | + $stmt->execute(); |
| 68 | + |
| 69 | + $row = $stmt->fetch(PDO::FETCH_BOUND); |
| 70 | + |
| 71 | + if (!checkData($value, $hexValue)) { |
| 72 | + echo "Fetched binary value as UTF-8 string ($buffered): $value\n"; |
| 73 | + } |
| 74 | + } catch (PdoException $e) { |
| 75 | + echo "Caught exception in fetchBinary ($buffered):\n"; |
| 76 | + echo $e->getMessage() . PHP_EOL; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function fetchAsString($conn, $buffered) |
| 81 | +{ |
| 82 | + global $tableName, $strColumn, $strValue; |
| 83 | + global $nstrColumn, $nstrValue; |
| 84 | + |
| 85 | + try { |
| 86 | + $query = "SELECT $strColumn, $nstrColumn FROM $tableName"; |
| 87 | + if ($buffered) { |
| 88 | + $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE=>PDO::SQLSRV_CURSOR_BUFFERED)); |
| 89 | + } else { |
| 90 | + $stmt = $conn->prepare($query); |
| 91 | + } |
| 92 | + $stmt->execute(); |
| 93 | + |
| 94 | + $stmt->bindColumn($strColumn, $value1, PDO::PARAM_STR); |
| 95 | + $stmt->bindColumn($nstrColumn, $value2, PDO::PARAM_STR); |
| 96 | + $row = $stmt->fetch(PDO::FETCH_BOUND); |
| 97 | + |
| 98 | + if (!checkData($value1, $strValue)) { |
| 99 | + echo "Fetched string value ($buffered): $value1\n"; |
| 100 | + } |
| 101 | + |
| 102 | + if (!checkData($value2, $nstrValue)) { |
| 103 | + echo "Fetched string value ($buffered): $value2\n"; |
| 104 | + } |
| 105 | + $stmt->execute(); |
| 106 | + |
| 107 | + $stmt->bindColumn($strColumn, $value, PDO::PARAM_STR, 0, PDO::SQLSRV_ENCODING_SYSTEM); |
| 108 | + $row = $stmt->fetch(PDO::FETCH_BOUND); |
| 109 | + |
| 110 | + if (!checkData($value, $strValue)) { |
| 111 | + echo "Fetched string value: $value\n"; |
| 112 | + } |
| 113 | + } catch (PdoException $e) { |
| 114 | + echo "Caught exception in fetchBinary ($buffered):\n"; |
| 115 | + echo $e->getMessage() . PHP_EOL; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +try { |
| 120 | + $conn = connect(); |
| 121 | + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 122 | + |
| 123 | + // Create table of one max column |
| 124 | + $colMeta = array(new ColumnMeta('varbinary(max)', $binaryColumn), |
| 125 | + new ColumnMeta('varchar(max)', $strColumn), |
| 126 | + new ColumnMeta('nvarchar(max)', $nstrColumn)); |
| 127 | + createTable($conn, $tableName, $colMeta); |
| 128 | + |
| 129 | + // Insert one row |
| 130 | + $query = "INSERT INTO $tableName ($binaryColumn, $strColumn, $nstrColumn) VALUES (?, ?, ?)"; |
| 131 | + $stmt = $conn->prepare($query); |
| 132 | + $stmt->bindParam(1, $binaryValue, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); |
| 133 | + $stmt->bindParam(2, $strValue, PDO::PARAM_STR, 0, PDO::SQLSRV_ENCODING_SYSTEM); |
| 134 | + $stmt->bindParam(3, $nstrValue, PDO::PARAM_STR); |
| 135 | + $stmt->execute(); |
| 136 | + unset($stmt); |
| 137 | + |
| 138 | + // Starting fetching with or without client buffer |
| 139 | + fetchBinary($conn, false); |
| 140 | + fetchBinary($conn, true); |
| 141 | + |
| 142 | + fetchAsString($conn, false); |
| 143 | + fetchAsString($conn, true); |
| 144 | + |
| 145 | + dropTable($conn, $tableName); |
| 146 | + echo "Done\n"; |
| 147 | + unset($conn); |
| 148 | +} catch (PdoException $e) { |
| 149 | + echo $e->getMessage() . PHP_EOL; |
| 150 | +} |
| 151 | +?> |
| 152 | +--EXPECT-- |
| 153 | +Done |
0 commit comments