| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Tests the drivers' sequence (aka auto_increment) methods
|
|
|
5 |
*
|
|
|
6 |
* Executed by driver/05sequences.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: sequences.inc,v 1.11 2005/02/18 22:38:43 danielc Exp $
|
|
|
22 |
* @link http://pear.php.net/package/DB
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Local error handler
|
|
|
27 |
*/
|
|
|
28 |
function error_handler(&$obj) {
|
|
|
29 |
print "sequences.inc error_handler:\n ";
|
|
|
30 |
print $obj->getDebugInfo() . "\n\n";
|
|
|
31 |
}
|
|
|
32 |
ob_implicit_flush(true);
|
|
|
33 |
|
|
|
34 |
$drop = $dbh->dropSequence('test');
|
|
|
35 |
if (DB::isError($drop) &&
|
|
|
36 |
$drop->getCode() != DB_ERROR_NOSUCHTABLE)
|
|
|
37 |
{
|
|
|
38 |
print "Could not drop sequence...\n";
|
|
|
39 |
print $drop->getDebugInfo() . "\n\n";
|
|
|
40 |
if ($dbh->phptype == 'ibase' &&
|
|
|
41 |
$drop->getCode() == DB_ERROR_ACCESS_VIOLATION)
|
|
|
42 |
{
|
|
|
43 |
print "Use this query to provide the permissions needed:\n";
|
|
|
44 |
print ' grant all on RDB$GENERATORS to <USERNAME>';
|
|
|
45 |
}
|
|
|
46 |
exit;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// 1) test that sequences are not created if "ondemand" is false
|
|
|
50 |
|
|
|
51 |
$e = $dbh->nextId("test", false);
|
|
|
52 |
if (DB::isError($e) && $e->getCode() == DB_ERROR_NOSUCHTABLE) {
|
|
|
53 |
print "an error is the proper response here\n";
|
|
|
54 |
} else {
|
|
|
55 |
if (DB::isError($e)) {
|
|
|
56 |
if ($dbh->phptype == 'ibase' && $e->getCode() == DB_ERROR_SYNTAX) {
|
|
|
57 |
print "an error is the proper response here\n";
|
|
|
58 |
} else {
|
|
|
59 |
print "test 1) we expected to get back 'DB Error: no such table'.\n";
|
|
|
60 |
print "Here is the error we got:\n";
|
|
|
61 |
print 'Code: ' . $e->getCode() . "\n";
|
|
|
62 |
print 'Message: ' . $e->getMessage() . "\n";
|
|
|
63 |
print 'Debug: ' . $e->getDebugInfo() . "\n\n";
|
|
|
64 |
}
|
|
|
65 |
} else {
|
|
|
66 |
print "test 1) we expected to get back 'DB Error: no such table'.\n";
|
|
|
67 |
print "But an error wasn't generated\n\n";
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// 2) test that the sequence is not created but the error is
|
|
|
72 |
// handled by the class error handler
|
|
|
73 |
$dbh->setErrorHandling(PEAR_ERROR_PRINT,
|
|
|
74 |
"an error cought by the error handler is good\n");
|
|
|
75 |
$e = $dbh->nextId("test", false);
|
|
|
76 |
if (!DB::isError($e)) {
|
|
|
77 |
print "test 2) failed!\n";
|
|
|
78 |
}
|
|
|
79 |
$dbh->_default_error_mode = null;
|
|
|
80 |
|
|
|
81 |
// 3) test that sequences are created if "ondemand" is true, and that
|
|
|
82 |
// two successive nextIds return adjacent values
|
|
|
83 |
$a = $dbh->nextId("test");
|
|
|
84 |
$b = $dbh->nextId("test");
|
|
|
85 |
if (DB::isError($a)) {
|
|
|
86 |
print 'a: ' . $a->getDebugInfo() . "\n\n";
|
|
|
87 |
} else {
|
|
|
88 |
print "a=$a\n";
|
|
|
89 |
}
|
|
|
90 |
if (DB::isError($b)) {
|
|
|
91 |
print 'b: ' . $b->getDebugInfo() . "\n\n";
|
|
|
92 |
} else {
|
|
|
93 |
print "b=$b\n";
|
|
|
94 |
}
|
|
|
95 |
if (!DB::isError($a) && !DB::isError($b)) {
|
|
|
96 |
print 'b-a=' . ($b-$a) . "\n";
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// 4) test that the user-specified error handler is really disabled
|
|
|
100 |
// during nextId, with per-object handler as well as global handler
|
|
|
101 |
$dbh->dropSequence("test");
|
|
|
102 |
|
|
|
103 |
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'error_handler');
|
|
|
104 |
$c = $dbh->nextId("test");
|
|
|
105 |
if (!DB::isError($c)) {
|
|
|
106 |
print "c=$c\n";
|
|
|
107 |
}
|
|
|
108 |
$dbh->dropSequence("test");
|
|
|
109 |
$dbh->_default_error_mode = null;
|
|
|
110 |
$d = $dbh->nextId("test");
|
|
|
111 |
if (!DB::isError($d)) {
|
|
|
112 |
print "d=$d\n";
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// 5) test that the sequence is handled right when the table is empty
|
|
|
116 |
|
|
|
117 |
// Backend with real sequences may don't like that
|
|
|
118 |
PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
|
|
|
119 |
$dbh->query('DELETE FROM test_seq');
|
|
|
120 |
PEAR::popErrorHandling();
|
|
|
121 |
$e = $dbh->nextID('test');
|
|
|
122 |
if (DB::isError($d)) {
|
|
|
123 |
print 'e: ' . $d->getDebugInfo() . "\n\n";
|
|
|
124 |
} else {
|
|
|
125 |
print "e=$d\n";
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// final clean-up
|
|
|
129 |
$dbh->dropSequence("test");
|