Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmysqlidb.php
Go to the documentation of this file.
1 <?php
20 class eZMySQLiDB extends eZDBInterface
21 {
22 const RELATION_FOREIGN_KEY = 5;
23 const RELATION_FOREIGN_KEY_BIT = 32;
24
30 public function __construct( $parameters )
31 {
32 parent::__construct( $parameters );
33
34 if ( !extension_loaded( 'mysqli' ) )
35 {
36 if ( function_exists( 'eZAppendWarningItem' ) )
37 {
38 eZAppendWarningItem( array( 'error' => array( 'type' => 'ezdb',
39 'number' => eZDBInterface::ERROR_MISSING_EXTENSION ),
40 'text' => 'MySQLi extension was not found, the DB handler will not be initialized.' ) );
41 $this->IsConnected = false;
42 }
43 eZDebug::writeWarning( 'MySQLi extension was not found, the DB handler will not be initialized.', 'eZMySQLiDB' );
44 return;
45 }
46
47 eZDebug::createAccumulatorGroup( 'mysqli_total', 'Mysql Total' );
48
50 if ( !$this->DBWriteConnection )
51 {
52 $connection = $this->connect( $this->Server, $this->DB, $this->User, $this->Password, $this->SocketPath, $this->Charset, $this->Port );
53 if ( $this->IsConnected )
54 {
55 $this->DBWriteConnection = $connection;
56 }
57 }
58
59 // Connect to slave
60 if ( !$this->DBConnection )
61 {
62 if ( $this->UseSlaveServer === true )
63 {
64 $connection = $this->connect( $this->SlaveServer, $this->SlaveDB, $this->SlaveUser, $this->SlavePassword, $this->SocketPath, $this->Charset, $this->SlavePort );
65 }
66 else
67 {
68 $connection = $this->DBWriteConnection;
69 }
70
71 if ( $connection && $this->DBWriteConnection )
72 {
73 $this->DBConnection = $connection;
74 $this->IsConnected = true;
75 }
76 }
77
78 // Initialize TempTableList
79 $this->TempTableList = array();
80 }
81
86 function connect( $server, $db, $user, $password, $socketPath, $charset = null, $port = false )
87 {
88 $connection = false;
89
90 if ( $socketPath !== false )
91 {
92 ini_set( "mysqli.default_socket", $socketPath );
93 }
94
95 if ( $this->UsePersistentConnection == true )
96 {
97 // Only supported on PHP 5.3 (mysqlnd)
98 if ( version_compare( PHP_VERSION, '5.3' ) > 0 )
99 $server = 'p:' . $server;
100 else
101 eZDebug::writeWarning( 'mysqli only supports persistent connections when using php 5.3 and higher', __METHOD__ );
102 }
103
104 $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
105 eZDebug::accumulatorStart( 'mysqli_connection', 'mysqli_total', 'Database connection' );
106 try {
107 $connection = mysqli_connect( $server, $user, $password, null, (int)$port, $socketPath );
108 } catch( ErrorException $e ) {}
109 eZDebug::accumulatorStop( 'mysqli_connection' );
110 eZDebug::setHandleType( $oldHandling );
111
112 $maxAttempts = $this->connectRetryCount();
113 $waitTime = $this->connectRetryWaitTime();
114 $numAttempts = 1;
115 while ( !$connection && $numAttempts <= $maxAttempts )
116 {
117 sleep( $waitTime );
118
119 $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
120 eZDebug::accumulatorStart( 'mysqli_connection', 'mysqli_total', 'Database connection' );
121 try {
122 $connection = mysqli_connect( $server, $user, $password, null, (int)$port, $socketPath );
123 } catch( ErrorException $e ) {}
124 eZDebug::accumulatorStop( 'mysqli_connection' );
125 eZDebug::setHandleType( $oldHandling );
126
127 $numAttempts++;
128 }
129 $this->setError();
130
131 $this->IsConnected = true;
132
133 if ( !$connection )
134 {
135 eZDebug::writeError( "Connection error: Couldn't connect to database server. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
136 $this->IsConnected = false;
137 throw new eZDBNoConnectionException( $server, $this->ErrorMessage, $this->ErrorNumber );
138 }
139
140 if ( $this->IsConnected && $db != null )
141 {
142 $ret = mysqli_select_db( $connection, $db );
143 if ( !$ret )
144 {
145 $this->setError( $connection );
146 eZDebug::writeError( "Connection error: Couldn't select the database. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
147 $this->IsConnected = false;
148 }
149 }
150
151 if ( $charset !== null )
152 {
153 $originalCharset = $charset;
154 $charset = eZCharsetInfo::realCharsetCode( $charset );
155 }
156
157 if ( $this->IsConnected and $charset !== null )
158 {
159 $status = mysqli_set_charset( $connection, eZMySQLCharset::mapTo( $charset ) );
160 if ( !$status )
161 {
162 $this->setError();
163 eZDebug::writeWarning( "Connection warning: " . mysqli_errno( $connection ) . ": " . mysqli_error( $connection ), "eZMySQLiDB" );
164 }
165 }
166
167 return $connection;
168 }
169
170 function databaseName()
171 {
172 return 'mysql';
173 }
174
175 function bindingType( )
176 {
177 return eZDBInterface::BINDING_NO;
178 }
179
180 function bindVariable( $value, $fieldDef = false )
181 {
182 return $value;
183 }
184
193 function checkCharset( $charset, &$currentCharset )
194 {
195 // If we don't have a database yet we shouldn't check it
196 if ( !$this->DB )
197 return true;
198
199 $versionInfo = $this->databaseServerVersion();
200
201 if ( is_array( $charset ) )
202 {
203 $realCharset = array();
204 foreach ( $charset as $charsetItem )
205 $realCharset[] = eZCharsetInfo::realCharsetCode( $charsetItem );
206 }
207 else
208 $realCharset = eZCharsetInfo::realCharsetCode( $charset );
209
210 return $this->checkCharsetPriv( $realCharset, $currentCharset );
211 }
212
216 function checkCharsetPriv( $charset, &$currentCharset )
217 {
218 $query = "SHOW CREATE DATABASE `{$this->DB}`";
219 $status = mysqli_query( $this->DBConnection, $query );
220 $this->reportQuery( __CLASS__, $query, false, false );
221 if ( !$status )
222 {
223 $this->setError();
224 eZDebug::writeWarning( "Connection warning: " . mysqli_errno( $this->DBConnection ) . ": " . mysqli_error( $this->DBConnection ), "eZMySQLiDB" );
225 return false;
226 }
227
228 $numRows = mysqli_num_rows( $status );
229 if ( $numRows == 0 )
230 return false;
231
232 for ( $i = 0; $i < $numRows; ++$i )
233 {
234 $tmpRow = mysqli_fetch_array( $status, MYSQLI_ASSOC );
235 if ( $tmpRow['Database'] == $this->DB )
236 {
237 $createText = $tmpRow['Create Database'];
238 if ( preg_match( '#DEFAULT CHARACTER SET ([a-zA-Z0-9_-]+)#', $createText, $matches ) )
239 {
240 $currentCharset = $matches[1];
241 $currentCharset = eZCharsetInfo::realCharsetCode( $currentCharset );
242 // Convert charset names into something MySQL will understand
243 $unmappedCurrentCharset = eZMySQLCharset::mapFrom( $currentCharset );
244
245 if ( is_array( $charset ) )
246 {
247 if ( in_array( $unmappedCurrentCharset, $charset ) )
248 {
249 return $unmappedCurrentCharset;
250 }
251 }
252 else if ( $unmappedCurrentCharset == $charset )
253 {
254 return true;
255 }
256 return false;
257 }
258 break;
259 }
260 }
261 return true;
262 }
263
264 function query( $sql, $server = false )
265 {
266 if ( $this->IsConnected )
267 {
268 eZDebug::accumulatorStart( 'mysqli_query', 'mysqli_total', 'Mysqli_queries' );
269 $orig_sql = $sql;
270 // The converted sql should not be output
271 if ( $this->InputTextCodec )
272 {
273 eZDebug::accumulatorStart( 'mysqli_conversion', 'mysqli_total', 'String conversion in mysqli' );
274 $sql = $this->InputTextCodec->convertString( $sql );
275 eZDebug::accumulatorStop( 'mysqli_conversion' );
276 }
277
278 if ( $this->OutputSQL )
279 {
280 $this->startTimer();
281 }
282
283 $sql = trim( $sql );
284
285 // Check if we need to use the master or slave server by default
286 if ( $server === false )
287 {
288 $server = strncasecmp( $sql, 'select', 6 ) === 0 && $this->TransactionCounter == 0 ?
289 eZDBInterface::SERVER_SLAVE : eZDBInterface::SERVER_MASTER;
290 }
291
292 $connection = ( $server == eZDBInterface::SERVER_SLAVE ) ? $this->DBConnection : $this->DBWriteConnection;
293
294 $analysisText = false;
295 // If query analysis is enable we need to run the query
296 // with an EXPLAIN in front of it
297 // Then we build a human-readable table out of the result
298 if ( $this->QueryAnalysisOutput )
299 {
300 $analysisResult = mysqli_query( $connection, 'EXPLAIN ' . $sql );
301 if ( $analysisResult )
302 {
303 $numRows = mysqli_num_rows( $analysisResult );
304 $rows = array();
305 if ( $numRows > 0 )
306 {
307 for ( $i = 0; $i < $numRows; ++$i )
308 {
309 if ( $this->InputTextCodec )
310 {
311 $tmpRow = mysqli_fetch_array( $analysisResult, MYSQLI_ASSOC );
312 $convRow = array();
313 foreach( $tmpRow as $key => $row )
314 {
315 $convRow[$key] = $this->OutputTextCodec->convertString( $row );
316 }
317 $rows[$i] = $convRow;
318 }
319 else
320 $rows[$i] = mysqli_fetch_array( $analysisResult, MYSQLI_ASSOC );
321 }
322 }
323
324 // Figure out all columns and their maximum display size
325 $columns = array();
326 foreach ( $rows as $row )
327 {
328 foreach ( $row as $col => $data )
329 {
330 if ( !isset( $columns[$col] ) )
331 $columns[$col] = array( 'name' => $col,
332 'size' => strlen( $col ) );
333 $columns[$col]['size'] = max( $columns[$col]['size'], strlen( $data ) );
334 }
335 }
336
337 $delimiterLine = array();
338 $colLine = array();
339 // Generate the column line and the vertical delimiter
340 // The look of the table is taken from the MySQL CLI client
341 // It looks like this:
342 // +-------+-------+
343 // | col_a | col_b |
344 // +-------+-------+
345 // | txt | 42 |
346 // +-------+-------+
347 foreach ( $columns as $col )
348 {
349 $delimiterLine[] = str_repeat( '-', $col['size'] + 2 );
350 $colLine[] = ' ' . str_pad( $col['name'], $col['size'], ' ', STR_PAD_RIGHT ) . ' ';
351 }
352 $delimiterLine = '+' . join( '+', $delimiterLine ) . "+\n";
353 $analysisText = $delimiterLine;
354 $analysisText .= '|' . join( '|', $colLine ) . "|\n";
355 $analysisText .= $delimiterLine;
356
357 // Go trough all data and pad them to create the table correctly
358 foreach ( $rows as $row )
359 {
360 $rowLine = array();
361 foreach ( $columns as $col )
362 {
363 $name = $col['name'];
364 $size = $col['size'];
365 $data = isset( $row[$name] ) ? $row[$name] : '';
366 // Align numerical values to the right (ie. pad left)
367 $rowLine[] = ' ' . str_pad( $data, $size, ' ',
368 is_numeric( $data ) ? STR_PAD_LEFT : STR_PAD_RIGHT ) . ' ';
369 }
370 $analysisText .= '|' . join( '|', $rowLine ) . "|\n";
371 $analysisText .= $delimiterLine;
372 }
373
374 // Reduce memory usage
375 unset( $rows, $delimiterLine, $colLine, $columns );
376 }
377 }
378
379 $result = mysqli_query( $connection, $sql );
380
381 if ( $this->RecordError and !$result )
382 $this->setError();
383
384 if ( $this->OutputSQL )
385 {
386 $this->endTimer();
387
388 if ($this->timeTaken() > $this->SlowSQLTimeout)
389 {
390 $num_rows = mysqli_affected_rows( $connection );
391 $text = $sql;
392
393 // If we have some analysis text we append this to the SQL output
394 if ( $analysisText !== false )
395 $text = "EXPLAIN\n" . $text . "\n\nANALYSIS:\n" . $analysisText;
396
397 $this->reportQuery( __CLASS__ . '[' . $connection->host_info . ( $server == eZDBInterface::SERVER_MASTER ? ', on master' : '' ) . ']', $text, $num_rows, $this->timeTaken() );
398 }
399 }
400 eZDebug::accumulatorStop( 'mysqli_query' );
401 if ( $result )
402 {
403 return $result;
404 }
405 else
406 {
407 $this->setError();
408 $errorMessage = 'Query error (' . $this->ErrorNumber . '): ' . $this->ErrorMessage . '. Query: ' . $sql;
409 eZDebug::writeError( $errorMessage, __CLASS__ );
410 $oldRecordError = $this->RecordError;
411 // Turn off error handling while we unlock
412 $this->RecordError = false;
413 mysqli_query( $connection, 'UNLOCK TABLES' );
414 $this->RecordError = $oldRecordError;
415
416 $this->reportError();
417
418 // This is to behave the same way as other RDBMS PHP API as PostgreSQL
419 // functions which throws an error with a failing request.
420 if ( $this->errorHandling == eZDB::ERROR_HANDLING_STANDARD )
421 {
422 trigger_error( "mysqli_query(): $errorMessage", E_USER_ERROR );
423 }
424 else
425 {
426 throw new eZDBException( $this->ErrorMessage, $this->ErrorNumber );
427 }
428
429 return false;
430 }
431 }
432 else
433 {
434 eZDebug::writeError( "Trying to do a query without being connected to a database!", __CLASS__ );
435 }
436
437
438 }
439
440 function arrayQuery( $sql, $params = array(), $server = false )
441 {
442 $retArray = array();
443 if ( $this->IsConnected )
444 {
445 $limit = false;
446 $offset = 0;
447 $column = false;
448 // check for array parameters
449 if ( is_array( $params ) )
450 {
451 if ( isset( $params["limit"] ) and is_numeric( $params["limit"] ) )
452 $limit = $params["limit"];
453
454 if ( isset( $params["offset"] ) and is_numeric( $params["offset"] ) )
455 $offset = $params["offset"];
456
457 if ( isset( $params["column"] ) and ( is_numeric( $params["column"] ) or is_string( $params["column"] ) ) )
458 $column = $params["column"];
459 }
460
461 if ( $limit !== false and is_numeric( $limit ) )
462 {
463 $sql .= "\nLIMIT $offset, $limit ";
464 }
465 else if ( $offset !== false and is_numeric( $offset ) and $offset > 0 )
466 {
467 $sql .= "\nLIMIT $offset, 18446744073709551615"; // 2^64-1
468 }
469 $result = $this->query( $sql, $server );
470
471 if ( $result == false )
472 {
473 $this->reportQuery( __CLASS__, $sql, false, false );
474 return false;
475 }
476
477 $numRows = mysqli_num_rows( $result );
478 if ( $numRows > 0 )
479 {
480 if ( !is_string( $column ) )
481 {
482 eZDebug::accumulatorStart( 'mysqli_loop', 'mysqli_total', 'Looping result' );
483 for ( $i=0; $i < $numRows; $i++ )
484 {
485 if ( $this->InputTextCodec )
486 {
487 $tmpRow = mysqli_fetch_array( $result, MYSQLI_ASSOC );
488 $convRow = array();
489 foreach( $tmpRow as $key => $row )
490 {
491 eZDebug::accumulatorStart( 'mysqli_conversion', 'mysqli_total', 'String conversion in mysqli' );
492 $convRow[$key] = $this->OutputTextCodec->convertString( $row );
493 eZDebug::accumulatorStop( 'mysqli_conversion' );
494 }
495 $retArray[$i + $offset] = $convRow;
496 }
497 else
498 $retArray[$i + $offset] = mysqli_fetch_array( $result, MYSQLI_ASSOC );
499 }
500 eZDebug::accumulatorStop( 'mysqli_loop' );
501
502 }
503 else
504 {
505 eZDebug::accumulatorStart( 'mysqli_loop', 'mysqli_total', 'Looping result' );
506 for ( $i=0; $i < $numRows; $i++ )
507 {
508 $tmp_row = mysqli_fetch_array( $result, MYSQLI_ASSOC );
509 if ( $this->InputTextCodec )
510 {
511 eZDebug::accumulatorStart( 'mysqli_conversion', 'mysqli_total', 'String conversion in mysqli' );
512 $retArray[$i + $offset] = $this->OutputTextCodec->convertString( $tmp_row[$column] );
513 eZDebug::accumulatorStop( 'mysqli_conversion' );
514 }
515 else
516 $retArray[$i + $offset] =& $tmp_row[$column];
517 }
518 eZDebug::accumulatorStop( 'mysqli_loop' );
519 }
520 }
521 }
522 return $retArray;
523 }
524
525 function subString( $string, $from, $len = null )
526 {
527 if ( $len == null )
528 {
529 return " substring( $string from $from ) ";
530 }else
531 {
532 return " substring( $string from $from for $len ) ";
533 }
534 }
535
536 function concatString( $strings = array() )
537 {
538 $str = implode( "," , $strings );
539 return " concat( $str ) ";
540 }
541
542 function md5( $str )
543 {
544 return " MD5( $str ) ";
545 }
546
547 function bitAnd( $arg1, $arg2 )
548 {
549 return 'cast(' . $arg1 . ' & ' . $arg2 . ' AS SIGNED ) ';
550 }
551
552 function bitOr( $arg1, $arg2 )
553 {
554 return 'cast( ' . $arg1 . ' | ' . $arg2 . ' AS SIGNED ) ';
555 }
556
557 function supportedRelationTypeMask()
558 {
559 return eZDBInterface::RELATION_TABLE_BIT | self::RELATION_FOREIGN_KEY_BIT;
560 }
561
562 function supportedRelationTypes()
563 {
564 return array( self::RELATION_FOREIGN_KEY, eZDBInterface::RELATION_TABLE );
565 }
566
567 function relationCounts( $relationMask )
568 {
569 if ( $relationMask & eZDBInterface::RELATION_TABLE_BIT )
570 return $this->relationCount();
571 else
572 return 0;
573 }
574
575 function relationCount( $relationType = eZDBInterface::RELATION_TABLE )
576 {
577 if ( !in_array( $relationType, $this->supportedRelationTypes() ) )
578 {
579 eZDebug::writeError( "Unsupported relation type '$relationType'", __METHOD__ );
580 return false;
581 }
582 $count = false;
583 if ( $this->IsConnected )
584 {
585 switch ( $relationType )
586 {
587 case eZDBInterface::RELATION_TABLE:
588 {
589 $query = 'SHOW TABLES from `' . $this->DB .'`';
590 $result = mysqli_query( $this->DBConnection, $query );
591 $this->reportQuery( __CLASS__, $query, false, false );
592 $count = mysqli_num_rows( $result );
593 mysqli_free_result( $result );
594 } break;
595
596 case self::RELATION_FOREIGN_KEY:
597 {
598 $count = count( $this->relationList( self::RELATION_FOREIGN_KEY ) );
599 } break;
600 }
601 }
602 return $count;
603 }
604
605 function relationList( $relationType = eZDBInterface::RELATION_TABLE )
606 {
607 if ( !in_array( $relationType, $this->supportedRelationTypes() ) )
608 {
609 eZDebug::writeError( "Unsupported relation type '$relationType'", __METHOD__ );
610 return false;
611 }
612 if ( $this->IsConnected )
613 {
614 switch ( $relationType )
615 {
616 case eZDBInterface::RELATION_TABLE:
617 {
618 $tables = array();
619 $query = 'SHOW TABLES from `' . $this->DB .'`';
620 $result = mysqli_query( $this->DBConnection, $query );
621 $this->reportQuery( __CLASS__, $query, false, false );
622 while( $row = mysqli_fetch_row( $result ) )
623 {
624 $tables[] = $row[0];
625 }
626 mysqli_free_result( $result );
627 return $tables;
628 } break;
629
630 case self::RELATION_FOREIGN_KEY:
631 {
655 $foreignKeys = array();
656 foreach( $this->relationList( eZDBInterface::RELATION_TABLE ) as $table )
657 {
658 $query = "SHOW CREATE TABLE $table";
659 $result = mysqli_query( $this->DBConnection, $query );
660 $this->reportQuery( __CLASS__, $query, false, false );
661 if ( mysqli_num_rows( $result ) === 1 )
662 {
663 $row = mysqli_fetch_row( $result );
664 if ( strpos( $row[1], "CONSTRAINT" ) !== false )
665 {
666 if ( preg_match_all( '#CONSTRAINT [`"]([^`"]+)[`"] FOREIGN KEY \‍([`"].*[`"]\‍) REFERENCES [`"]([^`"]+)[`"] \‍([`"].*[`"]\‍)#', $row[1], $matches, PREG_PATTERN_ORDER ) )
667 {
668 // $foreignKeys[] = array( 'table' => $table, 'keys' => $matches[1] );
669 foreach( $matches[1] as $fkMatch )
670 {
671 $foreignKeys[] = array( 'table' => $table, 'fk' => $fkMatch );
672 }
673 }
674 }
675 }
676 }
677 return $foreignKeys;
678 }
679 }
680 }
681 }
682
683 function eZTableList( $server = eZDBInterface::SERVER_MASTER )
684 {
685 $tables = array();
686 if ( $this->IsConnected )
687 {
688 if ( $this->UseSlaveServer && $server == eZDBInterface::SERVER_SLAVE )
689 {
690 $connection = $this->DBConnection;
691 $db = $this->SlaveDB;
692 }
693 else
694 {
695 $connection = $this->DBWriteConnection;
696 $db = $this->DB;
697 }
698
699 $query = 'SHOW TABLES from `' . $db .'`';
700 $result = mysqli_query( $connection, $query );
701 $this->reportQuery( __CLASS__, $query, false, false );
702 while( $row = mysqli_fetch_row( $result ) )
703 {
704 $tableName = $row[0];
705 if ( substr( $tableName, 0, 2 ) == 'ez' )
706 {
707 $tables[$tableName] = eZDBInterface::RELATION_TABLE;
708 }
709 }
710 mysqli_free_result( $result );
711 }
712 return $tables;
713 }
714
715 function relationMatchRegexp( $relationType )
716 {
717 return "#^ez#";
718 }
719
720 function removeRelation( $relationName, $relationType )
721 {
722 $relationTypeName = $this->relationName( $relationType );
723 if ( !$relationTypeName )
724 {
725 eZDebug::writeError( "Unknown relation type '$relationType'", __METHOD__ );
726 return false;
727 }
728
729 if ( $this->IsConnected )
730 {
731 switch ( $relationType )
732 {
733 case self::RELATION_FOREIGN_KEY:
734 {
735 $sql = "ALTER TABLE {$relationName['table']} DROP FOREIGN KEY {$relationName['fk']}";
736 $this->query( $sql );
737 return true;
738 } break;
739
740 default:
741 {
742 $sql = "DROP $relationTypeName $relationName";
743 return $this->query( $sql );
744 }
745 }
746 }
747 return false;
748 }
749
755 public function relationName( $relationType )
756 {
757 if ( $relationType == self::RELATION_FOREIGN_KEY )
758 return 'FOREIGN_KEY';
759 else
760 return parent::relationName( $relationType );
761 }
762
763 function lock( $table )
764 {
765 if ( $this->IsConnected )
766 {
767 if ( is_array( $table ) )
768 {
769 $lockQuery = "LOCK TABLES";
770 $first = true;
771 foreach( array_keys( $table ) as $tableKey )
772 {
773 if ( $first == true )
774 $first = false;
775 else
776 $lockQuery .= ",";
777 $lockQuery .= " " . $table[$tableKey]['table'] . " WRITE";
778 }
779 $this->query( $lockQuery );
780 }
781 else
782 {
783 $this->query( "LOCK TABLES $table WRITE" );
784 }
785 }
786 }
787
788 function unlock()
789 {
790 if ( $this->IsConnected )
791 {
792 $this->query( "UNLOCK TABLES" );
793 }
794 }
795
799 function beginQuery()
800 {
801 return $this->query("BEGIN WORK");
802 }
803
807 function commitQuery()
808 {
809 return $this->query( "COMMIT" );
810 }
811
815 function rollbackQuery()
816 {
817 return mysqli_query( $this->DBWriteConnection, "ROLLBACK" );
818 }
819
827 function lastSerialID( $table = false, $column = false )
828 {
829 if ( $this->IsConnected )
830 {
831 $id = mysqli_insert_id( $this->DBWriteConnection );
832 return $id;
833 }
834
835 return false;
836 }
837
838 function escapeString( $str )
839 {
840 if ( $str === null )
841 {
842 return '';
843 }
844
845 if ( $this->IsConnected )
846 {
847 return mysqli_real_escape_string( $this->DBConnection, $str );
848 }
849 else
850 {
851 eZDebug::writeDebug( 'escapeString called before connection is made', __METHOD__ );
852 return mysqli_real_escape_string( $str );
853 }
854 }
855
856 function close()
857 {
858 if ( $this->IsConnected )
859 {
860 if ( $this->UseSlaveServer === true )
861 mysqli_close( $this->DBConnection );
862 mysqli_close( $this->DBWriteConnection );
863 }
864 }
865
866 function createDatabase( $dbName )
867 {
868 if ( $this->IsConnected )
869 {
870 $this->query( "CREATE DATABASE $dbName" );
871 $this->setError();
872 }
873 }
874
875 function removeDatabase( $dbName )
876 {
877 if ( $this->IsConnected )
878 {
879 $this->query( "DROP DATABASE $dbName" );
880 $this->setError();
881 }
882 }
883
888 function setError( $connection = false )
889 {
890 if ( $this->IsConnected )
891 {
892 if ( $connection === false )
893 $connection = $this->DBConnection;
894
895 if ( $connection instanceof MySQLi )
896 {
897 $this->ErrorMessage = mysqli_error( $connection );
898 $this->ErrorNumber = mysqli_errno( $connection );
899 }
900 }
901 else
902 {
903 $this->ErrorMessage = mysqli_connect_error();
904 $this->ErrorNumber = mysqli_connect_errno();
905 }
906 }
907
908 function availableDatabases()
909 {
910 $databaseArray = mysqli_query( $this->DBConnection, 'SHOW DATABASES' );
911
912 if ( $this->errorNumber() != 0 )
913 {
914 return null;
915 }
916
917 $databases = array();
918
919 $numRows = mysqli_num_rows( $databaseArray );
920 if ( $numRows == 0 )
921 {
922 return false;
923 }
924
925 while ( $row = mysqli_fetch_row( $databaseArray ) )
926 {
927 // we don't allow "mysql" or "information_schema" database to be shown anywhere
928 $curDB = $row[0];
929 if ( strcasecmp( $curDB, 'mysql' ) != 0 && strcasecmp( $curDB, 'information_schema' ) != 0 )
930 {
931 $databases[] = $curDB;
932 }
933 }
934 return $databases;
935 }
936
937 function databaseServerVersion()
938 {
939 if ( $this->IsConnected )
940 {
941 $versionInfo = mysqli_get_server_info( $this->DBConnection );
942
943 $versionArray = explode( '.', $versionInfo );
944
945 return array( 'string' => $versionInfo,
946 'values' => $versionArray );
947 }
948
949 return false;
950 }
951
952 function databaseClientVersion()
953 {
954 $versionInfo = mysqli_get_client_info();
955
956 $versionArray = explode( '.', $versionInfo );
957
958 return array( 'string' => $versionInfo,
959 'values' => $versionArray );
960 }
961
962 function isCharsetSupported( $charset )
963 {
964 return true;
965 }
966
967 function supportsDefaultValuesInsertion()
968 {
969 return false;
970 }
971
972 function dropTempTable( $dropTableQuery = '', $server = self::SERVER_SLAVE )
973 {
974 $dropTableQuery = str_ireplace( 'DROP TABLE', 'DROP TEMPORARY TABLE', $dropTableQuery );
975 parent::dropTempTable( $dropTableQuery, $server );
976 }
977
978 protected $TempTableList;
979
981 }
982
983 ?>
eZCharsetInfo\realCharsetCode
static realCharsetCode( $charsetCode)
Definition: ezcharsetinfo.php:186
eZDBInterface
Definition: ezdbinterface.php:20
eZDBInterface\$RecordError
$RecordError
Definition: ezdbinterface.php:1741
eZDBInterface\startTimer
startTimer()
Definition: ezdbinterface.php:380
eZDBInterface\reportQuery
reportQuery( $class, $sql, $numRows, $timeTaken, $asDebug=false)
Definition: ezdbinterface.php:353
eZDBInterface\SERVER_SLAVE
const SERVER_SLAVE
Definition: ezdbinterface.php:43
eZDBInterface\RELATION_TABLE_BIT
const RELATION_TABLE_BIT
Definition: ezdbinterface.php:31
eZDBInterface\BINDING_NO
const BINDING_NO
Definition: ezdbinterface.php:21
eZDBInterface\reportError
reportError()
Definition: ezdbinterface.php:1042
eZDBInterface\connectRetryCount
connectRetryCount()
Definition: ezdbinterface.php:458
eZDBInterface\SERVER_MASTER
const SERVER_MASTER
Definition: ezdbinterface.php:42
eZDBInterface\RELATION_TABLE
const RELATION_TABLE
Definition: ezdbinterface.php:25
eZDBInterface\errorNumber
errorNumber()
Definition: ezdbinterface.php:1388
eZDBInterface\ERROR_MISSING_EXTENSION
const ERROR_MISSING_EXTENSION
Definition: ezdbinterface.php:40
eZDBInterface\connectRetryWaitTime
connectRetryWaitTime()
Definition: ezdbinterface.php:468
eZDBInterface\$DB
$DB
Definition: ezdbinterface.php:1597
eZDBInterface\setError
setError()
Definition: ezdbinterface.php:1369
eZDBInterface\$DBConnection
$DBConnection
Definition: ezdbinterface.php:1605
eZDBInterface\$SlaveDB
$SlaveDB
Definition: ezdbinterface.php:1765
eZDBInterface\$DBWriteConnection
$DBWriteConnection
Definition: ezdbinterface.php:1613
eZDBInterface\timeTaken
timeTaken()
Definition: ezdbinterface.php:426
eZDBInterface\endTimer
endTimer()
Definition: ezdbinterface.php:390
eZDB\ERROR_HANDLING_STANDARD
const ERROR_HANDLING_STANDARD
Definition: ezdb.php:360
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDebug\HANDLE_EXCEPTION
const HANDLE_EXCEPTION
Definition: ezdebug.php:77
eZDebug\createAccumulatorGroup
static createAccumulatorGroup( $key, $name=false)
Definition: ezdebug.php:1314
eZDebug\setHandleType
static setHandleType( $type)
Definition: ezdebug.php:247
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZDebug\accumulatorStart
static accumulatorStart( $key, $inGroup=false, $name=false, $recursive=false)
Definition: ezdebug.php:1361
eZDebug\accumulatorStop
static accumulatorStop( $key, $recursive=false)
Definition: ezdebug.php:1389
eZMySQLCharset\mapFrom
static mapFrom( $charset)
Definition: ezmysqlcharset.php:80
eZMySQLCharset\mapTo
static mapTo( $charset)
Definition: ezmysqlcharset.php:64
eZMySQLiDB
The eZMySQLiDB class provides MySQL implementation of the database interface.
Definition: ezmysqlidb.php:21
eZMySQLiDB\bindVariable
bindVariable( $value, $fieldDef=false)
Definition: ezmysqlidb.php:180
eZMySQLiDB\eZTableList
eZTableList( $server=eZDBInterface::SERVER_MASTER)
Definition: ezmysqlidb.php:683
eZMySQLiDB\checkCharsetPriv
checkCharsetPriv( $charset, &$currentCharset)
Definition: ezmysqlidb.php:216
eZMySQLiDB\RELATION_FOREIGN_KEY
const RELATION_FOREIGN_KEY
Definition: ezmysqlidb.php:22
eZMySQLiDB\unlock
unlock()
Definition: ezmysqlidb.php:788
eZMySQLiDB\md5
md5( $str)
Definition: ezmysqlidb.php:542
eZMySQLiDB\bitAnd
bitAnd( $arg1, $arg2)
Definition: ezmysqlidb.php:547
eZMySQLiDB\rollbackQuery
rollbackQuery()
Definition: ezmysqlidb.php:815
eZMySQLiDB\bitOr
bitOr( $arg1, $arg2)
Definition: ezmysqlidb.php:552
eZMySQLiDB\removeDatabase
removeDatabase( $dbName)
Definition: ezmysqlidb.php:875
eZMySQLiDB\availableDatabases
availableDatabases()
Definition: ezmysqlidb.php:908
eZMySQLiDB\supportedRelationTypeMask
supportedRelationTypeMask()
Definition: ezmysqlidb.php:557
eZMySQLiDB\relationCounts
relationCounts( $relationMask)
Definition: ezmysqlidb.php:567
eZMySQLiDB\lastSerialID
lastSerialID( $table=false, $column=false)
Definition: ezmysqlidb.php:827
eZMySQLiDB\bindingType
bindingType()
Definition: ezmysqlidb.php:175
eZMySQLiDB\supportsDefaultValuesInsertion
supportsDefaultValuesInsertion()
Definition: ezmysqlidb.php:967
eZMySQLiDB\dropTempTable
dropTempTable( $dropTableQuery='', $server=self::SERVER_SLAVE)
Definition: ezmysqlidb.php:972
eZMySQLiDB\$TempTableList
$TempTableList
Definition: ezmysqlidb.php:978
eZMySQLiDB\subString
subString( $string, $from, $len=null)
Definition: ezmysqlidb.php:525
eZMySQLiDB\escapeString
escapeString( $str)
Definition: ezmysqlidb.php:838
eZMySQLiDB\removeRelation
removeRelation( $relationName, $relationType)
Definition: ezmysqlidb.php:720
eZMySQLiDB\RELATION_FOREIGN_KEY_BIT
const RELATION_FOREIGN_KEY_BIT
Definition: ezmysqlidb.php:23
eZMySQLiDB\checkCharset
checkCharset( $charset, &$currentCharset)
Definition: ezmysqlidb.php:193
eZMySQLiDB\concatString
concatString( $strings=array())
Definition: ezmysqlidb.php:536
eZMySQLiDB\relationList
relationList( $relationType=eZDBInterface::RELATION_TABLE)
Definition: ezmysqlidb.php:605
eZMySQLiDB\databaseName
databaseName()
Definition: ezmysqlidb.php:170
eZMySQLiDB\createDatabase
createDatabase( $dbName)
Definition: ezmysqlidb.php:866
eZMySQLiDB\arrayQuery
arrayQuery( $sql, $params=array(), $server=false)
Definition: ezmysqlidb.php:440
eZMySQLiDB\supportedRelationTypes
supportedRelationTypes()
Definition: ezmysqlidb.php:562
eZMySQLiDB\isCharsetSupported
isCharsetSupported( $charset)
Definition: ezmysqlidb.php:962
eZMySQLiDB\relationName
relationName( $relationType)
Definition: ezmysqlidb.php:755
eZMySQLiDB\relationCount
relationCount( $relationType=eZDBInterface::RELATION_TABLE)
Definition: ezmysqlidb.php:575
eZMySQLiDB\setError
setError( $connection=false)
Definition: ezmysqlidb.php:888
eZMySQLiDB\lock
lock( $table)
Definition: ezmysqlidb.php:763
eZMySQLiDB\databaseServerVersion
databaseServerVersion()
Definition: ezmysqlidb.php:937
eZMySQLiDB\connect
connect( $server, $db, $user, $password, $socketPath, $charset=null, $port=false)
Definition: ezmysqlidb.php:86
eZMySQLiDB\close
close()
Definition: ezmysqlidb.php:856
eZMySQLiDB\relationMatchRegexp
relationMatchRegexp( $relationType)
Definition: ezmysqlidb.php:715
eZMySQLiDB\beginQuery
beginQuery()
Definition: ezmysqlidb.php:799
eZMySQLiDB\commitQuery
commitQuery()
Definition: ezmysqlidb.php:807
eZMySQLiDB\__construct
__construct( $parameters)
Definition: ezmysqlidb.php:30
eZMySQLiDB\databaseClientVersion
databaseClientVersion()
Definition: ezmysqlidb.php:952
eZMySQLiDB\query
query( $sql, $server=false)
Definition: ezmysqlidb.php:264
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.