1: <?php
2:
3: /**
4: * Cubrid database server access classes
5: *
6: * @package objSQL
7: * @version 3.2.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 cubrid_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 = cubrid_connect( $this->obj_datasource[1], $this->obj_datasource[5], $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 cubrid_disconnect( $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 ( cubrid_error_code() !== 0 ) ? 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 cubrid_real_escape_string( $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() ) ? cubrid_error_msg() : 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: return array( cubrid_get_server_info( $this->obj_connection ), cubrid_get_charset( $this->obj_connection ), $this->obj_datasource[4] );
124: }
125:
126: /**
127: * Returns row count for named table with arguments
128: * Returns -1 if undetermined or failure
129: *
130: * @access protected
131: * @param str $table
132: * @param str $cols
133: * @param str $where
134: * @return int
135: */
136: protected function obj_db_rowcount( $table, $cols=false, $where=false )
137: {
138: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
139: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
140:
141: $query_stmt = cubrid_execute( $this->obj_connection, "SELECT COUNT($query_cols) FROM $table $query_where", CUBRID_EXEC_QUERY_ALL );
142:
143: if ( !is_resource( $query_stmt ) )
144: return -1;
145:
146: while ( $num_rows = cubrid_fetch( $query_stmt, CUBRID_NUM ) )
147: $rowcount = ( $num_rows[0] >= 0 ) ? $num_rows[0] : -1;
148:
149: cubrid_close_request( $query_stmt );
150:
151: return $rowcount;
152: }
153: }
154:
155:
156: /*************************************************************************************************************
157: * End database connection class
158: ************************************************************************************************************/
159:
160:
161: /*************************************************************************************************************
162: * Begin database statement class
163: ************************************************************************************************************/
164:
165:
166: class cubrid_statement
167: {
168: /**********************************************
169: * Internal variables
170: *********************************************/
171:
172: /**
173: * Database connection object
174: *
175: * @access private
176: * @var mixed
177: */
178: private $obj_connection;
179:
180: /**
181: * Query string
182: *
183: * @access private
184: * @var str
185: */
186: private $obj_query;
187:
188: /**********************************************
189: * Class methods
190: *********************************************/
191:
192: /**
193: * Constructor
194: *
195: * @access public
196: * @param str $query
197: * @param mixed $connection
198: */
199: function __construct( $query, $connection )
200: {
201: $this->obj_connection = $connection->obj_connection;
202: $this->obj_query = $query;
203: }
204:
205: /**
206: * Executes general query and returns resultset object
207: *
208: * @access public
209: * @return mixed
210: */
211: public function obj_query_execute()
212: {
213: $query_stmt = cubrid_execute( $this->obj_connection, $this->obj_query, CUBRID_EXEC_QUERY_ALL );
214:
215: return ( is_resource( $query_stmt ) ) ? new cubrid_resultset( $query_stmt, $this->obj_connection ) : false;
216: }
217: }
218:
219:
220: /*************************************************************************************************************
221: * End database statement class
222: ************************************************************************************************************/
223:
224:
225: /*************************************************************************************************************
226: * Begin database prepared statement class
227: ************************************************************************************************************/
228:
229:
230: class cubrid_prepare
231: {
232: /**********************************************
233: * Internal variables
234: *********************************************/
235:
236: /**
237: * Database connection object
238: *
239: * @access private
240: * @var mixed
241: */
242: private $obj_connection;
243:
244: /**
245: * Set obj_bind parameter counter
246: *
247: * @access private
248: * @var int
249: */
250: private $obj_parameter_cnt = 0;
251:
252: /**
253: * Prepared statement instance
254: *
255: * @access public
256: * @var bool
257: */
258: private $obj_prepare_instance;
259:
260: /**********************************************
261: * Class methods
262: *********************************************/
263:
264: /**
265: * Constructor
266: *
267: * @access public
268: * @param str $query
269: * @param mixed $connection
270: */
271: function __construct( $query, $connection )
272: {
273: $this->obj_connection = $connection->obj_connection;
274: $this->obj_prepare_init( $query );
275: }
276:
277: /**
278: * Sets parameters for prepared statement
279: *
280: * @access public
281: * @param mixed $param
282: */
283: public function obj_bind( $param )
284: {
285: $this->obj_parameter_cnt++;
286:
287: return cubrid_bind( $this->obj_prepare_instance, $this->obj_parameter_cnt, $param );
288: }
289:
290: /**
291: * Destroys prepared statement object
292: *
293: * @access public
294: * @return bool
295: */
296: public function obj_close_statement()
297: {
298: return ( cubrid_close_prepare( $this->obj_prepare_instance ) ) ? true : false;
299: }
300:
301: /**
302: * Binds parameters, executes prepared statement and returns resultset object
303: *
304: * @access public
305: * @return mixed
306: */
307: public function obj_execute()
308: {
309: $query_stmt = cubrid_execute( $this->obj_prepare_instance );
310:
311: if ( $query_stmt )
312: return new cubrid_resultset( $this->obj_prepare_instance, $this->obj_connection );
313: else
314: return false;
315: }
316:
317: /**
318: * Frees resultset memory from prepared statement object and resets binding parameters
319: *
320: * @access public
321: * @return bool
322: */
323: public function obj_free_statement()
324: {
325: $this->obj_parameter_cnt = 0;
326:
327: return cubrid_free_result( $this->obj_prepare_instance );
328: }
329:
330: /**
331: * Returns prepared statement instance
332: *
333: * @access private
334: * @param str $query
335: * @return bool
336: */
337: private function obj_prepare_init( $query )
338: {
339: $prepare_instance = cubrid_prepare( $this->obj_connection, $query );
340:
341: return ( is_resource( $prepare_instance ) ) ? $this->obj_prepare_instance = $prepare_instance : false;
342: }
343: }
344:
345:
346: /*************************************************************************************************************
347: * End database prepared statement class
348: ************************************************************************************************************/
349:
350:
351: /*************************************************************************************************************
352: * Begin database resultset class
353: ************************************************************************************************************/
354:
355:
356: class cubrid_resultset
357: {
358: /**********************************************
359: * Internal variables
360: *********************************************/
361:
362: /**
363: * Database connection object
364: *
365: * @access private
366: * @var mixed
367: */
368: private $obj_connection;
369:
370: /**
371: * Query record
372: *
373: * @access private
374: * @var array
375: */
376: private $obj_record = array();
377:
378: /**
379: * Query resultset object
380: *
381: * @access private
382: * @var mixed
383: */
384: private $obj_result = false;
385:
386: /**********************************************
387: * Class methods
388: *********************************************/
389: /**
390: * Constructor
391: *
392: * @access public
393: * @param mixed $result
394: * @param mixed $connection
395: */
396: public function __construct( $result, $connection )
397: {
398: $this->obj_connection = $connection;
399: $this->obj_result = $result;
400: }
401:
402: /**
403: * Returns number of affected rows from insert/delete/update query
404: * Returns -1 if undetermined or failure
405: *
406: * @access public
407: * @return int
408: */
409: public function obj_affected_rows()
410: {
411: $affected_rows = cubrid_affected_rows( $this->obj_connection );
412:
413: return ( $affected_rows !== false && $affected_rows >= 0 ) ? $affected_rows : -1;
414: }
415:
416: /**
417: * Returns resultset object as associative array
418: *
419: * @access public
420: * @return mixed
421: */
422: public function obj_fetch_assoc()
423: {
424: $result = cubrid_fetch( $this->obj_result, CUBRID_ASSOC );
425:
426: return ( is_array( $result ) && $result !== null && $result !== false ) ? $this->obj_record = $result : null;
427: }
428:
429: /**
430: * Returns resultset object as numeric array
431: *
432: * @access public
433: * @return mixed
434: */
435: public function obj_fetch_num()
436: {
437: $result = cubrid_fetch( $this->obj_result, CUBRID_NUM );
438:
439: return ( is_array( $result ) && $result !== null && $result !== false ) ? $this->obj_record = $result : null;
440: }
441:
442: /**
443: * Returns resultset object as object
444: *
445: * @access public
446: * @return mixed
447: */
448:
449: public function obj_fetch_object()
450: {
451: $result = cubrid_fetch( $this->obj_result, CUBRID_OBJECT );
452:
453: return ( is_object( $result ) && $result !== null && $result !== false ) ? $this->obj_record = $result : null;
454: }
455:
456: /**
457: * Returns resultset record
458: *
459: * @access public
460: * @param mixed $field
461: * @return mixed
462: */
463: public function obj_field( $field )
464: {
465: if ( $this->obj_result )
466: {
467: //get_magic_quotes deprecated in php 5.4 - added for backwards compatibility
468: return ( get_magic_quotes_runtime() ) ? stripslashes( $this->obj_record[$field] ) : $this->obj_record[$field];
469: }
470: }
471:
472: /**
473: * Frees resultset memory and destroys resultset object
474: *
475: * @access public
476: * @return bool
477: */
478: public function obj_free_result()
479: {
480: $this->obj_record = array();
481:
482: return ( cubrid_close_request( $this->obj_result ) ) ? true : false;
483: }
484:
485: /**
486: * Returns number of fields from query
487: * Returns -1 if undetermined or failure
488: *
489: * @access public
490: * @return int
491: */
492: public function obj_num_fields()
493: {
494: $num_cols = cubrid_num_cols( $this->obj_result );
495:
496: return ( $num_cols !== false && $num_cols >= 0 ) ? $num_cols : -1;
497: }
498:
499: /**
500: * Returns number rows from query
501: * Returns -1 if undetermined or failure
502: *
503: * @access public
504: * @return int
505: */
506: public function obj_num_rows()
507: {
508: $num_rows = cubrid_num_rows( $this->obj_result );
509:
510: return ( $num_rows !== false && $num_rows >= 0 ) ? $num_rows : -1;
511: }
512: }
513:
514:
515: /*************************************************************************************************************
516: * End database resultset class
517: ************************************************************************************************************/
518:
519:
520: /*************************************************************************************************************
521: * Begin database transaction class
522: ************************************************************************************************************/
523:
524:
525: class cubrid_transaction
526: {
527: /**********************************************
528: * Internal variables
529: *********************************************/
530:
531: /**
532: * Database connection instance
533: *
534: * @access private
535: * @var mixed
536: */
537: private $obj_connection;
538:
539: /**********************************************
540: * Class methods
541: *********************************************/
542:
543: /**
544: * Constructor
545: *
546: * @access public
547: * @param mixed $connection
548: */
549: public function __construct( $connection )
550: {
551: $this->obj_connection = $connection;
552:
553: //turn off autocommit
554: cubrid_set_autocommit( $this->obj_connection, CUBRID_AUTOCOMMIT_FALSE );
555: }
556:
557: /**
558: * Commits transaction for current transaction instance
559: *
560: * @access public
561: * @return bool
562: */
563: public function obj_commit()
564: {
565: return cubrid_commit( $this->obj_connection );
566: }
567:
568: /**
569: * Rollbacks transaction for current transaction instance
570: *
571: * @access public
572: * @param str $savepoint
573: * @return bool
574: */
575: public function obj_rollback( $savepoint=false )
576: {
577: if ( !$savepoint )
578: $rollback = cubrid_rollback( $this->obj_connection );
579: else
580: $rollback = cubrid_execute( $this->obj_connection, "ROLLBACK WORK TO SAVEPOINT $savepoint" );
581:
582: return ( $rollback || is_resource( $rollback ) ) ? true : false;
583: }
584:
585: /**
586: * Creates transaction savepoint for current transaction instance
587: *
588: * @access public
589: * @param str $savepoint
590: * @return bool
591: */
592: public function obj_savepoint( $savepoint )
593: {
594: $rollback = cubrid_execute( $this->obj_connection, "SAVEPOINT $savepoint" );
595:
596: return ( is_resource( $rollback ) ) ? true : false;
597: }
598: }
599:
600:
601: /*************************************************************************************************************
602: * End database transaction class
603: ************************************************************************************************************/
604:
605:
606: ?>