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.0';
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: $result = $this->obj_select( $table );
213: $last_page = ceil( $result->obj_num_rows()/$query_limit );
214: $result->obj_free_result();
215:
216: $query_stmt = new $this->obj_statement( $this->obj_helper_paging( $table, $cols, $where, $order_by, $query_limit, $query_offset ), $this );
217:
218: return array( $query_stmt->obj_query_execute(), $last_page );
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: public function obj_prepare_statement( $query, $param_vars=false )
230: {
231: return new $this->obj_prepare( $this, $query, $param_vars );
232: }
233:
234: 235: 236: 237: 238: 239: 240:
241: public function obj_query( $query )
242: {
243: $query_stmt = new $this->obj_statement( $query, $this );
244:
245: return $query_stmt->obj_query_execute();
246: }
247:
248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public function obj_select( $table, $cols=false, $where=false, $order_by=false, $sort_order=false )
260: {
261: $query_stmt = new $this->obj_statement( $this->obj_helper_select( $table, $cols, $where, $order_by, $sort_order ), $this );
262:
263: return $query_stmt->obj_query_execute();
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function obj_transaction()
273: {
274: return new $this->obj_transaction( $this->obj_connection );
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284: 285:
286: public function obj_update( $table, $data_array, $where=false )
287: {
288:
289: $query_stmt = new $this->obj_statement( $this->obj_helper_update( $table, $data_array, $where ), $this );
290:
291: return $query_stmt->obj_query_execute();
292: }
293:
294: 295: 296:
297:
298: 299: 300: 301: 302: 303: 304:
305: private function obj_helper_datasource( $datasource )
306: {
307: $obj_datasource = ( is_array( $datasource ) ) ? $datasource : explode( ',', $datasource );
308:
309: if ( in_array( strtolower( $obj_datasource[0] ), $this->obj_db_array ) )
310: {
311: $this->obj_db_type = strtolower( $obj_datasource[0] );
312: $this->obj_statement = "{$this->obj_db_type}_statement";
313: $this->obj_prepare = "{$this->obj_db_type}_prepare";
314: $this->obj_transaction = "{$this->obj_db_type}_transaction";
315:
316: require_once "drivers/{$this->obj_db_type}.php";
317:
318: $obj_connection = "{$this->obj_db_type}_connection";
319: $this->obj_instance = new $obj_connection( $obj_datasource );
320:
321: if ( $this->obj_instance )
322: return $this->obj_connection = $this->obj_instance->obj_db_connection();
323: else
324: return false;
325: }
326: else
327: {
328: trigger_error( "Unsupported database type: {$obj_datasource[0]}", E_USER_WARNING );
329:
330: return false;
331: }
332: }
333:
334: 335: 336: 337: 338: 339: 340: 341:
342: private function obj_helper_delete( $table, $where=false )
343: {
344: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
345:
346: return "DELETE FROM $table $query_where";
347: }
348:
349: 350: 351: 352: 353: 354: 355: 356:
357: private function obj_helper_insert( $table, $data_array )
358: {
359:
360: $array_keys = array_keys( $data_array );
361: $array_vals = array_values( $data_array );
362:
363:
364: for ( $i = 0; $i < count( $array_vals ); $i++ )
365: {
366: if ( !is_array( $array_vals[$i] ) )
367: {
368: $temp_array = explode( ',', $array_vals[$i] );
369: $array_vals[$i] = $temp_array;
370: }
371: }
372:
373: for ( $i = 0; $i < count( $array_vals[0] ); $i++ )
374: {
375: $query_insert[] = '(';
376:
377: for ( $j = 0; $j < count( $array_vals ); $j++ )
378: {
379: if ( is_string( $array_vals[$j][$i] ) && !is_numeric( $array_vals[$j][$i] ) )
380: $query_insert[] = "'{$array_vals[$j][$i]}'";
381: else
382: $query_insert[] = $array_vals[$j][$i];
383: }
384:
385: $query_insert[] = ')';
386: }
387:
388: $query_cols = implode( ',', $array_keys );
389: $query_vals = str_replace( array( ',),(,', ',)', '(,' ), array( '),(', ')', '('), implode( ',', $query_insert ) );
390:
391: return "INSERT INTO $table ($query_cols) VALUES $query_vals";
392: }
393:
394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405:
406: private function obj_helper_paging( $table, $cols=false, $where=false, $order_by=false, $limit=1, $offset=1 )
407: {
408: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
409: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
410: $query_order = ( !trim( $order_by ) ) ? '' : "ORDER BY $order_by";
411: $query_offset = ( ( $offset - 1 ) * $limit );
412:
413: if ( $this->obj_db_type === 'sqlsrv' )
414: {
415: $query_sql = "WITH temp_table AS (
416: SELECT ROW_NUMBER() OVER ( $query_order ) AS RowNumber, $query_cols
417: FROM $table $query_where )
418: SELECT *
419: FROM temp_table
420: WHERE RowNumber > $query_offset AND RowNumber <= ( $limit + $query_offset )";
421: }
422: else
423: {
424: if ( $this->obj_db_type === 'pgsql' )
425: $limit_str = "LIMIT $limit OFFSET $query_offset";
426: else
427: $limit_str = "LIMIT $query_offset,$limit";
428:
429: $query_sql = "SELECT $query_cols FROM $table $query_where $query_order $limit_str";
430: }
431:
432: return $query_sql;
433: }
434:
435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445:
446: private function obj_helper_select( $table, $cols=false, $where=false, $order_by=false, $sort_order=false )
447: {
448: $query_cols = ( !trim( $cols ) ) ? '*' : $cols;
449: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
450: $query_order = ( !trim( $order_by ) ) ? '' : "ORDER BY $order_by";
451: $query_sort = ( !trim( $sort_order ) || strtolower( $sort_order ) != 'desc' || strtolower( $sort_order ) != 'asc' ) ? '' : $sort_order;
452:
453: return "SELECT $query_cols FROM $table $query_where $query_order $query_sort";
454: }
455:
456: 457: 458: 459: 460: 461: 462: 463: 464:
465: private function obj_helper_update( $table, $data_array, $where=false )
466: {
467:
468: $query_cols = array_keys( $data_array );
469: $query_vals = array_values( $data_array );
470: $query_update = '';
471:
472: for ( $i = 0; $i < count( $data_array ); $i++ )
473: {
474: if ( is_string( $query_vals[$i] ) && !is_numeric( $query_vals[$i] ) )
475: $query_update .= "{$query_cols[$i]}='{$query_vals[$i]}',";
476: else
477: $query_update .= "{$query_cols[$i]}={$query_vals[$i]},";
478: }
479:
480: $query_update = rtrim( $query_update, ',' );
481: $query_where = ( !trim( $where ) ) ? '' : "WHERE $where";
482:
483: return "UPDATE $table SET $query_update $query_where";
484: }
485:
486:
487: 488: 489:
490:
491: 492: 493: 494: 495:
496: protected function obj_db_close() {}
497: protected function obj_db_connection() {}
498: protected function obj_db_error() {}
499: protected function obj_db_escape_data( $str ) {}
500: protected function obj_db_message() {}
501: protected function obj_db_info() {}
502: }
503:
504: abstract class obj_access
505: {
506: 507: 508: 509: 510:
511: abstract protected function obj_db_close();
512: abstract protected function obj_db_connection();
513: abstract protected function obj_db_error();
514: abstract protected function obj_db_escape_data( $str );
515: abstract protected function obj_db_message();
516: abstract protected function obj_db_info();
517: }
518:
519: ?>