[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/tests/driver/ -> 18get.phpt (source)

   1  --TEST--
   2  DB_driver::get
   3  --INI--
   4  error_reporting = 2047
   5  --SKIPIF--
   6  <?php
   7  
   8  /**
   9   * Calls the get*() methods in various ways against any DBMS.
  10   *
  11   * @see DB_Common::getAll(), DB_Common::getAssoc(), DB_Common::getCol()
  12   *      DB_Common::getListOf(), DB_Common::getOne(), DB_Common::getRow()
  13   *
  14   * @package  DB
  15   * @version  $Id: 18get.phpt,v 1.7 2004/09/22 22:16:47 danielc Exp $
  16   * @category Database
  17   * @author   Daniel Convissor <danielc@analysisandsolutions.com>
  18   * @internal
  19   */
  20  
  21  chdir(dirname(__FILE__));
  22  require_once  './skipif.inc';
  23  
  24  ?>
  25  --FILE--
  26  <?php
  27  
  28  // $Id: 18get.phpt,v 1.7 2004/09/22 22:16:47 danielc Exp $
  29  
  30  /**
  31   * Connect to the database and make the <kbd>phptest</kbd> table.
  32   */
  33  require_once  './mktable.inc';
  34  
  35  
  36  /**
  37   * Local error callback handler.
  38   *
  39   * Drops the phptest table, prints out an error message and kills the
  40   * process.
  41   *
  42   * @param object  $o  PEAR error object automatically passed to this method
  43   * @return void
  44   * @see PEAR::setErrorHandling()
  45   */
  46  function pe($o){
  47      global $dbh;
  48  
  49      $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  50      $dbh->query('DROP TABLE phptest');
  51  
  52      die($o->toString());
  53  }
  54  
  55  $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
  56  
  57  
  58  $dbh->query("INSERT INTO phptest VALUES (2, 'two', 'Two', '2002-02-22')");
  59  $dbh->query("INSERT INTO phptest VALUES (42, 'three', 'Three', '2003-03-23')");
  60  
  61  
  62  print "===================================================\n";
  63  print 'testing getOne: ';
  64  $ret =& $dbh->getOne("SELECT * FROM phptest WHERE c = 'Two'");
  65  print_r($ret);
  66  print "\n";
  67  
  68  print 'testing getOne with string params: ';
  69  $ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', 'Three');
  70  print_r($ret);
  71  print "\n";
  72  
  73  print 'testing getOne with array params: ';
  74  $ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', array('Two'));
  75  print_r($ret);
  76  print "\n";
  77  
  78  print "\n===================================================\n";
  79  print "testing getRow:\n";
  80  $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'");
  81  print_r($ret);
  82  
  83  print "testing getRow with null params, DB_FETCHMODE_ORDERED:\n";
  84  $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
  85          null, DB_FETCHMODE_ORDERED);
  86  print_r($ret);
  87  
  88  // THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
  89  // print "testing getRow with string params, DB_FETCHMODE_ORDERED:\n";
  90  // $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
  91  //         'Two', DB_FETCHMODE_ORDERED);
  92  // print_r($ret);
  93  //
  94  // testing getRow with string params, DB_FETCHMODE_ORDERED:
  95  // Array
  96  // (
  97  //     [0] => 2
  98  //     [1] => two
  99  //     [2] => Two
 100  //     [3] => 2002-02-22
 101  // )
 102  
 103     print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
 104     $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
 105             DB_FETCHMODE_ASSOC, array('Two'));
 106     print_r($ret);
 107     
 108     print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:\n";
 109     $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
 110             DB_FETCHMODE_ASSOC);
 111     print_r($ret);
 112  
 113  print "testing getRow with array params, DB_FETCHMODE_ASSOC:\n";
 114  $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
 115          array('Two'), DB_FETCHMODE_ASSOC);
 116  print_r($ret);
 117  
 118  print "testing getRow with array params, DB_FETCHMODE_OBJECT:\n";
 119  $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
 120          array('Two'), DB_FETCHMODE_OBJECT);
 121  print_r($ret);
 122  
 123  
 124  print "\n===================================================\n";
 125  print "testing getCol:\n";
 126  $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b");
 127  print_r($ret);
 128  
 129  print "testing getCol on query with no records:\n";
 130  $ret =& $dbh->getCol('SELECT * FROM phptest WHERE a > 200');
 131  print_r($ret);
 132  
 133  print "testing getCol with invalid column id:\n";
 134  $dbh->setErrorHandling(PEAR_ERROR_RETURN);
 135  $ret =& $dbh->getCol('SELECT b FROM phptest ORDER BY b', 1);
 136  if (DB::isError($ret)) {
 137      echo $ret->getMessage() . "\n";
 138  } else {
 139      print ">> Should have produced 'no such field' error\n";
 140  }
 141  $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
 142  
 143  print "testing getCol with 1 col:\n";
 144  $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 1);
 145  print_r($ret);
 146  
 147  print "testing getCol with b col:\n";
 148  $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 'b');
 149  print_r($ret);
 150  
 151  print "testing getCol with b col, scalar params:\n";
 152  $ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
 153          'b', 100);
 154  print_r($ret);
 155  
 156  print "testing getCol with b col, array params:\n";
 157  $ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
 158          'b', array(100));
 159  print_r($ret);
 160  
 161  
 162  print "\n===================================================\n";
 163  print "testing getAssoc:\n";
 164  $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b');
 165  print_r($ret);
 166  
 167  print "testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:\n";
 168  $ret =& $dbh->getAssoc("SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b",
 169                          false, null, DB_FETCHMODE_ORDERED);
 170  print_r($ret);
 171  
 172  print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:\n";
 173  $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
 174                          false, 100, DB_FETCHMODE_ASSOC);
 175  print_r($ret);
 176  
 177  print "testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:\n";
 178  $ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
 179                          false, 100, DB_FETCHMODE_ASSOC);
 180  print_r($ret);
 181  
 182  print "testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:\n";
 183  $ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
 184                          true, 100, DB_FETCHMODE_ASSOC);
 185  print_r($ret);
 186  
 187  print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:\n";
 188  $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
 189                          false, 100, DB_FETCHMODE_ASSOC, true);
 190  print_r($ret);
 191  
 192  print "testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:\n";
 193  $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
 194                          false, array(100), DB_FETCHMODE_OBJECT);
 195  print_r($ret);
 196  
 197  print "testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:\n";
 198  $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
 199                          false, array(100), DB_FETCHMODE_OBJECT, true);
 200  print_r($ret);
 201  
 202  
 203  print "\n===================================================\n";
 204  print "testing getAll:\n";
 205  $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'");
 206  print_r($ret);
 207  
 208  print "testing getAll with null params, DB_FETCHMODE_ORDERED:\n";
 209  $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
 210          null, DB_FETCHMODE_ORDERED);
 211  print_r($ret);
 212  
 213  // THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
 214  // print "testing getAll with string params, DB_FETCHMODE_ORDERED:\n";
 215  // $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ?',
 216  //         'Two', DB_FETCHMODE_ORDERED);
 217  // print_r($ret);
 218  //
 219  // testing getAll with string params, DB_FETCHMODE_ORDERED:
 220  // Array
 221  // (
 222  //     [0] => 2
 223  //     [1] => two
 224  //     [2] => Two
 225  //     [3] => 2002-02-22
 226  // )
 227  
 228     print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
 229     $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
 230             DB_FETCHMODE_ASSOC, array('Two', 'Three'));
 231     print_r($ret);
 232     
 233     print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:\n";
 234     $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
 235             DB_FETCHMODE_ASSOC);
 236     print_r($ret);
 237  
 238  print "testing getAll with array params, DB_FETCHMODE_ASSOC:\n";
 239  $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
 240          array('Two', 'Three'), DB_FETCHMODE_ASSOC);
 241  print_r($ret);
 242  
 243  print "testing getAll with array params, DB_FETCHMODE_OBJECT:\n";
 244  $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
 245          array('Two', 'Three'), DB_FETCHMODE_OBJECT);
 246  print_r($ret);
 247  
 248  
 249  print "\n===================================================\n";
 250  print 'testing getOne with null value in column: ';
 251  $dbh->query("INSERT INTO phptest VALUES (9, 'nine', '', NULL)");
 252  
 253  $ret =& $dbh->getOne('SELECT d FROM phptest WHERE a = 9');
 254  if ($ret === '') {
 255      print "empty string\n";
 256  } else {
 257      print 'type=' . gettype($ret) . ", value=$ret\n";
 258  }
 259  
 260  print 'testing getOne with empty string in column: ';
 261  $ret =& $dbh->getOne('SELECT c FROM phptest WHERE a = 9');
 262  if ($ret === '') {
 263      print "empty string\n";
 264  } else {
 265      print 'type=' . gettype($ret) . ", value=$ret\n";
 266  }
 267  
 268  
 269  print "\n===================================================\n";
 270  
 271  
 272  $dbh->setErrorHandling(PEAR_ERROR_RETURN);
 273  $dbh->query('DROP TABLE phptest');
 274  
 275  ?>
 276  --EXPECT--
 277  ===================================================
 278  testing getOne: 2
 279  testing getOne with string params: 42
 280  testing getOne with array params: 2
 281  
 282  ===================================================
 283  testing getRow:
 284  Array
 285  (
 286      [0] => 2
 287      [1] => two
 288      [2] => Two
 289      [3] => 2002-02-22
 290  )
 291  testing getRow with null params, DB_FETCHMODE_ORDERED:
 292  Array
 293  (
 294      [0] => 2
 295      [1] => two
 296      [2] => Two
 297      [3] => 2002-02-22
 298  )
 299  testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:
 300  Array
 301  (
 302      [a] => 2
 303      [b] => two
 304      [c] => Two
 305      [d] => 2002-02-22
 306  )
 307  testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:
 308  Array
 309  (
 310      [a] => 2
 311      [b] => two
 312      [c] => Two
 313      [d] => 2002-02-22
 314  )
 315  testing getRow with array params, DB_FETCHMODE_ASSOC:
 316  Array
 317  (
 318      [a] => 2
 319      [b] => two
 320      [c] => Two
 321      [d] => 2002-02-22
 322  )
 323  testing getRow with array params, DB_FETCHMODE_OBJECT:
 324  stdClass Object
 325  (
 326      [a] => 2
 327      [b] => two
 328      [c] => Two
 329      [d] => 2002-02-22
 330  )
 331  
 332  ===================================================
 333  testing getCol:
 334  Array
 335  (
 336      [0] => 42
 337      [1] => 42
 338      [2] => 2
 339  )
 340  testing getCol on query with no records:
 341  Array
 342  (
 343  )
 344  testing getCol with invalid column id:
 345  DB Error: no such field
 346  testing getCol with 1 col:
 347  Array
 348  (
 349      [0] => bing
 350      [1] => three
 351      [2] => two
 352  )
 353  testing getCol with b col:
 354  Array
 355  (
 356      [0] => bing
 357      [1] => three
 358      [2] => two
 359  )
 360  testing getCol with b col, scalar params:
 361  Array
 362  (
 363      [0] => bing
 364      [1] => three
 365      [2] => two
 366  )
 367  testing getCol with b col, array params:
 368  Array
 369  (
 370      [0] => bing
 371      [1] => three
 372      [2] => two
 373  )
 374  
 375  ===================================================
 376  testing getAssoc:
 377  Array
 378  (
 379      [42] => Array
 380          (
 381              [0] => three
 382              [1] => Three
 383          )
 384  
 385      [2] => Array
 386          (
 387              [0] => two
 388              [1] => Two
 389          )
 390  
 391  )
 392  testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:
 393  Array
 394  (
 395      [42] => Array
 396          (
 397              [0] => three
 398              [1] => Three
 399          )
 400  
 401      [2] => Array
 402          (
 403              [0] => two
 404              [1] => Two
 405          )
 406  
 407  )
 408  testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:
 409  Array
 410  (
 411      [42] => Array
 412          (
 413              [b] => three
 414              [c] => Three
 415          )
 416  
 417      [2] => Array
 418          (
 419              [b] => two
 420              [c] => Two
 421          )
 422  
 423  )
 424  testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:
 425  Array
 426  (
 427      [42] => three
 428      [2] => two
 429  )
 430  testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:
 431  Array
 432  (
 433      [42] => Array
 434          (
 435              [b] => three
 436          )
 437  
 438      [2] => Array
 439          (
 440              [b] => two
 441          )
 442  
 443  )
 444  testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:
 445  Array
 446  (
 447      [42] => Array
 448          (
 449              [0] => Array
 450                  (
 451                      [b] => bing
 452                      [c] => This is a test
 453                  )
 454  
 455              [1] => Array
 456                  (
 457                      [b] => three
 458                      [c] => Three
 459                  )
 460  
 461          )
 462  
 463      [2] => Array
 464          (
 465              [0] => Array
 466                  (
 467                      [b] => two
 468                      [c] => Two
 469                  )
 470  
 471          )
 472  
 473  )
 474  testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:
 475  Array
 476  (
 477      [42] => stdClass Object
 478          (
 479              [a] => 42
 480              [b] => three
 481              [c] => Three
 482          )
 483  
 484      [2] => stdClass Object
 485          (
 486              [a] => 2
 487              [b] => two
 488              [c] => Two
 489          )
 490  
 491  )
 492  testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:
 493  Array
 494  (
 495      [42] => Array
 496          (
 497              [0] => stdClass Object
 498                  (
 499                      [a] => 42
 500                      [b] => bing
 501                      [c] => This is a test
 502                  )
 503  
 504              [1] => stdClass Object
 505                  (
 506                      [a] => 42
 507                      [b] => three
 508                      [c] => Three
 509                  )
 510  
 511          )
 512  
 513      [2] => Array
 514          (
 515              [0] => stdClass Object
 516                  (
 517                      [a] => 2
 518                      [b] => two
 519                      [c] => Two
 520                  )
 521  
 522          )
 523  
 524  )
 525  
 526  ===================================================
 527  testing getAll:
 528  Array
 529  (
 530      [0] => Array
 531          (
 532              [0] => 2
 533              [1] => two
 534              [2] => Two
 535              [3] => 2002-02-22
 536          )
 537  
 538      [1] => Array
 539          (
 540              [0] => 42
 541              [1] => three
 542              [2] => Three
 543              [3] => 2003-03-23
 544          )
 545  
 546  )
 547  testing getAll with null params, DB_FETCHMODE_ORDERED:
 548  Array
 549  (
 550      [0] => Array
 551          (
 552              [0] => 2
 553              [1] => two
 554              [2] => Two
 555              [3] => 2002-02-22
 556          )
 557  
 558      [1] => Array
 559          (
 560              [0] => 42
 561              [1] => three
 562              [2] => Three
 563              [3] => 2003-03-23
 564          )
 565  
 566  )
 567  testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:
 568  Array
 569  (
 570      [0] => Array
 571          (
 572              [a] => 42
 573              [b] => three
 574              [c] => Three
 575              [d] => 2003-03-23
 576          )
 577  
 578      [1] => Array
 579          (
 580              [a] => 2
 581              [b] => two
 582              [c] => Two
 583              [d] => 2002-02-22
 584          )
 585  
 586  )
 587  testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:
 588  Array
 589  (
 590      [0] => Array
 591          (
 592              [a] => 2
 593              [b] => two
 594              [c] => Two
 595              [d] => 2002-02-22
 596          )
 597  
 598      [1] => Array
 599          (
 600              [a] => 42
 601              [b] => three
 602              [c] => Three
 603              [d] => 2003-03-23
 604          )
 605  
 606  )
 607  testing getAll with array params, DB_FETCHMODE_ASSOC:
 608  Array
 609  (
 610      [0] => Array
 611          (
 612              [a] => 42
 613              [b] => three
 614              [c] => Three
 615              [d] => 2003-03-23
 616          )
 617  
 618      [1] => Array
 619          (
 620              [a] => 2
 621              [b] => two
 622              [c] => Two
 623              [d] => 2002-02-22
 624          )
 625  
 626  )
 627  testing getAll with array params, DB_FETCHMODE_OBJECT:
 628  Array
 629  (
 630      [0] => stdClass Object
 631          (
 632              [a] => 42
 633              [b] => three
 634              [c] => Three
 635              [d] => 2003-03-23
 636          )
 637  
 638      [1] => stdClass Object
 639          (
 640              [a] => 2
 641              [b] => two
 642              [c] => Two
 643              [d] => 2002-02-22
 644          )
 645  
 646  )
 647  
 648  ===================================================
 649  testing getOne with null value in column: empty string
 650  testing getOne with empty string in column: empty string
 651  
 652  ===================================================


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7