ultimix
oracle_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 
30 
41  var $Connection = false;
42 
53  static $LockedTables = false;
54 
65  static $LockModes = false;
66 
80 
91  var $Config = false;
92 
107 
126  function connect( $ConfigRow )
127  {
128  try
129  {
130  $Config = explode( "#" , $ConfigRow );
131  $this->Host = $Config[ 0 ];
132  $this->Username = $Config[ 1 ];
133  $this->Password = $Config[ 2 ];
134  $this->Database = $Config[ 3 ];
135  $this->TablenamePrefix = $Config[ 4 ];
136 
137  $this->Connection = @oci_pconnect( $this->Username , $this->Password );
138  }
139  catch( Exception $e )
140  {
141  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
142  }
143  }
144 
163  function get_connection( $ForceReconnect = false )
164  {
165  try
166  {
167  if( $this->Connection === false || $ForceReconnect ===true )
168  {
169  if( $this->Connection !== false )
170  {
171  oci_close( $this->Connection );
172  }
173  $DBConfigSet = get_package( 'database::db_config_set' , 'last' , __FILE__ );
174  $DBConfigSet->load_config( dirname( __FILE__ ).'/conf/cf_mysql_database' );
175  $this->Connection = $DBConfigSet->connect( $this );
176  if( oci_error() !== false )
177  {
178  throw(
179  new Exception(
180  'An error occured while setting connection to the database '.$Error[ 'message' ]
181  )
182  );
183  }
184  }
185  return( $this->Connection );
186  }
187  catch( Exception $e )
188  {
189  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
190  }
191  }
192 
211  function query( $Query )
212  {
213  try
214  {
215  $Connection = $this->get_connection();
216 
217  $Query = str_replace( 'umx_' , $this->TablenamePrefix , $Query );
218 
219  $Stmt = oci_parse( $Connection , $Query );
220  oci_execute( $Stmt );
221 
222  if( ( $Error = oci_error() ) !== false )
223  {
224  throw( new Exception( 'An error occured while query execution '.$Error[ 'message' ] ) );
225  }
226 
227  return( $Result );
228  }
229  catch( Exception $e )
230  {
231  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
232  }
233  }
234 
253  function query_as( $theQueryMode )
254  {
255  try
256  {
257  self::$QueryMode = $theQueryMode;
258  }
259  catch( Exception $e )
260  {
261  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
262  }
263  }
264 
295  function select( $What , $Tables , $Condition = '1 = 1' )
296  {
297  try
298  {
299  $Result = $this->query( "SELECT $What FROM $Tables WHERE $Condition" );
300 
301  return( $this->fetch_results( $Result ) );
302  }
303  catch( Exception $e )
304  {
305  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
306  }
307  }
308 
331  function fetch_results( $Result )
332  {
333  try
334  {
335  $RetValues = array();
336 
337  for( $i = 0; $i < $Result->num_rows ; $i++ )
338  {
339  if( self::$QueryMode == DB_ASSOC_ARRAY )
340  {
341  $RetValues [] = $Result->fetch_array( MYSQLI_ASSOC );
342  }
343  elseif( self::$QueryMode == DB_OBJECT )
344  {
345  $RetValues [] = $Result->fetch_object();
346  }
347  }
348 
349  $Result->close();
350 
351  return( $RetValues );
352  }
353  catch( Exception $e )
354  {
355  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
356  }
357  }
358 
385  function insert( $Table , $Fields , $Values )
386  {
387  try
388  {
389  $this->query( "INSERT INTO $Table ( $Fields ) VALUES ( $Values )" );
390  }
391  catch( Exception $e )
392  {
393  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
394  }
395  }
396 
419  function delete( $Table , $Condition = '1 = 1' )
420  {
421  try
422  {
423  $this->query( "DELETE FROM $Table WHERE $Condition" );
424  }
425  catch( Exception $e )
426  {
427  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
428  }
429  }
430 
461  function update( $Table , $Fields , $Values , $Condition = '1 = 1' )
462  {
463  try
464  {
465  $SubQuery = '';
466 
467  for( $i = 0 ; $i < count( $Fields ) - 1 ; $i++ )
468  {
469  $SubQuery .= $Fields[ $i ].' = '.$Values[ $i ].' , ';
470  }
471  $SubQuery .= $Fields[ count( $Fields ) - 1 ].' = '.$Values[ count( $Fields ) - 1 ];
472 
473  $this->query( "UPDATE $Table SET $SubQuery WHERE $Condition" );
474  }
475  catch( Exception $e )
476  {
477  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
478  }
479  }
480 
503  function create( $Table , $FirstIndexField = 'id' )
504  {
505  try
506  {
507  $this->query(
508  "CREATE TABLE `$Table` ( `$FirstIndexField` INTEGER UNSIGNED NOT NULL DEFAULT ".
509  "NULL AUTO_INCREMENT , PRIMARY KEY ( `$FirstIndexField` ) )"
510  );
511  }
512  catch( Exception $e )
513  {
514  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
515  }
516  }
517 
536  function drop( $Table )
537  {
538  try
539  {
540  $this->query( "DROP TABLE $Table" );
541  }
542  catch( Exception $e )
543  {
544  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
545  }
546  }
547 
578  function add_column( $Table , $ColumnName , $Type , $Mode = 'NOT NULL AFTER `id`' )
579  {
580  try
581  {
582  $this->query( "ALTER TABLE `$Table` ADD COLUMN `$ColumnName` $Type $Mode" );
583  }
584  catch( Exception $e )
585  {
586  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
587  }
588  }
589 
612  function drop_column( $Table , $ColumnName )
613  {
614  try
615  {
616  $this->query( "ALTER TABLE `$Table` DROP COLUMN `$ColumnName`" );
617  }
618  catch( Exception $e )
619  {
620  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
621  }
622  }
623 
646  function lock( $Tables , $Modes )
647  {
648  try
649  {
650  throw( new Exception( "Table locking unavailable" ) );
651  }
652  catch( Exception $e )
653  {
654  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
655  }
656  }
657 
672  function unlock()
673  {
674  try
675  {
676  throw( new Exception( "Table locking unavailable" ) );
677  }
678  catch( Exception $e )
679  {
680  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
681  }
682  }
683 
702  function savepoint( $Savepoint )
703  {
704  try
705  {
706  $this->query( 'SAVEPOINT '.$Savepoint );
707  }
708  catch( Exception $e )
709  {
710  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
711  }
712  }
713 
732  function rollback( $Savepoint )
733  {
734  try
735  {
736  oci_rollback( $this->get_connection() );
737  }
738  catch( Exception $e )
739  {
740  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
741  }
742  }
743 
758  function transaction()
759  {
760  try
761  {
762  $this->query( 'START TRANSACTION' );
763  }
764  catch( Exception $e )
765  {
766  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
767  }
768  }
769 
784  function commit()
785  {
786  try
787  {
788  oci_commit( $this->get_connection() );
789  }
790  catch( Exception $e )
791  {
792  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
793  }
794  }
795  }
796 
797 ?>