commit
1ce4e85cbb
|
@ -1,5 +1,6 @@
|
|||
--TEST--
|
||||
PDO Connection Pooling Test on Unix
|
||||
--DESCRIPTION--
|
||||
This test assumes odbcinst.ini has not been modified.
|
||||
This test also requires root privileges to modify odbcinst.ini file on Linux.
|
||||
--SKIPIF--
|
||||
|
|
|
@ -22,7 +22,7 @@ catch( PDOException $e ) {
|
|||
}
|
||||
try{
|
||||
echo "\nTesting new connection after exception thrown in previous connection...\n";
|
||||
$tableName1 = GetTempTableName('tab1');
|
||||
$tableName1 = GetTempTableName('tab1', false);
|
||||
$conn = new PDO( $dsn, $username, $password );
|
||||
$sql = "CREATE TABLE $tableName1 (c1 int, c2 varchar(10))";
|
||||
$stmt = $conn->query($sql);
|
||||
|
|
159
test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt
Normal file
159
test/pdo_sqlsrv/pdo_fetch_complex_transactions.phpt
Normal file
|
@ -0,0 +1,159 @@
|
|||
--TEST--
|
||||
Test transactions commit, rollback and aborting in between
|
||||
--FILE--
|
||||
<?php
|
||||
include 'pdo_tools.inc';
|
||||
|
||||
function ComplexTransaction($conn, $tableName)
|
||||
{
|
||||
$stmt = $conn->query("CREATE TABLE $tableName ([c1_int] int, [c2_real] real)");
|
||||
$stmt = null;
|
||||
|
||||
$stmtSelect = $conn->prepare("SELECT * FROM $tableName");
|
||||
$stmtDelete = $conn->prepare("DELETE TOP(3) FROM $tableName");
|
||||
|
||||
// insert ten rows
|
||||
$numRows = 10;
|
||||
InsertData($conn, $tableName, $numRows);
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
$conn->beginTransaction();
|
||||
$stmtDelete->execute();
|
||||
$rowsAffected = $stmtDelete->rowCount();
|
||||
$conn->commit();
|
||||
echo "Committed deleting 3 rows\n";
|
||||
|
||||
$numRows = $numRows - $rowsAffected;
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
$conn->beginTransaction();
|
||||
$stmtDelete->execute();
|
||||
$conn->rollback();
|
||||
echo "Rolled back\n";
|
||||
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
$conn->beginTransaction();
|
||||
$stmtDelete->execute();
|
||||
$rowsAffected = $stmtDelete->rowCount();
|
||||
$conn->commit();
|
||||
echo "Committed deleting 3 rows\n";
|
||||
|
||||
$numRows = $numRows - $rowsAffected;
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
$conn->beginTransaction();
|
||||
$stmtDelete->execute();
|
||||
$conn->rollback();
|
||||
echo "Rolled back\n";
|
||||
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
$conn->beginTransaction();
|
||||
$stmtDelete->execute();
|
||||
|
||||
echo "Deletion aborted\n";
|
||||
|
||||
return $numRows;
|
||||
}
|
||||
|
||||
function InsertData($conn, $tableName, $count)
|
||||
{
|
||||
$stmt = $conn->prepare("INSERT INTO $tableName (c1_int, c2_real) VALUES (?, ?)");
|
||||
|
||||
for ($i = 0; $i < $count; $i++)
|
||||
{
|
||||
$v1 = $i + 1;
|
||||
$v2 = $v1 * 1.5;
|
||||
|
||||
$stmt->bindValue(1, $v1);
|
||||
$stmt->bindValue(2, $v2);
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
function FetchData($stmt, $tableName, $numRows, $fetchMode = false)
|
||||
{
|
||||
$numFetched = 0;
|
||||
$stmt->execute();
|
||||
if ($fetchMode)
|
||||
{
|
||||
$stmt->setFetchMode(PDO::FETCH_LAZY);
|
||||
while ($result = $stmt->fetch())
|
||||
$numFetched++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while ($result = $stmt->fetch(PDO::FETCH_LAZY))
|
||||
$numFetched++;
|
||||
}
|
||||
|
||||
echo "Number of rows fetched: $numFetched\n";
|
||||
if ($numFetched != $numRows)
|
||||
{
|
||||
echo "Expected $numRows rows.\n";
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("pdo_fetch_complex_transactions");
|
||||
try
|
||||
{
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$conn = new PDO( "sqlsrv:server=$serverName;Database=$database", $username, $password);
|
||||
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
|
||||
|
||||
$conn2 = new PDO( "sqlsrv:server=$serverName;Database=$database", $username, $password);
|
||||
$conn2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
|
||||
|
||||
$tableName = GetTempTableName('testTransaction', false);
|
||||
|
||||
// ComplexTransaction() returns number of rows left in $tableName
|
||||
$numRows = ComplexTransaction($conn, $tableName);
|
||||
// disconnect first connection, transaction aborted
|
||||
$conn = null;
|
||||
|
||||
// select table using the second connection
|
||||
$stmt = $conn2->prepare("SELECT * FROM $tableName");
|
||||
FetchData($stmt, $tableName, $numRows, true);
|
||||
|
||||
// drop test table
|
||||
$conn2->query("DROP TABLE $tableName");
|
||||
$conn2 = null;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("pdo_fetch_complex_transactions");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'pdo_fetch_complex_transactions' test...
|
||||
Number of rows fetched: 10
|
||||
Committed deleting 3 rows
|
||||
Number of rows fetched: 7
|
||||
Rolled back
|
||||
Number of rows fetched: 7
|
||||
Committed deleting 3 rows
|
||||
Number of rows fetched: 4
|
||||
Rolled back
|
||||
Number of rows fetched: 4
|
||||
Deletion aborted
|
||||
Number of rows fetched: 4
|
||||
|
||||
Done
|
||||
...Test 'pdo_fetch_complex_transactions' completed successfully.
|
219
test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt
Normal file
219
test/pdo_sqlsrv/pdo_fetch_cursor_scroll_random.phpt
Normal file
|
@ -0,0 +1,219 @@
|
|||
--TEST--
|
||||
Test with cursor scroll and select different rows in some random order
|
||||
--FILE--
|
||||
<?php
|
||||
include 'pdo_tools.inc';
|
||||
|
||||
function Cursor_ForwardOnly($conn, $tableName)
|
||||
{
|
||||
$stmt = $conn->exec("CREATE TABLE $tableName ([c1_int] int, [c2_tinyint] tinyint, [c3_smallint] smallint, [c4_bigint] bigint, [c5_bit] bit)");
|
||||
|
||||
// insert data
|
||||
$numRows = InsertData($conn, $tableName);
|
||||
|
||||
// select table
|
||||
$stmt = $conn->prepare("SELECT * FROM $tableName ORDER BY c1_int", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_FWDONLY));
|
||||
$stmt->execute();
|
||||
|
||||
$numRowsFetched = 0;
|
||||
while ($row = $stmt->fetch(PDO::FETCH_NUM))
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
$numRowsFetched++;
|
||||
}
|
||||
|
||||
if ($numRowsFetched != $numRows)
|
||||
echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n";
|
||||
}
|
||||
|
||||
function Cursor_Scroll_FetchRows($conn, $tableName)
|
||||
{
|
||||
$stmt = $conn->prepare("SELECT * FROM $tableName ORDER BY c1_int", array(constant('PDO::ATTR_CURSOR') => PDO::CURSOR_SCROLL));
|
||||
$stmt->execute();
|
||||
|
||||
GetFirstRow($stmt);
|
||||
GetNextRow($stmt);
|
||||
GetLastRow($stmt);
|
||||
GetPriorRow($stmt);
|
||||
GetAbsoluteRow($stmt, 7);
|
||||
GetAbsoluteRow($stmt, 2);
|
||||
GetRelativeRow($stmt, 3);
|
||||
GetPriorRow($stmt);
|
||||
GetRelativeRow($stmt, -4);
|
||||
GetAbsoluteRow($stmt, 0);
|
||||
GetNextRow($stmt);
|
||||
GetRelativeRow($stmt, 5);
|
||||
GetAbsoluteRow($stmt, -1);
|
||||
GetNextRow($stmt);
|
||||
GetLastRow($stmt);
|
||||
GetRelativeRow($stmt, 1);
|
||||
}
|
||||
|
||||
function InsertData($conn, $tableName)
|
||||
{
|
||||
$numRows = 0;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((1), (0), (null), (9223372036854775807), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((2), (null), (-32768), (9223372036854775807), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((3), (255), (-32768), (1035941737), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((4), (null), (4762), (804325764), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((5), (57), (32767), (-9223372036854775808), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((6), (201), (-32768), (450619355), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((7), (244), (-21244), (981345728), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((8), (143), (0), (-1330405117), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((9), (null), (null), (209123628), (0))");
|
||||
$numRows += $count;
|
||||
$count = $conn->exec("INSERT INTO $tableName (c1_int, c2_tinyint, c3_smallint, c4_bigint, c5_bit) VALUES ((10), (147), (21133), (-1), (0))");
|
||||
$numRows += $count;
|
||||
|
||||
return $numRows;
|
||||
}
|
||||
|
||||
function GetFirstRow($stmt)
|
||||
{
|
||||
echo "\nfirst row: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_FIRST, 0);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetNextRow($stmt)
|
||||
{
|
||||
echo "\nnext row: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT, 0);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetPriorRow($stmt)
|
||||
{
|
||||
echo "\nprior row: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR, 0);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetLastRow($stmt)
|
||||
{
|
||||
echo "\nlast row: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST, 0);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetRelativeRow($stmt, $offset)
|
||||
{
|
||||
echo "\nrow $offset from the current row: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_REL, $offset);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetAbsoluteRow($stmt, $offset)
|
||||
{
|
||||
echo "\nabsolute row with offset $offset: ";
|
||||
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $offset);
|
||||
if ($row)
|
||||
{
|
||||
echo "$row[0]\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("pdo_fetch_cursor_scroll_random");
|
||||
try
|
||||
{
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$conn = new PDO( "sqlsrv:server=$serverName;Database=$database", $username, $password);
|
||||
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
|
||||
|
||||
$tableName = GetTempTableName();
|
||||
|
||||
Cursor_ForwardOnly($conn, $tableName);
|
||||
Cursor_Scroll_FetchRows($conn, $tableName);
|
||||
|
||||
$conn = null;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("pdo_fetch_cursor_scroll_random");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'pdo_fetch_cursor_scroll_random' test...
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
|
||||
first row: 1
|
||||
|
||||
next row: 2
|
||||
|
||||
last row: 10
|
||||
|
||||
prior row: 9
|
||||
|
||||
absolute row with offset 7: 8
|
||||
|
||||
absolute row with offset 2: 3
|
||||
|
||||
row 3 from the current row: 6
|
||||
|
||||
prior row: 5
|
||||
|
||||
row -4 from the current row: 1
|
||||
|
||||
absolute row with offset 0: 1
|
||||
|
||||
next row: 2
|
||||
|
||||
row 5 from the current row: 7
|
||||
|
||||
absolute row with offset -1:
|
||||
next row: 1
|
||||
|
||||
last row: 10
|
||||
|
||||
row 1 from the current row:
|
||||
Done
|
||||
...Test 'pdo_fetch_cursor_scroll_random' completed successfully.
|
|
@ -17,26 +17,40 @@ function EndTest($testName)
|
|||
echo "...Test '$testName' completed successfully.\n";
|
||||
}
|
||||
|
||||
function GetTempTableName($table = '')
|
||||
function GetTempTableName($table = '', $temporary = true)
|
||||
{
|
||||
// A temporary table name with the '#' prefix will be automatically
|
||||
// dropped once the connection is closed
|
||||
// dropped once the connection is closed. Otherwise, the caller
|
||||
// should take care of dropping the temp table afterwards.
|
||||
|
||||
$timestamp = round(microtime(true)*1000);
|
||||
|
||||
$prefix = '';
|
||||
if ($temporary)
|
||||
$prefix = '#';
|
||||
|
||||
if (strlen($table) == 0)
|
||||
return "#pdo_test_table" . $timestamp;
|
||||
else
|
||||
return $table . $timestamp;
|
||||
$table = 'php_test_table';
|
||||
|
||||
return $prefix . $table . '_' . $timestamp;
|
||||
}
|
||||
|
||||
function GetTempProcName($proc = '')
|
||||
function GetTempProcName($proc = '', $temporary = true)
|
||||
{
|
||||
// A temporary stored procedure name with the '#' prefix will be
|
||||
// automatically dropped once the connection is closed
|
||||
// automatically dropped once the connection is closed. Otherwise,
|
||||
// the caller should take care of dropping the temp procedure afterwards.
|
||||
|
||||
$timestamp = round(microtime(true)*1000);
|
||||
|
||||
$prefix = '';
|
||||
if ($temporary)
|
||||
$prefix = '#';
|
||||
|
||||
if (strlen($proc) == 0)
|
||||
return "#php_test_proc" . $timestamp;
|
||||
else
|
||||
return $proc . $timestamp;
|
||||
$proc = 'php_test_proc';
|
||||
|
||||
return $prefix . $proc . '_' . $timestamp;
|
||||
}
|
||||
|
||||
function CompareNumericData($actual, $expected)
|
||||
|
|
150
test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt
Normal file
150
test/sqlsrv/sqlsrv_fetch_complex_transactions.phpt
Normal file
|
@ -0,0 +1,150 @@
|
|||
--TEST--
|
||||
Test transactions commit, rollback and aborting in between
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function ComplexTransaction($conn, $conn2)
|
||||
{
|
||||
$tableName = GetTempTableName('testTransaction', false);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_real] real)");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmtSelect = sqlsrv_prepare($conn, "SELECT * FROM $tableName");
|
||||
$stmtDelete = sqlsrv_prepare($conn, "DELETE TOP(3) FROM $tableName");
|
||||
|
||||
// insert ten rows
|
||||
$numRows = 10;
|
||||
InsertData($conn, $tableName, $numRows);
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
sqlsrv_begin_transaction($conn);
|
||||
sqlsrv_execute($stmtDelete);
|
||||
$rowsAffected = sqlsrv_rows_affected($stmtDelete);
|
||||
sqlsrv_commit($conn);
|
||||
echo "Committed deleting 3 rows\n";
|
||||
|
||||
$numRows = $numRows - $rowsAffected;
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
sqlsrv_begin_transaction($conn);
|
||||
sqlsrv_execute($stmtDelete);
|
||||
sqlsrv_rollback($conn);
|
||||
echo "Rolled back\n";
|
||||
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
sqlsrv_begin_transaction($conn);
|
||||
sqlsrv_execute($stmtDelete);
|
||||
$rowsAffected = sqlsrv_rows_affected($stmtDelete);
|
||||
sqlsrv_commit($conn);
|
||||
echo "Committed deleting 3 rows\n";
|
||||
|
||||
$numRows = $numRows - $rowsAffected;
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
sqlsrv_begin_transaction($conn);
|
||||
sqlsrv_execute($stmtDelete);
|
||||
sqlsrv_rollback($conn);
|
||||
echo "Rolled back\n";
|
||||
|
||||
FetchData($stmtSelect, $tableName, $numRows);
|
||||
|
||||
sqlsrv_begin_transaction($conn);
|
||||
sqlsrv_execute($stmtDelete);
|
||||
// disconnect first connection, transaction aborted
|
||||
sqlsrv_close($conn);
|
||||
echo "Deletion aborted\n";
|
||||
|
||||
// select table using the second connection
|
||||
$stmt = sqlsrv_prepare($conn2, "SELECT * FROM $tableName");
|
||||
FetchData($stmt, $tableName, $numRows);
|
||||
|
||||
sqlsrv_query($conn2, "DROP TABLE $tableName");
|
||||
}
|
||||
|
||||
function InsertData($conn, $tableName, $count)
|
||||
{
|
||||
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_real) VALUES (?, ?)", array(&$v1, &$v2));
|
||||
|
||||
for ($i = 0; $i < $count; $i++)
|
||||
{
|
||||
$v1 = $i + 1;
|
||||
$v2 = $v1 * 1.5;
|
||||
|
||||
sqlsrv_execute($stmt);
|
||||
}
|
||||
}
|
||||
|
||||
function FetchData($stmt, $tableName, $numRows)
|
||||
{
|
||||
$numFetched = 0;
|
||||
sqlsrv_execute($stmt);
|
||||
while ($result = sqlsrv_fetch($stmt))
|
||||
{
|
||||
$numFetched++;
|
||||
}
|
||||
|
||||
echo "Number of rows fetched: $numFetched\n";
|
||||
if ($numFetched != $numRows)
|
||||
{
|
||||
echo "Expected $numRows rows.\n";
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_fetch_complex_transactions");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password);
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
$conn2 = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn2 ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
ComplexTransaction($conn, $conn2);
|
||||
|
||||
sqlsrv_close($conn2); // $conn should have been closed
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_fetch_complex_transactions");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_fetch_complex_transactions' test...
|
||||
Number of rows fetched: 10
|
||||
Committed deleting 3 rows
|
||||
Number of rows fetched: 7
|
||||
Rolled back
|
||||
Number of rows fetched: 7
|
||||
Committed deleting 3 rows
|
||||
Number of rows fetched: 4
|
||||
Rolled back
|
||||
Number of rows fetched: 4
|
||||
Deletion aborted
|
||||
Number of rows fetched: 4
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_fetch_complex_transactions' completed successfully.
|
206
test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt
Normal file
206
test/sqlsrv/sqlsrv_fetch_cursor_static_scroll.phpt
Normal file
|
@ -0,0 +1,206 @@
|
|||
--TEST--
|
||||
Test with static cursor and select different rows in some random order
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function FetchRow_Query($conn)
|
||||
{
|
||||
$tableName = GetTempTableName();
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar] varchar(10))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
// insert data
|
||||
$numRows = 10;
|
||||
InsertData($conn, $tableName, $numRows);
|
||||
|
||||
// select table
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName", array(), array('Scrollable' => 'static'));
|
||||
|
||||
HasRows($stmt);
|
||||
$numRowsFetched = 0;
|
||||
while ($obj = sqlsrv_fetch_object($stmt))
|
||||
{
|
||||
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
|
||||
$numRowsFetched++;
|
||||
}
|
||||
|
||||
if ($numRowsFetched != $numRows)
|
||||
echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n";
|
||||
|
||||
GetFirstRow($stmt);
|
||||
GetNextRow($stmt);
|
||||
GetLastRow($stmt);
|
||||
GetPriorRow($stmt);
|
||||
GetAbsoluteRow($stmt, 7);
|
||||
GetAbsoluteRow($stmt, 2);
|
||||
GetRelativeRow($stmt, 3);
|
||||
GetPriorRow($stmt);
|
||||
GetRelativeRow($stmt, -4);
|
||||
GetAbsoluteRow($stmt, 0);
|
||||
GetNextRow($stmt);
|
||||
GetRelativeRow($stmt, 5);
|
||||
GetAbsoluteRow($stmt, -1);
|
||||
GetNextRow($stmt);
|
||||
GetLastRow($stmt);
|
||||
GetRelativeRow($stmt, 1);
|
||||
}
|
||||
|
||||
function InsertData($conn, $tableName, $numRows)
|
||||
{
|
||||
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_varchar) VALUES (?, ?)", array(&$v1, &$v2));
|
||||
|
||||
for ($i = 0; $i < $numRows; $i++)
|
||||
{
|
||||
$v1 = $i + 1;
|
||||
$v2 = "Row " . $v1;
|
||||
|
||||
sqlsrv_execute($stmt);
|
||||
}
|
||||
}
|
||||
|
||||
function GetFirstRow($stmt)
|
||||
{
|
||||
echo "\nfirst row: ";
|
||||
$result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
|
||||
if ($result)
|
||||
{
|
||||
$field1 = sqlsrv_get_field( $stmt, 0 );
|
||||
$field2 = sqlsrv_get_field( $stmt, 1 );
|
||||
echo "$field1, $field2\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetNextRow($stmt)
|
||||
{
|
||||
echo "\nnext row: ";
|
||||
$result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_NEXT);
|
||||
if ($result)
|
||||
{
|
||||
$field1 = sqlsrv_get_field( $stmt, 0 );
|
||||
$field2 = sqlsrv_get_field( $stmt, 1 );
|
||||
echo "$field1, $field2\n";
|
||||
}
|
||||
}
|
||||
|
||||
function GetPriorRow($stmt)
|
||||
{
|
||||
echo "\nprior row: ";
|
||||
$obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_PRIOR);
|
||||
if ($obj)
|
||||
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
|
||||
}
|
||||
|
||||
function GetLastRow($stmt)
|
||||
{
|
||||
echo "\nlast row: ";
|
||||
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, SQLSRV_SCROLL_LAST);
|
||||
if ($row)
|
||||
echo $row[0] . ", " . $row[1] . "\n";
|
||||
}
|
||||
|
||||
function GetRelativeRow($stmt, $offset)
|
||||
{
|
||||
echo "\nrow $offset from the current row: ";
|
||||
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_RELATIVE, $offset);
|
||||
if ($row)
|
||||
echo $row['c1_int'] . ", " . $row['c2_varchar'] . "\n";
|
||||
}
|
||||
|
||||
function GetAbsoluteRow($stmt, $offset)
|
||||
{
|
||||
echo "\nabsolute row with offset $offset: ";
|
||||
$obj = sqlsrv_fetch_object($stmt, null, null, SQLSRV_SCROLL_ABSOLUTE, $offset);
|
||||
if ($obj)
|
||||
echo $obj->c1_int . ", " . $obj->c2_varchar . "\n";
|
||||
}
|
||||
|
||||
function HasRows($stmt)
|
||||
{
|
||||
$rows = sqlsrv_has_rows( $stmt );
|
||||
if ( $rows != true )
|
||||
echo "Should have rows!\n";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_fetch_cursor_static_scroll");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password);
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
FetchRow_Query($conn);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_fetch_cursor_static_scroll");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_fetch_cursor_static_scroll' test...
|
||||
1, Row 1
|
||||
2, Row 2
|
||||
3, Row 3
|
||||
4, Row 4
|
||||
5, Row 5
|
||||
6, Row 6
|
||||
7, Row 7
|
||||
8, Row 8
|
||||
9, Row 9
|
||||
10, Row 10
|
||||
|
||||
first row: 1, Row 1
|
||||
|
||||
next row: 2, Row 2
|
||||
|
||||
last row: 10, Row 10
|
||||
|
||||
prior row: 9, Row 9
|
||||
|
||||
absolute row with offset 7: 8, Row 8
|
||||
|
||||
absolute row with offset 2: 3, Row 3
|
||||
|
||||
row 3 from the current row: 6, Row 6
|
||||
|
||||
prior row: 5, Row 5
|
||||
|
||||
row -4 from the current row: 1, Row 1
|
||||
|
||||
absolute row with offset 0: 1, Row 1
|
||||
|
||||
next row: 2, Row 2
|
||||
|
||||
row 5 from the current row: 7, Row 7
|
||||
|
||||
absolute row with offset -1:
|
||||
next row: 1, Row 1
|
||||
|
||||
last row: 10, Row 10
|
||||
|
||||
row 1 from the current row:
|
||||
Done
|
||||
...Test 'sqlsrv_fetch_cursor_static_scroll' completed successfully.
|
194
test/sqlsrv/sqlsrv_fetch_cursor_types.phpt
Normal file
194
test/sqlsrv/sqlsrv_fetch_cursor_types.phpt
Normal file
|
@ -0,0 +1,194 @@
|
|||
--TEST--
|
||||
Test various cursor types and whether they reflect changes in the database
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function Fetch_WithCursor($conn, $cursorType)
|
||||
{
|
||||
$tableName = GetTempTableName();
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_char] char(10))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
// insert four rows
|
||||
$numRows = 4;
|
||||
InsertData($conn, $tableName, 0, $numRows);
|
||||
|
||||
// select table
|
||||
$stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName", array(), array('Scrollable' => $cursorType));
|
||||
sqlsrv_execute($stmt);
|
||||
|
||||
GetNumRows($stmt, $cursorType);
|
||||
$numRowsFetched = 0;
|
||||
while ($obj = sqlsrv_fetch_object($stmt))
|
||||
{
|
||||
echo $obj->c1_int . "\n";
|
||||
$numRowsFetched++;
|
||||
}
|
||||
|
||||
if ($numRowsFetched != $numRows)
|
||||
echo "Number of rows fetched $numRowsFetched is wrong! Expected $numRows\n";
|
||||
|
||||
DeleteThenFetchLastRow($conn, $stmt, $tableName, 4);
|
||||
}
|
||||
|
||||
function InsertData($conn, $tableName, $start, $count)
|
||||
{
|
||||
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_char) VALUES (?, ?)", array(&$v1, &$v2));
|
||||
|
||||
$numRows = $start + $count;
|
||||
for ($i = $start; $i < $numRows; $i++)
|
||||
{
|
||||
$v1 = $i + 1;
|
||||
$v2 = "Row " . $v1;
|
||||
|
||||
sqlsrv_execute($stmt);
|
||||
}
|
||||
}
|
||||
|
||||
function DeleteThenFetchLastRow($conn, $stmt, $tableName, $id)
|
||||
{
|
||||
echo "\nNow delete the last row then try to fetch it...\n";
|
||||
$stmt2 = sqlsrv_query( $conn, "DELETE FROM $tableName WHERE [c1_int] = 4" );
|
||||
if ( $stmt2 !== false ) {
|
||||
sqlsrv_free_stmt( $stmt2 );
|
||||
}
|
||||
|
||||
$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_LAST );
|
||||
if ($result)
|
||||
{
|
||||
$field1 = sqlsrv_get_field( $stmt, 0 );
|
||||
$field2 = sqlsrv_get_field( $stmt, 1 );
|
||||
var_dump($field1);
|
||||
var_dump($field2);
|
||||
}
|
||||
else
|
||||
{
|
||||
var_dump($result);
|
||||
}
|
||||
}
|
||||
|
||||
function GetNumRows($stmt, $cursorType)
|
||||
{
|
||||
$expectedToFail = false;
|
||||
if ($cursorType == SQLSRV_CURSOR_FORWARD || $cursorType == SQLSRV_CURSOR_DYNAMIC)
|
||||
$expectedToFail = true;
|
||||
|
||||
$rowCount = 0;
|
||||
$rowCount = sqlsrv_num_rows( $stmt );
|
||||
if ($expectedToFail)
|
||||
{
|
||||
if ($rowCount === false)
|
||||
{
|
||||
echo "Error occurred in sqlsrv_num_rows, which is expected\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "sqlsrv_num_rows expected to fail!\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($rowCount === false)
|
||||
{
|
||||
echo "Error occurred in sqlsrv_num_rows, which is unexpected!\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Number of rows: $rowCount\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_fetch_cursor_types");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password);
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
echo "\nUsing SQLSRV_CURSOR_FORWARD...\n";
|
||||
Fetch_WithCursor($conn, SQLSRV_CURSOR_FORWARD);
|
||||
echo "\nUsing SQLSRV_CURSOR_DYNAMIC...\n";
|
||||
Fetch_WithCursor($conn, SQLSRV_CURSOR_DYNAMIC);
|
||||
echo "\nUsing SQLSRV_CURSOR_KEYSET...\n";
|
||||
Fetch_WithCursor($conn, SQLSRV_CURSOR_KEYSET);
|
||||
echo "\nUsing SQLSRV_CURSOR_STATIC...\n";
|
||||
Fetch_WithCursor($conn, SQLSRV_CURSOR_STATIC);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_fetch_cursor_types");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_fetch_cursor_types' test...
|
||||
|
||||
Using SQLSRV_CURSOR_FORWARD...
|
||||
Error occurred in sqlsrv_num_rows, which is expected
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
Now delete the last row then try to fetch it...
|
||||
bool(false)
|
||||
|
||||
Using SQLSRV_CURSOR_DYNAMIC...
|
||||
Error occurred in sqlsrv_num_rows, which is expected
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
Now delete the last row then try to fetch it...
|
||||
int(3)
|
||||
string(10) "Row 3 "
|
||||
|
||||
Using SQLSRV_CURSOR_KEYSET...
|
||||
Number of rows: 4
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
Now delete the last row then try to fetch it...
|
||||
bool(false)
|
||||
bool(false)
|
||||
|
||||
Using SQLSRV_CURSOR_STATIC...
|
||||
Number of rows: 4
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
Now delete the last row then try to fetch it...
|
||||
int(4)
|
||||
string(10) "Row 4 "
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_fetch_cursor_types' completed successfully.
|
137
test/sqlsrv/sqlsrv_fetch_invalid_types.phpt
Normal file
137
test/sqlsrv/sqlsrv_fetch_invalid_types.phpt
Normal file
File diff suppressed because one or more lines are too long
120
test/sqlsrv/sqlsrv_param_query_errors.phpt
Normal file
120
test/sqlsrv/sqlsrv_param_query_errors.phpt
Normal file
|
@ -0,0 +1,120 @@
|
|||
--TEST--
|
||||
Insert with query params but with wrong parameters or types
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function ParamQueryError_PhpType_Mismatch($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('PhpType_Mismatch');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array(1.5, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_FLOAT, SQLSRV_SQLTYPE_VARCHAR('max'))));
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
|
||||
$result = sqlsrv_fetch($stmt);
|
||||
if (! $result)
|
||||
{
|
||||
echo "Fetch should succeed\n";
|
||||
}
|
||||
$value0 = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
||||
$value1 = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
||||
if ($value0 != "1" || $value1 != "1.5")
|
||||
{
|
||||
echo "Data $value0 or $value1 unexpected\n";
|
||||
}
|
||||
}
|
||||
|
||||
function ParamQueryError_Dir_Invalid($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('Dir_Invalid');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", 32, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR('max'))));
|
||||
|
||||
print handle_errors() . "\n";
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", 'SQLSRV_PARAM_INTERNAL', SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR('max'))));
|
||||
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
function ParamQueryError_PhpType_Encoding($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('PhpType_Encoding');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('SQLSRV_ENC_UNKNOWN'), null)));
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
function ParamQueryError_PhpType_Invalid($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('PhpType_Invalid');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", SQLSRV_PARAM_IN, 'SQLSRV_PHPTYPE_UNKNOWN', SQLSRV_SQLTYPE_VARCHAR('max'))));
|
||||
print handle_errors() . "\n";
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", SQLSRV_PARAM_IN, 6, SQLSRV_SQLTYPE_VARCHAR('max'))));
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_param_query_errors");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password, 'CharacterSet'=>'UTF-8');
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
ParamQueryError_PhpType_Mismatch($conn);
|
||||
ParamQueryError_Dir_Invalid($conn);
|
||||
ParamQueryError_PhpType_Encoding($conn);
|
||||
ParamQueryError_PhpType_Invalid($conn);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_param_query_errors");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_param_query_errors' test...
|
||||
An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
|
||||
An invalid direction for parameter 2 was specified. SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, and SQLSRV_PARAM_INOUT are valid values.
|
||||
An invalid PHP type for parameter 2 was specified.
|
||||
An invalid PHP type for parameter 2 was specified.
|
||||
An invalid PHP type for parameter 2 was specified.
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_param_query_errors' completed successfully.
|
97
test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt
Normal file
97
test/sqlsrv/sqlsrv_param_query_invalid_inputs.phpt
Normal file
|
@ -0,0 +1,97 @@
|
|||
--TEST--
|
||||
Insert with query params but with various invalid inputs or boundaries
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function ParamQueryError_MinMaxScale($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('MinMaxScale');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_decimal] decimal(28,4), [c3_numeric] numeric(32,4))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_decimal) VALUES (?, ?)", array(1, array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_DECIMAL(28, 34))));
|
||||
print handle_errors() . "\n";
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_numeric) VALUES (?, ?)", array(1, array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NUMERIC(32, -1))));
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
function ParamQueryError_MinMaxSize($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('MinMaxSize');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varchar_max] varchar(max))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(0))));
|
||||
print handle_errors() . "\n";
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varchar_max) VALUES (?, ?)", array(1, array("Test Data", SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(9000))));
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
function ParamQueryError_MinMaxPrecision($conn)
|
||||
{
|
||||
$tableName = GetTempTableName('MinMaxPrecision');
|
||||
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_decimal] decimal(28,4), [c3_numeric] numeric(32,4))");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_numeric) VALUES (?, ?)", array(1, array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_NUMERIC(40, 0))));
|
||||
print handle_errors() . "\n";
|
||||
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_decimal) VALUES (?, ?)", array(1, array(0.0, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_DECIMAL(-1, 0))));
|
||||
print handle_errors() . "\n";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_param_query_invalid_inputs");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password, 'CharacterSet'=>'UTF-8');
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
ParamQueryError_MinMaxScale($conn);
|
||||
ParamQueryError_MinMaxSize($conn);
|
||||
ParamQueryError_MinMaxPrecision($conn);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_param_query_invalid_inputs");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_param_query_invalid_inputs' test...
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
An invalid size or precision for parameter 2 was specified.
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_param_query_invalid_inputs' completed successfully.
|
|
@ -6,7 +6,7 @@ include 'tools.inc';
|
|||
|
||||
function QueryTimeout($conn1, $conn2, $commit)
|
||||
{
|
||||
$tableName = GetTempTableName('testQueryTimeout');
|
||||
$tableName = GetTempTableName('testQueryTimeout', false);
|
||||
|
||||
$stmt = sqlsrv_query($conn1, "CREATE TABLE $tableName ([c1_int] int, [c2_tinyint] tinyint, [c3_smallint] smallint, [c4_bigint] bigint, [c5_bit] bit, [c6_float] float, [c7_real] real, [c8_decimal] decimal(28,4), [c9_numeric] numeric(32,4), [c10_money] money, [c11_smallmoney] smallmoney)");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
|
118
test/sqlsrv/sqlsrv_streams_empty_char.phpt
Normal file
118
test/sqlsrv/sqlsrv_streams_empty_char.phpt
Normal file
|
@ -0,0 +1,118 @@
|
|||
--TEST--
|
||||
Populate different test tables with character fields using empty stream data as inputs
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function EmptyStream_Char2Stream($conn, $fileName)
|
||||
{
|
||||
$tableName = GetTempTableName();
|
||||
|
||||
// create a test table
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_char] char(512), [c3_varchar] varchar(512), [c4_varchar_max] varchar(max), [c5_text] text)");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
// insert data
|
||||
$fname = fopen($fileName, "r");
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_char) VALUES (?, ?)", array(1, &$fname), array('SendStreamParamsAtExec' => 0));
|
||||
sqlsrv_send_stream_data($stmt);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
fclose($fname);
|
||||
|
||||
FetchData($conn, $tableName, 1);
|
||||
|
||||
$fname = fopen($fileName, "r");
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c3_varchar) VALUES (?, ?)", array(2, &$fname), array('SendStreamParamsAtExec' => 0));
|
||||
sqlsrv_send_stream_data($stmt);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
fclose($fname);
|
||||
|
||||
FetchData($conn, $tableName, 2);
|
||||
|
||||
$fname = fopen($fileName, "r");
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c4_varchar_max) VALUES (?, ?)", array(3, &$fname), array('SendStreamParamsAtExec' => 0));
|
||||
sqlsrv_send_stream_data($stmt);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
fclose($fname);
|
||||
|
||||
FetchData($conn, $tableName, 3);
|
||||
|
||||
$fname = fopen($fileName, "r");
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c5_text) VALUES (?, ?)", array(4, &$fname), array('SendStreamParamsAtExec' => 0));
|
||||
sqlsrv_send_stream_data($stmt);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
fclose($fname);
|
||||
|
||||
FetchData($conn, $tableName, 4);
|
||||
}
|
||||
|
||||
function FetchData($conn, $tableName, $fld)
|
||||
{
|
||||
$stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName WHERE c1_int = $fld");
|
||||
sqlsrv_execute($stmt);
|
||||
$result = sqlsrv_fetch($stmt);
|
||||
$stream = sqlsrv_get_field($stmt, $fld, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
|
||||
var_dump($stream);
|
||||
|
||||
sqlsrv_execute($stmt);
|
||||
$result = sqlsrv_fetch($stmt);
|
||||
$value = sqlsrv_get_field($stmt, $fld, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY));
|
||||
var_dump($value);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_streams_empty_char");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password, 'CharacterSet'=>'UTF-8');
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
// create an empty file
|
||||
$fileName = "sqlsrv_streams_empty_char.dat";
|
||||
$fp = fopen($fileName, "wb");
|
||||
fclose($fp);
|
||||
|
||||
EmptyStream_Char2Stream($conn, $fileName);
|
||||
|
||||
// delete the file
|
||||
unlink($fileName);
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_streams_empty_char");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_streams_empty_char' test...
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(false)
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_streams_empty_char' completed successfully.
|
93
test/sqlsrv/sqlsrv_streams_null_binary.phpt
Normal file
93
test/sqlsrv/sqlsrv_streams_null_binary.phpt
Normal file
|
@ -0,0 +1,93 @@
|
|||
--TEST--
|
||||
Populate different binary fields using null stream data as inputs.
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function NullStream_Bin2String($conn, $tableName)
|
||||
{
|
||||
$fname = null;
|
||||
$value = -2106133115;
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_varbinary, c3_varbinary_max, c4_image) VALUES (?, ?, ?, ?)", array($value, array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY(512)), array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max')), array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_IMAGE)));
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
FetchData($conn, $tableName, $value);
|
||||
}
|
||||
|
||||
function NullStreamPrep_Bin2String($conn, $tableName)
|
||||
{
|
||||
$fname = null;
|
||||
$value = -413736480;
|
||||
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_int, c2_varbinary, c3_varbinary_max, c4_image) VALUES (?, ?, ?, ?)", array($value, array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY(512)), array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max')), array(&$fname, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_IMAGE)));
|
||||
sqlsrv_execute($stmt);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
FetchData($conn, $tableName, $value);
|
||||
}
|
||||
|
||||
function FetchData($conn, $tableName, $value)
|
||||
{
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName WHERE c1_int = $value");
|
||||
$result = sqlsrv_fetch($stmt);
|
||||
$numfields = sqlsrv_num_fields($stmt);
|
||||
for ($i = 1; $i < $numfields; $i++)
|
||||
{
|
||||
$value = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
||||
var_dump($value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_streams_null_binary");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password);
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
// create a test table
|
||||
$tableName = GetTempTableName();
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_varbinary] varbinary(512), [c3_varbinary_max] varbinary(max), [c4_image] image)");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
NullStream_Bin2String($conn, $tableName);
|
||||
NullStreamPrep_Bin2String($conn, $tableName);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_streams_null_binary");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_streams_null_binary' test...
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_streams_null_binary' completed successfully.
|
79
test/sqlsrv/sqlsrv_streams_null_nchar.phpt
Normal file
79
test/sqlsrv/sqlsrv_streams_null_nchar.phpt
Normal file
|
@ -0,0 +1,79 @@
|
|||
--TEST--
|
||||
Populate different unicode character fields using null stream data as inputs
|
||||
--FILE--
|
||||
<?php
|
||||
include 'tools.inc';
|
||||
|
||||
function NullStream_Char2Stream($conn)
|
||||
{
|
||||
$tableName = GetTempTableName();
|
||||
|
||||
// create a test table
|
||||
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_nchar] nchar(512), [c3_nvarchar] nvarchar(512), [c4_nvarchar_max] nvarchar(max), [c5_ntext] ntext)");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$fname = null;
|
||||
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1_int, c2_nchar, c3_nvarchar, c4_nvarchar_max, c5_ntext) VALUES (?, ?, ?, ?, ?)", array(-187518515, &$fname, &$fname, &$fname, &$fname));
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
FetchData($conn, $tableName);
|
||||
}
|
||||
|
||||
function FetchData($conn, $tableName)
|
||||
{
|
||||
$stmt = sqlsrv_prepare($conn, "SELECT * FROM $tableName");
|
||||
sqlsrv_execute($stmt);
|
||||
$result = sqlsrv_fetch($stmt);
|
||||
$numfields = sqlsrv_num_fields($stmt);
|
||||
for ($i = 1; $i < $numfields; $i++)
|
||||
{
|
||||
$value = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY));
|
||||
var_dump($value);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RunTest
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
function RunTest()
|
||||
{
|
||||
StartTest("sqlsrv_streams_null_nchar");
|
||||
try
|
||||
{
|
||||
set_time_limit(0);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
|
||||
require_once("autonomous_setup.php");
|
||||
$database = "tempdb";
|
||||
|
||||
// Connect
|
||||
$connectionInfo = array('Database'=>$database, 'UID'=>$username, 'PWD'=>$password, 'CharacterSet'=>'UTF-8');
|
||||
$conn = sqlsrv_connect($serverName, $connectionInfo);
|
||||
if( !$conn ) { FatalError("Could not connect.\n"); }
|
||||
|
||||
NullStream_Char2Stream($conn);
|
||||
|
||||
sqlsrv_close($conn);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage();
|
||||
}
|
||||
echo "\nDone\n";
|
||||
EndTest("sqlsrv_streams_null_nchar");
|
||||
}
|
||||
|
||||
RunTest();
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
|
||||
...Starting 'sqlsrv_streams_null_nchar' test...
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
NULL
|
||||
|
||||
Done
|
||||
...Test 'sqlsrv_streams_null_nchar' completed successfully.
|
|
@ -43,26 +43,40 @@ function EndTest($testName)
|
|||
echo "...Test '$testName' completed successfully.\n";
|
||||
}
|
||||
|
||||
function GetTempTableName($table = '')
|
||||
function GetTempTableName($table = '', $temporary = true)
|
||||
{
|
||||
// A temporary table name with the '#' prefix will be automatically
|
||||
// dropped once the connection is closed
|
||||
// dropped once the connection is closed. Otherwise, the caller
|
||||
// should take care of dropping the temp table afterwards.
|
||||
|
||||
$timestamp = round(microtime(true)*1000);
|
||||
|
||||
$prefix = '';
|
||||
if ($temporary)
|
||||
$prefix = '#';
|
||||
|
||||
if (strlen($table) == 0)
|
||||
return "#php_test_table" . $timestamp;
|
||||
else
|
||||
return $table . $timestamp;
|
||||
$table = 'php_test_table';
|
||||
|
||||
return $prefix . $table . '_' . $timestamp;
|
||||
}
|
||||
|
||||
function GetTempProcName($proc = '')
|
||||
function GetTempProcName($proc = '', $temporary = true)
|
||||
{
|
||||
// A temporary stored procedure name with the '#' prefix will be
|
||||
// automatically dropped once the connection is closed
|
||||
// automatically dropped once the connection is closed. Otherwise,
|
||||
// the caller should take care of dropping the temp procedure afterwards.
|
||||
|
||||
$timestamp = round(microtime(true)*1000);
|
||||
|
||||
$prefix = '';
|
||||
if ($temporary)
|
||||
$prefix = '#';
|
||||
|
||||
if (strlen($proc) == 0)
|
||||
return "#php_test_proc" . $timestamp;
|
||||
else
|
||||
return $proc . $timestamp;
|
||||
$proc = 'php_test_proc';
|
||||
|
||||
return $prefix . $proc . '_' . $timestamp;
|
||||
}
|
||||
|
||||
function FatalError($errorMsg)
|
||||
|
|
Loading…
Reference in a new issue