ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

mssql_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 mssql_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 $Config = false;
89
100 var $Host;
101 var $Username;
102 var $Password;
103 var $Database;
104 var $TablenamePrefix;
105 var $DBLogging;
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 = mssql_pconnect( $this->Host , $this->Username , $this->Password );
137 mssql_select_db( $this->Database , $this->Connection );
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 mssql_close( $this->Connection );
172 }
173 $DBConfigSet = get_package( 'database::db_config_set' , 'last' , __FILE__ );
174 $DBConfigSet->load_config( dirname( __FILE__ ).'/conf/cf_mssql_database' );
175 $this->Connection = $DBConfigSet->connect( $this );
176 if( mssql_get_last_message() !== false )
177 {
178 throw(
179 new Exception(
180 'An error occured while setting connection to the database '.mssql_get_last_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 $Result = $Connection->query( $Query );
220
221 if( $Connection->errno !== 0 )
222 {
223 throw( new Exception( 'An error occured while query execution '.$Connection->error ) );
224 }
225
226 return( $Result );
227 }
228 catch( Exception $e )
229 {
230 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
231 }
232 }
233
252 function query_as( $theQueryMode )
253 {
254 try
255 {
256 self::$QueryMode = $theQueryMode;
257 }
258 catch( Exception $e )
259 {
260 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
261 }
262 }
263
294 function select( $What , $Tables , $Condition = '1 = 1' )
295 {
296 try
297 {
298 $Result = $this->query( "SELECT $What FROM $Tables WHERE $Condition" );
299
300 return( $this->fetch_results( $Result ) );
301 }
302 catch( Exception $e )
303 {
304 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
305 }
306 }
307
330 function fetch_results( $Result )
331 {
332 try
333 {
334 $RetValues = array();
335
336 for( $i = 0; $i < $Result->num_rows ; $i++ )
337 {
338 if( self::$QueryMode == DB_ASSOC_ARRAY )
339 {
340 $RetValues [] = $Result->fetch_array( MYSQLI_ASSOC );
341 }
342 elseif( self::$QueryMode == DB_OBJECT )
343 {
344 $RetValues [] = $Result->fetch_object();
345 }
346 }
347
348 $Result->close();
349
350 return( $RetValues );
351 }
352 catch( Exception $e )
353 {
354 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
355 }
356 }
357
384 function insert( $Table , $Fields , $Values )
385 {
386 try
387 {
388 $this->query( "INSERT INTO $Table ( $Fields ) VALUES ( $Values )" );
389 }
390 catch( Exception $e )
391 {
392 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
393 }
394 }
395
418 function delete( $Table , $Condition = '1 = 1' )
419 {
420 try
421 {
422 $this->query( "DELETE FROM $Table WHERE $Condition" );
423 }
424 catch( Exception $e )
425 {
426 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
427 }
428 }
429
460 function update( $Table , $Fields , $Values , $Condition = '1 = 1' )
461 {
462 try
463 {
464 $SubQuery = '';
465
466 for( $i = 0 ; $i < count( $Fields ) - 1 ; $i++ )
467 {
468 $SubQuery .= $Fields[ $i ].' = '.$Values[ $i ].' , ';
469 }
470 $SubQuery .= $Fields[ count( $Fields ) - 1 ].' = '.$Values[ count( $Fields ) - 1 ];
471
472 $this->query( "UPDATE $Table SET $SubQuery WHERE $Condition" );
473 }
474 catch( Exception $e )
475 {
476 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
477 }
478 }
479
502 function create( $Table , $FirstIndexField = 'id' )
503 {
504 try
505 {
506 $this->query(
507 "CREATE TABLE `$Table` ( `$FirstIndexField` INTEGER UNSIGNED NOT NULL DEFAULT NULL ".
508 "AUTO_INCREMENT , PRIMARY KEY ( `$FirstIndexField` ) )"
509 );
510 }
511 catch( Exception $e )
512 {
513 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
514 }
515 }
516
535 function drop( $Table )
536 {
537 try
538 {
539 $this->query( "DROP TABLE $Table" );
540 }
541 catch( Exception $e )
542 {
543 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
544 }
545 }
546
577 function add_column( $Table , $ColumnName , $Type , $Mode = 'NOT NULL AFTER `id`' )
578 {
579 try
580 {
581 $this->query( "ALTER TABLE `$Table` ADD COLUMN `$ColumnName` $Type $Mode" );
582 }
583 catch( Exception $e )
584 {
585 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
586 }
587 }
588
611 function drop_column( $Table , $ColumnName )
612 {
613 try
614 {
615 $this->query( "ALTER TABLE `$Table` DROP COLUMN `$ColumnName`" );
616 }
617 catch( Exception $e )
618 {
619 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
620 }
621 }
622
645 function lock( $Tables , $Modes )
646 {
647 try
648 {
649 throw( new Exception( "Table locking unavailable" ) );
650 }
651 catch( Exception $e )
652 {
653 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
654 }
655 }
656
671 function unlock()
672 {
673 try
674 {
675 throw( new Exception( "Table locking unavailable" ) );
676 }
677 catch( Exception $e )
678 {
679 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
680 }
681 }
682
701 function savepoint( $Savepoint )
702 {
703 try
704 {
705 $this->query( 'SAVEPOINT '.$Savepoint );
706 }
707 catch( Exception $e )
708 {
709 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
710 }
711 }
712
731 function rollback( $Savepoint )
732 {
733 try
734 {
735 $this->query( 'ROLLBACK TO SAVEPOINT '.$Savepoint );
736 }
737 catch( Exception $e )
738 {
739 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
740 }
741 }
742
757 function transaction()
758 {
759 try
760 {
761 $this->query( 'START TRANSACTION' );
762 }
763 catch( Exception $e )
764 {
765 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
766 }
767 }
768
783 function commit()
784 {
785 try
786 {
787 $this->query( 'COMMIT' );
788 }
789 catch( Exception $e )
790 {
791 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
792 }
793 }
794 }
795
796 ?>