Source for file errors.inc
Documentation is available at errors.inc
* Tests the drivers' error mapping
* Executed by driver/10errormap.phpt
* 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.
* @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.38 2007/02/06 06:05:01 aharvey 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
* @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?
function check_error($e, $expected_db_code, $should_be_error = true ) {
if ($e->getCode () == $expected_db_code) {
print "matches expected outcome\n";
print "UNEXPECTED OUTCOME...\n";
print ' PEAR::DB errorcode: ' . $e->getCode () . "\n";
print ' ' . $e->getUserInfo () . "\n";
print "\n UNEXPECTED OUTCOME... expected error but it wasn't\n";
print "UNEXPECTED OUTCOME... didn't expect error but it was\n";
print ' PEAR::DB errorcode: ' . $e->getCode () . "\n";
print ' ' . $e->getUserInfo () . "\n";
print "matches expected outcome\n";
* Local error callback handler
* @param object $o PEAR error object automatically passed to this method
* @see PEAR::setErrorHandling()
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');
print 'DB_ERROR_NOSUCHTABLE for drop: ';
$res = drop_table ($dbh, 'tableThatsBogus');
print 'DB_ERROR_NOT_FOUND for drop index: ';
switch ($dbh->phptype . ':' . $dbh->dbsyntax ) {
$res = $dbh->query ('DROP INDEX fakeindex');
$res = $dbh->query ('DROP INDEX phptest.fakeindex');
$res = $dbh->query ('DROP INDEX fakeindex FROM phptest');
$res = $dbh->query ('DROP INDEX fakeindex ON phptest');
print 'DB_ERROR_ALREADY_EXISTS for create table: ';
$res = $dbh->query ($test_mktable_query);
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)');
// FrontBase doesn't assign a specific code for this yet.
$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');
$res = $dbh->query ('CREATE TABLE a (a INTEGER NOT NULL)');
$res = $dbh->query ('CREATE UNIQUE INDEX apk ON a (a)');
$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)');
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');
print 'DB_ERROR_CONSTRAINT for unique key insert duplicate: ';
$res = drop_table ($dbh, 'a');
$dbh->pushErrorHandling (PEAR_ERROR_CALLBACK , 'pe');
$res = $dbh->query ('CREATE TABLE a (a INTEGER NOT NULL)');
$res = $dbh->query ('CREATE UNIQUE INDEX auk ON a (a)');
$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)');
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');
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');
$res = $dbh->query ('CREATE TABLE a (a INT NOT NULL, '
$res = $dbh->query ('CREATE TABLE b (b INT, '
. 'FOREIGN KEY (b) REFERENCES a (a)) '
$dbh->popErrorHandling ();
// 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))');
print "matches expected outcome\n";
print "DB_ERROR_CONSTRAINT for foreign key on delete: matches expected outcome\n";
print "WOW, it seems mSQL now supports references\n";
print "WOW, it seems mSQL now supports references\n";
$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)');
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');
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)');
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'");
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 ) {
print 'DB_ERROR_NOSUCHFIELD joining USING bogus column: ';
$res = $dbh->query ('SELECT * FROM phptest JOIN a USING (b)');
switch ($dbh->phptype . ':' . $dbh->dbsyntax ) {
/* FirebirdSQL 2 returns -902 (feature is not supported) for this test
* rather than a syntax error. For now, we'll test for both. */
print 'DB_ERROR_DIVZERO: ';
// Interbase detects the error on fetching
$res = $dbh->getAll ('SELECT 0/0 FROM phptest');
switch ($dbh->dbsyntax ) {
print 'DB_ERROR_INVALID_NUMBER putting chars in INT column: ';
$res = $dbh->query ("UPDATE phptest SET a = 'abc' WHERE a = 42");
print 'DB_ERROR_INVALID_NUMBER putting float in INT column: ';
$res = $dbh->query ("UPDATE phptest SET a = 8.9 WHERE a = 42");
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 ) {
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 ) {
print 'DB_ERROR_NOSUCHFIELD: ';
$res = $dbh->query ('SELECT e FROM phptest');
print 'DB_ERROR_SYNTAX: ';
$res = $dbh->query ('CREATE');
print 'DB_ERROR_VALUE_COUNT_ON_ROW: ';
$res = $dbh->query ('INSERT INTO phptest (a) VALUES (678, 2)');
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 ) {
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 ) {
drop_table ($dbh, 'phptest');
drop_table ($dbh, 'peartestnull');
Documentation generated on Tue, 20 Mar 2007 05:30:27 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|