1--TEST--
2PDO::ATTR_AUTOCOMMIT
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9$db = MySQLPDOTest::factory();
10?>
11--FILE--
12<?php
13    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
14    $db = MySQLPDOTest::factory();
15
16    // autocommit should be on by default
17    if (1 !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT)))
18        printf("[001] Expecting int/1 got %s\n", var_export($tmp, true));
19
20    // lets see if the server agrees to that
21    $row = $db->query('SELECT @@autocommit AS _autocommit')->fetch(PDO::FETCH_ASSOC);
22    if (!$row['_autocommit'])
23        printf("[002] Server autocommit mode should be on, got '%s'\n", var_export($row['_autocommit']));
24
25    // on -> off
26    if (!$db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0))
27        printf("[003] Cannot turn off autocommit\n");
28
29    $row = $db->query('SELECT @@autocommit AS _autocommit')->fetch(PDO::FETCH_ASSOC);
30    if ($row['_autocommit'])
31        printf("[004] Server autocommit mode should be off, got '%s'\n", var_export($row['_autocommit']));
32
33    // PDO thinks autocommit is off, but its manually turned on...
34    if (!$db->query('SET autocommit = 1'))
35        printf("[005] Cannot turn on server autocommit mode, %s\n", var_export($db->errorInfo(), true));
36
37    if (0 !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT)))
38        printf("[006] Expecting int/0 got %s\n", var_export($tmp, true));
39
40    // off -> on
41    if (!$db->query('SET autocommit = 0'))
42        printf("[007] Cannot turn off server autocommit mode, %s\n", var_export($db->errorInfo(), true));
43
44    if (!$db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1))
45        printf("[008] Cannot turn on autocommit\n");
46
47    $row = $db->query('SELECT @@autocommit AS _autocommit')->fetch(PDO::FETCH_ASSOC);
48    if (!$row['_autocommit'])
49        printf("[009] Server autocommit mode should be on, got '%s'\n", var_export($row['_autocommit']));
50
51    if (1 !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT)))
52        printf("[010] Expecting int/1 got %s\n", var_export($tmp, true));
53
54    if (MySQLPDOTest::detect_transactional_mysql_engine($db)) {
55        // nice, we have a transactional engine to play with
56
57        MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
58        $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
59        $num = $row['_num'];
60
61        $db->query("INSERT INTO test(id, label) VALUES (100, 'z')");
62        $num++;
63        $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
64        if ($row['_num'] != $num)
65            printf("[011] Insert has failed, test will fail\n");
66
67        // autocommit is on, no rollback possible
68        $db->query('ROLLBACK');
69        $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
70        if ($row['_num'] != $num)
71            printf("[012] ROLLBACK should not have undone anything\n");
72
73        if (!$db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0))
74            printf("[013] Cannot turn off autocommit\n");
75
76        $db->query('DELETE FROM test WHERE id = 100');
77        $db->query('ROLLBACK');
78        $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
79        if ($row['_num'] != $num)
80            printf("[014] ROLLBACK should have undone the DELETE\n");
81
82        $db->query('DELETE FROM test WHERE id = 100');
83        $db->query('COMMIT');
84        $num--;
85        $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC);
86        if ($row['_num'] != $num)
87            printf("[015] DELETE should have been committed\n");
88
89    }
90
91    print "done!";
92?>
93--CLEAN--
94<?php
95require __DIR__ . '/mysql_pdo_test.inc';
96MySQLPDOTest::dropTestTable();
97?>
98--EXPECT--
99done!
100