home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / 15quote.phpt < prev    next >
Encoding:
Text File  |  2004-10-01  |  5.6 KB  |  263 lines

  1. --TEST--
  2. DB_driver::quote
  3. --INI--
  4. error_reporting = 2047
  5. --SKIPIF--
  6. <?php chdir(dirname(__FILE__)); require_once './skipif.inc'; ?>
  7. --FILE--
  8. <?php
  9. require_once './connect.inc';
  10.  
  11.  
  12. /**
  13.  * Local error callback handler.
  14.  *
  15.  * Drops the phptest table, prints out an error message and kills the
  16.  * process.
  17.  *
  18.  * @param object  $o  PEAR error object automatically passed to this method
  19.  * @return void
  20.  * @see PEAR::setErrorHandling()
  21.  */
  22. function pe($o) {
  23.     global $dbh;
  24.  
  25.     $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  26.     $dbh->query('DROP TABLE pearquote');
  27.  
  28.     die($o->toString());
  29. }
  30.  
  31. // DBMS boolean column type simulation...
  32. $boolean_col_type = array(
  33.     'dbase'  => 'Logical',
  34.     'fbase'  => 'BOOLEAN',
  35.     'ibase'  => 'SMALLINT',
  36.     'ifx'    => 'SMALLINT',
  37.     'msql'   => 'INTEGER',
  38.     'mssql'  => 'BIT',
  39.     'mysql'  => 'TINYINT(1)',
  40.     'mysqli' => 'TINYINT(1)',
  41.     'oci8'   => 'NUMBER(1)',
  42.     'odbc'   => 'SMALLINT',
  43.     'pgsql'  => 'BOOLEAN',
  44.     'sqlite' => 'INTEGER',
  45.     'sybase' => 'TINYINT',
  46. );
  47.  
  48. // adjust things for specific DBMS's
  49.  
  50. if ($dbh->phptype == 'odbc') {
  51.     if ($dbh->dbsyntax == 'odbc') {
  52.         $type = $dbh->phptype;
  53.     } else {
  54.         $type = $dbh->dbsyntax;
  55.     }
  56. } else {
  57.     $type = $dbh->phptype;
  58. }
  59.  
  60. switch ($type) {
  61.     case 'access':
  62.         $decimal = 'SINGLE';
  63.         $null = '';
  64.         $chr  = 'VARCHAR(8)';
  65.         $identifier = 'q\ut "dnt';
  66.         break;
  67.     case 'db2':
  68.     case 'ibase':
  69.         $decimal = 'DECIMAL(3,1)';
  70.         $null = '';
  71.         $chr  = 'VARCHAR(8)';
  72.         $identifier = 'q\ut] "dn[t';
  73.         break;
  74.     case 'ifx':
  75.         // doing this for ifx to keep certain versions happy
  76.         $decimal = 'DECIMAL(3,1)';
  77.         $null = '';
  78.         $chr  = 'CHAR(8)';
  79.         $identifier = '';
  80.         break;
  81.     case 'msql':
  82.         $decimal = 'DECIMAL(3,1)';
  83.         $null = '';
  84.         $chr  = 'CHAR(8)';
  85.         $identifier = 'q\ut] "dn[t';
  86.         break;
  87.     case 'oci8':
  88.         $decimal = 'DECIMAL(3,1)';
  89.         $null = '';
  90.         $chr  = 'VARCHAR(8)';
  91.         $identifier = 'q\ut] dn[t';
  92.         break;
  93.     default:
  94.         $decimal = 'DECIMAL(3,1)';
  95.         $null = 'NULL';
  96.         $chr  = 'VARCHAR(8)';
  97.         $identifier = 'q\ut] "dn[t';
  98. }
  99.  
  100. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  101. $dbh->query('DROP TABLE pearquote');
  102.  
  103.  
  104. if ($identifier) {
  105.     $create = $dbh->query("
  106.         CREATE TABLE pearquote (
  107.           n $decimal $null,
  108.           s $chr $null,
  109.           " . $dbh->quoteIdentifier($identifier) . " $decimal $null,
  110.           b {$boolean_col_type[$dbh->phptype]} $null
  111.         )
  112.     ");
  113.  
  114.     if (DB::isError($create) ) {
  115.         pe($create);
  116.     }
  117.  
  118.     $info = $dbh->tableInfo('pearquote');
  119.     if (DB::isError($info) ) {
  120.         if ($info->code == DB_ERROR_NOT_CAPABLE) {
  121.             print "Creation of the delimited identifier worked.\n";
  122.         } else {
  123.             print "tableInfo() failed.\n";
  124.         }
  125.     } else {
  126.         if ($identifier == $info[2]['name']) {
  127.             print "Creation of the delimited identifier worked.\n";
  128.             // print "COLUMN NAME IS: {$info[2]['name']}\n";
  129.         } else {
  130.             print "Expected column name: '$identifier' ... ";
  131.             print "Actual column name: '{$info[2]['name']}'\n";
  132.         }
  133.     }
  134.  
  135. } else {
  136.     $dbh->query("
  137.         CREATE TABLE pearquote (
  138.           n DECIMAL(3,1) $null,
  139.           s $chr $null,
  140.           plainidentifier DECIMAL(3,1) $null,
  141.           b {$boolean_col_type[$dbh->phptype]} $null
  142.         )
  143.     ");
  144.     print "{$dbh->dsn['phptype']} does not handle delimited identifiers.\n";
  145. }
  146.  
  147.  
  148. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
  149.  
  150.  
  151. $strings = array(
  152.     "'",
  153.     "\"",
  154.     "\\",
  155.     "%",
  156.     "_",
  157.     "''",
  158.     "\"\"",
  159.     "\\\\",
  160.     "\\'\\'",
  161.     "\\\"\\\""
  162. );
  163.  
  164. $nums = array(
  165.     12.3,
  166.     15,
  167. );
  168.  
  169. $bools = array(
  170.     TRUE,
  171.     FALSE,
  172. );
  173.  
  174.  
  175. echo "String escape test: ";
  176. foreach ($strings as $s) {
  177.     $quoted = $dbh->quoteSmart($s);
  178.     $dbh->query("INSERT INTO pearquote (s) VALUES ($quoted)");
  179. }
  180. $diff = array_diff($strings, $res = $dbh->getCol("SELECT s FROM pearquote"));
  181. if (count($diff) > 0) {
  182.     echo "FAIL";
  183.     print_r($strings);
  184.     print_r($res);
  185. } else {
  186.     echo "OK";
  187. }
  188.  
  189. $dbh->query("DELETE FROM pearquote");
  190.  
  191.  
  192. echo "\nNumber escape test: ";
  193. foreach ($nums as $n) {
  194.     $quoted = $dbh->quoteSmart($n);
  195.     $dbh->query("INSERT INTO pearquote (n) VALUES ($quoted)");
  196. }
  197.  
  198. $diff = array();
  199. $res =& $dbh->getCol('SELECT n FROM pearquote ORDER BY n');
  200. foreach ($nums as $key => $val) {
  201.     if ($val != $res[$key]) {
  202.         $diff[] = "$val != {$res[$key]}";
  203.     }
  204. }
  205.  
  206. if (count($diff) > 0) {
  207.     echo "FAIL\n";
  208.     print_r($diff);
  209. } else {
  210.     echo 'OK';
  211. }
  212.  
  213. $dbh->query('DELETE FROM pearquote');
  214.  
  215.  
  216. echo "\nBoolean escape test: ";
  217. $i = 1;
  218. foreach ($bools as $b) {
  219.     $quoted = $dbh->quoteSmart($b);
  220.     $dbh->query("INSERT INTO pearquote (n, b) VALUES ($i, $quoted)");
  221.     $i++;
  222. }
  223.  
  224. $diff = array();
  225. $res =& $dbh->getCol('SELECT b, n FROM pearquote ORDER BY n');
  226. foreach ($bools as $key => $val) {
  227.     if ($val === true) {
  228.         if ($res[$key] == 1 || $res[$key] == true ||
  229.             substr(strtolower($res[$key]), 0, 1) == 't')
  230.         {
  231.             // good
  232.         } else {
  233.             $diff[] = "in:true != out:{$res[$key]}";
  234.         }
  235.     } else {
  236.         if ($res[$key] == 0 || $res[$key] == false ||
  237.             substr(strtolower($res[$key]), 0, 1) == 'f')
  238.         {
  239.             // good
  240.         } else {
  241.             $diff[] = "in:false != out:{$res[$key]}";
  242.         }
  243.     }
  244. }
  245.  
  246. if (count($diff) > 0) {
  247.     echo "FAIL\n";
  248.     print_r($diff);
  249. } else {
  250.     echo "OK\n";
  251. }
  252.  
  253.  
  254. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  255. $dbh->query('DROP TABLE pearquote');
  256.  
  257. ?>
  258. --EXPECT--
  259. Creation of the delimited identifier worked.
  260. String escape test: OK
  261. Number escape test: OK
  262. Boolean escape test: OK
  263.