2017-02-01 05:10:37 +03:00
|
|
|
--TEST--
|
|
|
|
Transaction operations: commit successful transactions
|
|
|
|
--SKIPIF--
|
2017-11-29 03:40:49 +03:00
|
|
|
<?php require('skipif_versions_old.inc'); ?>
|
2017-02-01 05:10:37 +03:00
|
|
|
--FILE--
|
|
|
|
<?php
|
2017-05-02 03:01:30 +03:00
|
|
|
require_once("MsCommon.inc");
|
2017-02-01 05:10:37 +03:00
|
|
|
|
2017-11-29 03:40:49 +03:00
|
|
|
function printContent($conn)
|
2017-02-01 05:10:37 +03:00
|
|
|
{
|
2017-05-02 03:01:30 +03:00
|
|
|
global $tableName;
|
|
|
|
$query = "SELECT * FROM $tableName";
|
2017-11-29 03:40:49 +03:00
|
|
|
|
|
|
|
// To ensure we always get the first row, use a where clause
|
|
|
|
$stmt = AE\executeQuery($conn, $query, "GroupId = ?", array("ID1"));
|
|
|
|
|
2017-10-05 00:22:56 +03:00
|
|
|
// Fetch first row
|
|
|
|
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
2017-05-02 03:01:30 +03:00
|
|
|
print_r($row);
|
2017-02-01 05:10:37 +03:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:40:49 +03:00
|
|
|
function runQuery($conn, $sql, $params)
|
|
|
|
{
|
|
|
|
if (AE\isColEncrypted()) {
|
|
|
|
$stmt = sqlsrv_prepare($conn, $sql, $params);
|
|
|
|
if ($stmt) {
|
|
|
|
sqlsrv_execute($stmt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$stmt = sqlsrv_query($conn, $sql, $params);
|
|
|
|
}
|
|
|
|
if (!$stmt) {
|
|
|
|
fatalError("Failed to run query $sql");
|
|
|
|
}
|
|
|
|
return $stmt;
|
2017-05-02 03:01:30 +03:00
|
|
|
}
|
2017-02-01 05:10:37 +03:00
|
|
|
|
2017-11-29 03:40:49 +03:00
|
|
|
// connect
|
|
|
|
$conn = AE\connect();
|
2017-02-01 05:10:37 +03:00
|
|
|
|
2017-11-29 03:40:49 +03:00
|
|
|
$tableName = 'srv_036_test';
|
2017-02-01 05:10:37 +03:00
|
|
|
// Create table
|
2017-11-29 03:40:49 +03:00
|
|
|
// Do not encrypt the integer columns because of the operations required later
|
|
|
|
$columns = array(new AE\ColumnMeta('VARCHAR(10)', 'GroupId', 'primary key'),
|
|
|
|
new AE\ColumnMeta('INT', 'Accepted', null, null, true),
|
|
|
|
new AE\ColumnMeta('INT', 'Tentative', 'NOT NULL CHECK (Tentative >= 0)', null, true));
|
|
|
|
$stmt = AE\createTable($conn, $tableName, $columns);
|
|
|
|
if (!$stmt) {
|
|
|
|
fatalError("Failed to create table $tableName\n");
|
|
|
|
}
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
// Set initial data
|
2017-11-29 03:40:49 +03:00
|
|
|
if (AE\isColEncrypted()) {
|
|
|
|
$stmt = sqlsrv_query($conn,
|
|
|
|
"INSERT INTO $tableName VALUES (?,?,?),(?,?,?)",
|
|
|
|
array(array('ID1', null, null, SQLSRV_SQLTYPE_VARCHAR(10)),
|
|
|
|
array(12, null, null, SQLSRV_SQLTYPE_INT),
|
|
|
|
array(5, null, null, SQLSRV_SQLTYPE_INT),
|
|
|
|
array('ID102', null, null, SQLSRV_SQLTYPE_VARCHAR(10)),
|
|
|
|
array(20, null, null, SQLSRV_SQLTYPE_INT),
|
|
|
|
array(1, null, null, SQLSRV_SQLTYPE_INT)));
|
|
|
|
} else {
|
|
|
|
$sql = "INSERT INTO $tableName VALUES ('ID1','12','5'),('ID102','20','1')";
|
|
|
|
$stmt = sqlsrv_query($conn, $sql);
|
|
|
|
}
|
|
|
|
if (!$stmt) {
|
|
|
|
fatalError("Failed to insert data\n");
|
|
|
|
}
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
//Initiate transaction
|
2017-10-05 00:22:56 +03:00
|
|
|
sqlsrv_begin_transaction($conn) ?: die(print_r(sqlsrv_errors(), true));
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
// Update parameters
|
|
|
|
$count = 4;
|
|
|
|
$groupId = "ID1";
|
|
|
|
$params = array($count, $groupId);
|
|
|
|
|
|
|
|
// Update Accepted column
|
2017-10-05 00:22:56 +03:00
|
|
|
$sql = "UPDATE $tableName SET Accepted = (Accepted + ?) WHERE GroupId = ?";
|
2017-11-29 03:40:49 +03:00
|
|
|
$stmt1 = runQuery($conn, $sql, $params);
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
// Update Tentative column
|
|
|
|
$sql = "UPDATE $tableName SET Tentative = (Tentative - ?) WHERE GroupId = ?";
|
2017-11-29 03:40:49 +03:00
|
|
|
$stmt2 = runQuery($conn, $sql, $params);
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
// Commit the transactions
|
2017-10-05 00:22:56 +03:00
|
|
|
if ($stmt1 && $stmt2) {
|
2017-05-02 03:01:30 +03:00
|
|
|
sqlsrv_commit($conn);
|
2017-10-05 00:22:56 +03:00
|
|
|
} else {
|
2017-05-02 03:01:30 +03:00
|
|
|
echo "\nERROR: $stmt1 and $stmt2 should be valid\n";
|
|
|
|
sqlsrv_rollback($conn);
|
|
|
|
echo "\nTransactions were rolled back.\n";
|
2017-02-01 05:10:37 +03:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:40:49 +03:00
|
|
|
printContent($conn);
|
|
|
|
|
|
|
|
dropTable($conn, $tableName);
|
2017-02-01 05:10:37 +03:00
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
print "Done"
|
|
|
|
?>
|
|
|
|
|
|
|
|
--EXPECT--
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[GroupId] => ID1
|
|
|
|
[Accepted] => 16
|
|
|
|
[Tentative] => 1
|
|
|
|
)
|
|
|
|
Done
|