| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Tests the drivers' transaction handling methods
|
|
|
5 |
*
|
|
|
6 |
* Executed by driver/11transactions.phpt
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
11 |
* that is available through the world-wide-web at the following URI:
|
|
|
12 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
13 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
14 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
15 |
*
|
|
|
16 |
* @category Database
|
|
|
17 |
* @package DB
|
|
|
18 |
* @author Daniel Convissor <danielc@php.net>
|
|
|
19 |
* @copyright 1997-2005 The PHP Group
|
|
|
20 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
21 |
* @version $Id: transactions.inc,v 1.9 2005/02/03 05:49:44 danielc Exp $
|
|
|
22 |
* @link http://pear.php.net/package/DB
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
// Testing here due to skip not working currently in head
|
|
|
26 |
if (!$dbh->features['transactions']) {
|
|
|
27 |
die('this driver does not support transactions');
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// View the table from a separate connection so we don't disturb
|
|
|
31 |
// the transaction.
|
|
|
32 |
$dbh2 = DB::connect($dbh->dsn);
|
|
|
33 |
|
|
|
34 |
function error_handler(&$obj) {
|
|
|
35 |
print "\n" . $obj->getDebugInfo() . "\n";
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
function dumptable($expected) {
|
|
|
39 |
global $dbh, $dbh2;
|
|
|
40 |
print implode(' ', $dbh->getCol('SELECT b FROM phptest'));
|
|
|
41 |
|
|
|
42 |
if (isset($dbh->transaction_opcount)) {
|
|
|
43 |
if ($expected == $dbh->transaction_opcount) {
|
|
|
44 |
print ". ops=ok\n";
|
|
|
45 |
} else {
|
|
|
46 |
print ". ops=$dbh->transaction_opcount\n";
|
|
|
47 |
}
|
|
|
48 |
} else {
|
|
|
49 |
print ". ops=ok\n";
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'error_handler');
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
$dbh->autoCommit(true);
|
|
|
57 |
$dbh->query("INSERT INTO phptest VALUES(1, 'one', 'One', '2001-02-19')");
|
|
|
58 |
|
|
|
59 |
print '1) after autocommit: ';
|
|
|
60 |
dumptable(0);
|
|
|
61 |
|
|
|
62 |
$dbh->autoCommit(false);
|
|
|
63 |
$dbh->query("INSERT INTO phptest VALUES(2, 'two', 'Two', '2001-02-20')");
|
|
|
64 |
$dbh->query("INSERT INTO phptest VALUES(3, 'three', 'Three', '2001-02-21')");
|
|
|
65 |
print '2) before commit: ';
|
|
|
66 |
dumptable(2);
|
|
|
67 |
|
|
|
68 |
$dbh->commit();
|
|
|
69 |
print '3) after commit: ';
|
|
|
70 |
dumptable(0);
|
|
|
71 |
|
|
|
72 |
$dbh->query("INSERT INTO phptest VALUES(4, 'four', 'Four', '2001-02-22')");
|
|
|
73 |
$dbh->query("INSERT INTO phptest VALUES(5, 'five', 'Five', '2001-02-23')");
|
|
|
74 |
print '4) before rollback: ';
|
|
|
75 |
dumptable(2);
|
|
|
76 |
|
|
|
77 |
$dbh->rollback();
|
|
|
78 |
print '5) after rollback: ';
|
|
|
79 |
dumptable(0);
|
|
|
80 |
$dbh->rollback();
|
|
|
81 |
|
|
|
82 |
$dbh->autoCommit(true);
|
|
|
83 |
$dbh->query("INSERT INTO phptest VALUES(6, 'six', 'Six', '2001-02-24')");
|
|
|
84 |
$dbh->query("INSERT INTO phptest VALUES(7, 'seven', 'Seven', '2001-02-25')");
|
|
|
85 |
print '6) before autocommit+rollback: ';
|
|
|
86 |
dumptable(0);
|
|
|
87 |
|
|
|
88 |
$dbh->rollback();
|
|
|
89 |
print '7) after autocommit+rollback: ';
|
|
|
90 |
dumptable(0);
|
|
|
91 |
|
|
|
92 |
print '8) testing that select doesn\'t disturbe opcount: ';
|
|
|
93 |
$dbh->autoCommit(false);
|
|
|
94 |
$dbh->simpleQuery("SELECT * FROM phptest");
|
|
|
95 |
$dbh->simpleQuery("SELECT a,c FROM phptest");
|
|
|
96 |
$dbh->simpleQuery("SELECT b,d FROM phptest");
|
|
|
97 |
if (empty($dbh->transaction_opcount)) {
|
|
|
98 |
print "ok\n";
|
|
|
99 |
} else {
|
|
|
100 |
print "failed (count=$dbh->transaction_opcount)\n";
|
|
|
101 |
}
|