Skip to content

Commit

Permalink
Merge pull request #285 from yitam/GH69
Browse files Browse the repository at this point in the history
Fix and tests for issue #69
  • Loading branch information
Hadis Kakanejadi Fard authored Feb 10, 2017
2 parents e363a44 + 9b035cc commit da36767
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/shared/core_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,11 @@ SQLRETURN sqlsrv_buffered_result_set::wide_to_system_string( SQLSMALLINT field_i
field_data = &row[ meta[ field_index ].offset ] + sizeof( SQLULEN ) + read_so_far;
}

if ( field_len == 0 ) { // empty string, no need for conversion
*out_buffer_length = 0;
return SQL_SUCCESS;
}

// allocate enough to handle WC -> DBCS conversion if it happens
temp_string = reinterpret_cast<SQLCHAR*>( sqlsrv_malloc( field_len, sizeof(char), sizeof(char)));

Expand Down
38 changes: 38 additions & 0 deletions test/pdo_sqlsrv/pdo_069_fetch_empty_nvarchar_buffered.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
GitHub issue #69 - fetching an empty nvarchar using client buffer
--SKIPIF--
--FILE--
<?php
// Connect
require_once("autonomous_setup.php");

$conn = new PDO("sqlsrv:server=$serverName", $username, $password);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$sql = "EXEC dbo.sp_executesql
N'DECLARE @x nvarchar(max)
SET @x = '''' -- empty string
SELECT @x AS [Empty_Nvarchar_Max]'";

$stmt = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$stmt->execute();

$return = $stmt->fetchAll( PDO::FETCH_ASSOC );
print_r($return);

// Free the statement and connection resources.
$stmt = null;
$conn = null;

print "Done";
?>
--EXPECT--
Array
(
[0] => Array
(
[Empty_Nvarchar_Max] =>
)

)
Done
44 changes: 44 additions & 0 deletions test/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
GitHub issue #69 - fetching an empty nvarchar using client buffer
--SKIPIF--
--FILE--
<?php
function print_errors()
{
die( print_r( sqlsrv_errors(), true));
}

function test()
{
require_once("autonomous_setup.php");

// Connect
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( !$conn ) { print_errors(); }

$sql = "EXEC dbo.sp_executesql
N'DECLARE @x nvarchar(max)
SET @x = '''' -- empty string
SELECT @x AS [Empty_Nvarchar_Max]'";

$stmt = sqlsrv_query($conn, $sql, [], ["Scrollable" => 'buffered']);
if (! $stmt) { print_errors(); }

$return = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
print_r($return);

// Free the statement and connection resources.
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
}

test();

print "Done";
?>
--EXPECT--
Array
(
[Empty_Nvarchar_Max] =>
)
Done

0 comments on commit da36767

Please sign in to comment.