php-sqlsrv/test/functional/sqlsrv/TC32_DeleteQuery.phpt

86 lines
2.3 KiB
Plaintext
Raw Normal View History

2017-05-04 01:00:31 +02:00
--TEST--
Delete Query Test
--DESCRIPTION--
Executes several INSERT queries followed by DELETE queries and
validates the outcome reported by "sqlsrv_rows_affected".
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2017-05-04 01:00:31 +02:00
--FILE--
<?php
require_once('MsCommon.inc');
2017-05-04 01:00:31 +02:00
function deleteQuery()
2017-05-04 01:00:31 +02:00
{
$testName = "Statement - Delete Query";
startTest($testName);
2017-05-04 01:00:31 +02:00
setup();
$conn1 = AE\connect();
2017-05-04 01:00:31 +02:00
$noRows = 10;
$tableName = 'TC32test';
AE\createTestTable($conn1, $tableName);
// Insert some random rows
$noRowsInserted = AE\insertTestRows($conn1, $tableName, $noRows);
2017-05-04 01:00:31 +02:00
$row = 1;
$keyValue = "0";
while ($row <= $noRowsInserted) {
$stmt1 = AE\selectFromTable($conn1, $tableName);
if (sqlsrv_fetch($stmt1) === false) {
fatalError("Failed to retrieve 1st row of data from test table", true);
2017-05-04 01:00:31 +02:00
}
$keyValue = sqlsrv_get_field($stmt1, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
sqlsrv_free_stmt($stmt1);
trace("Deleting rows from $tableName ...");
2017-05-04 01:00:31 +02:00
$delRows = 1;
2021-07-29 00:45:04 +02:00
if (empty($keyValue)) {
$stmt2 = AE\executeQuery($conn1, "DELETE TOP(1) FROM [$tableName]");
2017-05-04 01:00:31 +02:00
$cond = "(top row)";
} else {
$cond = "(c1_int = ?)";
$params = array($keyValue);
$stmt3 = AE\selectFromTable($conn1, $tableName, $cond, $params);
$delRows = rowCount($stmt3);
2017-05-04 01:00:31 +02:00
sqlsrv_free_stmt($stmt3);
$stmt2 = AE\executeQuery($conn1, "DELETE FROM [$tableName]", $cond, $params );
2017-05-04 01:00:31 +02:00
}
$numRows1 = sqlsrv_rows_affected($stmt2);
sqlsrv_free_stmt($stmt2);
trace(" $numRows1 row".(($numRows1 > 1) ? "s" : " ")." $cond.\n");
2017-05-04 01:00:31 +02:00
if ($numRows1 != $delRows) {
die("Unexpected row count at delete: $numRows1 instead of $delRows");
2017-05-04 01:00:31 +02:00
}
$row += $numRows1;
}
2017-10-26 19:48:36 +02:00
$stmt3 = executeQuery($conn1, "DELETE TOP(1) FROM [$tableName]");
2017-05-04 01:00:31 +02:00
$numRows2 = sqlsrv_rows_affected($stmt3);
sqlsrv_free_stmt($stmt3);
if ($numRows2 > 0) {
2017-05-04 01:00:31 +02:00
die("Unexpected row count at delete: $numRows2");
}
dropTable($conn1, $tableName);
2017-05-04 01:00:31 +02:00
sqlsrv_close($conn1);
endTest($testName);
2017-05-04 01:00:31 +02:00
}
try {
deleteQuery();
} catch (Exception $e) {
echo $e->getMessage();
2017-05-04 01:00:31 +02:00
}
?>
--EXPECT--
Test "Statement - Delete Query" completed successfully.