Overview

Packages

  • objSQL

Documentation

  • objSQL Connection
  • objSQL Error Handling
  • General Queries
  • Prepared Statements
  • Transactions
  • Resultset Methods
  • Helper Method - Select Query
  • Helper Method - Update Query
  • Helper Method - Insert Query
  • Helper Method - Delete Query
  • Helper Method - Paging Query
  • Utility Methods

Classes

  • mysql_connection
  • mysql_prepare
  • mysql_resultset
  • mysql_statement
  • mysql_transaction
  • objSQL
  • pgsql_connection
  • pgsql_prepare
  • pgsql_resultset
  • pgsql_statement
  • pgsql_transaction
  • sqlite3_connection
  • sqlite3_prepare
  • sqlite3_resultset
  • sqlite3_statement
  • sqlite3_transaction
  • sqlsrv_connection
  • sqlsrv_prepare
  • sqlsrv_resultset
  • sqlsrv_statement
  • sqlsrv_transaction
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * MySQL database server access classes
  5:  *
  6:  * @package objSQL
  7:  * @version 3.0.0
  8:  * @author MT Jordan <mtjo62@gmail.com>
  9:  * @copyright 2012
 10:  * @license zlib/libpng
 11:  * @link http://objsql.sourceforge.net 
 12:  */
 13: 
 14:  
 15: /*************************************************************************************************************
 16:  * Begin database connection/utility class
 17:  ************************************************************************************************************/
 18:  
 19:   
 20: class mysql_connection extends obj_access
 21: {
 22:     /**********************************************
 23:      * Internal variables
 24:      *********************************************/
 25: 
 26:     /**
 27:      * Database connection object
 28:      *
 29:      * @access private
 30:      * @var    mixed
 31:      */
 32:     private $obj_connection = false;
 33:     
 34:     /**
 35:      * Database connection information
 36:      *
 37:      * @access private
 38:      * @var array
 39:      */
 40:     private $obj_datasource;
 41: 
 42:     /**********************************************
 43:      * Class methods
 44:      *********************************************/
 45: 
 46:     /**
 47:      * Constructor
 48:      *
 49:      * @access public
 50:      * @param  array $datasource
 51:      */
 52:     public function __construct( $datasource )
 53:     {
 54:         $this->obj_datasource = $datasource;
 55:     }
 56: 
 57:     /**
 58:      * Returns database connection object
 59:      *
 60:      * @access protected
 61:      * @return mixed
 62:      */
 63:     protected function obj_db_connection()
 64:     {
 65:         if ( ( array_key_exists( 5, $this->obj_datasource ) ) && is_numeric( trim( $this->obj_datasource[5] ) ) )
 66:             $db_connection = new mysqli( $this->obj_datasource[1], $this->obj_datasource[2], $this->obj_datasource[3], $this->obj_datasource[4], $this->obj_datasource[5] );
 67:         else
 68:             $db_connection = new mysqli( $this->obj_datasource[1], $this->obj_datasource[2], $this->obj_datasource[3], $this->obj_datasource[4] );    
 69:         
 70:        return ( is_object( $db_connection ) ) ? $this->obj_connection = $db_connection : false;        
 71:     }
 72: 
 73:     /**
 74:      * Closes connection to database server
 75:      *
 76:      * @access protected
 77:      * @return bool
 78:      */
 79:     protected function obj_db_close()
 80:     {
 81:         return $this->obj_connection->close();
 82:     }
 83:     
 84:     /**
 85:      * Returns error flag for current connection instance
 86:      *
 87:      * @access protected
 88:      * @return bool
 89:      */
 90:     protected function obj_db_error()
 91:     {
 92:         return ( $this->obj_connection->errno ) ? true : false;
 93:     }
 94:     
 95:     /**
 96:      * Escapes string data for database insertion
 97:      *
 98:      * @access protected
 99:      * @param  mixed $data
100:      * @return mixed
101:      */
102:     protected function obj_db_escape_data( $data )
103:     {
104:         return $this->obj_connection->real_escape_string( $data );
105:     }
106:     
107:     /**
108:      * Returns error message for current connection instance
109:      *
110:      * @access protected
111:      * @return mixed
112:      */
113:     protected function obj_db_message()
114:     {
115:         return ( $this->obj_db_error() ) ? $this->obj_connection->error : null;
116:     }
117:     
118:     /**
119:      * Returns database server information
120:      *
121:      * @access protected
122:      * @return array
123:      */
124:     protected function obj_db_info()
125:     {
126:         return array( $this->obj_connection->server_version, $this->obj_connection->character_set_name(), $this->obj_datasource[4] );
127:     }
128: }
129: 
130: 
131: /*************************************************************************************************************
132:  * End database connection class
133:  ************************************************************************************************************/
134: 
135:  
136: /*************************************************************************************************************
137:  * Begin database statement class
138:  ************************************************************************************************************/
139:  
140: 
141: class mysql_statement
142: { 
143:     /**********************************************
144:      * Internal variables
145:      *********************************************/
146: 
147:     /**
148:      * Database connection object
149:      *
150:      * @access private
151:      * @var    mixed
152:      */
153:     private $obj_connection;
154: 
155:     /**
156:      * Query string
157:      *
158:      * @access private
159:      * @var    str
160:      */
161:     private $obj_query;
162:     
163:     /**********************************************
164:      * Class methods
165:      *********************************************/
166: 
167:     /**
168:      * Constructor
169:      *
170:      * @access public
171:      * @param  str   $query
172:      * @param  mixed $connection
173:      */
174:     function __construct( $query, $connection )
175:     {
176:         $this->obj_connection = $connection->obj_connection;
177:         $this->obj_query = $query;
178:     }
179:     
180:     /**
181:      * Executes general query and returns resultset object
182:      *
183:      * @access public
184:      * @return mixed
185:      */
186:     public function obj_query_execute()
187:     {
188:         $query_stmt = $this->obj_connection->query( $this->obj_query );
189:         
190:         return ( $query_stmt || is_object( $query_stmt ) ) ? new mysql_resultset( $query_stmt, $this->obj_connection ) : false;
191:     }
192:  }
193: 
194: 
195: /*************************************************************************************************************
196:  * End database statement class
197:  ************************************************************************************************************/
198:  
199:  
200: /*************************************************************************************************************
201:  * Begin database prepared statement class
202:  ************************************************************************************************************/
203:  
204: 
205: class mysql_prepare
206: {
207:     /**********************************************
208:      * Internal variables
209:      *********************************************/
210: 
211:     /**
212:      * Database connection object
213:      *
214:      * @access private
215:      * @var    mixed
216:      */
217:     private $obj_connection;
218: 
219:     /**
220:      * Prepared statement parameter values
221:      *
222:      * @access private
223:      * @var    array
224:      */
225:     private $obj_parameter = array();
226:     
227:     /**
228:      * Prepared statement parameter types
229:      *
230:      * @access private
231:      * @var    array
232:      */
233:     private $obj_parameter_type = array();
234: 
235:     /**
236:      * Prepared statement instance
237:      *
238:      * @access public
239:      * @var    bool
240:      */
241:     private $obj_prepare_instance;
242:     
243:     /**
244:      * Prepared statement parameter types
245:      *
246:      * @access private
247:      * @var    array
248:      */
249:     private $obj_query_type;
250:     
251:     /**********************************************
252:      * Class methods
253:      *********************************************/
254: 
255:     /**
256:      * Constructor
257:      *
258:      * @access public
259:      * @param  str   $query 
260:      * @param  mixed $connection
261:      */
262:     function __construct( $connection, $query ) 
263:     {
264:         $this->obj_connection = $connection->obj_connection;
265:         $this->obj_prepare_init( $query );
266:     }
267:     
268:     /**
269:      * Sets parameters and parameter types for prepared statement
270:      *
271:      * @access public
272:      * @param  mixed $param
273:      */
274:     public function obj_bind( $param )
275:     {
276:         $this->obj_parameter_type[] = $this->obj_param_type( $param );
277:         $this->obj_parameter[] = $param;
278:     }
279:     
280:     /**
281:      * Destroys prepared statement object
282:      *
283:      * @access public
284:      * @return bool 
285:      */
286:     public function obj_close_statement()
287:     {
288:         return $this->obj_prepare_instance->close();
289:     }
290:    
291:     /**
292:      * Binds parameters, executes prepared statement and returns resultset object
293:      *
294:      * @access public
295:      * @return mixed
296:      */
297:     public function obj_execute()
298:     {
299:         call_user_func_array( 'mysqli_stmt_bind_param', array_merge( array( $this->obj_prepare_instance, implode( '', $this->obj_parameter_type ) ), $this->obj_param_values() ) );
300:         $query_stmt = $this->obj_prepare_instance->execute();
301:         $query_result = $this->obj_prepare_instance->get_result();
302:         
303:         if ( $query_stmt )
304:         {
305:             if ( !$query_result )
306:                 return new mysql_resultset( $query_stmt, $this->obj_connection );
307:             else
308:                 return new mysql_resultset( $query_result, $this->obj_connection );
309:         }
310:         else
311:             return false;
312:     }
313:     
314:     /**
315:      * Frees resultset memory from prepared statement object and resets binding parameters
316:      *
317:      * @access public
318:      * @return bool 
319:      */
320:     public function obj_free_statement()
321:     {
322:         $this->obj_parameter = array();
323:         $this->obj_parameter_type = array();
324:         
325:         return $this->obj_prepare_instance->reset();
326:     }
327:     
328:     /**
329:      * Returns parameter datatype
330:      *
331:      * @access private
332:      * @param  mixed $param
333:      * @return mixed
334:      */
335:     private function obj_param_type( $param )
336:     {
337:         $param = trim( $param );
338:         
339:         if ( is_numeric( $param ) )
340:         {
341:             if ( substr_count( $param, '.' ) )
342:                 return 'd';
343:             else
344:                 return 'i';
345:         }
346:         elseif ( is_string( $param ) )
347:             return 's';
348:         else
349:             return 'b';
350:     }
351:     
352:     /**
353:      * Returns by reference bound parameter values
354:      *
355:      * @access private
356:      * @return array
357:      */
358:     private function obj_param_values()
359:     { 
360:         $param = array(); 
361:         $value = null;
362:         
363:         foreach( $this->obj_parameter as $key => $value ) 
364:             $param[$key] = &$this->obj_parameter[$key]; 
365:          
366:         return $param; 
367:     }
368:     
369:     /**
370:      * Returns prepared statement instance
371:      *
372:      * @access private
373:      * @param  str $query
374:      * @return bool
375:      */
376:     private function obj_prepare_init( $query )
377:     {
378:         return $this->obj_prepare_instance = $this->obj_connection->prepare( $query );
379:     }
380: }
381: 
382: 
383: /*************************************************************************************************************
384:  * End database prepared statement class
385:  ************************************************************************************************************/
386: 
387:  
388: /*************************************************************************************************************
389:  * Begin database resultset class
390:  ************************************************************************************************************/
391:  
392: 
393: class mysql_resultset
394: {
395:     /**********************************************
396:      * Internal variables
397:      *********************************************/
398: 
399:     /**
400:      * Database connection object
401:      *
402:      * @access private
403:      * @var    mixed
404:      */
405:     private $obj_connection;
406: 
407:     /**
408:      * Query record
409:      *
410:      * @access private
411:      * @var    array
412:      */
413:     private $obj_record = array();
414: 
415:     /**
416:      * Query resultset object
417:      *
418:      * @access private
419:      * @var    mixed
420:      */
421:     private $obj_result = false;
422: 
423:     /**********************************************
424:      * Class methods
425:      *********************************************/
426:     /**
427:      * Constructor
428:      *
429:      * @access public
430:      * @param  mixed $result
431:      * @param  mixed $connection
432:      */
433:     public function __construct( $result, $connection )
434:     {
435:         $this->obj_connection = $connection;
436:         $this->obj_result = $result;
437:     }
438: 
439:     /**
440:      * Returns number of affected rows from insert/delete/update query
441:      * Returns -1 if undetermined or failure
442:      *
443:      * @access public
444:      * @return int
445:      */
446:     public function obj_affected_rows()
447:     {
448:         $affected_rows = $this->obj_connection->affected_rows;
449:         
450:         return ( $affected_rows !== null && $affected_rows >= 0 ) ? $affected_rows : -1;
451:     }
452: 
453:     /**
454:      * Returns resultset object as associative array
455:      *
456:      * @access public
457:      * @return mixed
458:      */
459:     public function obj_fetch_assoc()
460:     {
461:         $result = $this->obj_result->fetch_assoc();
462:             
463:         return ( is_array( $result ) && $result !== null ) ? $this->obj_record = $result : null;
464:     }
465: 
466:     /**
467:      * Returns resultset object as numeric array
468:      *
469:      * @access public
470:      * @return mixed
471:      */
472:     public function obj_fetch_num()
473:     {
474:         $result = $this->obj_result->fetch_row();
475:             
476:         return ( is_array( $result ) && $result !== null ) ? $this->obj_record = $result : null;
477:     }
478:     
479:     /**
480:      * Returns resultset object as object
481:      *
482:      * @access public
483:      * @return mixed
484:      */
485:      
486:     public function obj_fetch_object()
487:     {
488:         $result = $this->obj_result->fetch_object();
489:             
490:         return ( is_object( $result ) && $result !== null ) ? $this->obj_record = $result : null;
491:     }
492: 
493:     /**
494:      * Returns resultset record
495:      *
496:      * @access public
497:      * @param  mixed $field
498:      * @return mixed
499:      */
500:     public function obj_field( $field )
501:     {
502:         if ( $this->obj_result )
503:         {
504:             //get_magic_quotes deprecated in php 5.4 - added for backwards compatibility
505:             return ( get_magic_quotes_runtime() ) ? stripslashes( $this->obj_record[$field] ) : $this->obj_record[$field];
506:         }
507:     }
508: 
509:     /**
510:      * Frees resultset memory and destroys resultset object
511:      *
512:      * @access public
513:      * @return bool
514:      */
515:     public function obj_free_result()
516:     {
517:         $this->obj_result->free();
518:         $this->obj_record = array();
519:             
520:         return ( !is_object( $this->obj_result = null ) ) ? true : false;
521:     }
522:     
523:     /**
524:      * Returns number of fields from query
525:      * Returns -1 if undetermined or failure
526:      *
527:      * @access public
528:      * @return int
529:      */
530:     public function obj_num_fields()
531:     {
532:         $num_cols = $this->obj_result->field_count;
533:         
534:         return ( $num_cols >= 0 ) ? $num_cols : -1; 
535:     }
536: 
537:     /**
538:      * Returns number rows from query
539:      * Returns -1 if undetermined or failure
540:      * 
541:      * @access public
542:      * @return int
543:      */
544:     public function obj_num_rows()
545:     {
546:         $num_rows = $this->obj_result->num_rows;
547:         
548:         return ( $num_rows >= 0 ) ? $num_rows : -1;
549:     }
550: }
551: 
552: 
553: /*************************************************************************************************************
554:  * End database resultset class
555:  ************************************************************************************************************/
556: 
557:  
558: /*************************************************************************************************************
559:  * Begin database transaction class
560:  ************************************************************************************************************/
561: 
562: 
563: class mysql_transaction
564: {
565:     /**
566:      * Database connection instance
567:      *
568:      * @access private
569:      * @var    mixed
570:      */
571:     private $obj_connection;
572:     
573:     /**********************************************
574:      * Class methods
575:      *********************************************/
576: 
577:     /**
578:      * Constructor
579:      *
580:      * @access public
581:      * @param  mixed $connection
582:      */
583:     public function __construct( $connection )
584:     {
585:         $this->obj_connection = $connection;
586: 
587:         //turn off autocommit
588:         $this->obj_autocommit_mode( $this->obj_connection->autocommit( false ) );
589:     }
590:     
591:     /**
592:      * Returns autocommit mode
593:      *
594:      * @access private
595:      * @param  bool $trans 
596:      * @return bool
597:      */
598:     private function obj_autocommit_mode( $trans )
599:     {
600:         return ( $trans ) ? true : false;
601:     }
602:     
603:     /**
604:      * Commits transaction for current transaction instance
605:      *
606:      * @access public
607:      * @return bool
608:      */
609:     public function obj_commit()
610:     {
611:         return $this->obj_connection->commit();
612:     }
613: 
614:     /**
615:      * Rollbacks transaction for current transaction instance
616:      *
617:      * @access public
618:      * @param  str $savepoint
619:      * @return bool
620:      */
621:     public function obj_rollback( $savepoint=false )
622:     {
623:         if ( !$savepoint ) 
624:             $rollback = $this->obj_connection->rollback();
625:         else 
626:             $rollback = $this->obj_connection->query( "ROLLBACK TO SAVEPOINT $savepoint" );
627:         
628:         return ( $rollback ) ? true : false;
629:     }
630: 
631:     /**
632:      * Creates transaction savepoint for current transaction instance
633:      *
634:      * @access public
635:      * @param  str $savepoint
636:      * @return bool
637:      */
638:     public function obj_savepoint( $savepoint )
639:     {
640:         return ( $this->obj_connection->query( "SAVEPOINT $savepoint" ) ) ? true : false;
641:     }
642: }
643: 
644: 
645: /*************************************************************************************************************
646:  * End database transaction class
647:  ************************************************************************************************************/
648:  
649:  
650: ?>
objSQL 3.0.0 API documentation generated by ApiGen 2.8.0