php-sqlsrv/test/functional/pdo_sqlsrv/pdo_param_output_variants.phpt

115 lines
4.3 KiB
Plaintext
Raw Normal View History

2017-05-15 23:35:57 +02:00
--TEST--
2017-05-16 23:32:34 +02:00
Test parametrized insert and sql_variant as an output parameter.
--DESCRIPTION--
Since output param is not supported for sql_variant columns, this test verifies a proper error message is returned
2017-05-15 23:35:57 +02:00
--FILE--
<?php
require_once("MsCommon_mid-refactor.inc");
function testReverse($conn)
2017-05-15 23:35:57 +02:00
{
$procName = getProcName('sqlReverse');
try {
2017-05-15 23:35:57 +02:00
$spCode = "CREATE PROC [$procName] @string AS SQL_VARIANT OUTPUT as SELECT @string = REVERSE(CAST(@string AS varchar(30)))";
$conn->exec($spCode);
} catch (Exception $e) {
2017-05-15 23:35:57 +02:00
echo "Failed to create the reverse procedure\n";
echo $e->getMessage();
}
try {
$stmt = $conn->prepare("{ CALL [$procName] (?) }");
2017-05-15 23:35:57 +02:00
$string = "123456789";
$stmt->bindParam(1, $string, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 30);
2017-05-15 23:35:57 +02:00
$stmt->execute();
// Connection with Column Encryption enabled works for non encrypted SQL_VARIANT
// Since SQLDescribeParam is called
2017-12-13 02:12:48 +01:00
if (isAEConnected() && $string === "987654321") {
2017-12-07 23:10:10 +01:00
echo "Testing input output parameter with SQL_VARIANT is successful.\n";
2017-12-01 01:26:39 +01:00
} else {
echo "Does REVERSE work? $string \n";
}
} catch (Exception $e) {
2017-05-15 23:35:57 +02:00
//echo "Failed when calling the reverse procedure\n";
$error = $e->getMessage();
2017-12-13 02:12:48 +01:00
if (!isAEConnected() && strpos($error, "Implicit conversion from data type sql_variant to nvarchar is not allowed.") !== false) {
2017-12-07 23:10:10 +01:00
echo "Testing input output parameter with SQL_VARIANT is successful.\n";
} else {
echo "$error\n";
}
}
2017-05-15 23:35:57 +02:00
}
function createVariantTable($conn, $tableName)
2017-05-15 23:35:57 +02:00
{
try {
createTable($conn, $tableName, array("c1_int" => "int", "c2_variant" => "sql_variant"));
} catch (Exception $e) {
2017-05-15 23:35:57 +02:00
echo "Failed to create a test table\n";
echo $e->getMessage();
}
2017-05-15 23:35:57 +02:00
$data = "This is to test if sql_variant works with output parameters";
2017-12-13 02:12:48 +01:00
if (!isAEConnected()) {
$tsql = "INSERT INTO [$tableName] ([c1_int], [c2_variant]) VALUES (1, ?)";
$stmt = $conn->prepare($tsql);
$result = $stmt->execute(array($data));
} else {
$tsql = "INSERT INTO [$tableName] ([c1_int], [c2_variant]) VALUES (?, ?)";
$stmt = $conn->prepare($tsql);
$intData = 1;
$result = $stmt->execute(array($intData, $data));
}
if (! $result) {
2017-05-15 23:35:57 +02:00
echo "Failed to insert data\n";
}
2017-05-15 23:35:57 +02:00
}
function testOutputParam($conn, $tableName)
2017-05-15 23:35:57 +02:00
{
// First, create a temporary stored procedure
$procName = getProcName('sqlVariant');
2017-05-15 23:35:57 +02:00
$spArgs = "@p1 int, @p2 sql_variant OUTPUT";
$spCode = "SET @p2 = ( SELECT [c2_variant] FROM $tableName WHERE [c1_int] = @p1 )";
$conn->exec("CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END");
2017-05-15 23:35:57 +02:00
$callArgs = "?, ?";
// Data to initialize $callResult variable. This variable should be different from
// the inserted data in the table
$initData = "A short text";
$callResult = $initData;
try {
2017-05-15 23:35:57 +02:00
$stmt = $conn->prepare("{ CALL [$procName] ($callArgs)}");
$stmt->bindValue(1, 1);
$stmt->bindParam(2, $callResult, PDO::PARAM_STR, 100);
$stmt->execute();
2017-12-13 02:12:48 +01:00
if (isAEConnected() && $callResult === "This is to test if sql_variant works with output parameters") {
2017-12-07 23:10:10 +01:00
echo "Testing output parameter with SQL_VARIANT is successful.\n";
} else {
echo "Does SELECT from table work? $callResult \n";
}
} catch (Exception $e) {
if (!strcmp($initData, $callResult)) {
2017-05-15 23:35:57 +02:00
echo "initialized data and result should be the same";
}
$error = $e->getMessage();
2017-12-13 02:12:48 +01:00
if (!isAEConnected() && strpos($error, "Operand type clash: nvarchar(max) is incompatible with sql_variant") !== false) {
2017-12-07 23:10:10 +01:00
echo "Testing output parameter with SQL_VARIANT is successful.\n";
} else {
echo "$error\n";
}
2017-05-15 23:35:57 +02:00
}
}
try {
// Connect
$conn = connect();
// Test with a simple stored procedure
testReverse($conn);
// Now test with another stored procedure
$tableName = getTableName();
createVariantTable($conn, $tableName);
testOutputParam($conn, $tableName);
$conn = null;
} catch (Exception $e) {
echo $e->getMessage();
}
2017-05-15 23:35:57 +02:00
?>
2017-12-01 01:26:39 +01:00
--EXPECT--
2017-12-07 23:10:10 +01:00
Testing input output parameter with SQL_VARIANT is successful.
Testing output parameter with SQL_VARIANT is successful.