forked from microsoft/msphpsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added TVP support to non-procedure statements (microsoft#1309)
- Loading branch information
Showing
5 changed files
with
474 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
test/functional/pdo_sqlsrv/pdo_1307_tvp_non_procedure.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--TEST-- | ||
Verify Github Issue 1307 is fixed. | ||
--DESCRIPTION-- | ||
To show that table-valued parameters work with non-procedure statements | ||
--SKIPIF-- | ||
<?php require('skipif.inc'); ?> | ||
--FILE-- | ||
<?php | ||
require_once("MsSetup.inc"); | ||
require_once("MsCommon_mid-refactor.inc"); | ||
|
||
function cleanup($conn, $tvpname, $testTable) | ||
{ | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); | ||
|
||
$dropTableType = dropTableTypeSQL($conn, $tvpname); | ||
$conn->exec($dropTableType); | ||
$conn->exec("DROP TABLE IF EXISTS [$testTable]"); | ||
|
||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} | ||
|
||
function readData($conn, $testTable) | ||
{ | ||
$tsql = "SELECT id FROM $testTable ORDER BY id"; | ||
$stmt = $conn->query($tsql); | ||
$stmt->bindColumn('id', $ID); | ||
while ($row = $stmt->fetch( PDO::FETCH_BOUND ) ){ | ||
echo $ID . PHP_EOL; | ||
} | ||
} | ||
|
||
try { | ||
$conn = new PDO("sqlsrv:Server=$server;Database=$databaseName;", $uid, $pwd); | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
$tvpname = 'pdo_id_table'; | ||
$testTable = 'pdo_test_table'; | ||
|
||
cleanup($conn, $tvpname, $testTable); | ||
|
||
// Create the table type and test table | ||
$tsql = "CREATE TYPE $tvpname AS TABLE(id INT PRIMARY KEY)"; | ||
$conn->exec($tsql); | ||
|
||
$tsql = "CREATE TABLE $testTable (id INT PRIMARY KEY)"; | ||
$conn->exec($tsql); | ||
|
||
// Populate the table using the table type | ||
$tsql = "INSERT INTO $testTable SELECT * FROM ?"; | ||
$tvpinput = array($tvpname => [[1], [2], [3]]); | ||
|
||
$stmt = $conn->prepare($tsql); | ||
$stmt->bindParam(1, $tvpinput, PDO::PARAM_LOB); | ||
$result = $stmt->execute(); | ||
|
||
// Verify the results | ||
readData($conn, $testTable); | ||
|
||
// Use Merge statement next | ||
$tsql = <<<QRY | ||
MERGE INTO $testTable t | ||
USING ? s ON s.id = t.id | ||
WHEN NOT MATCHED THEN | ||
INSERT (id) VALUES(s.id); | ||
QRY; | ||
|
||
unset($tvpinput); | ||
$tvpinput = array($tvpname => [[5], [4], [3], [2]]); | ||
|
||
$stmt = $conn->prepare($tsql); | ||
$stmt->bindParam(1, $tvpinput, PDO::PARAM_LOB); | ||
$result = $stmt->execute(); | ||
|
||
// Verify the results | ||
readData($conn, $testTable); | ||
|
||
cleanup($conn, $tvpname, $testTable); | ||
|
||
echo "Done\n"; | ||
|
||
unset($stmt); | ||
unset($conn); | ||
} catch (PDOException $e) { | ||
var_dump($e); | ||
} | ||
?> | ||
--EXPECT-- | ||
1 | ||
2 | ||
3 | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
Done |
105 changes: 105 additions & 0 deletions
105
test/functional/pdo_sqlsrv/pdo_1307_tvp_non_procedure_schema.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--TEST-- | ||
Verify Github Issue 1307 is fixed but TVP and table are defined in a different schema | ||
--DESCRIPTION-- | ||
To show that table-valued parameters work with non-procedure statements | ||
--SKIPIF-- | ||
<?php require('skipif.inc'); ?> | ||
--FILE-- | ||
<?php | ||
require_once("MsSetup.inc"); | ||
require_once("MsCommon_mid-refactor.inc"); | ||
|
||
function cleanup($conn, $tvpname, $testTable, $schema) | ||
{ | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); | ||
|
||
$dropTableType = dropTableTypeSQL($conn, $tvpname, $schema); | ||
$conn->exec($dropTableType); | ||
$conn->exec("DROP TABLE IF EXISTS [$schema].[$testTable]"); | ||
$conn->exec("DROP SCHEMA IF EXISTS [$schema]"); | ||
|
||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} | ||
|
||
function readData($conn, $schema, $testTable) | ||
{ | ||
$tsql = "SELECT id FROM [$schema].[$testTable] ORDER BY id"; | ||
$stmt = $conn->query($tsql); | ||
$stmt->bindColumn('id', $ID); | ||
while ($row = $stmt->fetch( PDO::FETCH_BOUND ) ){ | ||
echo $ID . PHP_EOL; | ||
} | ||
} | ||
|
||
try { | ||
$conn = new PDO("sqlsrv:Server=$server;Database=$databaseName;", $uid, $pwd); | ||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
$tvpname = 'pdo_id_table2'; | ||
$testTable = 'pdo_test_table2'; | ||
$schema = 'pdo schema'; | ||
|
||
cleanup($conn, $tvpname, $testTable, $schema); | ||
|
||
// Create the schema | ||
$tsql = "CREATE SCHEMA [$schema]"; | ||
$conn->exec($tsql); | ||
|
||
// Create the table type and test table | ||
$tsql = "CREATE TYPE [$schema].[$tvpname] AS TABLE(id INT PRIMARY KEY)"; | ||
$conn->exec($tsql); | ||
|
||
$tsql = "CREATE TABLE [$schema].[$testTable] (id INT PRIMARY KEY)"; | ||
$conn->exec($tsql); | ||
|
||
// Populate the table using the table type | ||
$tsql = "INSERT INTO [$schema].[$testTable] SELECT * FROM ?"; | ||
$tvpinput = array($tvpname => [[5], [3], [1]], $schema); | ||
|
||
$stmt = $conn->prepare($tsql); | ||
$stmt->bindParam(1, $tvpinput, PDO::PARAM_LOB); | ||
$result = $stmt->execute(); | ||
|
||
// Verify the results | ||
readData($conn, $schema, $testTable); | ||
|
||
// Use Merge statement next | ||
$tsql = <<<QRY | ||
MERGE INTO [$schema].[$testTable] t | ||
USING ? s ON s.id = t.id | ||
WHEN NOT MATCHED THEN | ||
INSERT (id) VALUES(s.id); | ||
QRY; | ||
|
||
unset($tvpinput); | ||
$tvpinput = array($tvpname => [[2], [4], [6], [7]], $schema); | ||
|
||
$stmt = $conn->prepare($tsql); | ||
$stmt->bindParam(1, $tvpinput, PDO::PARAM_LOB); | ||
$result = $stmt->execute(); | ||
|
||
// Verify the results | ||
readData($conn, $schema, $testTable); | ||
|
||
cleanup($conn, $tvpname, $testTable, $schema); | ||
|
||
echo "Done\n"; | ||
|
||
unset($stmt); | ||
unset($conn); | ||
} catch (PDOException $e) { | ||
var_dump($e); | ||
} | ||
?> | ||
--EXPECT-- | ||
1 | ||
3 | ||
5 | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
Done |
Oops, something went wrong.