ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

mysql_database.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class mysql_database_1_0_0{
27
38 var $Connection = false;
39
50 static $LockedTables = false;
51
62 static $LockModes = false;
63
76 static $QueryMode = DB_ASSOC_ARRAY;
77
88 var $Host;
89 var $Username;
90 var $Password;
91 var $Database;
92 var $TablenamePrefix;
93
112 function connect( $ConfigRow )
113 {
114 try
115 {
116 $Config = explode( "#" , $ConfigRow );
117 $this->Host = $Config[ 0 ];
118 $this->Username = $Config[ 1 ];
119 $this->Password = $Config[ 2 ];
120 $this->Database = $Config[ 3 ];
121 $this->TablenamePrefix = $Config[ 4 ];
122
123 $this->Connection = @new mysqli( $this->Host , $this->Username , $this->Password , $this->Database );
124 }
125 catch( Exception $e )
126 {
127 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
128 }
129 }
130
151 function get_connection( $ForceReconnect = false )
152 {
153 try
154 {
155 if( $this->Connection === false || $ForceReconnect === true )
156 {
157 if( $this->Connection !== false )
158 {
159 $this->Connection->close();
160 }
161 $DBConfigSet = get_package( 'database::db_config_set' , 'last' , __FILE__ );
162 $DBConfigSet->load_config( dirname( __FILE__ ).'/conf/cf_mysql_database' );
163 $this->Connection = $DBConfigSet->connect( $this );
164
165 if( mysqli_connect_error() )
166 {
167 throw(
168 new Exception(
169 'An error occured while setting connection to the database '.mysqli_connect_error()
170 )
171 );
172 }
173 }
174 return( $this->Connection );
175 }
176 catch( Exception $e )
177 {
178 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
179 }
180 }
181
202 function query( $Query )
203 {
204 try
205 {
206 $this->get_connection();
207
208 $Query = str_replace( 'umx_' , $this->TablenamePrefix , $Query );
209
210 $Result = $this->Connection->query( $Query );
211
212 if( $this->Connection->errno !== 0 )
213 {
214 throw( new Exception( 'An error occured while query execution '.$this->Connection->error ) );
215 }
216
217 return( $Result );
218 }
219 catch( Exception $e )
220 {
221 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
222 }
223 }
224
243 function query_as( $theQueryMode )
244 {
245 try
246 {
247 self::$QueryMode = $theQueryMode;
248 }
249 catch( Exception $e )
250 {
251 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
252 }
253 }
254
285 function select( $What , $Tables , $Condition = '1 = 1' )
286 {
287 try
288 {
289 $Result = $this->query( "SELECT $What FROM $Tables WHERE $Condition" );
290
291 return( $this->fetch_results( $Result ) );
292 }
293 catch( Exception $e )
294 {
295 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
296 }
297 }
298
321 function fetch_results( $Result )
322 {
323 try
324 {
325 $RetValues = array();
326
327 for( $i = 0; $i < $Result->num_rows ; $i++ )
328 {
329 if( self::$QueryMode == DB_ASSOC_ARRAY )
330 {
331 $RetValues [] = $Result->fetch_array( MYSQLI_ASSOC );
332 }
333 elseif( self::$QueryMode == DB_ARRAY )
334 {
335 $RetValues [] = $Result->fetch_array( MYSQLI_NUM );
336 }
337 elseif( self::$QueryMode == DB_OBJECT )
338 {
339 $RetValues [] = $Result->fetch_object();
340 }
341 }
342
343 $Result->close();
344
345 return( $RetValues );
346 }
347 catch( Exception $e )
348 {
349 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
350 }
351 }
352
379 function insert( $Table , $Fields , $Values )
380 {
381 try
382 {
383 $this->query( "INSERT INTO $Table ( $Fields ) VALUES ( $Values )" );
384 }
385 catch( Exception $e )
386 {
387 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
388 }
389 }
390
413 function delete( $Table , $Condition = '1 = 1' )
414 {
415 try
416 {
417 $this->query( "DELETE FROM $Table WHERE $Condition" );
418 }
419 catch( Exception $e )
420 {
421 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
422 }
423 }
424
455 function update( $Table , $Fields , $Values , $Condition = '1 = 1' )
456 {
457 try
458 {
459 $SubQuery = '';
460
461 for( $i = 0 ; $i < count( $Fields ) - 1 ; $i++ )
462 {
463 $SubQuery .= $Fields[ $i ].' = '.$Values[ $i ].' , ';
464 }
465 $SubQuery .= $Fields[ count( $Fields ) - 1 ].' = '.$Values[ count( $Fields ) - 1 ];
466
467 $this->query( "UPDATE $Table SET $SubQuery WHERE $Condition" );
468 }
469 catch( Exception $e )
470 {
471 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
472 }
473 }
474
497 function create( $Table , $FirstIndexField = 'id' )
498 {
499 try
500 {
501 $this->query(
502 "CREATE TABLE `$Table` ( `$FirstIndexField` INTEGER UNSIGNED NOT NULL DEFAULT NULL ".
503 "AUTO_INCREMENT , PRIMARY KEY ( `$FirstIndexField` ) )"
504 );
505 }
506 catch( Exception $e )
507 {
508 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
509 }
510 }
511
530 function drop( $Table )
531 {
532 try
533 {
534 $this->query( "DROP TABLE $Table" );
535 }
536 catch( Exception $e )
537 {
538 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
539 }
540 }
541
572 function add_column( $Table , $ColumnName , $Type , $Mode = 'NOT NULL AFTER `id`' )
573 {
574 try
575 {
576 $this->query( "ALTER TABLE `$Table` ADD COLUMN `$ColumnName` $Type $Mode" );
577 }
578 catch( Exception $e )
579 {
580 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
581 }
582 }
583
606 function drop_column( $Table , $ColumnName )
607 {
608 try
609 {
610 $this->query( "ALTER TABLE `$Table` DROP COLUMN `$ColumnName`" );
611 }
612 catch( Exception $e )
613 {
614 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
615 }
616 }
617
640 function initial_lock( $Tables , $Modes )
641 {
642 try
643 {
644 $QueryPart = '';
645
648 for( $i = 0 ; $i < count( $Tables ) - 1 ; $i++ )
649 {
650 $QueryPart .= $Tables[ $i ].' '.$Modes[ $i ].', ';
651 }
652 $QueryPart .= $Tables[ count( $Tables ) - 1 ].' '.$Modes[ count( $Tables ) - 1 ];
653
654 $this->query( 'LOCK TABLES '.$QueryPart );
655
656 self::$LockedTables = $Tables;
657 self::$LockModes = $Modes;
658 }
659 catch( Exception $e )
660 {
661 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
662 }
663 }
664
691 function try_lock_table( $Tables , $Modes , $i )
692 {
693 try
694 {
695 $Key = array_search( $Tables[ $i ] , self::$LockedTables );
696
697 if( $Key === false )
698 {
699 throw( new Exception( "Lock tables error. Invalid lock logic." ) );
700 }
701 if( self::$LockedTables[ $Key ] == 'WRITE' )
702 {
703 /* nop */
704 }
705 else
706 {
707 /*if we are trying to change lock from READ to WRITE then we get an error */
708 if( $Modes[ $i ] === 'WRITE' && self::$LockModes[ $Key ] === 'READ' )
709 {
710 throw( new Exception( "Lock tables error. Invalid lock mode logic." ) );
711 }
712 }
713 }
714 catch( Exception $e )
715 {
716 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
717 }
718 }
719
742 function lock( $Tables , $Modes )
743 {
744 try
745 {
746 if( self::$LockedTables === false )
747 {
748 $this->initial_lock( $Tables , $Modes );
749 }
750 else
751 {
754 for( $i = 0 ; $i < count( $Tables ) ; $i++ )
755 {
756 $this->try_lock_table( $Tables , $Modes , $i );
757 }
758 }
759 }
760 catch( Exception $e )
761 {
762 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
763 }
764 }
765
780 function unlock()
781 {
782 try
783 {
784 self::$LockedTables = false;
785 self::$LockModes = false;
786 $this->query( 'UNLOCK TABLES' );
787 }
788 catch( Exception $e )
789 {
790 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
791 }
792 }
793
812 function savepoint( $Savepoint )
813 {
814 try
815 {
816 $this->query( 'SAVEPOINT '.$Savepoint );
817 }
818 catch( Exception $e )
819 {
820 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
821 }
822 }
823
842 function rollback( $Savepoint )
843 {
844 try
845 {
846 $this->query( 'ROLLBACK TO SAVEPOINT '.$Savepoint );
847 }
848 catch( Exception $e )
849 {
850 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
851 }
852 }
853
868 function transaction()
869 {
870 try
871 {
872 $this->query( 'START TRANSACTION' );
873 }
874 catch( Exception $e )
875 {
876 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
877 }
878 }
879
894 function commit()
895 {
896 try
897 {
898 $this->query( 'COMMIT' );
899 }
900 catch( Exception $e )
901 {
902 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
903 }
904 }
905 }
906
907 ?>