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