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