DB
[ class tree: DB ] [ index: DB ] [ all elements ]

Source for file errors.inc

Documentation is available at errors.inc

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