1: <?php
2:
3: /**
4: * Firebird database server access classes
5: *
6: * @package objSQL
7: * @version 3.3.0
8: * @author MT Jordan <mtjo62@gmail.com>
9: * @copyright 2013
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 firebird_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: $db_connection = ibase_connect( "{$this->obj_datasource[1]}:{$this->obj_datasource[4]}", $this->obj_datasource[2], $this->obj_datasource[3] );
66:
67: return ( is_resource( $db_connection ) ) ? $this->obj_connection = $db_connection : false;
68: }
69:
70: /**
71: * Closes connection to database server
72: *
73: * @access protected
74: * @return bool
75: */
76: protected function obj_db_close()
77: {
78: return ibase_close( $this->obj_connection );
79: }
80:
81: /**
82: * Returns error flag for current connection instance
83: *
84: * @access protected
85: * @return bool
86: */
87: protected function obj_db_error()
88: {
89: return ( ibase_errcode() != false ) ? true : false;
90: }
91:
92: /**
93: * Escapes string data for database insertion
94: *
95: * @access protected
96: * @param mixed $data
97: * @return mixed
98: */
99: protected function obj_db_escape_data( $data )
100: {
101: return str_replace( "'", "''", $data );
102: }
103:
104: /**
105: * Returns error message for current connection instance
106: *
107: * @access protected
108: * @return mixed
109: */
110: protected function obj_db_message()
111: {
112: return ( $this->obj_db_error() ) ? ibase_errmsg() : null;
113: }
114:
115: /**
116: * Returns database server information
117: *
118: * @access protected
119: * @return array
120: */
121: protected function obj_db_info()
122: {
123: $obj_service = ibase_service_attach( $this->obj_datasource[1], $this->obj_datasource[2], $this->obj_datasource[3] );
124: $obj_server_ver = ibase_server_info( $obj_service, IBASE_SVC_SERVER_VERSION );
125: ibase_service_detach( $obj_service );
126:
127: return array( $obj_server_ver, 'N/A', $this->obj_datasource[4] );
128: }
129:
130: /**
131: * Returns row count for named table with arguments
132: * Returns -1 if undetermined or failure
133: *
134: * @access protected
135: * @param str $table
136: * @param str $cols
137: * @param str $where
138: * @return int
139: */
140: protected function obj_db_rowcount( $table, $cols=false, $where=false )
141: {
142: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
143: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
144:
145: $query_stmt = ibase_query( $this->obj_connection, "SELECT COUNT($query_cols) FROM $table $query_where" );
146:
147: if ( !$query_stmt || !is_resource( $query_stmt ) )
148: return -1;
149:
150: while ( $num_rows = ibase_fetch_row( $query_stmt ) )
151: $rowcount = ( $num_rows[0] >= 0 ) ? $num_rows[0] : -1;
152:
153: ibase_free_result( $query_stmt );
154:
155: return $rowcount;
156: }
157: }
158:
159:
160: /*************************************************************************************************************
161: * End database connection class
162: ************************************************************************************************************/
163:
164:
165: /*************************************************************************************************************
166: * Begin database statement class
167: ************************************************************************************************************/
168:
169:
170: class firebird_statement
171: {
172: /**********************************************
173: * Internal variables
174: *********************************************/
175:
176: /**
177: * Database connection object
178: *
179: * @access private
180: * @var mixed
181: */
182: private $obj_connection;
183:
184: /**
185: * Query string
186: *
187: * @access private
188: * @var str
189: */
190: private $obj_query;
191:
192: /**
193: * Transaction resource
194: *
195: * @access private
196: * @var mixed
197: */
198: private $obj_trans_object;
199:
200: /**********************************************
201: * Class methods
202: *********************************************/
203:
204: /**
205: * Constructor
206: *
207: * @access public
208: * @param str $query
209: * @param mixed $connection
210: */
211: function __construct( $query, $connection, $dbtype, $transaction )
212: {
213: $this->obj_connection = $connection->obj_connection;
214: $this->obj_query = $query;
215: $this->obj_trans_object = $transaction;
216: }
217:
218: /**
219: * Executes general query and returns resultset object
220: *
221: * @access public
222: * @return mixed
223: */
224: public function obj_query_execute()
225: {
226: //if transaction is initiated, use transaction resource
227: if ( is_resource( $this->obj_trans_object ) || is_object( $this->obj_trans_object ) )
228: $query_stmt = ibase_query( $this->obj_trans_object, $this->obj_query );
229: else
230: $query_stmt = ibase_query( $this->obj_connection, $this->obj_query );
231:
232: return ( $query_stmt || is_resource( $query_stmt ) ) ? new firebird_resultset( $query_stmt, $this->obj_connection ) : false;
233: }
234: }
235:
236:
237: /*************************************************************************************************************
238: * End database statement class
239: ************************************************************************************************************/
240:
241:
242: /*************************************************************************************************************
243: * Begin database prepared statement class
244: ************************************************************************************************************/
245:
246:
247: class firebird_prepare
248: {
249: /**********************************************
250: * Internal variables
251: *********************************************/
252:
253: /**
254: * Database connection object
255: *
256: * @access private
257: * @var mixed
258: */
259: private $obj_connection;
260:
261: /**
262: * Prepared statement parameter values
263: *
264: * @access private
265: * @var array
266: */
267: private $obj_parameter = array();
268:
269: /**
270: * Prepared statement instance
271: *
272: * @access public
273: * @var bool
274: */
275: private $obj_prepare_instance;
276:
277: /**
278: * Statement resource
279: *
280: * @access private
281: * @var mixed
282: */
283: private $obj_query_stmt;
284:
285: /**
286: * Transaction resource
287: *
288: * @access private
289: * @var mixed
290: */
291: private $obj_trans_object;
292:
293: /**********************************************
294: * Class methods
295: *********************************************/
296:
297: /**
298: * Constructor
299: *
300: * @access public
301: * @param str $query
302: * @param mixed $connection
303: * @param str $dbtype
304: * @param mixed $params
305: * @param mixed $transaction
306: */
307: function __construct( $query, $connection, $dbtype=null, $params=null, $transaction=null )
308: {
309: $this->obj_connection = $connection->obj_connection;
310: $this->obj_prepare_init( $query, $transaction );
311: }
312:
313: /**
314: * Sets parameters and parameter types for prepared statement
315: *
316: * @access public
317: * @param mixed $param
318: */
319: public function obj_bind( $param )
320: {
321: $this->obj_parameter[] = $param;
322: }
323:
324: /**
325: * Destroys prepared statement object
326: *
327: * @access public
328: * @return bool
329: */
330: public function obj_close_statement()
331: {
332: return ibase_free_query( $this->obj_prepare_instance );
333: }
334:
335: /**
336: * Binds parameters, executes prepared statement and returns resultset object
337: *
338: * @access public
339: * @return mixed
340: */
341: public function obj_execute()
342: {
343: array_unshift( $this->obj_parameter, $this->obj_prepare_instance );
344:
345: $this->obj_query_stmt = call_user_func_array( 'ibase_execute', $this->obj_parameter );
346:
347: if ( $this->obj_query_stmt >= 0 || is_resource( $this->obj_query_stmt ) )
348: return new firebird_resultset( $this->obj_query_stmt, $this->obj_connection );
349: else
350: return false;
351: }
352:
353: /**
354: * Frees resultset memory from prepared statement object and resets binding parameters
355: *
356: * @access public
357: * @return bool
358: */
359: public function obj_free_statement()
360: {
361: $this->obj_parameter = array();
362:
363: if( is_resource( $this->obj_query_stmt ) )
364: return ibase_free_result( $this->obj_query_stmt );
365: elseif ( is_integer( $this->obj_query_stmt ) || $this->obj_query_stmt == true )
366: return true;
367: else
368: return false;
369: }
370:
371: /**
372: * Returns prepared statement instance
373: *
374: * @access private
375: * @param str $query
376: * @return bool
377: */
378: private function obj_prepare_init( $query, $transaction )
379: {
380: if ( is_object( $transaction ) || is_resource( $transaction ) )
381: return $this->obj_prepare_instance = ibase_prepare( $this->obj_connection, $transaction, $query );
382: else
383: return $this->obj_prepare_instance = ibase_prepare( $this->obj_connection, $query );
384: }
385: }
386:
387:
388: /*************************************************************************************************************
389: * End database prepared statement class
390: ************************************************************************************************************/
391:
392:
393: /*************************************************************************************************************
394: * Begin database resultset class
395: ************************************************************************************************************/
396:
397:
398: class firebird_resultset
399: {
400: /**********************************************
401: * Internal variables
402: *********************************************/
403:
404: /**
405: * Database connection object
406: *
407: * @access private
408: * @var mixed
409: */
410: private $obj_connection;
411:
412: /**
413: * Query record
414: *
415: * @access private
416: * @var array
417: */
418: private $obj_record = array();
419:
420: /**
421: * Query resultset object
422: *
423: * @access private
424: * @var mixed
425: */
426: private $obj_result = false;
427:
428: /**********************************************
429: * Class methods
430: *********************************************/
431: /**
432: * Constructor
433: *
434: * @access public
435: * @param mixed $result
436: * @param mixed $connection
437: */
438: public function __construct( $result, $connection )
439: {
440: $this->obj_connection = $connection;
441: $this->obj_result = $result;
442: }
443:
444: /**
445: * Returns number of affected rows from insert/delete/update query
446: * Returns -1 if undetermined or failure
447: *
448: * @access public
449: * @return int
450: */
451: public function obj_affected_rows()
452: {
453: $affected_rows = ( is_resource( $this->obj_result ) ) ? ibase_affected_rows( $this->obj_connection ) : $this->obj_result;
454:
455: return ( $affected_rows >= 0 ) ? $affected_rows : -1;
456: }
457:
458: /**
459: * Returns resultset object as associative array
460: *
461: * @access public
462: * @return mixed
463: */
464: public function obj_fetch_assoc()
465: {
466: $result = ibase_fetch_assoc( $this->obj_result );
467:
468: return ( is_array( $result ) && $result != false ) ? $this->obj_record = $result : null;
469: }
470:
471: /**
472: * Returns resultset object as numeric array
473: *
474: * @access public
475: * @return mixed
476: */
477: public function obj_fetch_num()
478: {
479: $result = ibase_fetch_row( $this->obj_result );
480:
481: return ( is_array( $result ) && $result != false ) ? $this->obj_record = $result : null;
482: }
483:
484: /**
485: * Returns resultset object as object
486: *
487: * @access public
488: * @return mixed
489: */
490:
491: public function obj_fetch_object()
492: {
493: $result = ibase_fetch_object( $this->obj_result );
494:
495: return ( is_object( $result ) && $result != false ) ? $this->obj_record = $result : null;
496: }
497:
498: /**
499: * Returns resultset record
500: *
501: * @access public
502: * @param mixed $field
503: * @return mixed
504: */
505: public function obj_field( $field )
506: {
507: if ( $this->obj_result )
508: {
509: //get_magic_quotes deprecated in php 5.4 - added for backwards compatibility
510: return ( get_magic_quotes_runtime() ) ? stripslashes( $this->obj_record[strtoupper( $field )] ) : $this->obj_record[strtoupper( $field )];
511: }
512: }
513:
514: /**
515: * Frees resultset memory and destroys resultset object
516: *
517: * @access public
518: * @return bool
519: */
520: public function obj_free_result()
521: {
522: $this->obj_record = array();
523:
524: return ibase_free_result( $this->obj_result );
525: }
526:
527: /**
528: * Returns number of fields from query
529: * Returns -1 if undetermined or failure
530: *
531: * @access public
532: * @return int
533: */
534: public function obj_num_fields()
535: {
536: $num_cols = ibase_num_fields( $this->obj_result );
537:
538: return ( $num_cols >= 0 ) ? $num_cols : -1;
539: }
540:
541: /**
542: * Returns number rows from query
543: * Returns -1 if undetermined or failure
544: *
545: * @access public
546: * @return int
547: */
548: public function obj_num_rows()
549: {
550: //Very inefficent - will timeout on larger recordsets
551: //Use the obj_row_count() method
552: $num_rows = 0;
553:
554: while ( ibase_fetch_row( $this->obj_result ) )
555: $num_rows++;
556:
557: return ( $num_rows >= 0 ) ? $num_rows : -1;
558: }
559: }
560:
561:
562: /*************************************************************************************************************
563: * End database resultset class
564: ************************************************************************************************************/
565:
566:
567: /*************************************************************************************************************
568: * Begin database transaction class
569: ************************************************************************************************************/
570:
571:
572: class firebird_transaction
573: {
574: /**********************************************
575: * Internal variables
576: *********************************************/
577:
578: /**
579: * Database connection instance
580: *
581: * @access private
582: * @var mixed
583: */
584: private $obj_connection;
585:
586: /**
587: * Transaction resource
588: *
589: * @access private
590: * @var mixed
591: */
592: public $obj_trans_object;
593:
594: /**********************************************
595: * Class methods
596: *********************************************/
597:
598: /**
599: * Constructor
600: *
601: * @access public
602: * @param mixed $connection
603: */
604: public function __construct( $connection )
605: {
606: $this->obj_connection = $connection;
607:
608: //turn off autocommit
609: $this->obj_trans_object = ibase_trans( $this->obj_connection );
610: }
611:
612: /**
613: * Commits transaction for current transaction instance
614: *
615: * @access public
616: * @return bool
617: */
618: public function obj_commit()
619: {
620: return ( is_resource( $this->obj_trans_object ) || is_object( $this->obj_trans_object ) ) ? ibase_commit( $this->obj_trans_object ) : false;
621: }
622:
623: /**
624: * Rollbacks transaction for current transaction instance
625: *
626: * @access public
627: * @param str $savepoint
628: * @return bool
629: */
630: public function obj_rollback( $savepoint=false )
631: {
632: if ( !$savepoint )
633: $rollback = ibase_rollback( $this->obj_trans_object );
634: else
635: $rollback = ibase_query( $this->obj_trans_object, "ROLLBACK TO SAVEPOINT $savepoint" );
636:
637: return ( $rollback || is_resource( $rollback ) || is_object( $rollback ) ) ? true : false;
638: }
639:
640: /**
641: * Creates transaction savepoint for current transaction instance
642: *
643: * @access public
644: * @param str $savepoint
645: * @return bool
646: */
647: public function obj_savepoint( $savepoint )
648: {
649: return ( ibase_query( $this->obj_trans_object, "SAVEPOINT $savepoint" ) ) ? true : false;
650: }
651: }
652:
653:
654: /*************************************************************************************************************
655: * End database transaction class
656: ************************************************************************************************************/
657:
658:
659: ?>