From 6140f44922a28313098b10aee8c5ad760aa13558 Mon Sep 17 00:00:00 2001 From: Xaver Loppenstedt Date: Sat, 11 Feb 2017 12:19:35 +0100 Subject: [PATCH] Add support for data type sql_variant --- source/shared/core_stmt.cpp | 3 +- test/sqlsrv/bug_127.phpt | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/sqlsrv/bug_127.phpt diff --git a/source/shared/core_stmt.cpp b/source/shared/core_stmt.cpp index a50fe26ad..aba7ed708 100644 --- a/source/shared/core_stmt.cpp +++ b/source/shared/core_stmt.cpp @@ -1432,6 +1432,7 @@ void calc_string_size( sqlsrv_stmt* stmt, SQLUSMALLINT field_index, SQLLEN sql_t case SQL_TYPE_DATE: case SQL_SS_TIME2: case SQL_SS_TIMESTAMPOFFSET: + case SQL_SS_VARIANT: { // unixODBC 2.3.1 requires wide calls to support pooling core::SQLColAttributeW( stmt, field_index + 1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &size TSRMLS_CC ); @@ -1449,7 +1450,7 @@ void calc_string_size( sqlsrv_stmt* stmt, SQLUSMALLINT field_index, SQLLEN sql_t } default: - DIE ( "Unexpected SQL type encountered in calc_string_size." ); + DIE ( "Unexpected SQL type encountered in calc_string_size. sql_type = %1!d!", sql_type ); } } catch( core::CoreException& e ) { diff --git a/test/sqlsrv/bug_127.phpt b/test/sqlsrv/bug_127.phpt new file mode 100644 index 000000000..d2a65f35f --- /dev/null +++ b/test/sqlsrv/bug_127.phpt @@ -0,0 +1,67 @@ +--TEST-- +Bug #127 (sqlsrv: data type sql_variant is unsupported) +--SKIPIF-- + +--INI-- +--FILE-- + $database, 'Uid' => $username, 'PWD' => $password]); +print 'sqlsrv connection successfull: '.($conn !== false ? 'yes' : 'no').PHP_EOL; + +function build_sql($value) { + // See https://msdn.microsoft.com/en-us/library/ms178550.aspx + $properties= ['BaseType', 'Precision', 'Scale', 'TotalBytes', 'Collation', 'MaxLength']; + + $sql = "DECLARE @v1 sql_variant; SET @v1 = {$value}; "; + $sql .= "SELECT @v1 as Value"; + foreach ($properties as $property) { + $sql .= ", SQL_VARIANT_PROPERTY(@v1, '{$property}') as {$property}"; + } + $sql .= ";"; + + return $sql; +} + +function test_variant_type($conn, $type, $value) { + $result = sqlsrv_query($conn, build_sql($value)); + print 'sqlsrv query successfull: '.($result !== false ? 'yes' : 'no').PHP_EOL; + + $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC); + print 'sql_variant ' . $type . ' php type: ' . gettype($row['Value']) . PHP_EOL; + print 'sql_variant ' . $type . ' value: ' . $row['Value'] . PHP_EOL; + print 'sql_variant ' . $type . ' base type: ' . $row['BaseType'] . PHP_EOL; + print 'sql_variant ' . $type . ' precision: ' . $row['Precision'] . PHP_EOL; + print 'sql_variant ' . $type . ' scale: ' . $row['Scale'] . PHP_EOL; + print 'sql_variant ' . $type . ' total bytes: ' . $row['TotalBytes'] . PHP_EOL; + print 'sql_variant ' . $type . ' collation: ' . $row['Collation'] . PHP_EOL; + print 'sql_variant ' . $type . ' max length: ' . $row['MaxLength'] . PHP_EOL; + + sqlsrv_free_stmt($result); +} + +test_variant_type($conn, 'string', "'ABC'"); +test_variant_type($conn, 'float', "cast (46279.1 as decimal(8,2))"); + +?> +--EXPECT-- +sqlsrv connection successfull: yes +sqlsrv query successfull: yes +sql_variant string php type: string +sql_variant string value: ABC +sql_variant string base type: varchar +sql_variant string precision: 0 +sql_variant string scale: 0 +sql_variant string total bytes: 11 +sql_variant string collation: SQL_Latin1_General_CP1_CI_AS +sql_variant string max length: 3 +sqlsrv query successfull: yes +sql_variant float php type: string +sql_variant float value: 46279.10 +sql_variant float base type: decimal +sql_variant float precision: 8 +sql_variant float scale: 2 +sql_variant float total bytes: 9 +sql_variant float collation: +sql_variant float max length: 5