[ 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/mysql/ -> mysql_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 mysql_driver_ADOConnection extends ADOConnection
  12  {
  13      var $nameQuote = '`';
  14      var $sysDate = 'CURDATE()';
  15      var $sysTimeStamp = 'NOW()';
  16  
  17  	function mysql_driver_ADOConnection()
  18      {
  19          $this->dbtype = 'mysql';
  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('mysql_connect')) return false;
  31  
  32          $this->host = $host;
  33          if (!empty($this->port)) $this->host .= ":" . $this->port;
  34          $this->username = $username;
  35          $this->password = $password;
  36          $this->database = $database;        
  37          $this->persistent = $persistent;
  38          $this->forcenewconnection = $forcenew;
  39  
  40          if($this->persistent == 1)
  41          {
  42              if (strnatcmp(PHP_VERSION, '4.3.0') >= 0)
  43                  $this->connectionId = @mysql_pconnect( $this->host, $this->username, $this->password, $this->clientFlags );
  44              else
  45                  $this->connectionId = @mysql_pconnect( $this->host, $this->username, $this->password );
  46          }
  47          else
  48          {
  49              if (strnatcmp(PHP_VERSION, '4.3.0') >= 0)
  50                  $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password, $this->forcenewconnection, $this->clientFlags );
  51              else if (strnatcmp(PHP_VERSION, '4.2.0') >= 0)
  52                  $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password, $this->forcenewconnection );
  53              else
  54                  $this->connectionId = @mysql_connect( $this->host, $this->username, $this->password );
  55          }
  56  
  57          if ($this->connectionId === false)
  58          {
  59              if ($fn = $this->raiseErrorFn) 
  60                  $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
  61              return false;
  62          }
  63  
  64          if (!empty($this->database)) return $this->SelectDB( $this->database );
  65          return true;
  66      } 
  67  
  68      /**

  69       * Choose a database to connect.

  70       *

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

  72       * @return         true or false

  73       * @access public

  74       */
  75  
  76  	function SelectDB($dbname)
  77      {
  78          $this->database = $dbname;
  79  
  80          if ($this->connectionId === false)
  81          {
  82              $this->connectionId = false;
  83              return false;
  84          }
  85          else
  86          {
  87              $result = @mysql_select_db( $this->database, $this->connectionId );
  88  
  89              if($result === false)
  90              {
  91                  if($this->createdatabase == true)
  92                  {
  93                      $result = @mysql_query( "CREATE DATABASE IF NOT EXISTS " . $this->database, $this->connectionId );
  94                      if ($result === false) { // error handling if query fails
  95                          return false;
  96                      } 
  97                      $result = @mysql_select_db( $this->database, $this->connectionId );
  98                      if($result === false)
  99                      {
 100                          return false;
 101                      }
 102                  }
 103                  else
 104                  {
 105                      return false;
 106                  }
 107              }
 108              return true;
 109          }
 110      } 
 111  
 112      /**

 113       * Return database error message

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

 115       * 

 116       * @access public

 117       */
 118  
 119  	function ErrorMsg()
 120      {
 121          return @mysql_error($this->connectionId);
 122      }
 123  
 124      /**

 125       * Return database error number

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

 127       * 

 128       * @access public

 129       */
 130  
 131  	function ErrorNo()
 132      {
 133          return @mysql_errno($this->connectionId);
 134      }
 135  
 136      /**

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

 138       * 

 139       * @access public 

 140       * @return integer Affected rows

 141       */
 142  
 143  	function Affected_Rows()
 144      {
 145          return @mysql_affected_rows($this->connectionId);
 146      } 
 147  
 148      /**

 149       * Returns the last record id of an inserted item

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

 151       * 

 152       * @access public 

 153       */
 154  
 155  	function Insert_ID()
 156      {
 157          return @mysql_insert_id($this->connectionId);
 158      }
 159  
 160      /**

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

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

 163       * 

 164       * @param string            the string to quote

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

 166       *

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

 168       */
 169  
 170  	function qstr($string, $magic_quotes=false)
 171      {
 172          if (!$magic_quotes) {
 173              if (strnatcmp(PHP_VERSION, '4.3.0') >= 0) {
 174                  return "'" . mysql_real_escape_string($string) . "'";
 175              }
 176              $string = str_replace("'", "\\'" , str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string)));
 177              return  "'" . $string . "'"; 
 178          }
 179          return "'" . str_replace('\\"', '"', $string) . "'";
 180      }
 181  
 182  	function QMagic($string)
 183      {
 184          return $this->qstr($string, get_magic_quotes_gpc());
 185      }
 186  
 187      /**

 188       * Returns concatenated string

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

 190       * 

 191       * @return concatenated string

 192       */
 193  	function Concat()
 194      {
 195          $arr = func_get_args();
 196          $list = implode(', ', $arr); 
 197  
 198          if (strlen($list) > 0) return "CONCAT($list)";
 199          else return '';
 200      }
 201  
 202  	function IfNull( $field, $ifNull ) 
 203      {
 204          return " IFNULL($field, $ifNull) ";
 205      }
 206  
 207      /**

 208       * Closes database connection

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

 210       * 

 211       * @access public 

 212       */
 213  
 214  	function Close()
 215      {
 216          @mysql_close( $this->connectionId );
 217          $this->connectionId = false;
 218      }
 219  
 220       /**

 221       * Returns All Records in an array

 222       *

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

 224       * @access public 

 225       */
 226  
 227      function &GetAll($sql, $inputarr = false)
 228      {
 229          $data =& $this->GetArray($sql, $inputarr);
 230          return $data;
 231      }
 232  
 233       /**

 234       * Returns All Records in an array

 235       *

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

 237       * @access public 

 238       */
 239  
 240      function &GetArray($sql, $inputarr = false)
 241      {
 242          $data = false;
 243          $result =& $this->Execute($sql, $inputarr);
 244          if ($result)
 245          {
 246              $data =& $result->GetArray();
 247              $result->Close();
 248          }
 249          return $data;
 250      }
 251  
 252      /**

 253       * Executes SQL query and instantiates resultset methods

 254       * 

 255       * @access private 

 256       * @return mixed Resultset methods

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

 377       * mysqlResultSet Constructor

 378       * 

 379       * @access private 

 380       * @param string $record 

 381       * @param string $resultId 

 382       */
 383  
 384  	function mysql_driver_ResultSet( $resultId, $connectionId )
 385      {
 386          $this->fields = array();
 387          $this->connectionId = $connectionId;
 388          $this->record = array();
 389          $this->resultId = $resultId;
 390          $this->EOF = false;
 391      } 
 392  
 393      /**

 394       * Frees resultset

 395       * 

 396       * @access public 

 397       */
 398  
 399  	function Close()
 400      {
 401          @mysql_free_result( $this->resultId );
 402          $this->fields = array();
 403          $this->resultId = false;
 404      } 
 405  
 406      /**

 407       * Returns field name from select query

 408       * 

 409       * @access public 

 410       * @param string $field

 411       * @return string Field name

 412       */
 413  
 414  	function fields( $field )
 415      {
 416          return $this->fields[$field];
 417      } 
 418  
 419      /**

 420       * Returns numrows from select query

 421       * 

 422       * @access public 

 423       * @return integer Numrows

 424       */
 425  
 426  	function RecordCount()
 427      {
 428          return $this->_numOfRows;
 429      } 
 430  
 431      /**

 432       * Returns num of fields from select query

 433       * 

 434       * @access public 

 435       * @return integer numfields

 436       */
 437  
 438  	function FieldCount()
 439      {
 440          return $this->_numOfFields;
 441      } 
 442  
 443      /**

 444       * Returns next record

 445       * 

 446       * @access public 

 447       */
 448  
 449  	function MoveNext()
 450      {
 451          if (@$this->fields = mysql_fetch_array($this->resultId,$this->fetchMode)) {
 452              $this->_currentRow += 1;
 453              return true;
 454          }
 455          if (!$this->EOF) {
 456              $this->_currentRow += 1;
 457              $this->EOF = true;
 458          }
 459          return false;
 460      } 
 461  
 462      /**

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

 464       *

 465       * @return true or false

 466       */
 467  
 468  	function MoveFirst() 
 469      {
 470          if ($this->_currentRow == 0) return true;
 471          return $this->Move(0);            
 472      }            
 473  
 474      /**

 475       * Returns the Last Record

 476       * 

 477       * @access public 

 478       */
 479  
 480  	function MoveLast()
 481      {
 482          if ($this->EOF) return false;
 483          return $this->Move($this->_numOfRows - 1);
 484      }
 485  
 486      /**

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

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

 489       *

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

 491       *

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

 493       */
 494  
 495  	function Move($rowNumber = 0) 
 496      {
 497          if ($rowNumber == $this->_currentRow) return true;
 498          $this->EOF = false;
 499             if ($this->_numOfRows > 0){
 500              if ($rowNumber >= $this->_numOfRows - 1){
 501                  $rowNumber = $this->_numOfRows - 1;
 502              }
 503            }
 504  
 505          if ($this->_seek($rowNumber)) {
 506              $this->_currentRow = $rowNumber;
 507              if ($this->_fetch()) {
 508                  return true;
 509              }
 510              $this->fields = false;    
 511          }
 512          $this->EOF = true;
 513          return false;
 514      }
 515  
 516      /**

 517       * Perform Seek to specific row

 518       * 

 519       * @access private 

 520       */
 521  
 522  	function _seek($row)
 523      {
 524          if ($this->_numOfRows == 0) return false;
 525          return @mysql_data_seek($this->resultId,$row);
 526      }
 527  
 528      /**

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

 530       * 

 531       * @access private 

 532       */
 533  
 534  	function _fetch()
 535      {
 536          $this->fields = @mysql_fetch_array($this->resultId,$this->fetchMode);
 537          return is_array($this->fields);
 538      }
 539  
 540      /**

 541       * Check to see if last record reached

 542       * 

 543       * @access public 

 544       */
 545  
 546  	function EOF()
 547      {
 548          if( $this->_currentRow < $this->_numOfRows)
 549          {
 550              return false;
 551          }
 552          else
 553          {
 554              $this->EOF = true;
 555              return true;
 556          }
 557      } 
 558  
 559      /**

 560       * Returns All Records in an array

 561       * 

 562       * @access public 

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

 564       */
 565  
 566      function &GetArray($nRows = -1)
 567      {
 568          $results = array();
 569          $cnt = 0;
 570          while (!$this->EOF && $nRows != $cnt) {
 571              $results[] = $this->fields;
 572              $this->MoveNext();
 573              $cnt++;
 574          }
 575          return $results;
 576      } 
 577  
 578      function &GetRows($nRows = -1) 
 579      {
 580          $arr =& $this->GetArray($nRows);
 581          return $arr;
 582      }
 583  
 584      function &GetAll($nRows = -1)
 585      {
 586          $arr =& $this->GetArray($nRows);
 587          return $arr;
 588      }
 589  
 590      /**

 591      * Fetch field information for a table. 

 592      *

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

 594      */
 595  	function FetchField($fieldOffset = -1) 
 596      {
 597          if ($fieldOffset != -1) {
 598              $fieldObject = @mysql_fetch_field($this->resultId, $fieldOffset);
 599              $fieldObject->max_length = @mysql_field_len($this->resultId,$fieldOffset);
 600          }
 601          else
 602          {
 603              $fieldObject = @mysql_fetch_field($this->resultId);
 604              $fieldObject->max_length = @mysql_field_len($this->resultId);
 605          }
 606          return $fieldObject;
 607      }
 608  
 609  }
 610  
 611  ?>


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