Skip to content

Commit 12e6535

Browse files
authored
Merge pull request #297 from Hadis-Fard/dev
issue #270 fix
2 parents 2610243 + 9ec0956 commit 12e6535

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

source/pdo_sqlsrv/pdo_stmt.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,10 @@ int pdo_sqlsrv_stmt_get_col_data(pdo_stmt_t *stmt, int colno,
721721
sqlsrv_phptype sqlsrv_php_type;
722722
SQLSRV_ASSERT( colno >= 0 && colno < static_cast<int>( driver_stmt->current_meta_data.size()),
723723
"Invalid column number in pdo_sqlsrv_stmt_get_col_data" );
724-
sqlsrv_php_type = driver_stmt->sql_type_to_php_type( static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[ colno ]->field_type ),
725-
static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[ colno ]->field_size ),
726-
true );
727724

728725
// set the encoding if the user specified one via bindColumn, otherwise use the statement's encoding
729-
sqlsrv_php_type.typeinfo.encoding = driver_stmt->encoding();
726+
sqlsrv_php_type = driver_stmt->sql_type_to_php_type( static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[colno]->field_type ),
727+
static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[colno]->field_size ), true );
730728

731729
// if a column is bound to a type different than the column type, figure out a way to convert it to the
732730
// type they want
@@ -1306,6 +1304,8 @@ sqlsrv_phptype pdo_sqlsrv_stmt::sql_type_to_php_type( SQLINTEGER sql_type, SQLUI
13061304
"Invalid encoding on the connection. Must not be invalid or default." );
13071305
}
13081306

1307+
sqlsrv_phptype.typeinfo.encoding = local_encoding;
1308+
13091309
switch( sql_type ) {
13101310
case SQL_BIT:
13111311
case SQL_INTEGER:
@@ -1316,7 +1316,6 @@ sqlsrv_phptype pdo_sqlsrv_stmt::sql_type_to_php_type( SQLINTEGER sql_type, SQLUI
13161316
}
13171317
else {
13181318
sqlsrv_phptype.typeinfo.type = SQLSRV_PHPTYPE_STRING;
1319-
sqlsrv_phptype.typeinfo.encoding = local_encoding;
13201319
}
13211320
break;
13221321
case SQL_FLOAT:
@@ -1326,7 +1325,6 @@ sqlsrv_phptype pdo_sqlsrv_stmt::sql_type_to_php_type( SQLINTEGER sql_type, SQLUI
13261325
}
13271326
else {
13281327
sqlsrv_phptype.typeinfo.type = SQLSRV_PHPTYPE_STRING;
1329-
sqlsrv_phptype.typeinfo.encoding = local_encoding;
13301328
}
13311329
break;
13321330
case SQL_BIGINT:
@@ -1345,7 +1343,6 @@ sqlsrv_phptype pdo_sqlsrv_stmt::sql_type_to_php_type( SQLINTEGER sql_type, SQLUI
13451343
case SQL_WLONGVARCHAR:
13461344
case SQL_SS_XML:
13471345
sqlsrv_phptype.typeinfo.type = SQLSRV_PHPTYPE_STRING;
1348-
sqlsrv_phptype.typeinfo.encoding = local_encoding;
13491346
break;
13501347
case SQL_BINARY:
13511348
case SQL_LONGVARBINARY:
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
--TEST--
2+
Test fetch from binary, varbinary, varbinary(max), image columns, without setting binary encoding.
3+
--DESCRIPTION--
4+
Verifies GitHub issue 270 is fixed, users could not retrieve the data as inserted in binary columns without setting the binary encoding either on stmt or using bindCoulmn encoding.
5+
This test verifies that the data inserted in binary columns can be retrieved using fetch, fetchColumn, fetchObject, and fetchAll functions.
6+
7+
--FILE--
8+
<?php
9+
10+
require_once("autonomous_setup.php");
11+
12+
$tableName = 'test_binary'.rand();
13+
$columns = array( 'col1', 'col2', 'col3', 'col4');
14+
15+
// Connect
16+
$conn = new PDO( "sqlsrv:server=$serverName;Database=tempdb", $username, $password );
17+
18+
$sql = "CREATE TABLE $tableName ( $columns[0] binary(50), $columns[1] VARBINARY(50), $columns[2] VARBINARY(MAX), $columns[3] image)";
19+
$conn->exec($sql);
20+
21+
$icon = base64_decode("This is some text to test retrieving from binary type columns");
22+
23+
// Insert data using bind parameters
24+
$sql = "INSERT INTO $tableName($columns[0], $columns[1], $columns[2], $columns[3]) VALUES(?, ?, ?, ?)";
25+
$stmt = $conn->prepare($sql);
26+
$stmt->bindParam(1, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
27+
$stmt->bindParam(2, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
28+
$stmt->bindParam(3, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
29+
$stmt->bindParam(4, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
30+
$stmt->execute();
31+
32+
// loop through each column in the table
33+
foreach ($columns as $col){
34+
test_fetch($conn, $tableName, $col, $icon);
35+
}
36+
// DROP table
37+
$conn->query("DROP TABLE $tableName") ?: die();
38+
39+
//free statement and connection
40+
$stmt = null;
41+
$conn = null;
42+
43+
print_r("Test finished successfully");
44+
45+
//calls various fetch methods
46+
function test_fetch($conn, $tableName, $columnName, $input){
47+
48+
$len = strlen($input);
49+
$result = "";
50+
$sql = "SELECT $columnName from $tableName";
51+
52+
$stmt = $conn->query($sql);
53+
$stmt->bindColumn(1, $result, PDO::PARAM_LOB);
54+
$stmt->fetch(PDO::FETCH_BOUND);
55+
//binary is fixed size, to evaluate output, compare it using strncmp
56+
if( strncmp($result, $input, $len) !== 0){
57+
print_r("\nRetrieving using bindColumn failed");
58+
}
59+
60+
$result = "";
61+
$stmt = $conn->query($sql);
62+
$stmt->bindColumn(1, $result, PDO::PARAM_LOB, 0 , PDO::SQLSRV_ENCODING_BINARY);
63+
$stmt->fetch(PDO::FETCH_BOUND);
64+
if( strncmp($result, $input, $len) !== 0){
65+
print_r("\nRetrieving using bindColumn with encoding set failed");
66+
}
67+
68+
$result = "";
69+
$stmt = $conn->query($sql);
70+
$result = $stmt->fetchColumn();
71+
if( strncmp($result, $input, $len) !== 0){
72+
print_r("\nRetrieving using fetchColumn failed");
73+
}
74+
75+
$result = "";
76+
$stmt = $conn->query($sql);
77+
$result = $stmt->fetchObject();
78+
if( strncmp($result->$columnName, $input, $len) !== 0){
79+
print_r("\nRetrieving using fetchObject failed");
80+
}
81+
82+
$result = "";
83+
$stmt = $conn->query($sql);
84+
$result = $stmt->fetchAll( PDO::FETCH_COLUMN );
85+
if( strncmp($result[0], $input, $len) !== 0){
86+
print_r("\nRetrieving using fetchAll failed");
87+
}
88+
}
89+
90+
?>
91+
--EXPECT--
92+
Test finished successfully

0 commit comments

Comments
 (0)