Skip to content

Commit 6a5a37a

Browse files
author
Hadis Fard
authored
Merge pull request #402 from yitam/outputParam
extracted select output param into a different test
2 parents ef8ca1f + 507188c commit 6a5a37a

2 files changed

Lines changed: 87 additions & 26 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--TEST--
2+
Test sql_variant as an output parameter
3+
--DESCRIPTION--
4+
Since output param is not supported for sql_variant columns, this test verifies a proper error message is returned
5+
--FILE--
6+
<?php
7+
include 'MsCommon.inc';
8+
9+
function TestSimpleSelect($conn, $tableName)
10+
{
11+
$count = 0;
12+
13+
$stmt = $conn->prepare("SELECT ? = COUNT(* ) FROM $tableName");
14+
$stmt->bindParam( 1, $count, PDO::PARAM_INT, 4 );
15+
$stmt->execute();
16+
echo "Number of rows: $count\n";
17+
18+
$value = 'xx';
19+
20+
$stmt = $conn->prepare("SELECT ? = c2_variant FROM $tableName");
21+
$stmt->bindParam( 1, $value, PDO::PARAM_STR, 50 );
22+
$stmt->execute();
23+
echo "Variant column: $value\n\n";
24+
25+
}
26+
27+
function CreateVariantTable($conn, $tableName)
28+
{
29+
try
30+
{
31+
$stmt = $conn->exec("CREATE TABLE [$tableName] ([c1_int] int, [c2_variant] sql_variant)");
32+
}
33+
catch (Exception $e)
34+
{
35+
echo "Failed to create a test table\n";
36+
echo $e->getMessage();
37+
}
38+
39+
$tsql = "INSERT INTO [$tableName] ([c1_int], [c2_variant]) VALUES (1, ?)";
40+
41+
$data = "This is to test if sql_variant works with output parameters";
42+
43+
$stmt = $conn->prepare($tsql);
44+
$result = $stmt->execute(array($data));
45+
if (! $result)
46+
echo "Failed to insert data\n";
47+
}
48+
49+
function RunTest()
50+
{
51+
StartTest("pdo_param_output_select_variant");
52+
try
53+
{
54+
include("MsSetup.inc");
55+
// Connect
56+
$conn = new PDO( "sqlsrv:server=$server;Database=$databaseName", $uid, $pwd);
57+
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
58+
echo "\n";
59+
60+
// Now test with another stored procedure
61+
$tableName = GetTempTableName();
62+
CreateVariantTable($conn, $tableName);
63+
64+
// Test a simple select to get output
65+
TestSimpleSelect($conn, $tableName);
66+
67+
$conn = null;
68+
}
69+
catch (Exception $e)
70+
{
71+
echo $e->getMessage();
72+
}
73+
echo "\nDone\n";
74+
EndTest("pdo_param_output_select_variant");
75+
}
76+
77+
RunTest();
78+
79+
?>
80+
--EXPECT--
81+

82+
Number of rows: 1
83+
SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Implicit conversion from data type sql_variant to nvarchar(max) is not allowed. Use the CONVERT function to run this query.
84+
Done
85+
Test "pdo_param_output_select_variant" completed successfully.

test/pdo_sqlsrv/pdo_param_output_variants.phpt

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
--TEST--
22
Test parametrized insert and sql_variant as an output parameter.
33
--DESCRIPTION--
4-
parameterized queries is not supported for Sql_Variant columns, this test, verifies a proper error message is returned
4+
Since output param is not supported for sql_variant columns, this test verifies a proper error message is returned
55
--FILE--
66
<?php
77
include 'MsCommon.inc';
88

9-
function TestSimpleSelect($conn)
10-
{
11-
$value = 0;
12-
13-
$stmt = $conn->prepare("SELECT ? = COUNT(* ) FROM cd_info");
14-
$stmt->bindParam( 1, $value, PDO::PARAM_INT, 4 );
15-
$stmt->execute();
16-
echo "Number of items: $value\n";
17-
18-
$title = 'xx';
19-
20-
$stmt = $conn->prepare("SELECT ? = title FROM cd_info WHERE artist LIKE 'Led%'");
21-
$stmt->bindParam( 1, $title, PDO::PARAM_STR, 25 );
22-
$stmt->execute();
23-
echo "CD Title: $title\n\n";
24-
25-
}
26-
279
function TestReverse($conn)
2810
{
2911
$procName = GetTempProcName('sqlReverse');
@@ -127,16 +109,13 @@ function RunTest()
127109
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
128110
echo "\n";
129111

130-
// Test a simple select to get output
131-
TestSimpleSelect($conn);
132-
133112
// Test with a simple stored procedure
134113
TestReverse($conn);
135114

136115
// Now test with another stored procedure
137116
$tableName = GetTempTableName();
138117
CreateVariantTable($conn, $tableName);
139-
118+
140119
TestOutputParam($conn, $tableName);
141120

142121
$conn = null;
@@ -154,9 +133,6 @@ RunTest();
154133
?>
155134
--EXPECT--
156135

157-
Number of items: 7
158-
CD Title: Led Zeppelin 1
159-
160136
SQLSTATE[22018]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Operand type clash: nvarchar(max) is incompatible with sql_variant
161137
SQLSTATE[22018]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Operand type clash: nvarchar(max) is incompatible with sql_variant
162138

0 commit comments

Comments
 (0)