1--TEST--
2PDO::ATTR_AUTOCOMMIT
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13    $db = MySQLPDOTest::factory();
14
15    // autocommit should be on by default
16    if (true !== ($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 (false !== ($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 (true !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT)))
51        printf("[010] Expecting int/1 got %s\n", var_export($tmp, true));
52
53    $table = 'pdo_mysql_attr_autocommit';
54    if (MySQLPDOTest::detect_transactional_mysql_engine($db)) {
55        // nice, we have a transactional engine to play with
56
57        MySQLPDOTest::createTestTable($table, $db, MySQLPDOTest::detect_transactional_mysql_engine($db));
58        $row = $db->query("SELECT COUNT(*) AS _num FROM {$table}")->fetch(PDO::FETCH_ASSOC);
59        $num = $row['_num'];
60
61        $db->query("INSERT INTO {$table} (id, label) VALUES (100, 'z')");
62        $num++;
63        $row = $db->query("SELECT COUNT(*) AS _num FROM {$table}")->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 {$table}")->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 {$table} WHERE id = 100");
77        $db->query('ROLLBACK');
78        $row = $db->query("SELECT COUNT(*) AS _num FROM {$table}")->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 {$table} WHERE id = 100");
83        $db->query('COMMIT');
84        $num--;
85        $row = $db->query("SELECT COUNT(*) AS _num FROM {$table}")->fetch(PDO::FETCH_ASSOC);
86        if ($row['_num'] != $num)
87            printf("[015] DELETE should have been committed\n");
88    }
89
90    print "done!";
91?>
92--CLEAN--
93<?php
94require __DIR__ . '/inc/mysql_pdo_test.inc';
95$db = MySQLPDOTest::factory();
96$db->query('DROP TABLE IF EXISTS pdo_mysql_attr_autocommit');
97?>
98--EXPECT--
99done!
100