1: <?php
2:
3: /**
4: * SQLite3 database server access classes
5: *
6: * @package objSQL
7: * @version 3.0.1
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 sqlite3_connection extends obj_access
21: {
22: /**********************************************
23: * Internal variables
24: *********************************************/
25:
26: /**
27: * Database connection instance
28: *
29: * @access private
30: * @var mixed
31: */
32: private $obj_connection;
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[4];
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 = new SQLite3( $this->obj_datasource );
66:
67: return ( is_object( $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 $this->obj_connection->close();
79: }
80:
81: /**
82: * Returns error flag for current connection object
83: *
84: * @access protected
85: * @return bool
86: */
87: protected function obj_db_error()
88: {
89: return ( $this->obj_connection->lastErrorCode() ) ? 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 $this->obj_connection->escapeString( $data );
102: }
103:
104: /**
105: * Returns error message for current connection object
106: *
107: * @access protected
108: * @return str
109: */
110: protected function obj_db_message()
111: {
112: return ( $this->obj_db_error() ) ? $this->obj_connection->lastErrorMsg() : 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( $this->obj_connection->version()['versionString'], 'utf8', $this->obj_datasource );
124: }
125: }
126:
127:
128: /*************************************************************************************************************
129: * End database connection class
130: ************************************************************************************************************/
131:
132:
133: /*************************************************************************************************************
134: * Begin database statement class
135: ************************************************************************************************************/
136:
137:
138: class sqlite3_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 = $this->obj_connection->query( $this->obj_query );
186:
187: return ( $query_stmt || is_object( $query_stmt ) ) ? new sqlite3_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 sqlite3_prepare
203: {
204: /**********************************************
205: * Internal variables
206: *********************************************/
207:
208: /**
209: * Set obj_bind parameter counter
210: *
211: * @access private
212: * @var int
213: */
214: private $obj_parameter_cnt = 0;
215:
216: /**
217: * Database connection object
218: *
219: * @access private
220: * @var mixed
221: */
222: private $obj_connection;
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: * Set parameters for prepared statement
251: *
252: * @access public
253: * @param mixed $param
254: * @return bool
255: */
256: public function obj_bind( $param )
257: {
258: $this->obj_parameter_cnt++;
259:
260: return $this->obj_prepare_instance->bindValue( $this->obj_parameter_cnt, $param );
261: }
262:
263: /**
264: * Destroys prepared statement object
265: *
266: * @access public
267: * @return bool
268: */
269: public function obj_close_statement()
270: {
271: return ( $this->obj_prepare_instance->close() ) ? true : false;
272: }
273:
274: /**
275: * Executes prepared statement and returns resultset object
276: *
277: * @access public
278: * @return mixed
279: */
280: public function obj_execute()
281: {
282: $query_stmt = $this->obj_prepare_instance->execute();
283:
284: return ( is_object( $query_stmt ) ) ? new sqlite3_resultset( $query_stmt, $this->obj_connection ) : false;
285: }
286:
287: /**
288: * Resets prepared statement object and binding parameters
289: *
290: * @access public
291: * @return bool
292: */
293: public function obj_free_statement()
294: {
295: $this->obj_parameter_cnt = 0;
296: $this->obj_prepare_instance->clear();
297:
298: return $this->obj_prepare_instance->reset();
299: }
300:
301: /**
302: * Returns prepared statement object
303: *
304: * @access private
305: * @param str $query
306: * @return mixed
307: */
308: private function obj_prepare_init( $query )
309: {
310: $prepare_instance = $this->obj_connection->prepare( $query );
311:
312: return ( is_object( $prepare_instance ) ) ? $this->obj_prepare_instance = $prepare_instance : false;
313: }
314: }
315:
316:
317: /*************************************************************************************************************
318: * End database prepared statement class
319: ************************************************************************************************************/
320:
321:
322: /*************************************************************************************************************
323: * Begin database resultset class
324: ************************************************************************************************************/
325:
326:
327: class sqlite3_resultset
328: {
329: /**********************************************
330: * Internal variables
331: *********************************************/
332:
333: /**
334: * Database connection object
335: *
336: * @access private
337: * @var mixed
338: */
339: private $obj_connection;
340:
341: /**
342: * Query record
343: *
344: * @access private
345: * @var array
346: */
347: private $obj_record = array();
348:
349: /**
350: * Query resultset object
351: *
352: * @access private
353: * @var mixed
354: */
355: private $obj_result;
356:
357: /**********************************************
358: * Class methods
359: *********************************************/
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: * Return 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 = $this->obj_connection->changes();
384:
385: return ( $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 = $this->obj_result->fetchArray( SQLITE3_ASSOC );
397:
398: return ( is_array( $result ) && $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 = $this->obj_result->fetchArray( SQLITE3_NUM );
410:
411: return ( is_array( $result ) && $result !== false ) ? $this->obj_record = $result : null;
412: }
413:
414: /**
415: * Return resultset object as object
416: *
417: * @access public
418: * @return mixed
419: */
420: public function obj_fetch_object()
421: {
422: $result = $this->obj_result->fetchArray( SQLITE3_ASSOC );
423:
424: return ( is_array( $result ) && $result !== false ) ? $this->obj_record = ( object )$result : null;
425: }
426:
427: /**
428: * Returns resultset record
429: *
430: * @access public
431: * @param mixed $field
432: * @return mixed
433: */
434: public function obj_field( $field )
435: {
436: //get_magic_quotes deprecated in php 5.4 - added for backwards compatibility
437: return ( get_magic_quotes_runtime() ) ? stripslashes( $this->obj_record[$field] ) : $this->obj_record[$field];
438: }
439:
440: /**
441: * Frees resultset memory and destroys resultset object
442: *
443: * @access public
444: * @return bool
445: */
446: public function obj_free_result()
447: {
448: $this->obj_record = array();
449:
450: return ( $this->obj_result->finalize() ) ? true : false;
451: }
452:
453: /**
454: * Returns number of fields from query
455: * Returns -1 if undetermined or failure
456: *
457: * @access public
458: * @return int
459: */
460: public function obj_num_fields()
461: {
462: $num_cols = $this->obj_result->numColumns();
463:
464: return ( $num_cols >= 0 ) ? $num_cols : -1;
465: }
466:
467: /**
468: * Return number of rows from query
469: * Returns -1 if undetermined or failure
470: *
471: * @access public
472: * @return int
473: */
474: public function obj_num_rows()
475: {
476: $row_cnt = 0;
477:
478: while ( $this->obj_result->fetchArray( SQLITE3_ASSOC ) )
479: $row_cnt++;
480:
481: return ( $row_cnt >= 0 ) ? $row_cnt : -1;
482: }
483: }
484:
485:
486: /*************************************************************************************************************
487: * End database resultset class
488: ************************************************************************************************************/
489:
490:
491: /*************************************************************************************************************
492: * Begin database transaction class
493: ************************************************************************************************************/
494:
495:
496: class sqlite3_transaction
497: {
498: /**********************************************
499: * Internal variables
500: *********************************************/
501:
502: /**
503: * Database connection instance
504: *
505: * @access private
506: * @var mixed
507: */
508: private $obj_connection;
509:
510: /**********************************************
511: * Class methods
512: *********************************************/
513:
514: /**
515: * Constructor
516: *
517: * @access public
518: * @param mixed $connection
519: */
520: public function __construct( $connection )
521: {
522: $this->obj_connection = $connection;
523:
524: //turn off autocommit
525: $this->obj_autocommit_mode( $this->obj_connection->exec( 'BEGIN' ) );
526: }
527:
528: /**
529: * Returns autocommit mode
530: *
531: * @access private
532: * @param bool $query
533: * @return bool
534: */
535: private function obj_autocommit_mode( $query )
536: {
537: return ( $query ) ? true : false;
538: }
539:
540: /**
541: * Commits transaction for current transaction instance
542: *
543: * @access public
544: * @return bool
545: */
546: public function obj_commit()
547: {
548: return $this->obj_connection->exec( 'COMMIT' );
549: }
550:
551: /**
552: * Rollbacks transaction for current transaction instance
553: *
554: * @access public
555: * @param str $savepoint
556: * @return bool
557: */
558: public function obj_rollback( $savepoint=false )
559: {
560: $rollback = ( !$savepoint ) ? 'ROLLBACK' : "ROLLBACK TO $savepoint";
561:
562: return $this->obj_connection->exec( $rollback );
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: return $this->obj_connection->exec( "SAVEPOINT $savepoint" );
575: }
576: }
577:
578:
579: /*************************************************************************************************************
580: * End database transaction class
581: ************************************************************************************************************/
582:
583:
584: ?>