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

162 lines
4 KiB
Plaintext
Raw Normal View History

2017-03-08 02:25:39 +01:00
--TEST--
2017-03-08 18:55:29 +01:00
test rowCount() with different querying method and test nextRowset() with different fetch
2017-03-08 02:25:39 +01:00
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
2017-03-08 02:25:39 +01:00
--FILE--
<?php
include_once("MsCommon_mid-refactor.inc");
2017-03-08 02:25:39 +01:00
function rowCountQuery($exec)
2017-03-08 02:25:39 +01:00
{
$conn = connect();
$tableName = getTableName('testRowCount');
createTable($conn, $tableName, array("c1_int" => "int", "c2_real" => "real"));
2017-03-08 18:55:29 +01:00
$numRows = 5;
for ($i = 1; $i <= $numRows; $i++) {
$r = $i * 1.0;
insertRow($conn, $tableName, array("c1_int" => $i, "c2_real" => $r));
2017-03-08 02:25:39 +01:00
}
fetchRowsets($conn, $tableName, $numRows);
for ($i = 1; $i <= $numRows; $i++) {
updateData($conn, $tableName, $i, $exec);
2017-03-08 02:25:39 +01:00
}
deleteData($conn, $tableName, $exec);
dropTable($conn, $tableName);
unset($stmt);
unset($conn);
2017-03-08 02:25:39 +01:00
}
function updateData($conn, $tableName, $value, $exec)
2017-03-08 02:25:39 +01:00
{
$newValue = $value * 100;
$rowCount = 0;
if (isColEncrypted()) {
// need to bind parameters for updating encrypted columns
$query = "UPDATE $tableName SET c1_int = ? WHERE (c1_int = ?)";
$stmt = $conn->prepare($query);
2017-11-04 01:01:09 +01:00
if ($rowCount > 0) {
echo "Number of rows affected prior to execution should be 0!\n";
}
$stmt->bindParam(1, $newValue);
$stmt->bindParam(2, $value);
2017-03-08 02:25:39 +01:00
$stmt->execute();
$rowCount = $stmt->rowCount();
} else {
2017-11-04 01:01:09 +01:00
$query = "UPDATE $tableName SET c1_int = $newValue WHERE (c1_int = $value)";
if ($exec) {
$rowCount = $conn->exec($query);
} else {
$stmt = $conn->prepare($query);
$rowCount = $stmt->rowCount();
if ($rowCount > 0) {
echo "Number of rows affected prior to execution should be 0!\n";
}
$stmt->execute();
$rowCount = $stmt->rowCount();
}
2017-03-08 02:25:39 +01:00
}
if ($rowCount !== 1) {
2017-03-08 02:25:39 +01:00
echo "Number of rows affected should be 1!\n";
}
unset($stmt);
2017-03-08 02:25:39 +01:00
}
function compareValues($actual, $expected)
2017-03-08 18:55:29 +01:00
{
if ($actual != $expected) {
2017-03-08 18:55:29 +01:00
echo "Unexpected value $value returned! Expected $expected.\n";
}
}
function fetchRowsets($conn, $tableName, $numRows)
2017-03-08 18:55:29 +01:00
{
if (!isColEncrypted()) {
$query = "SELECT [c1_int] FROM $tableName ORDER BY [c1_int]";
} else {
// ORDER BY is not supported in encrypted columns
$query = "SELECT [c1_int] FROM $tableName";
}
2017-03-08 18:55:29 +01:00
$queries = $query . ';' . $query . ';' . $query;
$stmt = $conn->query($queries);
$i = 0;
while ($row = $stmt->fetch(PDO::FETCH_LAZY)) {
2017-03-08 18:55:29 +01:00
$value = (int)$row['c1_int'];
compareValues($value, ++$i);
2017-03-08 18:55:29 +01:00
}
if ($i != $numRows) {
2017-03-08 18:55:29 +01:00
echo "Number of rows fetched $i is unexpected!\n";
}
2017-03-08 18:55:29 +01:00
$result = $stmt->nextRowset();
if ($result == false) {
2017-03-08 18:55:29 +01:00
echo "Missing result sets!\n";
}
2017-03-08 18:55:29 +01:00
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
$i = 0;
foreach ($rows as $row) {
foreach ($row as $key => $value) {
2017-03-08 18:55:29 +01:00
$value = (int)$value;
compareValues($value, ++$i);
2017-03-08 18:55:29 +01:00
}
}
2017-03-08 18:55:29 +01:00
$result = $stmt->nextRowset();
if ($result == false) {
2017-03-08 18:55:29 +01:00
echo "Missing result sets!\n";
}
2017-03-08 18:55:29 +01:00
$stmt->bindColumn('c1_int', $value);
$i = 0;
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
compareValues($value, ++$i);
2017-03-08 18:55:29 +01:00
}
2017-03-08 18:55:29 +01:00
$result = $stmt->nextRowset();
if ($result != false) {
2017-03-08 18:55:29 +01:00
echo "Number of result sets exceeding expectation!\n";
}
}
function deleteData($conn, $tableName, $exec)
2017-03-08 02:25:39 +01:00
{
$query = "DELETE TOP(3) FROM $tableName";
2017-03-08 02:25:39 +01:00
$rowCount = 0;
if ($exec) {
2017-03-08 02:25:39 +01:00
$rowCount = $conn->exec($query);
} else {
2017-03-08 02:25:39 +01:00
$stmt = $conn->query($query);
$rowCount = $stmt->rowCount();
}
if ($rowCount <= 0) {
echo "Number of rows affected should be > 0!\n";
2017-03-08 02:25:39 +01:00
}
unset($stmt);
}
2017-03-08 02:25:39 +01:00
echo "Starting test...\n";
try {
rowCountQuery(true);
2017-11-04 01:01:09 +01:00
rowCountQuery(false);
} catch (Exception $e) {
echo $e->getMessage();
}
echo "Done\n";
2017-03-08 02:25:39 +01:00
?>
--EXPECT--
2017-05-02 21:00:53 +02:00
Starting test...
2017-03-08 02:25:39 +01:00
Done