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