ultimix
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 
16  define( 'DB_ASSOC_ARRAY' , 1 );
17  define( 'DB_OBJECT' , 2 );
18  define( 'DB_ARRAY' , 3 );
19 
31 
42  var $CachedMultyFS = false;
43  var $DatabaseLogger = false;
44  var $PageComposer = false;
45  var $Security = false;
46  var $Settings = false;
47  var $String = false;
48  var $Text = false;
49  var $Tags = false;
50 
61  var $DatabaseAccessObject = false;
62 
73  var $DatabaseEncoding = 'UTF-8';
74 
85  var $QueryCounter = 0;
86 
98 
110 
121  static $Session = false;
122 
137  function __construct()
138  {
139  try
140  {
141  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
142  $this->DatabaseLogger = get_package( 'database::database_logger' , 'last' , __FILE__ );
143  $this->PageComposer = get_package( 'page::page_composer' , 'last' , __FILE__ );
144  $this->Security = get_package( 'security' , 'last' , __FILE__ );
145  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
146  $this->String = get_package( 'string' , 'last' , __FILE__ );
147  $this->Text = get_package( 'string::text' , 'last' , __FILE__ );
148  $this->Tags = get_package( 'string::tags' , 'last' , __FILE__ );
149  }
150  catch( Exception $e )
151  {
152  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
153  }
154  }
155 
174  private function compile_config_line( $ConfigLine )
175  {
176  try
177  {
178  $this->Settings->load_settings( $ConfigLine );
179  $PackageName = $this->Settings->get_setting( 'package_name' );
180  $PackageVersion = $this->Settings->get_setting( 'package_version' , 'last' );
181  $AddLimitations = $this->Settings->get_setting( 'data_limitation' );
182  $Package = get_package( $PackageName , $PackageVersion , __FILE__ );
183  $Package->set_add_limitations( $AddLimitations );
184  }
185  catch( Exception $e )
186  {
187  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
188  }
189  }
190 
205  private function apply_add_limitations()
206  {
207  try
208  {
209  $Config = $this->CachedMultyFS->get_config( __FILE__ , 'cf_add_limitations' , 'cleaned' );
210 
211  if( $Config != '' )
212  {
213  $Config = explode( "\n" , $Config );
214 
215  foreach( $Config as $k => $ConfigLine )
216  {
217  $this->compile_config_line( $ConfigLine );
218  }
219  }
220  }
221  catch( Exception $e )
222  {
223  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
224  }
225  }
226 
245  private function setup( &$Config )
246  {
247  try
248  {
249  $this->DatabaseEncoding = $Config[ 2 ];
250  $this->DBLogging = intval( @$Config[ 3 ] ) === 1 ? true : false;
251  $this->TablenamePrefix = $this->DatabaseAccessObject->TablenamePrefix;
252  self::$Session = microtime( true );
253  $this->DatabaseAccessObject->query( 'SET AUTOCOMMIT = 0' );
254  $this->DatabaseAccessObject->query( 'SET NAMES utf8' );
255  $this->apply_add_limitations();
256  }
257  catch( Exception $e )
258  {
259  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
260  }
261  }
262 
281  function get_object()
282  {
283  try
284  {
285  if( $this->DatabaseAccessObject === false )
286  {
287  $Config = $this->CachedMultyFS->get_config( __FILE__ , 'cf_database_settings' );
288  $Config = explode( '#' , $Config );
289  $this->DatabaseAccessObject = get_package( $Config[ 0 ] , $Config[ 1 ] , __FILE__ );
290 
291  $this->DatabaseAccessObject->get_connection();
292 
293  $this->setup( $Config );
294  }
295 
296  return( $this->DatabaseAccessObject );
297  }
298  catch( Exception $e )
299  {
300  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
301  }
302  }
303 
326  function query( $Query )
327  {
328  try
329  {
330  $Start = microtime( true );
331  $this->QueryCounter++;
332  $DBO = $this->get_object();
333  $Query = str_replace( array( '[eq]' ) , array( '=' ) , $Query );
334 
335  if( $this->DatabaseEncoding != 'UTF-8' )
336  {
337  $Result = $this->Text->iconv(
338  $this->DatabaseEncoding , 'UTF-8' , $DBO->query( $this->Tags->compile_ultimix_tags( $Query ) )
339  );
340  }
341  else
342  {
343  $Result = $DBO->query( $this->Tags->compile_ultimix_tags( $Query ) );
344  }
345 
346  $this->DatabaseLogger->log_query( $Query , $Start , $this->DBLogging );
347 
348  return( $Result );
349  }
350  catch( Exception $e )
351  {
352  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
353  }
354  }
355 
374  function query_as( $theQueryMode )
375  {
376  try
377  {
378  $DBO = $this->get_object();
379  $DBO->query_as( $theQueryMode );
380  }
381  catch( Exception $e )
382  {
383  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
384  }
385  }
386 
417  function select( $What , $Tables , $Condition = '1 = 1' )
418  {
419  try
420  {
421  $this->QueryCounter++;
422  $DBO = $this->get_object();
423 
424  $Result = $DBO->select( $What , $Tables , $Condition );
425 
426  if( $this->DatabaseEncoding != 'UTF-8' )
427  {
428  $Result = $this->Text->iconv( $this->DatabaseEncoding , 'UTF-8' , $Result );
429  }
430 
431  return( $Result );
432  }
433  catch( Exception $e )
434  {
435  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
436  }
437  }
438 
461  function fetch_results( $Result )
462  {
463  try
464  {
465  $DBO = $this->get_object();
466 
467  $Result = $DBO->fetch_results( $Result );
468 
469  if( $this->DatabaseEncoding != 'UTF-8' )
470  {
471  $Result = $this->Text->iconv( $this->DatabaseEncoding , 'UTF-8' , $Result );
472  }
473 
474  return( $Result );
475  }
476  catch( Exception $e )
477  {
478  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
479  }
480  }
481 
508  function insert( $Table , $Fields , $Values )
509  {
510  try
511  {
512  $this->QueryCounter++;
513  $DBO = $this->get_object();
514  $DBO->insert( $Table , $Fields , $Values );
515  }
516  catch( Exception $e )
517  {
518  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
519  }
520  }
521 
544  function delete( $Table , $Condition = '1 = 1' )
545  {
546  try
547  {
548  $this->QueryCounter++;
549  $DBO = $this->get_object();
550  $DBO->delete( $Table , $Condition );
551  }
552  catch( Exception $e )
553  {
554  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
555  }
556  }
557 
588  function update( $Table , $Fields , $Values , $Condition = '1 = 1' )
589  {
590  try
591  {
592  $this->QueryCounter++;
593  $DBO = $this->get_object();
594  $DBO->update( $Table , $Fields , $Values , $Condition );
595  }
596  catch( Exception $e )
597  {
598  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
599  }
600  }
601 
624  function create( $Table , $FirstIndexField = 'id' )
625  {
626  try
627  {
628  $this->QueryCounter++;
629  $DBO = $this->get_object();
630  $DBO->create( $Table , $FirstIndexField );
631  }
632  catch( Exception $e )
633  {
634  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
635  }
636  }
637 
656  function drop( $Table )
657  {
658  try
659  {
660  $this->QueryCounter++;
661  $DBO = $this->get_object();
662  $DBO->drop( $Table );
663  }
664  catch( Exception $e )
665  {
666  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
667  }
668  }
669 
692  function lock( $Tables , $Modes )
693  {
694  try
695  {
696  $this->QueryCounter++;
697  $DBO = $this->get_object();
698  $DBO->lock( $Tables , $Modes );
699  }
700  catch( Exception $e )
701  {
702  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
703  }
704  }
705 
720  function unlock()
721  {
722  try
723  {
724  $this->QueryCounter++;
725  $DBO = $this->get_object();
726  $DBO->unlock();
727  }
728  catch( Exception $e )
729  {
730  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
731  }
732  }
733 
752  function savepoint( $Savepoint )
753  {
754  try
755  {
756  $this->QueryCounter++;
757  $DBO = $this->get_object();
758  $DBO->savepoint( $Savepoint );
759  }
760  catch( Exception $e )
761  {
762  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
763  }
764  }
765 
784  function rollback( $Savepoint )
785  {
786  try
787  {
788  $this->QueryCounter++;
789  $DBO = $this->get_object();
790  $DBO->rollback( $Savepoint );
791  }
792  catch( Exception $e )
793  {
794  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
795  }
796  }
797 
812  function transaction()
813  {
814  try
815  {
816  $this->QueryCounter++;
817  $DBO = $this->get_object();
818  $DBO->transaction();
819  }
820  catch( Exception $e )
821  {
822  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
823  }
824  }
825 
840  function commit()
841  {
842  try
843  {
844  $this->QueryCounter++;
845  $DBO = $this->get_object();
846  $DBO->commit();
847  }
848  catch( Exception $e )
849  {
850  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
851  }
852  }
853  }
854 
855 ?>