home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / 15quote.phpt < prev    next >
Encoding:
Text File  |  2004-03-24  |  5.6 KB  |  261 lines

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