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