[ Index ]
 

Code source de PHP PEAR 1.4.5

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

title

Body

[fermer]

/MDB/ -> peardb_wrapper.php (source)

   1  <?php
   2  // +----------------------------------------------------------------------+
   3  // | PHP Version 4                                                        |
   4  // +----------------------------------------------------------------------+
   5  // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
   6  // | Stig. S. Bakken, Lukas Smith                                         |
   7  // | All rights reserved.                                                 |
   8  // +----------------------------------------------------------------------+
   9  // | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
  10  // | API as well as database abstraction for PHP applications.            |
  11  // | This LICENSE is in the BSD license style.                            |
  12  // |                                                                      |
  13  // | Redistribution and use in source and binary forms, with or without   |
  14  // | modification, are permitted provided that the following conditions   |
  15  // | are met:                                                             |
  16  // |                                                                      |
  17  // | Redistributions of source code must retain the above copyright       |
  18  // | notice, this list of conditions and the following disclaimer.        |
  19  // |                                                                      |
  20  // | Redistributions in binary form must reproduce the above copyright    |
  21  // | notice, this list of conditions and the following disclaimer in the  |
  22  // | documentation and/or other materials provided with the distribution. |
  23  // |                                                                      |
  24  // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25  // | Lukas Smith nor the names of his contributors may be used to endorse |
  26  // | or promote products derived from this software without specific prior|
  27  // | written permission.                                                  |
  28  // |                                                                      |
  29  // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30  // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31  // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32  // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33  // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34  // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35  // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36  // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37  // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38  // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39  // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40  // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41  // +----------------------------------------------------------------------+
  42  // | Author: Lukas Smith <smith@backendmedia.com>                         |
  43  // +----------------------------------------------------------------------+
  44  //
  45  // $Id: peardb_wrapper.php,v 1.32.4.2 2004/04/08 17:19:01 lsmith Exp $
  46  //
  47  
  48  /*
  49   * The method mapErrorCode in each MDB_dbtype implementation maps
  50   * native error codes to one of these.
  51   *
  52   * If you add an error code here, make sure you also add a textual
  53   * version of it in DB::errorMessage().
  54   */
  55  
  56  define('DB_OK',                         MDB_OK);
  57  define('DB_ERROR',                      MDB_ERROR);
  58  define('DB_ERROR_SYNTAX',               MDB_ERROR_SYNTAX);
  59  define('DB_ERROR_CONSTRAINT',           MDB_ERROR_CONSTRAINT);
  60  define('DB_ERROR_NOT_FOUND',            MDB_ERROR_NOT_FOUND);
  61  define('DB_ERROR_ALREADY_EXISTS',       MDB_ERROR_ALREADY_EXISTS);
  62  define('DB_ERROR_UNSUPPORTED',          MDB_ERROR_UNSUPPORTED);
  63  define('DB_ERROR_MISMATCH',             MDB_ERROR_MISMATCH);
  64  define('DB_ERROR_INVALID',              MDB_ERROR_INVALID);
  65  define('DB_ERROR_NOT_CAPABLE',          MDB_ERROR_NOT_CAPABLE);
  66  define('DB_ERROR_TRUNCATED',            MDB_ERROR_TRUNCATED);
  67  define('DB_ERROR_INVALID_NUMBER',       MDB_ERROR_INVALID_NUMBER);
  68  define('DB_ERROR_INVALID_DATE',         MDB_ERROR_INVALID_DATE);
  69  define('DB_ERROR_DIVZERO',              MDB_ERROR_DIVZERO);
  70  define('DB_ERROR_NODBSELECTED',         MDB_ERROR_NODBSELECTED);
  71  define('DB_ERROR_CANNOT_CREATE',        MDB_ERROR_CANNOT_CREATE);
  72  define('DB_ERROR_CANNOT_DELETE',        MDB_ERROR_CANNOT_DELETE);
  73  define('DB_ERROR_CANNOT_DROP',          MDB_ERROR_CANNOT_DROP);
  74  define('DB_ERROR_NOSUCHTABLE',          MDB_ERROR_NOSUCHTABLE);
  75  define('DB_ERROR_NOSUCHFIELD',          MDB_ERROR_NOSUCHFIELD);
  76  define('DB_ERROR_NEED_MORE_DATA',       MDB_ERROR_NEED_MORE_DATA);
  77  define('DB_ERROR_NOT_LOCKED',           MDB_ERROR_NOT_LOCKED);
  78  define('DB_ERROR_VALUE_COUNT_ON_ROW',   MDB_ERROR_VALUE_COUNT_ON_ROW);
  79  define('DB_ERROR_INVALID_DSN',          MDB_ERROR_INVALID_DSN);
  80  define('DB_ERROR_CONNECT_FAILED',       MDB_ERROR_CONNECT_FAILED);
  81  define('DB_ERROR_EXTENSION_NOT_FOUND',  MDB_ERROR_EXTENSION_NOT_FOUND);
  82  define('DB_ERROR_ACCESS_VIOLATION',     MDB_ERROR_ACCESS_VIOLATION);
  83  define('DB_ERROR_NOSUCHDB',             MDB_ERROR_NOSUCHDB);
  84  
  85  define('DB_WARNING',           -1000);
  86  define('DB_WARNING_READ_ONLY', -1001);
  87  
  88  define('DB_PARAM_SCALAR',   MDB_PARAM_SCALAR);
  89  define('DB_PARAM_OPAQUE',   MDB_PARAM_OPAQUE);
  90  define('DB_PARAM_MISC',     MDB_PARAM_MISC);
  91  
  92  define('DB_BINMODE_PASSTHRU',   MDB_BINMODE_PASSTHRU);
  93  define('DB_BINMODE_RETURN',     MDB_BINMODE_RETURN);
  94  define('DB_BINMODE_CONVERT',    MDB_BINMODE_CONVERT);
  95  
  96  define('DB_FETCHMODE_DEFAULT',      MDB_FETCHMODE_DEFAULT);
  97  define('DB_FETCHMODE_ORDERED',      MDB_FETCHMODE_ORDERED);
  98  define('DB_FETCHMODE_ASSOC',        MDB_FETCHMODE_ASSOC);
  99  define('DB_FETCHMODE_OBJECT',       3);
 100  define('DB_FETCHMODE_FLIPPED',      MDB_FETCHMODE_FLIPPED);
 101  
 102  define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
 103  define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
 104  define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
 105  
 106  define('DB_TABLEINFO_ORDER',        MDB_TABLEINFO_ORDER);
 107  define('DB_TABLEINFO_ORDERTABLE',   MDB_TABLEINFO_ORDERTABLE);
 108  define('DB_TABLEINFO_FULL',         MDB_TABLEINFO_FULL);
 109  
 110  define('DB_AUTOQUERY_INSERT', 1);
 111  define('DB_AUTOQUERY_UPDATE', 2);
 112  
 113  /**
 114   * Wrapper that makes MDB behave like PEAR DB
 115   *
 116   * @package MDB
 117   * @category Database
 118   * @author  Lukas Smith <smith@backendmedia.com>
 119   */
 120  class DB
 121  {
 122      function &factory($type)
 123      {
 124          $db =& MDB::factory($type);
 125          if(PEAR::isError($db)) {
 126              return($db);
 127          }
 128          $obj =& new MDB_PEAR_PROXY($db);
 129          return($obj);
 130      }
 131  
 132      function &connect($dsn, $options = FALSE)
 133      {
 134          if (!is_array($options) && $options) {
 135              $options['persistent'] = TRUE;
 136          }
 137          $db =& MDB::connect($dsn, $options);
 138          if(PEAR::isError($db)) {
 139              return($db);
 140          }
 141          $obj =& new MDB_PEAR_PROXY($db);
 142          return($obj);
 143      }
 144  
 145      function apiVersion()
 146      {
 147          return(2);
 148      }
 149  
 150      function isError($value)
 151      {
 152          return(MDB::isError($value));
 153      }
 154  
 155      function isManip($query)
 156      {
 157          return(MDB::isManip($query));
 158      }
 159  
 160      function errorMessage($value)
 161      {
 162          return(MDB::errorMessage($value));
 163      }
 164  
 165      function parseDSN($dsn)
 166      {
 167          return(MDB::parseDSN($dsn));
 168      }
 169  
 170      function assertExtension($name)
 171      {
 172          if (!extension_loaded($name)) {
 173              $dlext = OS_WINDOWS ? '.dll' : '.so';
 174              @dl($name . $dlext);
 175          }
 176          return extension_loaded($name);
 177      }
 178  }
 179  
 180  /**
 181   * MDB_Error implements a class for reporting portable database error
 182   * messages.
 183   *
 184   * @package MDB
 185   * @category Database
 186   * @author  Stig Bakken <ssb@fast.no>
 187   */
 188  class DB_Error extends PEAR_Error
 189  {
 190      function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
 191                $level = E_USER_NOTICE, $debuginfo = NULL)
 192      {
 193          if (is_int($code)) {
 194              $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code, $mode, $level, $debuginfo);
 195          } else {
 196              $this->PEAR_Error("DB Error: $code", DB_ERROR, $mode, $level, $debuginfo);
 197          }
 198      }
 199  }
 200  
 201  /**
 202   * Wrapper that makes MDB behave like PEAR DB
 203   *
 204   * @package MDB
 205   * @category Database
 206   * @author  Lukas Smith <smith@backendmedia.com>
 207   */
 208  class DB_result
 209  {
 210      var $dbh;
 211      var $result;
 212      var $row_counter = NULL;
 213  
 214      var $limit_from  = NULL;
 215  
 216      var $limit_count = NULL;
 217  
 218      function DB_result(&$dbh, $result)
 219      {
 220          $this->dbh = &$dbh;
 221          $this->result = $result;
 222      }
 223  
 224      function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = 0)
 225      {
 226          $arr = $this->dbh->fetchInto($this->result, $fetchmode, $rownum);
 227          if(is_array($arr)) {
 228              return($arr);
 229          }
 230          else {
 231              return(NULL);
 232          }
 233      }
 234  
 235      function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = 0)
 236      {
 237          $arr = $this->dbh->fetchInto($this->result, $fetchmode, $rownum);
 238          if(MDB::isError($arr)) {
 239              return($arr);
 240          }
 241          if($arr === NULL) {
 242              return($arr);
 243          }
 244          return(DB_OK);
 245      }
 246  
 247      function numCols()
 248      {
 249          return($this->dbh->numCols($this->result));
 250      }
 251  
 252      function numRows()
 253      {
 254          return($this->dbh->numRows($this->result));
 255      }
 256  
 257      function nextResult()
 258      {
 259          return($this->dbh->nextResult($this->result));
 260      }
 261  
 262      function free()
 263      {
 264          $err = $this->dbh->freeResult($this->result);
 265          if(MDB::isError($err)) {
 266              return($err);
 267          }
 268          $this->result = FALSE;
 269          return(TRUE);
 270      }
 271  
 272      function tableInfo($mode = NULL)
 273      {
 274          return($this->dbh->tableInfo($this->result, $mode));
 275      }
 276  
 277      function getRowCounter()
 278      {
 279          return($this->dbh->highest_fetched_row[$this->result]);
 280      }
 281  }
 282  
 283  class DB_row
 284  {
 285      function DB_row(&$arr)
 286      {
 287          for (reset($arr); $key = key($arr); next($arr)) {
 288              $this->$key = &$arr[$key];
 289          }
 290      }
 291  }
 292  
 293  class MDB_PEAR_PROXY
 294  {
 295      var $MDB_object;
 296  
 297      function MDB_PEAR_PROXY($MDB_object)
 298      {
 299          $this->MDB_object = $MDB_object;
 300          $this->MDB_object->option['sequence_col_name'] = 'id';
 301      }
 302  
 303      function connect($dsninfo, $persistent = FALSE)
 304      {
 305          return($this->MDB_object->connect($dsninfo, $persistent));
 306      }
 307  
 308      function disconnect()
 309      {
 310          return($this->MDB_object->disconnect());
 311      }
 312  
 313      function quoteString($string)
 314      {
 315          $string = $this->_quote($string);
 316          if ($string{0} == "'") {
 317              return substr($string, 1, -1);
 318          }
 319          return($string);
 320      }
 321  
 322      function quote($string)
 323      {
 324          if ($string === NULL) {
 325              return 'NULL';
 326          }
 327          return($this->MDB_object->_quote($string));
 328      }
 329  
 330      function provides($feature)
 331      {
 332          return($this->MDB_object->support($feature));
 333      }
 334  
 335      function errorCode($nativecode)
 336      {
 337          return($this->MDB_object->errorCode($nativecode));
 338      }
 339  
 340      function errorMessage($dbcode)
 341      {
 342          return($this->MDB_object->errorMessage($dbcode));
 343      }
 344  
 345      function &raiseError($code = DB_ERROR, $mode = NULL, $options = NULL,
 346                           $userinfo = NULL, $nativecode = NULL)
 347      {
 348          return($this->MDB_object->raiseError($code = DB_ERROR, $mode, $options, $userinfo, $nativecode));
 349      }
 350  
 351      function setFetchMode($fetchmode, $object_class = NULL)
 352      {
 353          return($this->MDB_object->setFetchMode($fetchmode, $object_class));
 354      }
 355  
 356      function setOption($option, $value)
 357      {
 358          return($this->MDB_object->setOption($option, $value));
 359      }
 360  
 361      function getOption($option)
 362      {
 363          return($this->MDB_object->getOption($option));
 364      }
 365  
 366      function prepare($query)
 367      {
 368          return($this->MDB_object->prepareQuery($query));
 369      }
 370  
 371      function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = false)
 372      {
 373          $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
 374          return($this->prepare($query));
 375      }
 376  
 377      function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false)
 378      {
 379          $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where);
 380          return($this->execute($sth, array_values($fields_values)));
 381      }
 382  
 383      function buildManipSQL($table, $table_fields, $mode, $where = false)
 384      {
 385          if (count($table_fields) == 0) {
 386              $this->raiseError(DB_ERROR_NEED_MORE_DATA);
 387          }
 388          $first = true;
 389          switch ($mode) {
 390              case DB_AUTOQUERY_INSERT:
 391                  $values = '';
 392                  $names = '';
 393                  while (list(, $value) = each($table_fields)) {
 394                      if ($first) {
 395                          $first = false;
 396                      } else {
 397                          $names .= ',';
 398                          $values .= ',';
 399                      }
 400                      $names .= $value;
 401                      $values .= '?';
 402                  }
 403                  return "INSERT INTO $table ($names) VALUES ($values)";
 404                  break;
 405              case DB_AUTOQUERY_UPDATE:
 406                  $set = '';
 407                  while (list(, $value) = each($table_fields)) {
 408                      if ($first) {
 409                          $first = false;
 410                      } else {
 411                          $set .= ',';
 412                      }
 413                      $set .= "$value = ?";
 414                  }
 415                  $sql = "UPDATE $table SET $set";
 416                  if ($where) {
 417                      $sql .= " WHERE $where";
 418                  }
 419                  return($sql);
 420                  break;
 421              default:
 422                  $this->raiseError(DB_ERROR_SYNTAX);
 423          }
 424      }
 425  
 426      function execute($stmt, $data = FALSE)
 427      {
 428          $result = $this->MDB_object->execute($stmt, NULL, $data);
 429          if (MDB::isError($result) || $result === DB_OK) {
 430              return($result);
 431          } else {
 432              return new DB_result($this->MDB_object, $result);
 433          }
 434      }
 435  
 436      function executeMultiple( $stmt, &$data )
 437      {
 438          return($this->MDB_object->executeMultiple($stmt, NULL, $data));
 439      }
 440  
 441      function &query($query, $params = array()) {
 442          if (sizeof($params) > 0) {
 443              $sth = $this->MDB_object->prepare($query);
 444              if (MDB::isError($sth)) {
 445                  return($sth);
 446              }
 447              return($this->MDB_object->execute($sth, $params));
 448          } else {
 449              $result = $this->MDB_object->query($query);
 450              if (MDB::isError($result) || $result === DB_OK) {
 451                  return($result);
 452              } else {
 453                  return new DB_result($this->MDB_object, $result);
 454              }
 455          }
 456      }
 457  
 458      function simpleQuery($query) {
 459          return($this->MDB_object->query($query));
 460      }
 461  
 462      function limitQuery($query, $from, $count)
 463      {
 464          $result = $this->MDB_object->limitQuery($query, NULL, $from, $count);
 465          if (MDB::isError($result) || $result === DB_OK) {
 466              return($result);
 467          } else {
 468              return new DB_result($this->MDB_object, $result);
 469          }
 470      }
 471  
 472      function &getOne($query, $params = array())
 473      {
 474          return($this->MDB_object->getOne($query, NULL, $params));
 475      }
 476  
 477      function &getRow($query,
 478                       $params = NULL,
 479                       $fetchmode = DB_FETCHMODE_DEFAULT)
 480      {
 481          return($this->MDB_object->getRow($query, NULL, $params, NULL, $fetchmode));
 482      }
 483  
 484      function &getCol($query, $col = 0, $params = array())
 485      {
 486          return($this->MDB_object->getCol($query, NULL, $params, NULL, $col));
 487      }
 488  
 489      function &getAssoc($query, $force_array = FALSE, $params = array(),
 490                         $fetchmode = DB_FETCHMODE_ORDERED, $group = FALSE)
 491      {
 492          return($this->MDB_object->getAssoc($query, NULL, $params, NULL, $fetchmode, $force_array, $group));
 493      }
 494  
 495      function &getAll($query,
 496                       $params = NULL,
 497                       $fetchmode = DB_FETCHMODE_DEFAULT)
 498      {
 499          return($this->MDB_object->getAll($query, NULL, $params, NULL, $fetchmode));
 500      }
 501  
 502      function autoCommit($onoff = FALSE)
 503      {
 504          return($this->MDB_object->autoCommit($onoff));
 505      }
 506  
 507      function commit()
 508      {
 509          return($this->MDB_object->commit());
 510      }
 511  
 512      function rollback()
 513      {
 514          return($this->MDB_object->rollback());
 515      }
 516  
 517      function numRows($result)
 518      {
 519          return($this->MDB_object->numRows($result));
 520      }
 521  
 522      function affectedRows()
 523      {
 524          return($this->MDB_object->affectedRows());
 525      }
 526  
 527      function errorNative()
 528      {
 529          return($this->MDB_object->errorNative());
 530      }
 531  
 532      function nextId($seq_name, $ondemand = TRUE)
 533      {
 534          return($this->MDB_object->nextId($seq_name, $ondemand));
 535      }
 536  
 537      function createSequence($seq_name)
 538      {
 539          return($this->MDB_object->createSequence($seq_name, 1));
 540      }
 541  
 542      function dropSequence($seq_name)
 543      {
 544          return($this->MDB_object->dropSequence($seq_name));
 545      }
 546  
 547      function tableInfo($result, $mode = NULL)
 548      {
 549          return($this->MDB_object->tableInfo($result, $mode));
 550      }
 551  
 552      function getTables()
 553      {
 554          return($this->getListOf('tables'));
 555      }
 556  
 557      function getListOf($type)
 558      {
 559          switch ($type) {
 560              case 'tables':
 561                  return($this->MDB_object->listTables());
 562              case 'views':
 563                  return($this->MDB_object->listViews());
 564              case 'users':
 565                  return($this->MDB_object->listUsers());
 566              case 'functions':
 567                  return($this->MDB_object->listFunctions());
 568              case 'databases':
 569                  return($this->MDB_object->listDatabases());
 570              default:
 571                  return($this->raiseError(DB_ERROR_UNSUPPORTED));
 572          }
 573      }
 574  }
 575  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7