[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/vendor/creole/drivers/mysql/ -> MySQLConnection.php (source)

   1  <?php
   2  /*
   3   *  $Id: MySQLConnection.php,v 1.18 2004/09/01 14:00:28 dlawson_mi Exp $
   4   *
   5   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   6   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   7   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   8   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   9   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16   *
  17   * This software consists of voluntary contributions made by many individuals
  18   * and is licensed under the LGPL. For more information please see
  19   * <http://creole.phpdb.org>.
  20   */
  21   
  22  require_once 'creole/Connection.php';
  23  require_once 'creole/common/ConnectionCommon.php';
  24  include_once 'creole/drivers/mysql/MySQLResultSet.php';
  25  
  26  /**
  27   * MySQL implementation of Connection.
  28   * 
  29   * 
  30   * @author    Hans Lellelid <hans@xmpl.org>
  31   * @author    Stig Bakken <ssb@fast.no> 
  32   * @author    Lukas Smith
  33   * @version   $Revision: 1.18 $
  34   * @package   creole.drivers.mysql
  35   */ 
  36  class MySQLConnection extends ConnectionCommon implements Connection {
  37  
  38      /** Current database (used in mysql_select_db()). */
  39      private $database;
  40      
  41      /**
  42       * Connect to a database and log in as the specified user.
  43       *
  44       * @param $dsn the data source name (see DB::parseDSN for syntax)
  45       * @param $flags Any conneciton flags.
  46       * @access public
  47       * @throws SQLException
  48       * @return void
  49       */
  50      function connect($dsninfo, $flags = 0)
  51      {
  52          if (!extension_loaded('mysql')) {
  53              throw new SQLException('mysql extension not loaded');
  54          }
  55  
  56          $this->dsn = $dsninfo;
  57          $this->flags = $flags;
  58          
  59          $persistent = ($flags & Creole::PERSISTENT) === Creole::PERSISTENT;
  60  
  61          if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') {
  62              $dbhost = ':' . $dsninfo['socket'];
  63          } else {
  64              $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  65              if (!empty($dsninfo['port'])) {
  66                  $dbhost .= ':' . $dsninfo['port'];
  67              }
  68          }
  69          $user = $dsninfo['username'];
  70          $pw = $dsninfo['password'];
  71          
  72          $encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null;
  73          
  74          $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
  75  
  76          @ini_set('track_errors', true);
  77          if ($dbhost && $user && $pw) {
  78              $conn = @$connect_function($dbhost, $user, $pw);
  79          } elseif ($dbhost && $user) {
  80              $conn = @$connect_function($dbhost, $user);
  81          } elseif ($dbhost) {
  82              $conn = @$connect_function($dbhost);
  83          } else {
  84              $conn = false;
  85          }
  86          @ini_restore('track_errors');
  87          if (empty($conn)) {
  88              if (($err = @mysql_error()) != '') {
  89                  throw new SQLException("connect failed", $err);
  90              } elseif (empty($php_errormsg)) {
  91                  throw new SQLException("connect failed");
  92              } else {
  93                  throw new SQLException("connect failed", $php_errormsg);
  94              }
  95          }
  96  
  97          if ($dsninfo['database']) {
  98              if (!@mysql_select_db($dsninfo['database'], $conn)) {
  99                 switch(mysql_errno($conn)) {
 100                          case 1049:
 101                              $exc = new SQLException("no such database", mysql_error($conn));         
 102                          break;
 103                          case 1044:
 104                              $exc = new SQLException("access violation", mysql_error($conn));
 105                          break;
 106                          default:
 107                             $exc = new SQLException("cannot select database", mysql_error($conn));
 108                  }
 109                  
 110                  throw $exc;
 111                  
 112              }
 113              // fix to allow calls to different databases in the same script
 114              $this->database = $dsninfo['database'];
 115          }
 116  
 117          $this->dblink = $conn;
 118          
 119          if ($encoding) {
 120              $this->executeUpdate("SET NAMES " . $encoding);
 121          }
 122      }    
 123      
 124      /**
 125       * @see Connection::getDatabaseInfo()
 126       */
 127      public function getDatabaseInfo()
 128      {
 129          require_once 'creole/drivers/mysql/metadata/MySQLDatabaseInfo.php';
 130          return new MySQLDatabaseInfo($this);
 131      }
 132      
 133      /**
 134       * @see Connection::getIdGenerator()
 135       */
 136      public function getIdGenerator()
 137      {
 138          require_once 'creole/drivers/mysql/MySQLIdGenerator.php';
 139          return new MySQLIdGenerator($this);
 140      }
 141      
 142      /**
 143       * @see Connection::prepareStatement()
 144       */
 145      public function prepareStatement($sql) 
 146      {
 147          require_once 'creole/drivers/mysql/MySQLPreparedStatement.php';
 148          return new MySQLPreparedStatement($this, $sql);
 149      }
 150      
 151      /**
 152       * @see Connection::prepareCall()
 153       */
 154      public function prepareCall($sql) {
 155          throw new SQLException('MySQL does not support stored procedures.');
 156      }
 157      
 158      /**
 159       * @see Connection::createStatement()
 160       */
 161      public function createStatement()
 162      {
 163          require_once 'creole/drivers/mysql/MySQLStatement.php';
 164          return new MySQLStatement($this);
 165      }
 166          
 167      /**
 168       * @see Connection::disconnect()
 169       */
 170      function close()
 171      {
 172          $ret = mysql_close($this->dblink);
 173          $this->dblink = null;
 174          return $ret;
 175      }
 176      
 177      /**
 178       * @see Connection::applyLimit()
 179       */
 180      public function applyLimit(&$sql, $offset, $limit)
 181      {
 182          if ( $limit > 0 ) {
 183              $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit;
 184          } else if ( $offset > 0 ) {
 185              $sql .= " LIMIT " . $offset . ", 18446744073709551615";
 186          }
 187      }
 188  
 189      /**
 190       * @see Connection::executeQuery()
 191       */
 192      function executeQuery($sql, $fetchmode = null)
 193      {
 194          $this->lastQuery = $sql;
 195          if ($this->database) {
 196              if (!@mysql_select_db($this->database, $this->dblink)) {
 197                  throw new SQLException('No database selected', mysql_error($this->dblink));
 198              }
 199          }
 200          $result = @mysql_query($sql, $this->dblink);
 201          if (!$result) {
 202              throw new SQLException('Could not execute query', mysql_error($this->dblink), $sql);
 203          }
 204          return new MySQLResultSet($this, $result, $fetchmode);
 205      }
 206      
 207      /**
 208       * @see Connection::executeUpdate()
 209       */
 210      function executeUpdate($sql)
 211      {    
 212          $this->lastQuery = $sql;
 213  
 214          if ($this->database) {
 215              if (!@mysql_select_db($this->database, $this->dblink)) {
 216                      throw new SQLException('No database selected', mysql_error($this->dblink));
 217              }
 218          }
 219          
 220          $result = @mysql_query($sql, $this->dblink);
 221          if (!$result) {
 222              throw new SQLException('Could not execute update', mysql_error($this->dblink), $sql);
 223          }        
 224          return (int) mysql_affected_rows($this->dblink);
 225      }
 226  
 227      /**
 228       * Start a database transaction.
 229       * @throws SQLException
 230       * @return void
 231       */
 232      protected function beginTrans()
 233      {
 234          $result = @mysql_query('SET AUTOCOMMIT=0', $this->dblink);
 235          $result = @mysql_query('BEGIN', $this->dblink);
 236          if (!$result) {
 237              throw new SQLException('Could not begin transaction', mysql_error($this->dblink));
 238          }
 239      }
 240          
 241      /**
 242       * Commit the current transaction.
 243       * @throws SQLException
 244       * @return void
 245       */
 246      protected function commitTrans()
 247      {
 248          if ($this->database) {
 249              if (!@mysql_select_db($this->database, $this->dblink)) {
 250                   throw new SQLException('No database selected', mysql_error($this->dblink));
 251              }
 252          }
 253          $result = @mysql_query('COMMIT', $this->dblink);
 254          $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
 255          if (!$result) {
 256              throw new SQLException('Can not commit transaction', mysql_error($this->dblink));                
 257          }
 258      }
 259  
 260      /**
 261       * Roll back (undo) the current transaction.
 262       * @throws SQLException
 263       * @return void
 264       */
 265      protected function rollbackTrans()
 266      {
 267          if ($this->database) {
 268              if (!@mysql_select_db($this->database, $this->dblink)) {
 269                  throw new SQLException('No database selected', mysql_error($this->dblink));
 270              }
 271          }
 272          $result = @mysql_query('ROLLBACK', $this->dblink);
 273          $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
 274          if (!$result) {
 275              throw new SQLException('Could not rollback transaction', mysql_error($this->dblink));
 276          }
 277      }
 278  
 279      /**
 280       * Gets the number of rows affected by the data manipulation
 281       * query.
 282       *
 283       * @return int Number of rows affected by the last query.
 284       */
 285      function getUpdateCount()
 286      {
 287          return (int) @mysql_affected_rows($this->dblink);
 288      }
 289      
 290  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7