00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00037 include_once( "lib/ezutils/classes/ezdebug.php" );
00038 include_once( "lib/ezutils/classes/ezini.php" );
00039 include_once( "lib/ezdb/classes/ezdbinterface.php" );
00040
eZDBInterface
00042 {
eZMySQLDB( $parameters )
00047 {
00048 $this->eZDBInterface( $parameters );
00049
00050 if ( !extension_loaded( 'mysql' ) )
00051 {
00052 if ( function_exists( 'eZAppendWarningItem' ) )
00053 {
00054 eZAppendWarningItem( array( 'error' => array( 'type' => 'ezdb',
00055 'number' => EZ_DB_ERROR_MISSING_EXTENSION ),
00056 'text' => 'MySQL extension was not found, the DB handler will not be initialized.' ) );
00057 $this->IsConnected = false;
00058 return;
00059 }
00060 }
00061
00062 $socketPath();
00063
00065 if ( $this->DBWriteConnection == false )
00066 {
00067 $connection = $this->socketPath );
00068 if ( $connection )
00069 {
00070 $this->DBWriteConnection = $connection;
00071 }
00072 }
00073
00074
00075 if ( $this->DBConnection == false )
00076 {
00077 if ( $this->UseSlaveServer === true )
00078 {
00079 $connection = $this->socketPath );
00080 }
00081 else
00082 {
00083 $connection =& $this->DBWriteConnection;
00084 }
00085
00086 if ( $connection and $this->DBWriteConnection )
00087 {
00088 $this->DBConnection = $connection;
00089 $this->IsConnected = true;
00090 }
00091 }
00092
00093 eZDebug::createAccumulatorGroup( 'mysql_total', 'Mysql Total' );
00094 }
00095
socketPath )
00101 {
00102 $connection = false;
00103
00104 if ( $socketPath !== false )
00105 {
00106 ini_set( "mysql.default_socket", $socketPath );
00107 }
00108
00109 if ( $this->UsePersistentConnection == true )
00110 {
00111 $connection = @mysql_pconnect( $server, $user, $password );
00112 }
00113 else
00114 {
00115 $connection = @mysql_connect( $server, $user, $password );
00116 }
00117 $dbErrorText = mysql_error();
00118 $maxAttempts = $this->connectRetryCount();
00119 $waitTime = $this->connectRetryWaitTime();
00120 $numAttempts = 1;
00121 while ( $connection == false and $numAttempts <= $maxAttempts )
00122 {
00123 sleep( $waitTime );
00124 if ( $this->UsePersistentConnection == true )
00125 {
00126 $connection = @mysql_pconnect( $this->Server, $this->User, $this->Password );
00127 }
00128 else
00129 {
00130 $connection = @mysql_connect( $this->Server, $this->User, $this->Password );
00131 }
00132 $numAttempts++;
00133 }
00134 $this->setError();
00135
00136 $isConnected = true;
00137
00138 if ( $connection == false )
00139 {
00140 eZDebug::writeError( "Connection error: Couldn't connect to database. Please try again later or inform the system administrator.\n$dbErrorText", "eZMySQLDB" );
00141 $isConnected = false;
00142 }
00143
00144 if ( $isConnected )
00145 {
00146 $ret = @mysql_select_db( $db, $connection );
00147 $this->setError();
00148
00149 if ( !$ret )
00150 {
00151 eZDebug::writeError( "Connection error: " . @mysql_errno( $connection ) . ": " . @mysql_error( $connection ), "eZMySQLDB" );
00152 $isConnected = false;
00153 }
00154 }
00155 return $connection;
00156 }
00157
databaseName()
00162 {
00163 return 'mysql';
00164 }
00165
query( $sql )
00170 {
00171 if ( $this->isConnected() )
00172 {
00173 eZDebug::accumulatorStart( 'mysql_query', 'mysql_total', 'Mysql_queries' );
00174 $orig_sql = $sql;
00175
00176 if ( $this->UseBuiltinEncoding and
00177 $this->InputTextCodec->conversionRequired() )
00178 {
00179 eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
00180 $sql =& $this->InputTextCodec->convertString( $sql );
00181 eZDebug::accumulatorStop( 'mysql_conversion' );
00182 }
00183
00184 if ( $this->OutputSQL )
00185 {
00186 $this->startTimer();
00187 }
00188
00189 $sql = trim( $sql );
00190
00191 $isWriteQuery = true;
00192 if ( stristr( $sql, "select" ) )
00193 {
00194 $isWriteQuery = false;
00195 }
00196
00197
00198 if ( preg_match( "/create\s+temporary/i", $sql ) )
00199 {
00200 $isWriteQuery = false;
00201 }
00202
00203 if ( $isWriteQuery )
00204 {
00205 $connection = $this->DBWriteConnection;
00206 }
00207 else
00208 {
00209 $connection = $this->DBConnection;
00210 }
00211
00212 $result =& mysql_query( $sql, $connection );
00213 if ( $this->RecordError )
00214 $this->setError();
00215
00216 if ( $this->OutputSQL )
00217 {
00218 $this->endTimer();
00219
00220 $num_rows = mysql_affected_rows( $connection );
00221 $this->eZMySQLDB', $sql, $num_rows, $this->timeTaken() );
00222 }
00223 eZDebug::accumulatorStop( 'mysql_query' );
00224 if ( $result )
00225 {
00226 return $result;
00227 }
00228 else
00229 {
00230 eZDebug::writeError( "Query error: " . mysql_error( $connection ) . ". Query: ". $sql, "eZMySQLDB" );
00231 $this->RecordError = false;
00232 $this->unlock();
00233 $this->RecordError = true;
00234
00235 return false;
00236 }
00237 mysql_free_result( $result );
00238 }
00239 else
00240 {
00241 eZDebug::writeError( "Trying to do a query without being connected to a database!", "eZMySQLDB" );
00242 }
00243
00244
00245 }
00246
arrayQuery( $sql, $params = array() )
00251 {
00252 $retArray = array();
00253 if ( $this->isConnected() )
00254 {
00255 $limit = false;
00256 $offset = 0;
00257 $column = false;
00258
00259 if ( is_array( $params ) )
00260 {
00261 if ( isset( $params["limit"] ) and is_numeric( $params["limit"] ) )
00262 $limit = $params["limit"];
00263
00264 if ( isset( $params["offset"] ) and is_numeric( $params["offset"] ) )
00265 $offset = $params["offset"];
00266
00267 if ( isset( $params["column"] ) and is_numeric( $params["column"] ) )
00268 $column = $params["column"];
00269 }
00270
00271 if ( $limit !== false and is_numeric( $limit ) )
00272 {
00273 $sql .= "\nLIMIT $offset, $limit ";
00274 }
00275 $result =& $this->query( $sql );
00276
00277 if ( $result == false )
00278 {
00279 $this->eZMySQLDB', $sql, false, false );
00280 return false;
00281 }
00282
00283 if ( mysql_num_rows( $result ) > 0 )
00284 {
00285 if ( !is_string( $column ) )
00286 {
00287 eZDebug::accumulatorStart( 'mysql_loop', 'mysql_total', 'Looping result' );
00288 for ( $i=0; $i < mysql_num_rows($result); $i++ )
00289 {
00290 if ( $this->UseBuiltinEncoding and
00291 $this->InputTextCodec->conversionRequired() )
00292 {
00293 $tmp_row =& mysql_fetch_array( $result, MYSQL_ASSOC );
00294 unset( $conv_row );
00295 $conv_row = array();
00296 reset( $tmp_row );
00297 while( ( $key = key( $tmp_row ) ) !== null )
00298 {
00299 eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
00300 $conv_row[$key] =& $this->OutputTextCodec->convertString( $tmp_row[$key] );
00301 eZDebug::accumulatorStop( 'mysql_conversion' );
00302 next( $tmp_row );
00303 }
00304 $retArray[$i + $offset] =& $conv_row;
00305 }
00306 else
00307 $retArray[$i + $offset] =& mysql_fetch_array( $result, MYSQL_ASSOC );
00308 }
00309 eZDebug::accumulatorStop( 'mysql_loop' );
00310
00311 }
00312 else
00313 {
00314 eZDebug::accumulatorStart( 'mysql_loop', 'mysql_total', 'Looping result' );
00315 for ( $i=0; $i < mysql_num_rows($result); $i++ )
00316 {
00317 $tmp_row =& mysql_fetch_array( $result, MYSQL_ASSOC );
00318 if ( $this->UseBuiltinEncoding and
00319 $this->InputTextCodec->conversionRequired() )
00320 {
00321 eZDebug::accumulatorStart( 'mysql_conversion', 'mysql_total', 'String conversion in mysql' );
00322 $retArray[$i + $offset] =& $this->OutputTextCodec->convertString( $tmp_row[$column] );
00323 eZDebug::accumulatorStop( 'mysql_conversion' );
00324 }
00325 else
00326 $retArray[$i + $offset] =& $tmp_row[$column];
00327 }
00328 eZDebug::accumulatorStop( 'mysql_loop' );
00329 }
00330 }
00331 }
00332 return $retArray;
00333 }
00334
00338 function subString( $string, $from, $len = null )
00339 {
00340 if ( $len == null )
00341 {
00342 return " substring( $string from $from ) ";
00343 }else
00344 {
00345 return " substring( $string from $from for $len ) ";
00346 }
00347 }
00348
00349 function concatString( $strings = array() )
00350 {
00351 $str = implode( "," , $strings );
00352 return " concat( $str ) ";
00353 }
00354
00355 function md5( $str )
00356 {
00357 return " MD5( $str ) ";
00358 }
00359
supportedRelationTypeMask()
00364 {
00365 return EZ_DB_RELATION_TABLE_BIT;
00366 }
00367
supportedRelationTypes()
00372 {
00373 return array( EZ_DB_RELATION_TABLE );
00374 }
00375
relationCounts( $relationMask )
00380 {
00381 if ( $relationMask & EZ_DB_RELATION_TABLE_BIT )
00382 return $this->relationCount();
00383 else
00384 return 0;
00385 }
00386
relationCount( $relationType = EZ_DB_RELATION_TABLE )
00391 {
00392 if ( $relationType != EZ_DB_RELATION_TABLE )
00393 {
00394 eZMySQLDB::relationCount' );
00395 return false;
00396 }
00397 $count = false;
00398 if ( $this->isConnected() )
00399 {
00400 $result =& mysql_list_tables( $this->DB, $this->DBConnection );
00401 $count = mysql_num_rows( $result );
00402 mysql_free_result( $result );
00403 }
00404 return $count;
00405 }
00406
relationList( $relationType = EZ_DB_RELATION_TABLE )
00411 {
00412 if ( $relationType != EZ_DB_RELATION_TABLE )
00413 {
00414 eZMySQLDB::relationList' );
00415 return false;
00416 }
00417 $tables = array();
00418 if ( $this->isConnected() )
00419 {
00420 $result =& mysql_list_tables( $this->DB, $this->DBConnection );
00421 $count = mysql_num_rows( $result );
00422 for ( $i = 0; $i < $count; ++ $i )
00423 {
00424 $tables[] = mysql_tablename( $result, $i );
00425 }
00426 mysql_free_result( $result );
00427 }
00428 return $tables;
00429 }
00430
relationName, $relationType )
00435 {
00436 $relationTypeName = $this->relationName( $relationType );
00437 if ( !$relationTypeName )
00438 {
00439 eZMySQLDB::removeRelation' );
00440 return false;
00441 }
00442
00443 if ( $this->isConnected() )
00444 {
00445 $sql = "DROP $relationTypeName $relationName";
00446 return $this->query( $sql );
00447 }
00448 return false;
00449 }
00450
lock( $table )
00455 {
00456 if ( $this->isConnected() )
00457 {
00458 $this->query( "LOCK TABLES $table WRITE" );
00459 }
00460 }
00461
unlock()
00466 {
00467 if ( $this->isConnected() )
00468 {
00469 $this->query( "UNLOCK TABLES" );
00470 }
00471 }
00472
begin()
00477 {
00478 if ( $this->isConnected() )
00479 {
00480 $this->query( "BEGIN WORK" );
00481 }
00482 }
00483
commit()
00488 {
00489 if ( $this->isConnected() )
00490 {
00491 $this->query( "COMMIT" );
00492 }
00493 }
00494
rollback()
00499 {
00500 if ( $this->isConnected() )
00501 {
00502 $this->query( "ROLLBACK" );
00503 }
00504 }
00505
lastSerialID( $table, $column )
00510 {
00511 if ( $this->isConnected() )
00512 {
00513 $id = mysql_insert_id( $this->DBWriteConnection );
00514 return $id;
00515 }
00516 else
00517 return false;
00518 }
00519
escapeString( $str )
00524 {
00525 return mysql_escape_string( $str );
00526 }
00527
close()
00532 {
00533 if ( $this->isConnected() )
00534 {
00535 @mysql_close( $this->DBConnection );
00536 @mysql_close( $this->DBWriteConnection );
00537 }
00538 }
00539
createDatabase( $dbName )
00544 {
00545 if ( $this->DBConnection != false )
00546 {
00547 mysql_create_db( $dbName, $this->DBConnection );
00548 $this->setError();
00549 }
00550 }
00551
setError()
00556 {
00557 if ( $this->DBConnection )
00558 {
00559 $this->ErrorMessage = mysql_error( $this->DBConnection );
00560 $this->ErrorNumber = mysql_errno( $this->DBConnection );
00561 }
00562 else
00563 {
00564 $this->ErrorMessage = mysql_error();
00565 $this->ErrorNumber = mysql_errno();
00566 }
00567 }
00568
00569
00571 }
00572
00573 ?>