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