diff --git a/source/shared/core_results.cpp b/source/shared/core_results.cpp index 1f1593a2a..1f33913e1 100644 --- a/source/shared/core_results.cpp +++ b/source/shared/core_results.cpp @@ -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( sqlsrv_malloc( field_len, sizeof(char), sizeof(char))); diff --git a/test/pdo_sqlsrv/pdo_069_fetch_empty_nvarchar_buffered.phpt b/test/pdo_sqlsrv/pdo_069_fetch_empty_nvarchar_buffered.phpt new file mode 100644 index 000000000..b650dc548 --- /dev/null +++ b/test/pdo_sqlsrv/pdo_069_fetch_empty_nvarchar_buffered.phpt @@ -0,0 +1,38 @@ +--TEST-- +GitHub issue #69 - fetching an empty nvarchar using client buffer +--SKIPIF-- +--FILE-- +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 \ No newline at end of file diff --git a/test/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt b/test/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt new file mode 100644 index 000000000..c1568d997 --- /dev/null +++ b/test/sqlsrv/srv_069_fetch_empty_nvarchar_buffered.phpt @@ -0,0 +1,44 @@ +--TEST-- +GitHub issue #69 - fetching an empty nvarchar using client buffer +--SKIPIF-- +--FILE-- + '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 \ No newline at end of file