ultimix
postgre_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 
27 
38  var $Connection = false;
39 
50  static $LockedTables = false;
51 
62  static $LockModes = false;
63 
77 
88  var $Config = false;
89 
100  var $Host;
106 
125  function connect( $ConfigRow )
126  {
127  try
128  {
129  $Config = explode( "#" , $ConfigRow );
130  $this->Host = $Config[ 0 ];
131  $this->Username = $Config[ 1 ];
132  $this->Password = $Config[ 2 ];
133  $this->Database = $Config[ 3 ];
134  $this->TablenamePrefix = $Config[ 4 ];
135 
136  $this->Connection = @pg_pconnect(
137  'host='.$this->Host.' user='.$this->Username.' password='.$this->Password.' dbname='.$this->Database
138  );
139  }
140  catch( Exception $e )
141  {
142  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
143  }
144  }
145 
164  function get_connection( $ForceReconnect = false )
165  {
166  try
167  {
168  if( $this->Connection === false || $ForceReconnect ===true )
169  {
170  if( $this->Connection !== false )
171  {
172  pg_close( $this->Connection );
173  }
174  $DBConfigSet = get_package( 'database::db_config_set' , 'last' , __FILE__ );
175  $DBConfigSet->load_config( dirname( __FILE__ ).'/conf/cf_postgre_database' );
176  $this->Connection = $DBConfigSet->connect( $this );
177  if( pg_last_error() !== false )
178  {
179  throw(
180  new Exception(
181  'An error occured while setting connection to the database '.pg_last_error()
182  )
183  );
184  }
185  }
186  return( $this->Connection );
187  }
188  catch( Exception $e )
189  {
190  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
191  }
192  }
193 
212  function query( $Query )
213  {
214  try
215  {
216  $Connection = $this->get_connection();
217 
218  $Start = microtime( true );
219  $Query = str_replace( 'umx_' , $this->TablenamePrefix , $Query );
220  $Result = pg_query( $Connection , $Query );
221  if( $Connection->errno !== 0 )
222  {
223  throw( new Exception( 'An error occured while query execution '.$Connection->error ) );
224  }
225  $End = microtime( true );
226 
227  if( $this->DBLogging )
228  {
229  $this->insert(
230  'umx_actions' ,
231  'action_name , time , info' , "'".self::$QueryName."' , ".( $End - $Start )." , '".
232  htmlspecialchars( $Query , ENT_QUOTES )."'"
233  );
234  }
235 
236  return( $Result );
237  }
238  catch( Exception $e )
239  {
240  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
241  }
242  }
243 
262  function query_as( $theQueryMode )
263  {
264  try
265  {
266  self::$QueryMode = $theQueryMode;
267  }
268  catch( Exception $e )
269  {
270  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
271  }
272  }
273 
304  function select( $What , $Tables , $Condition = '1 = 1' )
305  {
306  try
307  {
308  $Result = $this->query( "SELECT $What FROM $Tables WHERE $Condition" );
309 
310  return( $this->fetch_results( $Result ) );
311  }
312  catch( Exception $e )
313  {
314  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
315  }
316  }
317 
340  function fetch_results( $Result )
341  {
342  try
343  {
344  $RetValues = array();
345 
346  for( $i = 0; $i < $Result->num_rows ; $i++ )
347  {
348  if( self::$QueryMode == DB_ASSOC_ARRAY )
349  {
350  $RetValues [] = $Result->fetch_array( MYSQLI_ASSOC );
351  }
352  elseif( self::$QueryMode == DB_OBJECT )
353  {
354  $RetValues [] = $Result->fetch_object();
355  }
356  }
357 
358  $Result->close();
359 
360  return( $RetValues );
361  }
362  catch( Exception $e )
363  {
364  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
365  }
366  }
367 
394  function insert( $Table , $Fields , $Values )
395  {
396  try
397  {
398  $this->query( "INSERT INTO $Table ( $Fields ) VALUES ( $Values )" );
399  }
400  catch( Exception $e )
401  {
402  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
403  }
404  }
405 
428  function delete( $Table , $Condition = '1 = 1' )
429  {
430  try
431  {
432  $this->query( "DELETE FROM $Table WHERE $Condition" );
433  }
434  catch( Exception $e )
435  {
436  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
437  }
438  }
439 
470  function update( $Table , $Fields , $Values , $Condition = '1 = 1' )
471  {
472  try
473  {
474  $SubQuery = '';
475 
476  for( $i = 0 ; $i < count( $Fields ) - 1 ; $i++ )
477  {
478  $SubQuery .= $Fields[ $i ].' = '.$Values[ $i ].' , ';
479  }
480  $SubQuery .= $Fields[ count( $Fields ) - 1 ].' = '.$Values[ count( $Fields ) - 1 ];
481 
482  $this->query( "UPDATE $Table SET $SubQuery WHERE $Condition" );
483  }
484  catch( Exception $e )
485  {
486  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
487  }
488  }
489 
512  function create( $Table , $FirstIndexField = 'id' )
513  {
514  try
515  {
516  $this->query(
517  "CREATE TABLE `$Table` ( `$FirstIndexField` INTEGER UNSIGNED NOT NULL DEFAULT NULL ".
518  "AUTO_INCREMENT , PRIMARY KEY ( `$FirstIndexField` ) )"
519  );
520  }
521  catch( Exception $e )
522  {
523  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
524  }
525  }
526 
545  function drop( $Table )
546  {
547  try
548  {
549  $this->query( "DROP TABLE $Table" );
550  }
551  catch( Exception $e )
552  {
553  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
554  }
555  }
556 
587  function add_column( $Table , $ColumnName , $Type , $Mode = 'NOT NULL AFTER `id`' )
588  {
589  try
590  {
591  $this->query( "ALTER TABLE `$Table` ADD COLUMN `$ColumnName` $Type $Mode" );
592  }
593  catch( Exception $e )
594  {
595  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
596  }
597  }
598 
621  function drop_column( $Table , $ColumnName )
622  {
623  try
624  {
625  $this->query( "ALTER TABLE `$Table` DROP COLUMN `$ColumnName`" );
626  }
627  catch( Exception $e )
628  {
629  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
630  }
631  }
632 
655  function lock( $Tables , $Modes )
656  {
657  try
658  {
659  throw( new Exception( "Table locking unavailable" ) );
660  }
661  catch( Exception $e )
662  {
663  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
664  }
665  }
666 
681  function unlock()
682  {
683  try
684  {
685  throw( new Exception( "Table locking unavailable" ) );
686  }
687  catch( Exception $e )
688  {
689  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
690  }
691  }
692 
711  function savepoint( $Savepoint )
712  {
713  try
714  {
715  $this->query( 'SAVEPOINT '.$Savepoint );
716  }
717  catch( Exception $e )
718  {
719  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
720  }
721  }
722 
741  function rollback( $Savepoint )
742  {
743  try
744  {
745  $this->query( 'ROLLBACK TO SAVEPOINT '.$Savepoint );
746  }
747  catch( Exception $e )
748  {
749  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
750  }
751  }
752 
767  function transaction()
768  {
769  try
770  {
771  $this->query( 'START TRANSACTION' );
772  }
773  catch( Exception $e )
774  {
775  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
776  }
777  }
778 
793  function commit()
794  {
795  try
796  {
797  $this->query( 'COMMIT' );
798  }
799  catch( Exception $e )
800  {
801  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
802  }
803  }
804  }
805 
806 ?>