[ 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_odbc.php (source)

   1  <?php
   2  /* SVN FILE: $Id: dbo_odbc.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  
   4  /**
   5   * ODBC 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.10.5.1790
  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 DBO.
  33   */
  34  uses ('model' . DS . 'datasources' . DS . 'dbo_source');
  35  
  36  /**
  37   * Short description for class.
  38   *
  39   * Long description for class
  40   *
  41   * @package        cake
  42   * @subpackage    cake.cake.libs.model.dbo
  43   */
  44  class DboOdbc extends DboSource{
  45  
  46  /**
  47   * Driver description
  48   *
  49   * @var string
  50   */
  51      var $description = "ODBC DBO Driver";
  52  
  53  /**
  54   * Table/column starting quote
  55   *
  56   * @var string
  57   */
  58      var $startQuote = "`";
  59  
  60  /**
  61   * Table/column end quote
  62   *
  63   * @var string
  64   */
  65      var $endQuote = "`";
  66  
  67  /**
  68   * Driver base configuration
  69   *
  70   * @var array
  71   */
  72      var $_baseConfig = array('persistent' => true,
  73                  'login' => 'root',
  74                  'password' => '',
  75                  'database' => 'cake',
  76                  'connect'  => 'odbc_pconnect'
  77      );
  78  
  79  /**
  80   * Enter description here...
  81   *
  82   * @var unknown_type
  83   */
  84  
  85      var $columns = array();
  86  
  87      //    var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'),
  88      //                        'string' => array('name' => 'varchar', 'limit' => '255'),
  89      //                        'text' => array('name' => 'text'),
  90      //                        'integer' => array('name' => 'int', 'limit' => '11'),
  91      //                        'float' => array('name' => 'float'),
  92      //                        'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d h:i:s', 'formatter' => 'date'),
  93      //                        'timestamp' => array('name' => 'datetime', 'format' => 'Y-m-d h:i:s', 'formatter' => 'date'),
  94      //                        'time' => array('name' => 'time', 'format' => 'h:i:s', 'formatter' => 'date'),
  95      //                        'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
  96      //                        'binary' => array('name' => 'blob'),
  97      //                        'boolean' => array('name' => 'tinyint', 'limit' => '1'));
  98  
  99  /**
 100   * Connects to the database using options in the given configuration array.
 101   *
 102   * @return boolean True if the database could be connected, else false
 103   */
 104  	function connect() {
 105          $config = $this->config;
 106          $connect = $config['connect'];
 107  
 108          $this->connected = false;
 109          $this->connection = $connect($config['database'], $config['login'], $config['password']);
 110  
 111          if ($this->connection) {
 112                  $this->connected = true;
 113          }
 114  
 115          return $this->connected;
 116      }
 117  
 118  /**
 119   * Disconnects from database.
 120   *
 121   * @return boolean True if the database could be disconnected, else false
 122   */
 123  	function disconnect() {
 124          return@odbc_close($this->connection);
 125      }
 126  
 127  /**
 128   * Executes given SQL statement.
 129   *
 130   * @param string $sql SQL statement
 131   * @return resource Result resource identifier
 132   * @access protected
 133   */
 134  	function _execute($sql) {
 135          return odbc_exec($this->connection, $sql);
 136      }
 137  
 138  /**
 139   * Returns a row from given resultset as an array .
 140   *
 141   * @param bool $assoc Associative array only, or both?
 142   * @return array The fetched row as an array
 143   */
 144  	function fetchRow($assoc = false) {
 145          if (is_resource($this->_result)) {
 146              $this->resultSet($this->_result);
 147              $resultRow = $this->fetchResult();
 148              return $resultRow;
 149          } else {
 150              return null;
 151          }
 152      }
 153  /**
 154   * Returns an array of sources (tables) in the database.
 155   *
 156   * @return array Array of tablenames in the database
 157   */
 158  	function listSources() {
 159  
 160          $cache = parent::listSources();
 161          if ($cache != null) {
 162              return $cache;
 163          }
 164  
 165          /*$result = odbc_tables($this->connection);
 166          if (function_exists('odbc_fetch_row')) {
 167              echo 'GOOD';
 168          } else {
 169              echo 'BAD';
 170          }*/
 171  
 172          $result = odbc_tables($this->connection);
 173  
 174          $tables = array();
 175          while (odbc_fetch_row($result)) {
 176              array_push($tables, odbc_result($result, "TABLE_NAME"));
 177          }
 178  
 179          foreach( $tables as $t ) {
 180              echo "$t\n";
 181          }
 182  
 183          parent::listSources($tables);
 184          return $tables;
 185      }
 186  /**
 187   * Returns an array of the fields in given table name.
 188   *
 189   * @param Model $model Model object to describe
 190   * @return array Fields in table. Keys are name and type
 191   */
 192      function &describe(&$model) {
 193          $cache=parent::describe($model);
 194  
 195          if ($cache != null) {
 196                  return $cache;
 197          }
 198  
 199          $fields=array();
 200          $sql='SELECT * FROM ' . $this->fullTableName($model) . ' LIMIT 1';
 201          $result=odbc_exec($this->connection, $sql);
 202  
 203          $count=odbc_num_fields($result);
 204  
 205          for($i = 1; $i <= $count; $i++) {
 206                  $cols[$i - 1] = odbc_field_name($result, $i);
 207          }
 208  
 209          foreach($cols as $column) {
 210                  $type
 211                  = odbc_field_type(
 212                      odbc_exec($this->connection, "SELECT " . $column . " FROM " . $this->fullTableName($model)),
 213                      1);
 214                  array_push($fields, array('name' => $column,
 215                                                  'type' => $type));
 216          }
 217  
 218          $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
 219          return $fields;
 220      }
 221  
 222  	function name($data) {
 223          if ($data == '*') {
 224                  return '*';
 225          }
 226  
 227          $pos=strpos($data, '`');
 228  
 229          if ($pos === false) {
 230                  $data = '' . str_replace('.', '.', $data) . '';
 231          //$data = '`'. str_replace('.', '`.`', $data) .'`';
 232          }
 233  
 234          return $data;
 235      }
 236  
 237  /**
 238   * Returns a quoted and escaped string of $data for use in an SQL statement.
 239   *
 240   * @param string $data String to be prepared for use in an SQL statement
 241   * @param string $column The column into which this data will be inserted
 242   * @return string Quoted and escaped
 243   * @todo Add logic that formats/escapes data based on column type
 244   */
 245  	function value($data, $column = null) {
 246          $parent=parent::value($data, $column);
 247  
 248          if ($parent != null) {
 249                  return $parent;
 250          }
 251  
 252          if ($data === null) {
 253                  return 'NULL';
 254          }
 255  
 256          // $data = mysql_real_escape_string($data, $this->connection);
 257  
 258          if (!is_numeric($data)) {
 259                  $return = "'" . $data . "'";
 260          } else {
 261                  $return = $data;
 262          }
 263  
 264          return $return;
 265      }
 266  
 267  /**
 268   * Not sure about this one, MySQL needs it but does ODBC?  Safer just to leave it
 269   * Translates between PHP boolean values and MySQL (faked) boolean values
 270   *
 271   * @param mixed $data Value to be translated
 272   * @return mixed Converted boolean value
 273   */
 274  	function boolean($data) {
 275          if ($data === true || $data === false) {
 276                  if ($data === true) {
 277                      return 1;
 278                  }
 279  
 280                  return 0;
 281          } else {
 282                  if (intval($data !== 0)) {
 283                      return true;
 284                  }
 285  
 286                  return false;
 287          }
 288      }
 289  
 290  /**
 291   * Begin a transaction
 292   *
 293   * @param unknown_type $model
 294   * @return boolean True on success, false on fail
 295   * (i.e. if the database/model does not support transactions).
 296   */
 297  	function begin(&$model) {
 298          if (parent::begin($model)) {
 299                  if (odbc_autocommit($this->connection, false)) {
 300                      $this->__transactionStarted = true;
 301                      return true;
 302                  }
 303          }
 304  
 305          return false;
 306      }
 307  
 308  /**
 309   * Commit a transaction
 310   *
 311   * @param unknown_type $model
 312   * @return boolean True on success, false on fail
 313   * (i.e. if the database/model does not support transactions,
 314   * or a transaction has not started).
 315   */
 316  	function commit(&$model) {
 317          if (parent::commit($model)) {
 318                  if (odbc_commit($this->connection)) {
 319                      $this->__transactionStarted = false;
 320                      return true;
 321                  }
 322          }
 323  
 324          return false;
 325      }
 326  
 327  /**
 328   * Rollback a transaction
 329   *
 330   * @param unknown_type $model
 331   * @return boolean True on success, false on fail
 332   * (i.e. if the database/model does not support transactions,
 333   * or a transaction has not started).
 334   */
 335  	function rollback(&$model) {
 336          if (parent::rollback($model)) {
 337                  $this->__transactionStarted=false;
 338                  return odbc_rollback($this->connection);
 339          }
 340  
 341          return false;
 342      }
 343  
 344  /**
 345   * Returns a formatted error message from previous database operation.
 346   *
 347   * @return string Error message with error number
 348   */
 349  	function lastError() {
 350          if (odbc_error($this->connection)) {
 351                  return odbc_error($this->connection) . ': ' . odbc_errormsg($this->connection);
 352          }
 353  
 354          return null;
 355      }
 356  
 357  /**
 358   * Returns number of affected rows in previous database operation. If no previous operation exists,
 359   * this returns false.
 360   *
 361   * @return int Number of affected rows
 362   */
 363  	function lastAffected() {
 364          if ($this->_result) {
 365                  return null;
 366          }
 367  
 368          return null;
 369      }
 370  
 371  /**
 372   * Returns number of rows in previous resultset. If no previous resultset exists,
 373   * this returns false.
 374   *
 375   * @return int Number of rows in resultset
 376   */
 377  	function lastNumRows() {
 378          if ($this->_result) {
 379                  return@odbc_num_rows($this->_result);
 380          }
 381  
 382          return null;
 383      }
 384  
 385  /**
 386   * Returns the ID generated from the previous INSERT operation.
 387   *
 388   * @param unknown_type $source
 389   * @return int
 390   */
 391  	function lastInsertId($source = null) {
 392          $result=$this->fetchAll('SELECT @@IDENTITY');
 393          return $result[0];
 394      }
 395  
 396  /**
 397   * Enter description here...
 398   *
 399   * @param string $real Real database-layer column type (i.e. "varchar(255)")
 400   */
 401  	function column($real) {
 402          if (is_array($real)) {
 403                  $col=$real['name'];
 404  
 405                  if (isset($real['limit'])) {
 406                      $col .= '(' . $real['limit'] . ')';
 407                  }
 408  
 409                  return $col;
 410          }
 411  
 412          return $real;
 413      }
 414  
 415  /**
 416   * Enter description here...
 417   *
 418   * @param unknown_type $results
 419   */
 420  	function resultSet(&$results) {
 421          $this->results=&$results;
 422          $this->map=array();
 423          $num_fields   =odbc_num_fields($results);
 424          $index        =0;
 425          $j            =0;
 426  
 427          while($j < $num_fields) {
 428                  $column = odbc_fetch_array($results, $j);
 429  
 430                  if (!empty($column->table)) {
 431                      $this->map[$index++] = array($column->table,
 432                                  $column->name);
 433                  } else {
 434                      echo array(0,
 435                                  $column->name);
 436  
 437                      $this->map[$index++]=array(0,
 438                                  $column->name);
 439                  }
 440  
 441                  $j++;
 442          }
 443      }
 444  
 445  /**
 446   * Fetches the next row from the current result set
 447   *
 448   * @return unknown
 449   */
 450  	function fetchResult() {
 451          if ($row = odbc_fetch_row($this->results)) {
 452                  $resultRow=array();
 453                  $i=0;
 454  
 455                  foreach($row as $index => $field) {
 456                      list($table, $column)      = $this->map[$index];
 457                      $resultRow[$table][$column]=$row[$index];
 458                      $i++;
 459                  }
 460  
 461                  return $resultRow;
 462          } else {
 463                  return false;
 464          }
 465      }
 466  
 467  	function buildSchemaQuery($schema) {
 468          $search=array('{AUTOINCREMENT}',
 469                      '{PRIMARY}',
 470                      '{UNSIGNED}',
 471                      '{FULLTEXT}',
 472                      '{FULLTEXT_MYSQL}',
 473                      '{BOOLEAN}',
 474                      '{UTF_8}');
 475  
 476          $replace=array('int(11) not null auto_increment',
 477                      'primary key',
 478                      'unsigned',
 479                      'FULLTEXT',
 480                      'FULLTEXT',
 481                      'enum (\'true\', \'false\') NOT NULL default \'true\'',
 482                      '/*!40100 CHARACTER SET utf8 COLLATE utf8_unicode_ci */');
 483  
 484          $query=trim(str_replace($search, $replace, $schema));
 485          return $query;
 486      }
 487  }
 488  ?>


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