[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/lib/ezdb/classes/ -> ezmysqldb.php (source)

   1  <?php
   2  //
   3  // $Id$
   4  //
   5  // Definition of eZMySQLDB class
   6  //
   7  // Created on: <12-Feb-2002 15:54:17 bf>
   8  //
   9  // SOFTWARE NAME: eZ publish
  10  // SOFTWARE RELEASE: 3.9.0
  11  // BUILD VERSION: 17785
  12  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  13  // SOFTWARE LICENSE: GNU General Public License v2.0
  14  // NOTICE: >
  15  //   This program is free software; you can redistribute it and/or
  16  //   modify it under the terms of version 2.0  of the GNU General
  17  //   Public License as published by the Free Software Foundation.
  18  //
  19  //   This program is distributed in the hope that it will be useful,
  20  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  21  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22  //   GNU General Public License for more details.
  23  //
  24  //   You should have received a copy of version 2.0 of the GNU General
  25  //   Public License along with this program; if not, write to the Free
  26  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  27  //   MA 02110-1301, USA.
  28  //
  29  //
  30  
  31  /*!
  32    \class eZMySQLDB ezmysqldb.php
  33    \ingroup eZDB
  34    \brief The eZMySQLDB class provides MySQL implementation of the database interface.
  35  
  36    eZMySQLDB is the MySQL implementation of eZDB.
  37    \sa eZDB
  38  */
  39  
  40  include_once ( "lib/ezutils/classes/ezdebug.php" );
  41  include_once ( "lib/ezutils/classes/ezini.php" );
  42  include_once ( "lib/ezdb/classes/ezdbinterface.php" );
  43  
  44  class eZMySQLDB extends eZDBInterface
  45  {
  46      /*!
  47        Create a new eZMySQLDB object and connects to the database backend.
  48      */
  49      function eZMySQLDB( $parameters )
  50      {
  51          $this->eZDBInterface( $parameters );
  52  
  53          $this->CharsetMapping = array( 'iso-8859-1' => 'latin1',
  54                                         'iso-8859-2' => 'latin2',
  55                                         'iso-8859-8' => 'hebrew',
  56                                         'iso-8859-7' => 'greek',
  57                                         'iso-8859-9' => 'latin5',
  58                                         'iso-8859-13' => 'latin7',
  59                                         'windows-1250' => 'cp1250',
  60                                         'windows-1251' => 'cp1251',
  61                                         'windows-1256' => 'cp1256',
  62                                         'windows-1257' => 'cp1257',
  63                                         'utf-8' => 'utf8',
  64                                         'koi8-r' => 'koi8r',
  65                                         'koi8-u' => 'koi8u' );
  66  
  67          if ( !extension_loaded( 'mysql' ) )
  68          {
  69              if ( function_exists( 'eZAppendWarningItem' ) )
  70              {
  71                  eZAppendWarningItem( array( 'error' => array( 'type' => 'ezdb',
  72                                                                'number' => EZ_DB_ERROR_MISSING_EXTENSION ),
  73                                              'text' => 'MySQL extension was not found, the DB handler will not be initialized.' ) );
  74                  $this->IsConnected = false;
  75                  return;
  76              }
  77          }
  78  
  79          /// Connect to master server
  80          if ( $this->DBWriteConnection == false )
  81          {
  82              $connection = $this->connect( $this->Server, $this->DB, $this->User, $this->Password, $this->SocketPath, $this->Charset );
  83              if ( $this->IsConnected )
  84              {
  85                  $this->DBWriteConnection = $connection;
  86              }
  87          }
  88  
  89          // Connect to slave
  90          if ( $this->DBConnection == false )
  91          {
  92              if ( $this->UseSlaveServer === true )
  93              {
  94                  $connection = $this->connect( $this->SlaveServer, $this->SlaveDB, $this->SlaveUser, $this->SlavePassword, $this->SocketPath, $this->Charset );
  95              }
  96              else
  97              {
  98                  $connection =& $this->DBWriteConnection;
  99              }
 100  
 101              if ( $connection and $this->DBWriteConnection )
 102              {
 103                  $this->DBConnection = $connection;
 104                  $this->IsConnected = true;
 105              }
 106          }
 107  
 108          eZDebug::createAccumulatorGroup( 'mysql_total', 'Mysql Total' );
 109      }
 110  
 111      /*!
 112       \private
 113       Opens a new connection to a MySQL database and returns the connection
 114      */
 115      function connect( $server, $db, $user, $password, $socketPath, $charset = null )
 116      {
 117          $connection = false;
 118  
 119          if ( $socketPath !== false )
 120          {
 121              ini_set( "mysql.default_socket", $socketPath );
 122          }
 123  
 124          if ( $this->UsePersistentConnection == true )
 125          {
 126              $connection = @mysql_pconnect( $server, $user, $password );
 127          }
 128          else
 129          {
 130              $connection = @mysql_connect( $server, $user, $password, true );
 131          }
 132          $dbErrorText = mysql_error();
 133          $maxAttempts = $this->connectRetryCount();
 134          $waitTime = $this->connectRetryWaitTime();
 135          $numAttempts = 1;
 136          while ( $connection == false and $numAttempts <= $maxAttempts )
 137          {
 138              sleep( $waitTime );
 139              if ( $this->UsePersistentConnection == true )
 140              {
 141                  $connection = @mysql_pconnect( $this->Server, $this->User, $this->Password );
 142              }
 143              else
 144              {
 145                  $connection = @mysql_connect( $this->Server, $this->User, $this->Password );
 146              }
 147              $numAttempts++;
 148          }
 149          $this->setError();
 150  
 151          $this->IsConnected = true;
 152  
 153          if ( $connection == false )
 154          {
 155              eZDebug::writeError( "Connection error: Couldn't connect to database. Please try again later or inform the system administrator.\n$dbErrorText", "eZMySQLDB" );
 156              $this->IsConnected = false;
 157          }
 158  
 159          if ( $this->IsConnected && $db != null )
 160          {
 161              $ret = @mysql_select_db( $db, $connection );
 162              $this->setError();
 163              if ( !$ret )
 164              {
 165                  eZDebug::writeError( "Connection error: " . @mysql_errno( $connection ) . ": " . @mysql_error( $connection ), "eZMySQLDB" );
 166                  $this->IsConnected = false;
 167              }
 168          }
 169  
 170          if ( $charset !== null )
 171          {
 172              $originalCharset = $charset;
 173              include_once ( 'lib/ezi18n/classes/ezcharsetinfo.php' );
 174              $charset = eZCharsetInfo::realCharsetCode( $charset );
 175              // Convert charset names into something MySQL will understand
 176              if ( isset( $this->CharsetMapping[ $charset ] ) )
 177                  $charset = $this->CharsetMapping[ $charset ];
 178          }
 179  
 180          if ( $this->IsConnected and $charset !== null and $this->isCharsetSupported( $charset ) )
 181          {
 182              $versionInfo = $this->databaseServerVersion();
 183  
 184              // We require MySQL 4.1.1 to use the new character set functionality,
 185              // MySQL 4.1.0 does not have a full implementation of this, see:
 186              // http://dev.mysql.com/doc/mysql/en/Charset.html
 187              if ( version_compare( $versionInfo['string'], '4.1.1' ) >= 0 )
 188              {
 189                  $query = "SET NAMES '" . $charset . "'";
 190                  $status = @mysql_query( $query, $connection );
 191                  $this->reportQuery( 'eZMySQLDB', $query, false, false );
 192                  if ( !$status )
 193                  {
 194                      $this->setError();
 195                      eZDebug::writeWarning( "Connection warning: " . @mysql_errno( $connection ) . ": " . @mysql_error( $connection ), "eZMySQLDB" );
 196                  }
 197              }
 198          }
 199  
 200          return $connection;
 201      }
 202  
 203      /*!
 204       \reimp
 205      */
 206      function databaseName()
 207      {
 208          return 'mysql';
 209      }
 210  
 211      /*!
 212        \reimp
 213      */
 214      function bindingType( )
 215      {
 216          return EZ_DB_BINDING_NO;
 217      }
 218  
 219      /*!
 220        \reimp
 221      */
 222      function bindVariable( &$value, $fieldDef = false )
 223      {
 224          return $value;
 225      }
 226  
 227      /*!
 228        Checks if the requested character set matches the one used in the database.
 229  
 230        \return \c true if it matches or \c false if it differs.
 231        \param[out] $currentCharset The charset that the database uses.
 232                                    will only be set if the match fails.
 233                                    Note: This will be specific to the database.
 234  
 235        \note There will be no check for databases using MySQL 4.1.0 or lower since
 236              they do not have proper character set handling.
 237      */
 238      function checkCharset( $charset, &$currentCharset )
 239      {
 240          // If we don't have a database yet we shouldn't check it
 241          if ( !$this->DB )
 242              return true;
 243  
 244          $versionInfo = $this->databaseServerVersion();
 245  
 246          // We require MySQL 4.1.1 to use the new character set functionality,
 247          // MySQL 4.1.0 does not have a full implementation of this, see:
 248          // http://dev.mysql.com/doc/mysql/en/Charset.html
 249          // Older version should not check character sets
 250          if ( version_compare( $versionInfo['string'], '4.1.1' ) < 0 )
 251              return true;
 252  
 253          include_once ( 'lib/ezi18n/classes/ezcharsetinfo.php' );
 254  
 255          if ( is_array( $charset ) )
 256          {
 257              foreach ( $charset as $charsetItem )
 258                  $realCharset[] = eZCharsetInfo::realCharsetCode( $charsetItem );
 259          }
 260          else
 261              $realCharset = eZCharsetInfo::realCharsetCode( $charset );
 262  
 263          return $this->checkCharsetPriv( $realCharset, $currentCharset );
 264      }
 265  
 266      /*!
 267       \private
 268      */
 269      function checkCharsetPriv( $charset, &$currentCharset )
 270      {
 271          $query = "SHOW CREATE DATABASE `{$this->DB}`";
 272          $status = @mysql_query( $query, $this->DBConnection );
 273          $this->reportQuery( 'eZMySQLDB', $query, false, false );
 274          if ( !$status )
 275          {
 276              $this->setError();
 277              eZDebug::writeWarning( "Connection warning: " . @mysql_errno( $this->DBConnection ) . ": " . @mysql_error( $this->DBConnection ), "eZMySQLDB" );
 278              return false;
 279          }
 280  
 281          $numRows = mysql_num_rows( $status );
 282          if ( $numRows == 0 )
 283              return false;
 284  
 285          for ( $i = 0; $i < $numRows; ++$i )
 286          {
 287              $tmpRow = mysql_fetch_array( $status, MYSQL_ASSOC );
 288              if ( $tmpRow['Database'] == $this->DB )
 289              {
 290                  $createText = $tmpRow['Create Database'];
 291                  if ( preg_match( '#DEFAULT CHARACTER SET ([a-zA-Z0-9_-]+)#', $createText, $matches ) )
 292                  {
 293                      $currentCharset = $matches[1];
 294                      include_once ( 'lib/ezi18n/classes/ezcharsetinfo.php' );
 295                      $currentCharset = eZCharsetInfo::realCharsetCode( $currentCharset );
 296                      // Convert charset names into something MySQL will understand
 297  
 298                      $key = array_search( $currentCharset, $this->CharsetMapping );
 299                      $unmappedCurrentCharset = ( $key === false ) ? $currentCharset : $key;
 300  
 301                      if ( is_array( $charset ) )
 302                      {
 303                          if ( in_array( $unmappedCurrentCharset, $charset ) )
 304                          {
 305                              return $unmappedCurrentCharset;
 306                          }
 307                      }
 308                      else if ( $unmappedCurrentCharset == $charset )
 309                      {
 310                          return true;
 311                      }
 312                      return false;
 313                  }
 314                  break;
 315              }
 316          }
 317          return true;
 318      }
 319  
 320      /*!
 321       \reimp
 322      */
 323      function query( $sql )
 324      {
 325          if ( $this->IsConnected )
 326          {
 327              eZDebug::accumulatorStart( 'mysql_query', 'mysql_total', 'Mysql_queries' );
 328              $orig_sql = $sql;
 329              // The converted sql should not be output
 330              if ( $this->InputTextCodec )
 331              {
 332                  eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
 333                  $sql = $this->InputTextCodec->convertString( $sql );
 334                  eZDebug::accumulatorStop( 'mysql_conversion' );
 335              }
 336  
 337              if ( $this->OutputSQL )
 338              {
 339                  $this->startTimer();
 340              }
 341              // Check if it's a write or read sql query
 342              $sql = trim( $sql );
 343  
 344              $isWriteQuery = true;
 345              if ( strncasecmp( $sql, 'select', 6 ) === 0 )
 346              {
 347                  $isWriteQuery = false;
 348              }
 349  
 350              // Send temporary create queries to slave server
 351              if ( preg_match( "/create\s+temporary/i", $sql ) )
 352              {
 353                  $isWriteQuery = false;
 354              }
 355  
 356              if ( $isWriteQuery )
 357              {
 358                  $connection = $this->DBWriteConnection;
 359              }
 360              else
 361              {
 362                  $connection = $this->DBConnection;
 363              }
 364  
 365              $analysisText = false;
 366              // If query analysis is enable we need to run the query
 367              // with an EXPLAIN in front of it
 368              // Then we build a human-readable table out of the result
 369              if ( $this->QueryAnalysisOutput )
 370              {
 371                  $analysisResult = mysql_query( 'EXPLAIN ' . $sql, $connection );
 372                  if ( $analysisResult )
 373                  {
 374                      $numRows = mysql_num_rows( $analysisResult );
 375                      $rows = array();
 376                      if ( $numRows > 0 )
 377                      {
 378                          for ( $i = 0; $i < $numRows; ++$i )
 379                          {
 380                              if ( $this->InputTextCodec )
 381                              {
 382                                  $tmpRow = mysql_fetch_array( $analysisResult, MYSQL_ASSOC );
 383                                  $convRow = array();
 384                                  foreach( $tmpRow as $key => $row )
 385                                  {
 386                                      $convRow[$key] = $this->OutputTextCodec->convertString( $row );
 387                                  }
 388                                  $rows[$i] = $convRow;
 389                              }
 390                              else
 391                                  $rows[$i] = mysql_fetch_array( $analysisResult, MYSQL_ASSOC );
 392                          }
 393                      }
 394  
 395                      // Figure out all columns and their maximum display size
 396                      $columns = array();
 397                      foreach ( $rows as $row )
 398                      {
 399                          foreach ( $row as $col => $data )
 400                          {
 401                              if ( !isset( $columns[$col] ) )
 402                                  $columns[$col] = array( 'name' => $col,
 403                                                          'size' => strlen( $col ) );
 404                              $columns[$col]['size'] = max( $columns[$col]['size'], strlen( $data ) );
 405                          }
 406                      }
 407  
 408                      $analysisText = '';
 409                      $delimiterLine = array();
 410                      // Generate the column line and the vertical delimiter
 411                      // The look of the table is taken from the MySQL CLI client
 412                      // It looks like this:
 413                      // +-------+-------+
 414                      // | col_a | col_b |
 415                      // +-------+-------+
 416                      // | txt   |    42 |
 417                      // +-------+-------+
 418                      foreach ( $columns as $col )
 419                      {
 420                          $delimiterLine[] = str_repeat( '-', $col['size'] + 2 );
 421                          $colLine[] = ' ' . str_pad( $col['name'], $col['size'], ' ', STR_PAD_RIGHT ) . ' ';
 422                      }
 423                      $delimiterLine = '+' . join( '+', $delimiterLine ) . "+\n";
 424                      $analysisText = $delimiterLine;
 425                      $analysisText .= '|' . join( '|', $colLine ) . "|\n";
 426                      $analysisText .= $delimiterLine;
 427  
 428                      // Go trough all data and pad them to create the table correctly
 429                      foreach ( $rows as $row )
 430                      {
 431                          $rowLine = array();
 432                          foreach ( $columns as $col )
 433                          {
 434                              $name = $col['name'];
 435                              $size = $col['size'];
 436                              $data = isset( $row[$name] ) ? $row[$name] : '';
 437                              // Align numerical values to the right (ie. pad left)
 438                              $rowLine[] = ' ' . str_pad( $row[$name], $size, ' ',
 439                                                          is_numeric( $row[$name] ) ? STR_PAD_LEFT : STR_PAD_RIGHT ) . ' ';
 440                          }
 441                          $analysisText .= '|' . join( '|', $rowLine ) . "|\n";
 442                          $analysisText .= $delimiterLine;
 443                      }
 444  
 445                      // Reduce memory usage
 446                      unset( $rows, $delimiterLine, $colLine, $columns );
 447                  }
 448              }
 449  
 450              $result = mysql_query( $sql, $connection );
 451  
 452              if ( $this->RecordError and !$result )
 453                  $this->setError();
 454  
 455              if ( $this->OutputSQL )
 456              {
 457                  $this->endTimer();
 458  
 459                  if ($this->timeTaken() > $this->SlowSQLTimeout)
 460                  {
 461                      $num_rows = mysql_affected_rows( $connection );
 462                      $text = $sql;
 463  
 464                      // If we have some analysis text we append this to the SQL output
 465                      if ( $analysisText !== false )
 466                          $text = "EXPLAIN\n" . $text . "\n\nANALYSIS:\n" . $analysisText;
 467  
 468                      $this->reportQuery( 'eZMySQLDB', $text, $num_rows, $this->timeTaken() );
 469                  }
 470              }
 471              eZDebug::accumulatorStop( 'mysql_query' );
 472              if ( $result )
 473              {
 474                  return $result;
 475              }
 476              else
 477              {
 478                  eZDebug::writeError( "Query error: " . mysql_error( $connection ) . ". Query: ". $sql, "eZMySQLDB"  );
 479                  $oldRecordError = $this->RecordError;
 480                  // Turn off error handling while we unlock
 481                  $this->RecordError = false;
 482                  $this->unlock();
 483                  $this->RecordError = $oldRecordError;
 484  
 485                  $this->reportError();
 486  
 487                  return false;
 488              }
 489          }
 490          else
 491          {
 492              eZDebug::writeError( "Trying to do a query without being connected to a database!", "eZMySQLDB"  );
 493          }
 494  
 495  
 496      }
 497  
 498      /*!
 499       \reimp
 500      */
 501      function arrayQuery( $sql, $params = array() )
 502      {
 503          $retArray = array();
 504          if ( $this->IsConnected )
 505          {
 506              $limit = false;
 507              $offset = 0;
 508              $column = false;
 509              // check for array parameters
 510              if ( is_array( $params ) )
 511              {
 512                  if ( isset( $params["limit"] ) and is_numeric( $params["limit"] ) )
 513                      $limit = $params["limit"];
 514  
 515                  if ( isset( $params["offset"] ) and is_numeric( $params["offset"] ) )
 516                      $offset = $params["offset"];
 517  
 518                  if ( isset( $params["column"] ) and is_numeric( $params["column"] ) )
 519                      $column = $params["column"];
 520              }
 521  
 522              if ( $limit !== false and is_numeric( $limit ) )
 523              {
 524                  $sql .= "\nLIMIT $offset, $limit ";
 525              }
 526              else if ( $offset !== false and is_numeric( $offset ) and $offset > 0 )
 527              {
 528                  $sql .= "\nLIMIT $offset, 18446744073709551615"; // 2^64-1
 529              }
 530              $result = $this->query( $sql );
 531  
 532              if ( $result == false )
 533              {
 534                  $this->reportQuery( 'eZMySQLDB', $sql, false, false );
 535                  return false;
 536              }
 537  
 538              $numRows = mysql_num_rows( $result );
 539              if ( $numRows > 0 )
 540              {
 541                  if ( !is_string( $column ) )
 542                  {
 543                      eZDebug::accumulatorStart( 'mysql_loop', 'mysql_total', 'Looping result' );
 544                      for ( $i=0; $i < $numRows; $i++ )
 545                      {
 546                          if ( $this->InputTextCodec )
 547                          {
 548                              $tmpRow = mysql_fetch_array( $result, MYSQL_ASSOC );
 549                              $convRow = array();
 550                              foreach( $tmpRow as $key => $row )
 551                              {
 552                                  eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
 553                                  $convRow[$key] = $this->OutputTextCodec->convertString( $row );
 554                                  eZDebug::accumulatorStop( 'mysql_conversion' );
 555                              }
 556                              $retArray[$i + $offset] = $convRow;
 557                          }
 558                          else
 559                              $retArray[$i + $offset] = mysql_fetch_array( $result, MYSQL_ASSOC );
 560                      }
 561                      eZDebug::accumulatorStop( 'mysql_loop' );
 562  
 563                  }
 564                  else
 565                  {
 566                      eZDebug::accumulatorStart( 'mysql_loop', 'mysql_total', 'Looping result' );
 567                      for ( $i=0; $i < $numRows; $i++ )
 568                      {
 569                          $tmp_row = mysql_fetch_array( $result, MYSQL_ASSOC );
 570                          if ( $this->InputTextCodec )
 571                          {
 572                              eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
 573                              $retArray[$i + $offset] = $this->OutputTextCodec->convertString( $tmp_row[$column] );
 574                              eZDebug::accumulatorStop( 'mysql_conversion' );
 575                          }
 576                          else
 577                              $retArray[$i + $offset] =& $tmp_row[$column];
 578                      }
 579                      eZDebug::accumulatorStop( 'mysql_loop' );
 580                  }
 581              }
 582          }
 583          return $retArray;
 584      }
 585  
 586      function subString( $string, $from, $len = null )
 587      {
 588          if ( $len == null )
 589          {
 590              return " substring( $string from $from ) ";
 591          }else
 592          {
 593              return " substring( $string from $from for $len ) ";
 594          }
 595      }
 596  
 597      function concatString( $strings = array() )
 598      {
 599          $str = implode( "," , $strings );
 600          return " concat( $str  ) ";
 601      }
 602  
 603      function md5( $str )
 604      {
 605          return " MD5( $str ) ";
 606      }
 607  
 608      /*!
 609       \reimp
 610      */
 611      function supportedRelationTypeMask()
 612      {
 613          return EZ_DB_RELATION_TABLE_BIT;
 614      }
 615  
 616      /*!
 617       \reimp
 618      */
 619      function supportedRelationTypes()
 620      {
 621          return array( EZ_DB_RELATION_TABLE );
 622      }
 623  
 624      /*!
 625       \reimp
 626      */
 627      function relationCounts( $relationMask )
 628      {
 629          if ( $relationMask & EZ_DB_RELATION_TABLE_BIT )
 630              return $this->relationCount();
 631          else
 632              return 0;
 633      }
 634  
 635      /*!
 636        \reimp
 637      */
 638      function relationCount( $relationType = EZ_DB_RELATION_TABLE )
 639      {
 640          if ( $relationType != EZ_DB_RELATION_TABLE )
 641          {
 642              eZDebug::writeError( "Unsupported relation type '$relationType'", 'eZMySQLDB::relationCount' );
 643              return false;
 644          }
 645          $count = false;
 646          if ( $this->IsConnected )
 647          {
 648              $result = mysql_list_tables( $this->DB, $this->DBConnection );
 649              $count = mysql_num_rows( $result );
 650              mysql_free_result( $result );
 651          }
 652          return $count;
 653      }
 654  
 655      /*!
 656        \reimp
 657      */
 658      function relationList( $relationType = EZ_DB_RELATION_TABLE )
 659      {
 660          if ( $relationType != EZ_DB_RELATION_TABLE )
 661          {
 662              eZDebug::writeError( "Unsupported relation type '$relationType'", 'eZMySQLDB::relationList' );
 663              return false;
 664          }
 665          $tables = array();
 666          if ( $this->IsConnected )
 667          {
 668              $result = mysql_list_tables( $this->DB, $this->DBConnection );
 669              $count = mysql_num_rows( $result );
 670              for ( $i = 0; $i < $count; ++ $i )
 671              {
 672                  $tables[] = mysql_tablename( $result, $i );
 673              }
 674              mysql_free_result( $result );
 675          }
 676          return $tables;
 677      }
 678  
 679      /*!
 680       \reimp
 681      */
 682      function eZTableList()
 683      {
 684          $tables = array();
 685          if ( $this->IsConnected )
 686          {
 687              $result = mysql_list_tables( $this->DB, $this->DBConnection );
 688              $count = mysql_num_rows( $result );
 689              for ( $i = 0; $i < $count; ++ $i )
 690              {
 691                  $tableName = mysql_tablename( $result, $i );
 692                  if ( substr( $tableName, 0, 2 ) == 'ez' )
 693                  {
 694                      $tables[$tableName] = EZ_DB_RELATION_TABLE;
 695                  }
 696              }
 697              mysql_free_result( $result );
 698          }
 699          return $tables;
 700      }
 701  
 702      /*!
 703       \reimp
 704      */
 705      function relationMatchRegexp( $relationType )
 706      {
 707          return "#^ez#";
 708      }
 709  
 710      /*!
 711        \reimp
 712      */
 713      function removeRelation( $relationName, $relationType )
 714      {
 715          $relationTypeName = $this->relationName( $relationType );
 716          if ( !$relationTypeName )
 717          {
 718              eZDebug::writeError( "Unknown relation type '$relationType'", 'eZMySQLDB::removeRelation' );
 719              return false;
 720          }
 721  
 722          if ( $this->IsConnected )
 723          {
 724              $sql = "DROP $relationTypeName $relationName";
 725              return $this->query( $sql );
 726          }
 727          return false;
 728      }
 729  
 730      /*!
 731       \reimp
 732      */
 733      function lock( $table )
 734      {
 735          if ( $this->IsConnected )
 736          {
 737              if ( is_array( $table ) )
 738              {
 739                  $lockQuery = "LOCK TABLES";
 740                  $first = true;
 741                  foreach( array_keys( $table ) as $tableKey )
 742                  {
 743                      if ( $first == true )
 744                          $first = false;
 745                      else
 746                          $lockQuery .= ",";
 747                      $lockQuery .= " " . $table[$tableKey]['table'] . " WRITE";
 748                  }
 749                  $this->query( $lockQuery );
 750              }
 751              else
 752              {
 753                  $this->query( "LOCK TABLES $table WRITE" );
 754              }
 755          }
 756      }
 757  
 758      /*!
 759       \reimp
 760      */
 761      function unlock()
 762      {
 763          if ( $this->IsConnected )
 764          {
 765              $this->query( "UNLOCK TABLES" );
 766          }
 767      }
 768  
 769      /*!
 770       \reimp
 771       The query to start the transaction.
 772      */
 773      function beginQuery()
 774      {
 775          return $this->query("BEGIN WORK");
 776      }
 777  
 778      /*!
 779       \reimp
 780       The query to commit the transaction.
 781      */
 782      function commitQuery()
 783      {
 784          return $this->query( "COMMIT" );
 785      }
 786  
 787      /*!
 788       \reimp
 789       The query to cancel the transaction.
 790      */
 791      function rollbackQuery()
 792      {
 793          return $this->query( "ROLLBACK" );
 794      }
 795  
 796      /*!
 797       \reimp
 798      */
 799      function lastSerialID( $table = false, $column = false )
 800      {
 801          if ( $this->IsConnected )
 802          {
 803              $id = mysql_insert_id( $this->DBWriteConnection );
 804              return $id;
 805          }
 806          else
 807              return false;
 808      }
 809  
 810      /*!
 811       \reimp
 812      */
 813      function escapeString( $str )
 814      {
 815          return mysql_escape_string( $str );
 816      }
 817  
 818      /*!
 819       \reimp
 820      */
 821      function close()
 822      {
 823          if ( $this->IsConnected )
 824          {
 825              @mysql_close( $this->DBConnection );
 826              @mysql_close( $this->DBWriteConnection );
 827          }
 828      }
 829  
 830      /*!
 831       \reimp
 832      */
 833      function createDatabase( $dbName )
 834      {
 835          if ( $this->DBConnection != false )
 836          {
 837              mysql_create_db( $dbName, $this->DBConnection );
 838              $this->setError();
 839          }
 840      }
 841  
 842      /*!
 843       \reimp
 844      */
 845      function setError()
 846      {
 847          if ( $this->DBConnection )
 848          {
 849              $this->ErrorMessage = mysql_error( $this->DBConnection );
 850              $this->ErrorNumber = mysql_errno( $this->DBConnection );
 851          }
 852          else
 853          {
 854              $this->ErrorMessage = mysql_error();
 855              $this->ErrorNumber = mysql_errno();
 856          }
 857      }
 858  
 859      /*!
 860       \reimp
 861      */
 862      function availableDatabases()
 863      {
 864          $databaseArray = mysql_list_dbs( $this->DBConnection );
 865  
 866          if ( $this->errorNumber() != 0 )
 867          {
 868              return null;
 869          }
 870  
 871          $databases = array();
 872          $i = 0;
 873          $numRows = mysql_num_rows( $databaseArray );
 874          if ( count( $numRows ) == 0 )
 875          {
 876              return false;
 877          }
 878  
 879          while ( $i < $numRows )
 880          {
 881              // we don't allow "mysql" database to be shown anywhere
 882              $curDB = mysql_db_name( $databaseArray, $i );
 883              if ( strcasecmp( $curDB, 'mysql' ) != 0 )
 884                  $databases[] = $curDB;
 885              ++$i;
 886          }
 887          return $databases;
 888      }
 889  
 890      /*!
 891       \reimp
 892      */
 893      function databaseServerVersion()
 894      {
 895          $versionInfo = mysql_get_server_info();
 896  
 897          $versionArray = explode( '.', $versionInfo );
 898  
 899          return array( 'string' => $versionInfo,
 900                        'values' => $versionArray );
 901      }
 902  
 903      /*!
 904       \reimp
 905      */
 906      function databaseClientVersion()
 907      {
 908          $versionInfo = mysql_get_client_info();
 909  
 910          $versionArray = explode( '.', $versionInfo );
 911  
 912          return array( 'string' => $versionInfo,
 913                        'values' => $versionArray );
 914      }
 915  
 916      /*!
 917       \reimp
 918      */
 919      function isCharsetSupported( $charset )
 920      {
 921          if ( $charset == 'utf-8' )
 922          {
 923              $versionInfo = $this->databaseServerVersion();
 924  
 925              // We require MySQL 4.1.1 to use the new character set functionality,
 926              // MySQL 4.1.0 does not have a full implementation of this, see:
 927              // http://dev.mysql.com/doc/mysql/en/Charset.html
 928              return ( version_compare( $versionInfo['string'], '4.1.1' ) >= 0 );
 929          }
 930          else
 931          {
 932              return true;
 933          }
 934      }
 935  
 936      var $CharsetMapping;
 937  
 938      /// \privatesection
 939  }
 940  
 941  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7