1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: class objSQL extends obj_access
15: {
16: 17: 18:
19:
20: 21: 22: 23: 24: 25:
26: public $obj_connection = false;
27:
28: 29: 30: 31: 32: 33:
34: private $obj_db_array = array( 'mysql',
35: 'pgsql',
36: 'sqlite3',
37: 'sqlsrv' );
38:
39: 40: 41: 42: 43: 44:
45: private $obj_db_type;
46:
47: 48: 49: 50: 51: 52:
53: private $obj_instance;
54:
55: 56: 57: 58: 59: 60:
61: private $obj_prepare;
62:
63: 64: 65: 66: 67: 68:
69: private $obj_statement;
70:
71: 72: 73: 74: 75: 76:
77: private $obj_transaction;
78:
79: 80: 81: 82: 83: 84:
85: private $obj_version = '3.0.1';
86:
87: 88: 89:
90:
91: 92: 93: 94: 95: 96:
97: public function __construct( $datasource )
98: {
99: $this->obj_helper_datasource( $datasource );
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function obj_close()
109: {
110: return $this->obj_instance->obj_db_close();
111: }
112:
113: 114: 115: 116: 117: 118: 119: 120:
121: public function obj_delete( $table, $where=false )
122: {
123: $query_stmt = new $this->obj_statement( $this->obj_helper_delete( $table, $where ), $this );
124:
125: return $query_stmt->obj_query_execute();
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function obj_error()
135: {
136: return $this->obj_instance->obj_db_error();
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function obj_error_message()
146: {
147: return $this->obj_instance->obj_db_message();
148: }
149:
150: 151: 152: 153: 154: 155: 156:
157: public function obj_escape( $data )
158: {
159: return $this->obj_instance->obj_db_escape_data( $data );
160: }
161:
162: 163: 164: 165: 166: 167:
168: public function obj_info()
169: {
170: return array( 'OBJSQL_VERSION' => $this->obj_version,
171: 'DATABASE_NAME' => $this->obj_instance->obj_db_info()[2],
172: 'DATABASE_TYPE' => $this->obj_db_type,
173: 'DATABASE_VERSION' => $this->obj_instance->obj_db_info()[0],
174: 'DATABASE_CHARSET' => $this->obj_instance->obj_db_info()[1],
175: 'PHP_VERSION' => phpversion() );
176: }
177:
178: 179: 180: 181: 182: 183: 184: 185:
186: public function obj_insert( $table, $data_array )
187: {
188:
189: $query_stmt = new $this->obj_statement( $this->obj_helper_insert( $table, $data_array ), $this );
190:
191: return $query_stmt->obj_query_execute();
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205:
206: public function obj_paging( $table, $cols=false, $where=false, $order_by=false, $limit=1, $offset=1 )
207: {
208:
209: $query_offset = ( is_numeric( $offset ) && $offset > 0 ) ? ( int )$offset : 1;
210: $query_limit = ( is_numeric( $limit ) && $limit > 0 ) ? ( int )$limit : 1;
211:
212: $query_where = ( !trim( $where ) ) ? '' : $where;
213:
214: $result = $this->obj_select( $table, '*', $query_where );
215: $last_page = ceil( $result->obj_num_rows()/$query_limit );
216: $result->obj_free_result();
217:
218: $query_stmt = new $this->obj_statement( $this->obj_helper_paging( $table, $cols, $where, $order_by, $query_limit, $query_offset ), $this );
219:
220: return array( $query_stmt->obj_query_execute(), $last_page );
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230:
231: public function obj_prepare_statement( $query, $param_vars=false )
232: {
233: return new $this->obj_prepare( $this, $query, $param_vars );
234: }
235:
236: 237: 238: 239: 240: 241: 242:
243: public function obj_query( $query )
244: {
245: $query_stmt = new $this->obj_statement( $query, $this );
246:
247: return $query_stmt->obj_query_execute();
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260:
261: public function obj_select( $table, $cols=false, $where=false, $order_by=false, $sort_order=false )
262: {
263: $query_stmt = new $this->obj_statement( $this->obj_helper_select( $table, $cols, $where, $order_by, $sort_order ), $this );
264:
265: return $query_stmt->obj_query_execute();
266: }
267:
268: 269: 270: 271: 272: 273:
274: public function obj_transaction()
275: {
276: return new $this->obj_transaction( $this->obj_connection );
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286: 287:
288: public function obj_update( $table, $data_array, $where=false )
289: {
290:
291: $query_stmt = new $this->obj_statement( $this->obj_helper_update( $table, $data_array, $where ), $this );
292:
293: return $query_stmt->obj_query_execute();
294: }
295:
296: 297: 298:
299:
300: 301: 302: 303: 304: 305: 306:
307: private function obj_helper_datasource( $datasource )
308: {
309: $obj_datasource = ( is_array( $datasource ) ) ? $datasource : explode( ',', $datasource );
310:
311: if ( in_array( strtolower( $obj_datasource[0] ), $this->obj_db_array ) )
312: {
313: $this->obj_db_type = strtolower( $obj_datasource[0] );
314: $this->obj_statement = "{$this->obj_db_type}_statement";
315: $this->obj_prepare = "{$this->obj_db_type}_prepare";
316: $this->obj_transaction = "{$this->obj_db_type}_transaction";
317:
318: require_once "drivers/{$this->obj_db_type}.php";
319:
320: $obj_connection = "{$this->obj_db_type}_connection";
321: $this->obj_instance = new $obj_connection( $obj_datasource );
322:
323: if ( $this->obj_instance )
324: return $this->obj_connection = $this->obj_instance->obj_db_connection();
325: else
326: return false;
327: }
328: else
329: {
330: trigger_error( "Unsupported database type: {$obj_datasource[0]}", E_USER_WARNING );
331:
332: return false;
333: }
334: }
335:
336: 337: 338: 339: 340: 341: 342: 343:
344: private function obj_helper_delete( $table, $where=false )
345: {
346: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
347:
348: return "DELETE FROM $table $query_where";
349: }
350:
351: 352: 353: 354: 355: 356: 357: 358:
359: private function obj_helper_insert( $table, $data_array )
360: {
361:
362: $array_keys = array_keys( $data_array );
363: $array_vals = array_values( $data_array );
364:
365:
366: for ( $i = 0; $i < count( $array_vals ); $i++ )
367: {
368: if ( !is_array( $array_vals[$i] ) )
369: {
370: $temp_array = explode( ',', $array_vals[$i] );
371: $array_vals[$i] = $temp_array;
372: }
373: }
374:
375: for ( $i = 0; $i < count( $array_vals[0] ); $i++ )
376: {
377: $query_insert[] = '(';
378:
379: for ( $j = 0; $j < count( $array_vals ); $j++ )
380: {
381: if ( is_string( $array_vals[$j][$i] ) && !is_numeric( $array_vals[$j][$i] ) )
382: $query_insert[] = "'{$array_vals[$j][$i]}'";
383: else
384: $query_insert[] = $array_vals[$j][$i];
385: }
386:
387: $query_insert[] = ')';
388: }
389:
390: $query_cols = implode( ',', $array_keys );
391: $query_vals = str_replace( array( ',),(,', ',)', '(,' ), array( '),(', ')', '('), implode( ',', $query_insert ) );
392:
393: return "INSERT INTO $table ($query_cols) VALUES $query_vals";
394: }
395:
396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407:
408: private function obj_helper_paging( $table, $cols=false, $where=false, $order_by=false, $limit=1, $offset=1 )
409: {
410: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
411: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
412: $query_order = ( !trim( $order_by ) ) ? '' : "ORDER BY $order_by";
413: $query_offset = ( ( $offset - 1 ) * $limit );
414:
415: if ( $this->obj_db_type === 'sqlsrv' )
416: {
417:
418: if ( explode( '.', $this->obj_instance->obj_db_info()[0] ) < 11 )
419: {
420: $query_sql = "WITH temp_table AS (
421: SELECT ROW_NUMBER() OVER ( $query_order ) AS RowNumber, $query_cols
422: FROM $table $query_where )
423: SELECT *
424: FROM temp_table
425: WHERE RowNumber > $query_offset AND RowNumber <= ( $limit + $query_offset )";
426: }
427: else
428: {
429:
430: $query_sql = "SELECT $query_cols FROM $table $query_where $query_order
431: OFFSET $query_offset ROWS
432: FETCH NEXT $limit ROWS ONLY";
433: }
434: }
435: else
436: {
437: if ( $this->obj_db_type === 'pgsql' )
438: $limit_str = "LIMIT $limit OFFSET $query_offset";
439: else
440: $limit_str = "LIMIT $query_offset,$limit";
441:
442: $query_sql = "SELECT $query_cols FROM $table $query_where $query_order $limit_str";
443: }
444:
445: return $query_sql;
446: }
447:
448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458:
459: private function obj_helper_select( $table, $cols=false, $where=false, $order_by=false, $sort_order=false )
460: {
461: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
462: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
463: $query_order = ( !trim( $order_by ) ) ? '' : "ORDER BY $order_by";
464: $query_sort = ( !trim( $sort_order ) || strtolower( $sort_order ) != 'desc' || strtolower( $sort_order ) != 'asc' ) ? '' : $sort_order;
465:
466: return "SELECT $query_cols FROM $table $query_where $query_order $query_sort";
467: }
468:
469: 470: 471: 472: 473: 474: 475: 476: 477:
478: private function obj_helper_update( $table, $data_array, $where=false )
479: {
480:
481: $query_cols = array_keys( $data_array );
482: $query_vals = array_values( $data_array );
483: $query_update = '';
484:
485: for ( $i = 0; $i < count( $data_array ); $i++ )
486: {
487: if ( is_string( $query_vals[$i] ) && !is_numeric( $query_vals[$i] ) )
488: $query_update .= "{$query_cols[$i]}='{$query_vals[$i]}',";
489: else
490: $query_update .= "{$query_cols[$i]}={$query_vals[$i]},";
491: }
492:
493: $query_update = rtrim( $query_update, ',' );
494: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
495:
496: return "UPDATE $table SET $query_update $query_where";
497: }
498:
499:
500: 501: 502:
503:
504: 505: 506: 507: 508:
509: protected function obj_db_close() {}
510: protected function obj_db_connection() {}
511: protected function obj_db_error() {}
512: protected function obj_db_escape_data( $str ) {}
513: protected function obj_db_message() {}
514: protected function obj_db_info() {}
515: }
516:
517: abstract class obj_access
518: {
519: 520: 521: 522: 523:
524: abstract protected function obj_db_close();
525: abstract protected function obj_db_connection();
526: abstract protected function obj_db_error();
527: abstract protected function obj_db_escape_data( $str );
528: abstract protected function obj_db_message();
529: abstract protected function obj_db_info();
530: }
531:
532: ?>