[ Index ]
 

Code source de CMS made simple 1.0.5

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

title

Body

[fermer]

/lib/adodb_lite/adodbSQL_drivers/mysqli/ -> mysqli_driver.inc (source)

   1  <?php
   2  
   3  /**

   4   * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with 

   5   * a subset of the ADODB Command Syntax. 

   6   * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,

   7   * PostgresSQL64, PostgresSQL7, SqLite and Sybase.

   8   * 

   9   */
  10  
  11  class mysqli_driver_ADOConnection extends ADOConnection
  12  {
  13      var $nameQuote = '`';
  14      var $sysDate = 'CURDATE()';
  15      var $sysTimeStamp = 'NOW()';
  16  
  17  	function mysqli_driver_ADOConnection()
  18      {
  19          $this->dbtype = 'mysqli';
  20      }
  21  
  22      /**

  23       * Connection to database server and selected database

  24       * 

  25       * @access private 

  26       */
  27  
  28  	function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew)
  29      {
  30          if (!function_exists('mysqli_real_connect')) return false;
  31  
  32          $this->host = $host;
  33          $this->username = $username;
  34          $this->password = $password;
  35          $this->database = $database;        
  36          $this->persistent = $persistent;
  37          $this->forcenewconnection = $forcenew;
  38  
  39          $this->connectionId = @mysqli_init();
  40          @mysqli_real_connect( $this->connectionId, $this->host, $this->username, $this->password, $this->database, $this->port, $this->socket, $this->clientFlags );
  41  
  42          if (mysqli_connect_errno() != 0) 
  43          {
  44              $this->connectionId = false;
  45          }
  46  
  47          if ($this->connectionId === false)
  48          {
  49              if ($fn = $this->raiseErrorFn) 
  50                  $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
  51              return false;
  52          }
  53  
  54          if (!empty($this->database)) return $this->SelectDB( $this->database );
  55          return true;
  56      } 
  57  
  58      /**

  59       * Choose a database to connect.

  60       *

  61       * @param dbname     is the name of the database to select

  62       * @return         true or false

  63       * @access public

  64       */
  65  
  66  	function SelectDB($dbname)
  67      {
  68          $this->database = $dbname;
  69  
  70          if ($this->connectionId === false)
  71          {
  72              $this->connectionId = false;
  73              return false;
  74          }
  75          else
  76          {
  77              $result = @mysqli_select_db( $this->database, $this->connectionId );
  78  
  79              if($result === false)
  80              {
  81                  if($this->createdatabase == true)
  82                  {
  83                      $result = @mysqli_query( $this->connectionId, "CREATE DATABASE IF NOT EXISTS " . $this->database );
  84                      if ($result === false) { // error handling if query fails
  85                          return false;
  86                      } 
  87                      $result = @mysqli_select_db( $this->connectionId, $this->database );
  88                      if($result === false)
  89                      {
  90                          return false;
  91                      }
  92                  }
  93                  else
  94                  {
  95                      return false;
  96                  }
  97              }
  98              return true;
  99          }
 100      } 
 101  
 102      /**

 103       * Return database error message

 104       * Usage: $errormessage =& $db->ErrorMsg();

 105       * 

 106       * @access public

 107       */
 108  
 109  	function ErrorMsg()
 110      {
 111          return @mysqli_error($this->connectionId);
 112      }
 113  
 114      /**

 115       * Return database error number

 116       * Usage: $errorbo =& $db->ErrorNo();

 117       * 

 118       * @access public

 119       */
 120  
 121  	function ErrorNo()
 122      {
 123          return @mysqli_errno($this->connectionId);
 124      }
 125  
 126      /**

 127       * Returns # of affected rows from insert/delete/update query

 128       * 

 129       * @access public 

 130       * @return integer Affected rows

 131       */
 132  
 133  	function Affected_Rows()
 134      {
 135          return @mysqli_affected_rows($this->connectionId);
 136      } 
 137  
 138      /**

 139       * Returns the last record id of an inserted item

 140       * Usage: $db->Insert_ID();

 141       * 

 142       * @access public 

 143       */
 144  
 145  	function Insert_ID()
 146      {
 147          return @mysqli_insert_id($this->connectionId);
 148      }
 149  
 150      /**

 151       * Correctly quotes a string so that all strings are escape coded.

 152       * An example is  $db->qstr("Haven't a clue.");

 153       * 

 154       * @param string            the string to quote

 155       * @param [magic_quotes]    if $s is GET/POST var, set to get_magic_quotes_gpc().

 156       *

 157       * @return  single-quoted string IE: 'Haven\'t a clue.'

 158       */
 159  
 160  	function qstr($string, $magic_quotes=false)
 161      {
 162          if (!$magic_quotes) {
 163              if (strnatcmp(PHP_VERSION, '4.3.0') >= 0 && function_exists('mysqli_real_escape_string')) {
 164                  return "'" . mysqli_real_escape_string($this->connectionId, $string) . "'";
 165              }
 166              $string = str_replace("'", "\\'" , str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string)));
 167              return  "'" . $string . "'"; 
 168          }
 169          return "'" . str_replace('\\"', '"', $string) . "'";
 170      }
 171  
 172  	function QMagic($string)
 173      {
 174          return $this->qstr($string, get_magic_quotes_gpc());
 175      }
 176  
 177      /**

 178       * Returns concatenated string

 179       * Usage: $db->Concat($str1,$str2);

 180       * 

 181       * @return concatenated string

 182       */
 183  	function Concat()
 184      {
 185          $arr = func_get_args();
 186          $list = implode(', ', $arr); 
 187  
 188          if (strlen($list) > 0) return "CONCAT($list)";
 189          else return '';
 190      }
 191  
 192  	function IfNull( $field, $ifNull ) 
 193      {
 194          return " IFNULL($field, $ifNull) ";
 195      }
 196  
 197      /**

 198       * Closes database connection

 199       * Usage: $db->close();

 200       * 

 201       * @access public 

 202       */
 203  
 204  	function Close()
 205      {
 206          @mysqli_close( $this->connectionId );
 207          $this->connectionId = false;
 208      }
 209  
 210       /**

 211       * Returns All Records in an array

 212       *

 213       * Usage: $db->GetAll($sql);

 214       * @access public 

 215       */
 216  
 217      function &GetAll($sql, $inputarr = false)
 218      {
 219          $data =& $this->GetArray($sql, $inputarr);
 220          return $data;
 221      }
 222  
 223       /**

 224       * Returns All Records in an array

 225       *

 226       * Usage: $db->GetArray($sql);

 227       * @access public 

 228       */
 229  
 230      function &GetArray($sql, $inputarr = false)
 231      {
 232          $data = false;
 233          $result =& $this->Execute($sql, $inputarr);
 234          if ($result)
 235          {
 236              $data =& $result->GetArray();
 237              $result->Close();
 238          }
 239          return $data;
 240      }
 241  
 242      /**

 243       * Executes SQL query and instantiates resultset methods

 244       * 

 245       * @access private 

 246       * @return mixed Resultset methods

 247       */
 248  
 249      function &do_query( $sql, $offset, $nrows, $inputarr=false )
 250      {
 251          global $ADODB_FETCH_MODE;
 252  
 253          $false = false;
 254  
 255          $limit = '';
 256          if ($offset != -1 || $nrows != -1)
 257          {
 258              $offset = ($offset>=0) ? $offset . "," : '';
 259               $limit = ' LIMIT ' . $offset . ' ' . $nrows;
 260          }
 261  
 262          if ($inputarr && is_array($inputarr)) {
 263              $sqlarr = explode('?', $sql);
 264              if (!is_array(reset($inputarr))) $inputarr = array($inputarr);
 265              foreach($inputarr as $arr) {
 266                  $sql = ''; $i = 0;
 267                  foreach($arr as $v) {
 268                      $sql .= $sqlarr[$i];
 269                      switch(gettype($v)){
 270                          case 'string':
 271                              $sql .= $this->qstr($v);
 272                              break;
 273                          case 'double':
 274                              $sql .= str_replace(',', '.', $v);
 275                              break;
 276                          case 'boolean':
 277                              $sql .= $v ? 1 : 0;
 278                              break;
 279                          default:
 280                              if ($v === null)
 281                                  $sql .= 'NULL';
 282                              else $sql .= $v;
 283                      }
 284                      $i += 1;
 285                  }
 286                  $sql .= $sqlarr[$i];
 287                  if ($i+1 != sizeof($sqlarr))    
 288                      return $false;
 289                  $this->sql = $sql . $limit;
 290                  $time_start = array_sum(explode(' ', microtime()));
 291                  $this->query_count++;
 292                  $resultId = @mysqli_query($this->connectionId,  $this->sql );
 293                  $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start);
 294                  if($this->debug)
 295                  {
 296                      $this->outp($sql . $limit);
 297                  }
 298                  if ($resultId === false) { // error handling if query fails
 299                      if ($fn = $this->raiseErrorFn)
 300                          $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
 301                      return $false;
 302                  } 
 303              }
 304          }
 305          else
 306          {
 307                  $this->sql = $sql . $limit;
 308                  $time_start = array_sum(explode(' ', microtime()));
 309                  $this->query_count++;
 310                  $resultId = @mysqli_query($this->connectionId,  $this->sql );
 311                  $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start);
 312                  if($this->debug)
 313                  {
 314                      $this->outp($sql . $limit);
 315                  }
 316          }
 317  
 318          if ($resultId === false) { // error handling if query fails
 319              if ($fn = $this->raiseErrorFn)
 320                  $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
 321              return $false;
 322          } 
 323  
 324          if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
 325              $rs =& new ADORecordSet_empty();
 326              return $rs;
 327          }
 328  
 329          $resultset_name = $this->last_module_name . "_ResultSet";
 330          $recordset = new $resultset_name( $resultId, $this->connectionId );
 331  
 332          $recordset->_currentRow = 0;
 333  
 334          switch ($ADODB_FETCH_MODE)
 335          {
 336              case ADODB_FETCH_NUM: $recordset->fetchMode = MYSQLI_NUM; break;
 337              case ADODB_FETCH_ASSOC:$recordset->fetchMode = MYSQLI_ASSOC; break;
 338              default:
 339              case ADODB_FETCH_DEFAULT:
 340              case ADODB_FETCH_BOTH:$recordset->fetchMode = MYSQLI_BOTH; break;
 341          }
 342  
 343          $recordset->_numOfRows = @mysqli_num_rows( $resultId );
 344          if( $recordset->_numOfRows == 0)
 345          {
 346              $recordset->EOF = true;
 347          }
 348          $recordset->_numOfFields = @mysqli_num_fields( $resultId );
 349          $recordset->_fetch();
 350  
 351          return $recordset;
 352      } 
 353  } 
 354  
 355  class mysqli_driver_ResultSet
 356  {
 357      var $connectionId;
 358      var $fields;
 359      var $resultId;
 360      var $_currentRow = 0;
 361      var $_numOfRows = -1;
 362      var $_numOfFields = -1;
 363      var $fetchMode;
 364      var $EOF;
 365  
 366      /**

 367       * mysqlResultSet Constructor

 368       * 

 369       * @access private 

 370       * @param string $record 

 371       * @param string $resultId 

 372       */
 373  
 374  	function mysqli_driver_ResultSet( $resultId, $connectionId )
 375      {
 376          $this->fields = array();
 377          $this->connectionId = $connectionId;
 378          $this->record = array();
 379          $this->resultId = $resultId;
 380          $this->EOF = false;
 381      } 
 382  
 383      /**

 384       * Frees resultset

 385       * 

 386       * @access public 

 387       */
 388  
 389  	function Close()
 390      {
 391          @mysqli_free_result( $this->resultId );
 392          $this->fields = array();
 393          $this->resultId = false;
 394      } 
 395  
 396      /**

 397       * Returns field name from select query

 398       * 

 399       * @access public 

 400       * @param string $field

 401       * @return string Field name

 402       */
 403  
 404  	function fields( $field )
 405      {
 406          return $this->fields[$field];
 407      } 
 408  
 409      /**

 410       * Returns numrows from select query

 411       * 

 412       * @access public 

 413       * @return integer Numrows

 414       */
 415  
 416  	function RecordCount()
 417      {
 418          return $this->_numOfRows;
 419      } 
 420  
 421      /**

 422       * Returns num of fields from select query

 423       * 

 424       * @access public 

 425       * @return integer numfields

 426       */
 427  
 428  	function FieldCount()
 429      {
 430          return $this->_numOfFields;
 431      } 
 432  
 433      /**

 434       * Returns next record

 435       * 

 436       * @access public 

 437       */
 438  
 439  	function MoveNext()
 440      {
 441          if (@$this->fields = mysqli_fetch_array($this->resultId,$this->fetchMode)) {
 442              $this->_currentRow += 1;
 443              return true;
 444          }
 445          if (!$this->EOF) {
 446              $this->_currentRow += 1;
 447              $this->EOF = true;
 448          }
 449          return false;
 450      } 
 451  
 452      /**

 453       * Move to the first row in the recordset. Many databases do NOT support this.

 454       *

 455       * @return true or false

 456       */
 457  
 458  	function MoveFirst() 
 459      {
 460          if ($this->_currentRow == 0) return true;
 461          return $this->Move(0);            
 462      }            
 463  
 464      /**

 465       * Returns the Last Record

 466       * 

 467       * @access public 

 468       */
 469  
 470  	function MoveLast()
 471      {
 472          if ($this->EOF) return false;
 473          return $this->Move($this->_numOfRows - 1);
 474      } 
 475  
 476      /**

 477       * Random access to a specific row in the recordset. Some databases do not support

 478       * access to previous rows in the databases (no scrolling backwards).

 479       *

 480       * @param rowNumber is the row to move to (0-based)

 481       *

 482       * @return true if there still rows available, or false if there are no more rows (EOF).

 483       */
 484  
 485  	function Move($rowNumber = 0) 
 486      {
 487          if ($rowNumber == $this->_currentRow) return true;
 488          $this->EOF = false;
 489             if ($this->_numOfRows > 0){
 490              if ($rowNumber >= $this->_numOfRows - 1){
 491                  $rowNumber = $this->_numOfRows - 1;
 492              }
 493            }
 494  
 495          if ($this->_seek($rowNumber)) {
 496              $this->_currentRow = $rowNumber;
 497              if ($this->_fetch()) {
 498                  return true;
 499              }
 500              $this->fields = false;    
 501          }
 502          $this->EOF = true;
 503          return false;
 504      }
 505  
 506      /**

 507       * Perform Seek to specific row

 508       * 

 509       * @access private 

 510       */
 511  
 512  	function _seek($row)
 513      {
 514          if ($this->_numOfRows == 0) return false;
 515          return @mysqli_data_seek($this->resultId,$row);
 516      }
 517  
 518      /**

 519       * Fills field array with first database element when query initially executed

 520       * 

 521       * @access private 

 522       */
 523  
 524  	function _fetch()
 525      {
 526          $this->fields = @mysqli_fetch_array($this->resultId,$this->fetchMode);
 527          return is_array($this->fields);
 528      }
 529  
 530      /**

 531       * Check to see if last record reached

 532       * 

 533       * @access public 

 534       */
 535  
 536  	function EOF()
 537      {
 538          if( $this->_currentRow < $this->_numOfRows)
 539          {
 540              return false;
 541          }
 542          else
 543          {
 544              $this->EOF = true;
 545              return true;
 546          }
 547      } 
 548  
 549      /**

 550       * Returns All Records in an array

 551       * 

 552       * @access public 

 553       * @param [nRows]  is the number of rows to return. -1 means every row.

 554       */
 555  
 556      function &GetArray($nRows = -1)
 557      {
 558          $results = array();
 559          $cnt = 0;
 560          while (!$this->EOF && $nRows != $cnt) {
 561              $results[] = $this->fields;
 562              $this->MoveNext();
 563              $cnt++;
 564          }
 565          return $results;
 566      } 
 567  
 568      function &GetRows($nRows = -1) 
 569      {
 570          $arr =& $this->GetArray($nRows);
 571          return $arr;
 572      }
 573  
 574      function &GetAll($nRows = -1)
 575      {
 576          $arr =& $this->GetArray($nRows);
 577          return $arr;
 578      }
 579  
 580      /**

 581      * Fetch field information for a table. 

 582      *

 583      * @return object containing the name, type and max_length

 584      */
 585  	function FetchField($fieldOffset = -1) 
 586      {
 587          // $fieldOffset not supported by mysqli

 588          $fieldObject = @mysqli_fetch_field($this->resultId);
 589          return $fieldObject;
 590      }
 591  }
 592  ?>


Généré le : Tue Apr 3 18:50:37 2007 par Balluche grâce à PHPXref 0.7