ultimix
database_algorithms.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( 'CREATION_DATE' , 1 );
17  define( 'MODIFICATION_DATE' , 2 );
18  define( 'PUBLICATION_DATE' , 4 );
19  define( 'OWNER' , 8 );
20 
32 
43  var $Database = false;
44  var $Security = false;
45  var $UserAlgorithms = false;
46 
61  function __construct()
62  {
63  try
64  {
65  $this->Database = get_package( 'database' , 'last' , __FILE__ );
66  $this->Security = get_package( 'security' , 'last' , __FILE__ );
67  }
68  catch( Exception $e )
69  {
70  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
71  }
72  }
73 
92  function set( $Field , $Value )
93  {
94  $this->$Field = $Value;
95  }
96 
119  function execute_query_set( $SetOfQueries )
120  {
121  try
122  {
123  $SetOfQueries = explode( ";\n" , $SetOfQueries );
124 
125  foreach( $SetOfQueries as $Query )
126  {
127  $this->Database->query( $Query );
128  $this->Database->commit();
129  }
130  }
131  catch( Exception $e )
132  {
133  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
134  }
135  }
136 
151  function try_connect()
152  {
153  try
154  {
155  $Obj = $this->Database->get_object( true );
156 
157  $Obj->get_connection();
158 
159  return( true );
160  }
161  catch( Exception $e )
162  {
163  return( false );
164  }
165  }
166 
209  function select_condition( $Start = false , $Limit = false , $Field = false ,
210  $Order = false , $Condition = '1 = 1' , $NativeTable = false )
211  {
212  try
213  {
214  if( $Start !== false )
215  {
216  $Start = $this->Security->get( $Start , 'integer' );
217  $Limit = $this->Security->get( $Limit , 'integer' );
218 
219  if( $Field === false )
220  {
221  $Field = $NativeTable !== false ? "$NativeTable.id" : 'id';
222  $Order = 'DESC';
223  }
224  else
225  {
226  $Field = $this->Security->get( $Field , 'command' );
227  $Order = $this->Security->get( $Order , 'command' );
228  $Order = ( $Order === 'ascending' || $Order === 'ASC' ) ? 'ASC' : 'DESC';
229  }
230 
231  $Condition = "$Condition ORDER BY $Field $Order LIMIT $Start , $Limit";
232  }
233 
234  return( $Condition );
235  }
236  catch( Exception $e )
237  {
238  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
239  }
240  }
241 
272  function add_security_fields( $Fields , $Values , $AddFields = 0 )
273  {
274  try
275  {
276  if( $AddFields & OWNER )
277  {
278  $UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
279  $Fields [] = 'owner';
280  $Values [] = $UserAlgorithms->get_id();
281  }
282 
283  return( array( $Fields , $Values ) );
284  }
285  catch( Exception $e )
286  {
287  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
288  }
289  }
290 
321  function add_special_fields( $Fields , $Values , $AddFields = 0 )
322  {
323  try
324  {
325  if( $AddFields & CREATION_DATE )
326  {
327  $Fields [] = 'creation_date';
328  $Values [] = 'NOW()';
329  }
330  if( $AddFields & MODIFICATION_DATE )
331  {
332  $Fields [] = 'modification_date';
333  $Values [] = 'NOW()';
334  }
335  if( $AddFields & PUBLICATION_DATE )
336  {
337  $Fields [] = 'publication_date';
338  $Values [] = 'NOW()';
339  }
340 
341  return( $this->add_security_fields( $Fields , $Values , $AddFields ) );
342  }
343  catch( Exception $e )
344  {
345  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
346  }
347  }
348 
375  function compile_fields_values( &$Record , $AddFields = 0 )
376  {
377  try
378  {
379  $Fields = array();
380  $Values = array();
381 
382  foreach( $Record as $f => $v )
383  {
384  if( $v !== '_no_update' && $f !== 'master_id' && $f !== 'master_type' )
385  {
386  $Fields [] = $f;
387 
388  if( is_array( $v ) || is_object( $v ) )
389  {
390  $Values [] = "'".serialize( $v )."'";
391  }
392  else
393  {
394  $Values [] = "'".$v."'";
395  }
396  }
397  }
398 
399  return( $this->add_special_fields( $Fields , $Values , $AddFields ) );
400  }
401  catch( Exception $e )
402  {
403  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
404  }
405  }
406 
437  function create( $TableName , &$Fields , &$Values )
438  {
439  try
440  {
441  $this->Database->lock( array( $TableName ) , array( 'WRITE' ) );
442 
443  $this->Database->insert( $TableName , implode( ',' , $Fields ) , implode( ',' , $Values ) );
444  $this->Database->commit();
445 
446  $id = $this->Database->select( '*' , $TableName , '1 = 1 ORDER by id DESC LIMIT 0 , 1' );
447  $id = get_field( $id[ 0 ] , 'id' );
448 
449  $this->Database->unlock();
450 
451  return( $id );
452  }
453  catch( Exception $e )
454  {
455  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
456  }
457  }
458 
485  function count_of_records( $TableName , $Condition = '1 = 1' )
486  {
487  try
488  {
489  $CountOfRecords = $this->Database->select( 'COUNT( * ) AS count_of_records' , $TableName , $Condition );
490 
491  $CountOfRecords = get_field( $CountOfRecords[ 0 ] , 'count_of_records' );
492 
493  return( $CountOfRecords );
494  }
495  catch( Exception $e )
496  {
497  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
498  }
499  }
500 
527  function record_exists( $TableName , $Condition = '1 = 1' )
528  {
529  try
530  {
531  return( $this->count_of_records( $TableName , $Condition ) > 0 );
532  }
533  catch( Exception $e )
534  {
535  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
536  }
537  }
538 
558  {
559  try
560  {
561  $this->Database->query_as( DB_ARRAY );
562  $Result = $this->Database->query( 'SHOW TABLES' );
563  $ListOfTables = $this->Database->fetch_results( $Result );
564 
565  foreach( $ListOfTables as $i => $TableInfo )
566  {
567  $ListOfTables[ $i ] = $TableInfo[ 0 ];
568  }
569 
570  return( $ListOfTables );
571  }
572  catch( Exception $e )
573  {
574  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
575  }
576  }
577 
600  function get_list_of_fields( $TableName )
601  {
602  try
603  {
604  $TableName = $this->Security->get( $TableName , 'command' );
605 
606  $this->Database->query_as( DB_OBJECT );
607  $Result = $this->Database->query( "SHOW FIELDS FROM `$TableName`" );
608 
609  return( $this->Database->fetch_results( $Result ) );
610  }
611  catch( Exception $e )
612  {
613  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
614  }
615  }
616  }
617 
618 ?>