1--TEST-- 2PDO::rollBack() 3--SKIPIF-- 4<?php 5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc'); 6require_once(dirname(__FILE__) . 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(dirname(__FILE__) . 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 $db->rollBack(); 41 $row = $db->query('SELECT COUNT(*) AS _num FROM test')->fetch(PDO::FETCH_ASSOC); 42 if ($row['_num'] != $num) 43 printf("[002] ROLLBACK should have no effect because of the implicit COMMIT 44 triggered by DROP/CREATE TABLE\n"); 45 46 47 $db->query('DROP TABLE IF EXISTS test2'); 48 $db->query('CREATE TABLE test2(id INT) ENGINE=MyISAM'); 49 $db->beginTransaction(); 50 $db->query('INSERT INTO test2(id) VALUES (1)'); 51 $db->rollBack(); 52 $row = $db->query('SELECT COUNT(*) AS _num FROM test2')->fetch(PDO::FETCH_ASSOC); 53 if ($row['_num'] != 1) 54 printf("[003] ROLLBACK should have no effect\n"); 55 56 $db->query('DROP TABLE IF EXISTS test2'); 57 58 $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1); 59 $db->beginTransaction(); 60 $db->query('DELETE FROM test'); 61 $db->rollBack(); 62 var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT)); 63 64 $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0); 65 $db->beginTransaction(); 66 $db->query('DELETE FROM test'); 67 $db->rollBack(); 68 var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT)); 69 70 $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1); 71 $db->beginTransaction(); 72 $db->query('DELETE FROM test'); 73 $db->commit(); 74 var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT)); 75 76 $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 0); 77 $db->beginTransaction(); 78 $db->query('DELETE FROM test'); 79 $db->commit(); 80 var_dump($db->getAttribute(PDO::ATTR_AUTOCOMMIT)); 81 82 print "done!"; 83?> 84--CLEAN-- 85<?php 86require dirname(__FILE__) . '/mysql_pdo_test.inc'; 87$db = MySQLPDOTest::factory(); 88$db->exec('DROP TABLE IF EXISTS test'); 89$db->exec('DROP TABLE IF EXISTS test2'); 90?> 91--EXPECTF-- 92int(1) 93int(0) 94int(1) 95int(0) 96done! 97