2017-04-01 02:09:40 +02:00
|
|
|
--TEST--
|
2017-05-02 21:00:53 +02:00
|
|
|
test transaction rollback and commit
|
|
|
|
--DESCRIPTION--
|
2017-04-13 23:01:45 +02:00
|
|
|
starts a transaction, delete rows and rollback the transaction; starts a transaction, delete rows and commit
|
2017-04-01 02:09:40 +02:00
|
|
|
--SKIPIF--
|
2017-11-02 01:09:58 +01:00
|
|
|
<?php require('skipif_mid-refactor.inc'); ?>
|
2017-04-01 02:09:40 +02:00
|
|
|
--FILE--
|
|
|
|
<?php
|
2017-11-02 01:09:58 +01:00
|
|
|
require_once("MsCommon_mid-refactor.inc");
|
|
|
|
|
|
|
|
function deleteRows($conn, $tbname)
|
|
|
|
{
|
2017-11-04 01:10:45 +01:00
|
|
|
try {
|
|
|
|
if (!isColEncrypted()) {
|
|
|
|
$rows = $conn->exec("DELETE FROM $tbname WHERE col1 = 'a'");
|
|
|
|
} else {
|
|
|
|
// needs to find parameter for encrypted columns
|
|
|
|
$sql = "DELETE FROM $tbname WHERE col1 = ?";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
|
|
$col1 = "a";
|
|
|
|
$stmt->execute(array($col1));
|
|
|
|
$rows = $stmt->rowCount();
|
|
|
|
}
|
|
|
|
return $rows;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
var_dump($e->errorInfo);
|
2017-11-02 01:09:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$conn = connect();
|
|
|
|
|
|
|
|
$tbname = "Table1";
|
|
|
|
createTable($conn, $tbname, array("col1" => "char(1)", "col2" => "char(1)"));
|
|
|
|
|
|
|
|
insertRow($conn, $tbname, array("col1" => "a", "col2" => "b"));
|
|
|
|
insertRow($conn, $tbname, array("col1" => "a", "col2" => "c"));
|
|
|
|
|
2017-04-13 18:33:44 +02:00
|
|
|
//revert the inserts but roll back
|
2017-04-12 18:26:14 +02:00
|
|
|
$conn->beginTransaction();
|
2017-11-02 01:09:58 +01:00
|
|
|
$rows = deleteRows($conn, $tbname);
|
2017-04-01 02:09:40 +02:00
|
|
|
$conn->rollback();
|
2017-11-02 01:09:58 +01:00
|
|
|
$stmt = $conn->query("SELECT * FROM $tbname");
|
|
|
|
|
2017-04-12 18:26:14 +02:00
|
|
|
// Table1 should still have 2 rows since delete was rolled back
|
2017-11-02 01:09:58 +01:00
|
|
|
if (count($stmt->fetchAll()) == 2) {
|
2017-04-01 02:09:40 +02:00
|
|
|
echo "Transaction rolled back successfully\n";
|
2017-11-02 01:09:58 +01:00
|
|
|
} else {
|
2017-04-01 02:09:40 +02:00
|
|
|
echo "Transaction failed to roll back\n";
|
2017-11-02 01:09:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-13 18:33:44 +02:00
|
|
|
//revert the inserts then commit
|
|
|
|
$conn->beginTransaction();
|
2017-11-02 01:09:58 +01:00
|
|
|
$rows = deleteRows($conn, $tbname);
|
2017-04-13 18:33:44 +02:00
|
|
|
$conn->commit();
|
|
|
|
echo $rows." rows affected\n";
|
2017-11-02 01:09:58 +01:00
|
|
|
|
|
|
|
$stmt = $conn->query("SELECT * FROM $tbname");
|
|
|
|
if (count($stmt->fetchAll()) == 0) {
|
2017-04-13 18:33:44 +02:00
|
|
|
echo "Transaction committed successfully\n";
|
2017-11-02 01:09:58 +01:00
|
|
|
} else {
|
2017-04-13 18:33:44 +02:00
|
|
|
echo "Transaction failed to commit\n";
|
2017-11-02 01:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dropTable($conn, $tbname);
|
|
|
|
unset($stmt);
|
|
|
|
unset($conn);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
var_dump($e->errorInfo);
|
|
|
|
}
|
2017-04-01 02:09:40 +02:00
|
|
|
?>
|
|
|
|
--EXPECT--
|
2017-04-13 18:33:44 +02:00
|
|
|
Transaction rolled back successfully
|
|
|
|
2 rows affected
|
2017-11-02 01:09:58 +01:00
|
|
|
Transaction committed successfully
|