1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
<?php
/**
* Tests the drivers' error mapping
*
* Executed by driver/10errormap.phpt
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Database
* @package DB
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version $Id: errors.inc,v 1.35 2005/02/28 02:02:41 danielc Exp $
* @link http://pear.php.net/package/DB
*/
/**
* Determine if the error from the driver matches the error we expect
*
* If things are as we expect, print out "matches expected outcome"
*
* If things go wrong, print "UNEXPECTED OUTCOME" and display the
* error's information.
*
* @param object $e the DB_Error object from the query
* @param int $expected_db_code the DB_ERROR* constant to expect
* @param boolean $should_be_error does the DBMS consider this an error?
*
* @return void
*/
function check_error($e, $expected_db_code, $should_be_error = true) {
if ($should_be_error) {
if (DB::isError($e)) {
if ($e->getCode() == $expected_db_code) {
print "matches expected outcome\n";
} else {
print "UNEXPECTED OUTCOME...\n";
print ' PEAR::DB errorcode: ' . $e->getCode() . "\n";
print ' ' . $e->getUserInfo() . "\n";
}
} else {
print "\n UNEXPECTED OUTCOME... expected error but it wasn't\n";
}
} else {
if (DB::isError($e)) {
print "UNEXPECTED OUTCOME... didn't expect error but it was\n";
print ' PEAR::DB errorcode: ' . $e->getCode() . "\n";
print ' ' . $e->getUserInfo() . "\n";
} else {
print "matches expected outcome\n";
}
}
}
/**
* Local error callback handler
*
* @param object $o PEAR error object automatically passed to this method
* @return void
* @see PEAR::setErrorHandling()
*/
function pe($o) {
print "\n---------------\n";
print "Having problems creating a table for testing...\n";
print $o->getDebugInfo() . "\n";
print "---------------\n";
}
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
print 'DB_ERROR_NOSUCHTABLE for select: ';
$res = $dbh->query('SELECT * FROM tableThatsBogus');
check_error($res, DB_ERROR_NOSUCHTABLE);
print 'DB_ERROR_NOSUCHTABLE for drop: ';
$res = drop_table($dbh, 'tableThatsBogus');
check_error($res, DB_ERROR_NOSUCHTABLE);
print 'DB_ERROR_NOT_FOUND for drop index: ';
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'fbsql:fbsql':
case 'ibase:firebird':
case 'ibase:ibase':
case 'ifx:ifx':
case 'odbc:db2':
case 'oci8:oci8':
case 'pgsql:pgsql':
case 'sqlite:sqlite':
$res = $dbh->query('DROP INDEX fakeindex');
break;
case 'mssql:mssql':
case 'sybase:sybase':
$res = $dbh->query('DROP INDEX phptest.fakeindex');
break;
case 'msql:msql':
$res = $dbh->query('DROP INDEX fakeindex FROM phptest');
break;
default:
$res = $dbh->query('DROP INDEX fakeindex ON phptest');
}
check_error($res, DB_ERROR_NOT_FOUND);
print 'DB_ERROR_ALREADY_EXISTS for create table: ';
$res = $dbh->query($test_mktable_query);
check_error($res, DB_ERROR_ALREADY_EXISTS);
print 'DB_ERROR_ALREADY_EXISTS for create index: ';
$res = drop_table($dbh, 'a');
$dbh->pushErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
$res = $dbh->query('CREATE TABLE a (a INTEGER)');
$dbh->popErrorHandling();
$res = $dbh->query('CREATE INDEX aa_idx ON a (a)');
$res = $dbh->query('CREATE INDEX aa_idx ON a (a)');
switch ($dbh->phptype) {
case 'fbsql':
// FrontBase doesn't assign a specific code for this yet.
check_error($res, DB_ERROR_ALREADY_EXISTS, false);
break;
default:
check_error($res, DB_ERROR_ALREADY_EXISTS);
}
$res = drop_table($dbh, 'a');
print 'DB_ERROR_CONSTRAINT for primary key insert duplicate: ';
$res = drop_table($dbh, 'a');
$dbh->pushErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
switch ($dbh->phptype) {
case 'msql':
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL)');
$res = $dbh->query('CREATE UNIQUE INDEX apk ON a (a)');
break;
default:
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, PRIMARY KEY (a))');
}
$dbh->popErrorHandling();
$res = $dbh->query('INSERT INTO a VALUES (1)');
$res = $dbh->query('INSERT INTO a VALUES (1)');
check_error($res, DB_ERROR_CONSTRAINT);
print 'DB_ERROR_CONSTRAINT for primary key update duplicate: ';
$res = $dbh->query('INSERT INTO a VALUES (2)');
$res = $dbh->query('UPDATE a SET a=1 WHERE a=2');
check_error($res, DB_ERROR_CONSTRAINT);
print 'DB_ERROR_CONSTRAINT for unique key insert duplicate: ';
$res = drop_table($dbh, 'a');
$dbh->pushErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
switch ($dbh->phptype) {
case 'msql':
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL)');
$res = $dbh->query('CREATE UNIQUE INDEX auk ON a (a)');
break;
default:
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, UNIQUE (a))');
}
$dbh->popErrorHandling();
$res = $dbh->query('INSERT INTO a VALUES (1)');
$res = $dbh->query('INSERT INTO a VALUES (1)');
check_error($res, DB_ERROR_CONSTRAINT);
print 'DB_ERROR_CONSTRAINT for unique key update duplicate: ';
$res = $dbh->query('INSERT INTO a VALUES (2)');
$res = $dbh->query('UPDATE a SET a=1 WHERE a=2');
check_error($res, DB_ERROR_CONSTRAINT);
print 'DB_ERROR_CONSTRAINT for foreign key on insert: ';
$res = drop_table($dbh, 'b');
$res = drop_table($dbh, 'a');
$dbh->pushErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
switch ($dbh->phptype) {
case 'mysql':
case 'mysqli':
$res = $dbh->query('CREATE TABLE a (a INT NOT NULL, '
. 'PRIMARY KEY (a)) '
. 'TYPE=INNODB');
$res = $dbh->query('CREATE TABLE b (b INT, '
. 'INDEX par_ind (b), '
. 'FOREIGN KEY (b) REFERENCES a (a)) '
. 'TYPE=INNODB');
$dbh->popErrorHandling();
break;
case 'msql':
// msql does not support foreign keys
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL)');
$res = $dbh->query('CREATE UNIQUE INDEX auk ON a (a)');
$dbh->popErrorHandling();
$res = $dbh->query('CREATE TABLE b (b INTEGER REFERENCES a (a))');
if (DB::isError($res)) {
print "matches expected outcome\n";
print "DB_ERROR_CONSTRAINT for foreign key on delete: matches expected outcome\n";
} else {
print "WOW, it seems mSQL now supports references\n";
print "WOW, it seems mSQL now supports references\n";
}
break;
default:
$res = $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, PRIMARY KEY (a))');
$res = $dbh->query('CREATE TABLE b (b INTEGER REFERENCES a (a))');
$dbh->popErrorHandling();
}
if ($dbh->phptype != 'msql') {
$res = $dbh->query('INSERT INTO a (a) values (1)');
$res = $dbh->query('INSERT INTO b (b) values (2)');
switch ($dbh->phptype) {
case 'sqlite':
check_error($res, DB_ERROR_CONSTRAINT, false);
break;
default:
check_error($res, DB_ERROR_CONSTRAINT);
}
print 'DB_ERROR_CONSTRAINT for foreign key on delete: ';
$res = $dbh->query('INSERT INTO b (b) values (1)');
$res = $dbh->query('DELETE FROM a WHERE a = 1');
switch ($dbh->phptype) {
case 'sqlite':
check_error($res, DB_ERROR_CONSTRAINT, false);
break;
default:
check_error($res, DB_ERROR_CONSTRAINT);
}
}
print 'DB_ERROR_CONSTRAINT_NOT_NULL on insert: ';
$res = drop_table($dbh, 'peartestnull');
$dbh->pushErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
$res = $dbh->query('CREATE TABLE peartestnull (a CHAR(3) NOT NULL)');
$dbh->popErrorHandling();
$res = $dbh->query('INSERT INTO peartestnull VALUES (NULL)');
check_error($res, DB_ERROR_CONSTRAINT_NOT_NULL);
print 'DB_ERROR_CONSTRAINT_NOT_NULL on update: ';
$res = $dbh->query("INSERT INTO peartestnull VALUES ('one')");
$res = $dbh->query("UPDATE peartestnull SET a = NULL WHERE a = 'one'");
switch ($dbh->phptype) {
case 'mysql':
case 'mysqli':
check_error($res, DB_ERROR_CONSTRAINT_NOT_NULL, false);
break;
default:
check_error($res, DB_ERROR_CONSTRAINT_NOT_NULL);
}
print 'DB_ERROR_NOSUCHFIELD joining ON bogus column: ';
$res = $dbh->query('SELECT * FROM phptest JOIN a ON (phptest.a = a.b)');
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'msql:msql':
case 'odbc:access':
check_error($res, DB_ERROR_SYNTAX);
break;
default:
check_error($res, DB_ERROR_NOSUCHFIELD);
}
print 'DB_ERROR_NOSUCHFIELD joining USING bogus column: ';
$res = $dbh->query('SELECT * FROM phptest JOIN a USING (b)');
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'ibase:ibase':
case 'ibase:firebird':
case 'msql:msql':
case 'odbc:access':
case 'odbc:db2':
case 'sybase:sybase':
check_error($res, DB_ERROR_SYNTAX);
break;
default:
check_error($res, DB_ERROR_NOSUCHFIELD);
}
print 'DB_ERROR_DIVZERO: ';
// Interbase detects the error on fetching
$res = $dbh->getAll('SELECT 0/0 FROM phptest');
switch ($dbh->phptype) {
case 'odbc':
switch ($dbh->dbsyntax) {
case 'access':
check_error($res, DB_ERROR_DIVZERO, false);
break;
case 'db2':
check_error($res, DB_ERROR_DIVZERO);
break;
}
break;
case 'ibase':
case 'ifx':
case 'fbsql':
case 'mssql':
case 'mysql':
case 'mysqli':
case 'sqlite':
case 'sybase':
check_error($res, DB_ERROR_DIVZERO, false);
break;
case 'msql':
check_error($res, DB_ERROR_SYNTAX);
break;
default:
check_error($res, DB_ERROR_DIVZERO);
}
print 'DB_ERROR_INVALID_NUMBER putting chars in INT column: ';
$res = $dbh->query("UPDATE phptest SET a = 'abc' WHERE a = 42");
switch ($dbh->phptype) {
case 'mysql':
case 'mysqli':
case 'sqlite':
check_error($res, DB_ERROR_INVALID_NUMBER, false);
break;
default:
check_error($res, DB_ERROR_INVALID_NUMBER);
}
print 'DB_ERROR_INVALID_NUMBER putting float in INT column: ';
$res = $dbh->query("UPDATE phptest SET a = 8.9 WHERE a = 42");
switch ($dbh->phptype) {
case 'fbsql':
case 'ibase':
case 'ifx':
case 'mssql':
case 'mysql':
case 'mysqli':
case 'oci8':
case 'odbc':
case 'pgsql':
case 'sqlite':
check_error($res, DB_ERROR_INVALID_NUMBER, false);
break;
default:
check_error($res, DB_ERROR_INVALID_NUMBER);
}
print 'DB_ERROR_INVALID_NUMBER putting excessive int in INT column: ';
$res = $dbh->query("UPDATE phptest SET a = 18446744073709551616 WHERE a = 42");
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'ibase:ibase':
case 'ibase:firebird':
check_error($res, DB_ERROR_SYNTAX);
break;
case 'fbsql:fbsql':
case 'msql:msql':
case 'mssql:mssql':
case 'mysql:mysql':
case 'mysqli:mysqli':
case 'oci8:oci8':
case 'odbc:access':
case 'sqlite:sqlite':
check_error($res, DB_ERROR_INVALID_NUMBER, false);
break;
default:
check_error($res, DB_ERROR_INVALID_NUMBER);
}
print 'DB_ERROR_INVALID_NUMBER putting int in CHAR column: ';
$res = $dbh->query("UPDATE phptest SET b = 8 WHERE a = 42");
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'ibase:ibase':
case 'ibase:firebird':
case 'ifx:ifx':
case 'mssql:mssql':
case 'mysql:mysql':
case 'mysqli:mysqli':
case 'oci8:oci8':
case 'odbc:access':
case 'pgsql:pgsql':
case 'sqlite:sqlite':
check_error($res, DB_ERROR_INVALID_NUMBER, false);
break;
default:
check_error($res, DB_ERROR_INVALID_NUMBER);
}
print 'DB_ERROR_NOSUCHFIELD: ';
$res = $dbh->query('SELECT e FROM phptest');
check_error($res, DB_ERROR_NOSUCHFIELD);
print 'DB_ERROR_SYNTAX: ';
$res = $dbh->query('CREATE');
check_error($res, DB_ERROR_SYNTAX);
print 'DB_ERROR_VALUE_COUNT_ON_ROW: ';
$res = $dbh->query('INSERT INTO phptest (a) VALUES (678, 2)');
switch ($dbh->phptype) {
case 'msql':
check_error($res, DB_ERROR_VALUE_COUNT_ON_ROW, false);
break;
default:
check_error($res, DB_ERROR_VALUE_COUNT_ON_ROW);
}
print 'DB_ERROR_INVALID on CHAR column data too long: ';
$res = $dbh->query("INSERT INTO phptest (b) VALUES ('123456789.123456789.123456789.123456789.1')");
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'msql:msql':
case 'mssql:mssql':
case 'mysql:mysql':
case 'mysqli:mysqli':
case 'odbc:access':
case 'sqlite:sqlite':
case 'sybase:sybase':
check_error($res, DB_ERROR_INVALID, false);
break;
case 'fbsql:fbsql':
check_error($res, DB_ERROR_TRUNCATED);
break;
default:
check_error($res, DB_ERROR_INVALID);
}
print 'DB_ERROR_INVALID on VARCHAR column data too long: ';
$res = $dbh->query("INSERT INTO phptest (d) VALUES ('123456789.123456789.1')");
switch ($dbh->phptype . ':' . $dbh->dbsyntax) {
case 'msql:msql':
case 'mssql:mssql':
case 'mysql:mysql':
case 'mysqli:mysqli':
case 'odbc:access':
case 'sqlite:sqlite':
case 'sybase:sybase':
check_error($res, DB_ERROR_INVALID, false);
break;
case 'fbsql:fbsql':
check_error($res, DB_ERROR_TRUNCATED);
break;
default:
check_error($res, DB_ERROR_INVALID);
}
drop_table($dbh, 'phptest');
drop_table($dbh, 'b');
drop_table($dbh, 'a');
drop_table($dbh, 'peartestnull');
|