[ Index ]
 

Code source de CakePHP 1.1.13.4450

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

title

Body

[fermer]

/cake/libs/model/dbo/ -> dbo_adodb.php (source)

   1  <?php
   2  /* SVN FILE: $Id: dbo_adodb.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  
   4  /**
   5   * AdoDB layer for DBO.
   6   *
   7   * Long description for file
   8   *
   9   * PHP versions 4 and 5
  10   *
  11   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  12   * Copyright 2005-2007, Cake Software Foundation, Inc.
  13   *                                1785 E. Sahara Avenue, Suite 490-204
  14   *                                Las Vegas, Nevada 89104
  15   *
  16   * Licensed under The MIT License
  17   * Redistributions of files must retain the above copyright notice.
  18   *
  19   * @filesource
  20   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  21   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  22   * @package            cake
  23   * @subpackage        cake.cake.libs.model.dbo
  24   * @since            CakePHP(tm) v 0.2.9
  25   * @version            $Revision: 4409 $
  26   * @modifiedby        $LastChangedBy: phpnut $
  27   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  28   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  29   */
  30  
  31  /**
  32   * Include AdoDB files.
  33   */
  34  vendor ('adodb' . DS . 'adodb.inc');
  35  
  36  uses ('model' . DS . 'datasources' . DS . 'dbo_source');
  37  
  38  /**
  39   * AdoDB DBO implementation.
  40   *
  41   * Database abstraction implementation for the AdoDB library.
  42   *
  43   * @package        cake
  44   * @subpackage    cake.cake.libs.model.dbo
  45   */
  46  class DboAdodb extends DboSource {
  47  /**
  48   * Enter description here...
  49   *
  50   * @var string
  51   */
  52       var $description = "ADOdb DBO Driver";
  53  
  54  /**
  55   * ADOConnection object with which we connect.
  56   *
  57   * @var ADOConnection The connection object.
  58   * @access private
  59   */
  60       var $_adodb = null;
  61  
  62  /**
  63   * Array translating ADOdb column MetaTypes to cake-supported metatypes
  64   *
  65   * @var array
  66   * @access private
  67   */
  68       var $_adodb_column_types = array(
  69           'C' => 'string',
  70          'X' => 'text',
  71          'D' => 'date',
  72          'T' => 'timestamp',
  73          'L' => 'boolean',
  74          'N' => 'float',
  75          'I' => 'integer',
  76          'R' => 'integer', // denotes auto-increment or counter field
  77          'B' => 'binary'
  78      );
  79  
  80  /**
  81   * Connects to the database using options in the given configuration array.
  82   *
  83   * @param array $config Configuration array for connecting
  84   */
  85  	 function connect() {
  86          $config = $this->config;
  87          $persistent = strrpos($config['connect'], '|p');
  88  
  89          if ($persistent === false) {
  90              $adodb_driver = $config['connect'];
  91              $connect = 'Connect';
  92          } else {
  93              $adodb_driver = substr($config['connect'], 0, $persistent);
  94              $connect = 'PConnect';
  95          }
  96  
  97          $this->_adodb = NewADOConnection($adodb_driver);
  98          $adodb = &$this->_adodb;
  99          $this->connected = $adodb->$connect($config['host'], $config['login'], $config['password'], $config['database']);
 100          return $this->connected;
 101      }
 102  /**
 103   * Disconnects from database.
 104   *
 105   * @return boolean True if the database could be disconnected, else false
 106   */
 107  	 function disconnect() {
 108            return $this->_adodb->Close();
 109       }
 110  /**
 111   * Executes given SQL statement.
 112   *
 113   * @param string $sql SQL statement
 114   * @return resource Result resource identifier
 115   */
 116  	function _execute($sql) {
 117          global $ADODB_FETCH_MODE;
 118          $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
 119          return $this->_adodb->execute($sql);
 120      }
 121  /**
 122   * Returns a row from given resultset as an array .
 123   *
 124   * @return array The fetched row as an array
 125   */
 126  /**
 127   * Returns a row from current resultset as an array .
 128   *
 129   * @return array The fetched row as an array
 130   */
 131  	function fetchRow($sql = null) {
 132  
 133          if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
 134              if (!$this->execute($sql)) {
 135                  return null;
 136              }
 137          }
 138  
 139          if (!is_object($this->_result) || $this->_result->EOF) {
 140              return null;
 141          } else {
 142              $resultRow = $this->_result->FetchRow();
 143              $this->resultSet($resultRow);
 144              return $this->fetchResult();
 145          }
 146      }
 147  /**
 148   * Begin a transaction
 149   *
 150   * @param unknown_type $model
 151   * @return boolean True on success, false on fail
 152   * (i.e. if the database/model does not support transactions).
 153   */
 154  	function begin(&$model) {
 155          if (parent::begin($model)) {
 156              if ($this->_adodb->BeginTrans()) {
 157                  $this->__transactionStarted = true;
 158                  return true;
 159              }
 160          }
 161          return false;
 162      }
 163  /**
 164   * Commit a transaction
 165   *
 166   * @param unknown_type $model
 167   * @return boolean True on success, false on fail
 168   * (i.e. if the database/model does not support transactions,
 169   * or a transaction has not started).
 170   */
 171  	function commit(&$model) {
 172          if (parent::commit($model)) {
 173              $this->__transactionStarted = false;
 174              return $this->_adodb->CommitTrans();
 175          }
 176          return false;
 177      }
 178  /**
 179   * Rollback a transaction
 180   *
 181   * @param unknown_type $model
 182   * @return boolean True on success, false on fail
 183   * (i.e. if the database/model does not support transactions,
 184   * or a transaction has not started).
 185   */
 186  	function rollback(&$model) {
 187          if (parent::rollback($model)) {
 188              return $this->_adodb->RollbackTrans();
 189          }
 190          return false;
 191      }
 192  /**
 193   * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
 194   *
 195   * @return array Array of tablenames in the database
 196   */
 197  	function listSources() {
 198          $tables = $this->_adodb->MetaTables('TABLES');
 199  
 200            if (!sizeof($tables) > 0) {
 201                  trigger_error(ERROR_NO_TABLE_LIST, E_USER_NOTICE);
 202                  exit;
 203            }
 204  
 205            return $tables;
 206       }
 207  /**
 208   * Returns an array of the fields in the table used by the given model.
 209   *
 210   * @param AppModel $model Model object
 211   * @return array Fields in table. Keys are name and type
 212   */
 213  	function describe(&$model) {
 214          $cache = parent::describe($model);
 215          if ($cache != null) {
 216              return $cache;
 217          }
 218  
 219          $fields = false;
 220          $cols = $this->_adodb->MetaColumns($this->fullTableName($model, false));
 221  
 222          foreach($cols as $column) {
 223              $fields[] = array('name' => $column->name,
 224                                      'type' => $this->column($column->type));
 225          }
 226  
 227          $this->__cacheDescription($this->fullTableName($model, false), $fields);
 228          return $fields;
 229      }
 230  /**
 231   * Returns a formatted error message from previous database operation.
 232   *
 233   * @return string Error message
 234   */
 235  	function lastError() {
 236          return $this->_adodb->ErrorMsg();
 237      }
 238  /**
 239   * Returns number of affected rows in previous database operation, or false if no previous operation exists.
 240   *
 241   * @return int Number of affected rows
 242   */
 243  	function lastAffected() {
 244          return $this->_adodb->Affected_Rows();
 245      }
 246  /**
 247   * Returns number of rows in previous resultset, or false if no previous resultset exists.
 248   *
 249   * @return int Number of rows in resultset
 250   */
 251  	function lastNumRows() {
 252          return $this->_result ? $this->_result->RecordCount() : false;
 253      }
 254  /**
 255   * Returns the ID generated from the previous INSERT operation.
 256   *
 257   * @return int
 258   *
 259   * @Returns the last autonumbering ID inserted. Returns false if function not supported.
 260   */
 261  	 function lastInsertId() {
 262            return $this->_adodb->Insert_ID();
 263       }
 264  
 265  /**
 266   * Returns a LIMIT statement in the correct format for the particular database.
 267   *
 268   * @param int $limit Limit of results returned
 269   * @param int $offset Offset from which to start results
 270   * @return string SQL limit/offset statement
 271   * @todo Please change output string to whatever select your database accepts. adodb doesn't allow us to get the correct limit string out of it.
 272   */
 273  	 function limit($limit, $offset = null) {
 274           if (empty($limit)) {
 275               return null;
 276           }
 277          return " LIMIT {$limit}" . ($offset ? "{$offset}" : null);
 278       // please change to whatever select your database accepts
 279       // adodb doesn't allow us to get the correct limit string out of it
 280       }
 281  
 282  /**
 283   * Converts database-layer column types to basic types
 284   *
 285   * @param string $real Real database-layer column type (i.e. "varchar(255)")
 286   * @return string Abstract column type (i.e. "string")
 287   */
 288  	function column($real) {
 289          if (isset($this->_result)) {
 290              $adodb_metatyper = &$this->_result;
 291          } else {
 292              $adodb_metatyper = &$this->_adodb->execute('Select 1');
 293          }
 294  
 295          $interpreted_type = $adodb_metatyper->MetaType($real);
 296          if (!isset($this->_adodb_column_types[$interpreted_type])) {
 297              return 'text';
 298          }
 299  
 300          return $this->_adodb_column_types[$interpreted_type];
 301      }
 302  /**
 303   * Returns a quoted and escaped string of $data for use in an SQL statement.
 304   *
 305   * @param string $data String to be prepared for use in an SQL statement
 306   * @param string $column_type The type of the column into which this data will be inserted
 307   * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
 308   * @return string Quoted and escaped data
 309   */
 310  	function value($data, $column = null, $safe = false) {
 311          $parent = parent::value($data, $column, $safe);
 312          if ($parent != null) {
 313              return $parent;
 314          }
 315  
 316          if ($data === null) {
 317              return 'NULL';
 318          }
 319  
 320          if ($data === '') {
 321              return "''";
 322          }
 323          return $this->_adodb->qstr($data);
 324      }
 325  /**
 326   * Generates the fields list of an SQL query.
 327   *
 328   * @param Model $model
 329   * @param string $alias Alias tablename
 330   * @param mixed $fields
 331   * @return array
 332   */
 333  	function fields(&$model, $alias, $fields) {
 334          if (is_array($fields)) {
 335                  $fields = $fields;
 336          } else {
 337              if ($fields != null) {
 338                  if (strpos($fields, ',')) {
 339                      $fields = explode(',', $fields);
 340                  } else {
 341                      $fields = array($fields);
 342                  }
 343                  $fields = array_map('trim', $fields);
 344              } else {
 345                  foreach($model->_tableInfo->value as $field) {
 346                      $fields[] = $field['name'];
 347                  }
 348              }
 349          }
 350  
 351          $count = count($fields);
 352  
 353          if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
 354              for($i = 0; $i < $count; $i++) {
 355                  if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
 356                      $prepend = '';
 357                      if (strpos($fields[$i], 'DISTINCT') !== false) {
 358                          $prepend = 'DISTINCT ';
 359                          $fields[$i] = trim(r('DISTINCT', '', $fields[$i]));
 360                      }
 361  
 362                      $dot = strrpos($fields[$i], '.');
 363                      if ($dot === false) {
 364                          $fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '__' . $fields[$i]);
 365                      } else {
 366                          $build = explode('.', $fields[$i]);
 367                          $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '__' . $build[1]);
 368                      }
 369                  }
 370              }
 371          }
 372          return $fields;
 373      }
 374  /**
 375   * Enter description here...
 376   *
 377   * @param unknown_type $results
 378   */
 379  	function resultSet(&$results) {
 380          $num_fields = count($results);
 381          $fields = array_keys($results);
 382          $this->results =& $results;
 383          $this->map = array();
 384          $index = 0;
 385          $j = 0;
 386  
 387          while($j < $num_fields) {
 388              $columnName = $fields[$j];
 389  
 390              if (strpos($columnName, '__')) {
 391                  $parts = explode('__', $columnName);
 392                  $this->map[$index++] = array($parts[0], $parts[1]);
 393              } else {
 394                  $this->map[$index++] = array(0, $columnName);
 395              }
 396              $j++;
 397          }
 398      }
 399  /**
 400   * Fetches the next row from the current result set
 401   *
 402   * @return unknown
 403   */
 404  	function fetchResult() {
 405          if (!empty($this->results) && $row = $this->results) {
 406              $resultRow = array();
 407              $fields = array_keys($row);
 408              $count = count($fields);
 409              $i = 0;
 410              for ($i = 0; $i < $count; $i++) { //$row as $index => $field) {
 411                  list($table, $column) = $this->map[$i];
 412                  $resultRow[$table][$column] = $row[$fields[$i]];
 413              }
 414              return $resultRow;
 415          } else {
 416              return false;
 417          }
 418      }
 419  }
 420  
 421  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7