1--TEST--
2PDO::rollBack()
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8$db = MySQLPDOTest::factory();
9if (false == MySQLPDOTest::detect_transactional_mysql_engine($db))
10    die("skip Transactional engine not found");
11?>
12--FILE--
13<?php
14    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
15    $db = MySQLPDOTest::factory();
16    MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
17
18    $db->beginTransaction();
19
20    $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
21    $num = $row['_num'];
22
23    $db->query("INSERT INTO test(id, label) VALUES (100, 'z')");
24    $num++;
25    $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
26    if ($row['_num'] != $num)
27        printf("[001] INSERT has failed, test will fail\n");
28
29    $db->rollBack();
30    $num--;
31    $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
32    if ($row['_num'] != $num)
33        printf("[002] ROLLBACK has failed\n");
34
35    $db->beginTransaction();
36    $db->query("INSERT INTO test(id, label) VALUES (100, 'z')");
37    $db->query('DROP TABLE IF EXISTS test2');
38    $db->query('CREATE TABLE test2(id INT)');
39    $num++;
40    try {
41        $db->rollBack();
42        $failed = false;
43    } catch (PDOException $e) {
44        $failed = true;
45    }
46    if (!$failed) {
47        printf("[003] Rollback should have failed\n");
48    }
49
50    $db->query('DROP TABLE IF EXISTS test2');
51    $db->query('CREATE TABLE test2(id INT) ENGINE=MyISAM');
52    $db->beginTransaction();
53    $db->query('INSERT INTO test2(id) VALUES (1)');
54    $db->rollBack();
55    $row = $db->query('SELECT COUNT(*) AS _num FROM test2')->fetch(PDO::FETCH_ASSOC);
56    if ($row['_num'] != 1)
57        printf("[003] ROLLBACK should have no effect\n");
58
59    $db->query('DROP TABLE IF EXISTS test2');
60
61    $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
62    $db->beginTransaction();
63    $db->query('DELETE FROM test');
64    $db->rollBack();
65    var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT));
66
67    $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
68    $db->beginTransaction();
69    $db->query('DELETE FROM test');
70    $db->rollBack();
71    var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT));
72
73    $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
74    $db->beginTransaction();
75    $db->query('DELETE FROM test');
76    $db->commit();
77    var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT));
78
79    $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
80    $db->beginTransaction();
81    $db->query('DELETE FROM test');
82    $db->commit();
83    var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT));
84
85    print "done!";
86?>
87--CLEAN--
88<?php
89require __DIR__ . '/mysql_pdo_test.inc';
90$db = MySQLPDOTest::factory();
91$db->exec('DROP TABLE IF EXISTS test');
92$db->exec('DROP TABLE IF EXISTS test2');
93?>
94--EXPECT--
95int(1)
96int(0)
97int(1)
98int(0)
99done!
100