1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: class Database
14: {
15: 16: 17:
18: public $table;
19:
20:
21: 22: 23:
24: private $prefix;
25:
26:
27: 28: 29:
30: private $joins = array();
31:
32:
33: 34: 35:
36: private static $relations = array();
37:
38:
39: 40: 41:
42: private $current_table_relations = array();
43:
44:
45: 46: 47:
48: private static $tables_columns = array();
49:
50:
51: 52: 53:
54: private $current_table_columns = array();
55:
56:
57: 58: 59:
60: public $command = 'SELECT';
61:
62:
63: 64: 65:
66: public $where = array();
67:
68:
69: 70: 71:
72: public $order = array();
73:
74:
75: 76: 77:
78: public $limit = array();
79:
80:
81: 82: 83:
84: private static $connections = array();
85:
86:
87: 88: 89:
90: private $active_connection = 'default';
91:
92:
93: 94: 95:
96: public $columns = '*';
97:
98:
99: 100: 101:
102: public $distinct = FALSE;
103:
104:
105: 106: 107:
108: public $update_on_duplicate = FALSE;
109:
110:
111: 112: 113:
114: public $data = array();
115:
116:
117: 118: 119:
120: public $values = array();
121:
122:
123: 124: 125:
126: public $group_by;
127:
128:
129: 130: 131: 132: 133: 134: 135:
136: public function __construct($table = NULL, $connection_name = 'default')
137: {
138: if (isset(self::$connections[$connection_name]) === FALSE) {
139: trigger_error('Database connection <strong>"' . $connection_name . '"</strong> is not exist!', E_USER_ERROR);
140: }
141:
142: $this->active_connection = $connection_name;
143: $this->prefix = self::$connections[$connection_name]['prefix'];
144:
145: if ($table !== NULL) {
146: $this->table($table);
147: }
148: }
149:
150:
151: 152: 153: 154: 155: 156: 157:
158: public static function __callstatic($name, $arguments)
159: {
160: $db = new self;
161:
162: if (isset($arguments[0]) === FALSE) {
163: $arguments[0] = NULL;
164: }
165:
166: switch ($name) {
167: case 'query':
168: return $db->_query($arguments[0]);
169: break;
170:
171: case 'insert':
172: case 'update':
173: case 'save':
174: case 'delete':
175: case 'count':
176: $db->table($arguments[0]);
177: array_shift($arguments);
178: return call_user_func_array(array($db, '_' . $name), $arguments);
179: break;
180: }
181: }
182:
183:
184: 185: 186: 187: 188: 189: 190:
191: public function __call($name, $arguments)
192: {
193: if ($name === 'query') {
194: if (isset($arguments[0]) === FALSE) {
195: $arguments[0] = NULL;
196: }
197:
198: return $this->_query($arguments[0]);
199:
200: }
201:
202: return call_user_func_array(array($this, '_' . $name), $arguments);
203: }
204:
205:
206: 207: 208: 209: 210: 211:
212: public function &__get($property)
213: {
214: return $this->$property;
215: }
216:
217:
218: 219: 220: 221: 222: 223: 224: 225:
226: public static function connection($options, $connection_name = 'default')
227: {
228: if (isset(self::$connections[$connection_name]) === TRUE) {
229: trigger_error('Database connection <strong>"' . $connection_name . '"</strong> is already exist!', E_USER_ERROR);
230: }
231:
232: $options['link'] = NULL;
233: if (isset($options['prefix']) === FALSE) {
234: $options['prefix'] = '';
235: }
236:
237: self::$connections[$connection_name] = $options;
238: }
239:
240:
241: 242: 243: 244: 245:
246: private function connect()
247: {
248: $connection = &self::$connections[$this->active_connection];
249:
250: if (empty($connection['link']) === FALSE) {
251: return;
252: }
253:
254: $connection['link'] = mysql_connect($connection['host'], $connection['user'], $connection['password']);
255:
256: if (mysql_set_charset('utf8') === FALSE) {
257: trigger_error('Could not set connection charset!', E_USER_ERROR);
258: }
259:
260: if ($connection['link'] === FALSE) {
261: trigger_error('Could not connect to database server!', E_USER_ERROR);
262: }
263:
264: if (mysql_select_db($connection['database']) === FALSE) {
265: trigger_error('Could not select database!', E_USER_ERROR);
266: }
267: }
268:
269:
270: 271: 272: 273: 274: 275: 276:
277: public function table($table)
278: {
279: $this->table = $table;
280: if (self::$relations === NULL) {
281: self::$relations = $this->get_relations();
282: }
283:
284: if (isset(self::$relations[$table]) === TRUE) {
285: $this->current_table_relations = self::$relations[$table];
286:
287: } else {
288: $this->current_table_relations = array();
289: }
290:
291: if (self::$tables_columns === NULL) {
292: self::$tables_columns = $this->get_tables_columns();
293: }
294:
295: if (isset(self::$tables_columns[$table]) === TRUE) {
296: $this->current_table_columns = self::$tables_columns[$table];
297:
298: } else {
299: $this->current_table_columns = array();
300: }
301:
302: $this->reset();
303:
304: return $this;
305: }
306:
307:
308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319:
320: private function _query($sql = NULL)
321: {
322: $this->connect();
323:
324: $reset = TRUE;
325: if ($sql === FALSE) {
326: $reset = FALSE;
327: $sql = NULL;
328: }
329:
330: if ($sql === NULL) {
331: $sql = $this->build_sql();
332:
333: } else {
334: $sql = trim($sql);
335:
336: list($command, $count) = preg_split('/(\s+)|(\()/', $sql, 3);
337:
338: $count = strtoupper($count);
339: if ($count === 'COUNT') {
340: $this->command = 'COUNT';
341:
342: } else {
343: $this->command = strtoupper($command);
344: }
345: }
346:
347: if (DEBUG === TRUE) {
348: Log::debug($sql);
349: }
350:
351: $result = mysql_query($sql);
352:
353: if ($result === FALSE) {
354: trigger_error(mysql_error() . '<br /><code>' . $sql . '</code>', E_USER_ERROR);
355: }
356:
357: if ($reset === TRUE) {
358: $this->reset();
359: }
360:
361: if ($this->command === 'COUNT') {
362: if (empty($this->group_by) === TRUE) {
363: $count = mysql_fetch_array($result);
364: return reset($count);
365: }
366:
367: return new DatabaseResult($result);
368: }
369:
370: if ($this->command === 'SELECT') {
371: return new DatabaseResult($result);
372: }
373:
374: if ($this->command === 'INSERT') {
375: return mysql_insert_id();
376: }
377:
378: return $result;
379: }
380:
381:
382: 383: 384:
385: public function show_sql()
386: {
387: $sql = $this->build_sql();
388: echo '<pre>';
389: print_r($sql);
390: echo '</pre>';
391: exit;
392: }
393:
394:
395: 396: 397: 398: 399: 400: 401:
402: public function get_rows($columns = '*', $where = NULL)
403: {
404: $this->command = 'SELECT';
405: $this->columns = $columns;
406:
407: if ($where !== NULL) {
408: if (is_numeric($where) === TRUE
409: OR Arrays::is_numeric($where) === TRUE) {
410:
411: $where = array(array('id', $where));
412: }
413:
414: $this->where = $where;
415: }
416:
417: return $this->_query();
418: }
419:
420:
421: 422: 423: 424: 425: 426: 427:
428: public function get_row($columns = '*', $where = NULL)
429: {
430: $this->limit(1);
431: $result = $this->get_rows($columns, $where);
432:
433: if ($result === FALSE) {
434: return FALSE;
435: }
436:
437: return $result->fetch();
438: }
439:
440:
441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452:
453: public function _save($data, $update_on_duplicate = TRUE)
454: {
455: if (empty($data['id']) === FALSE) {
456: $this->command = 'UPDATE';
457: $this->where = array(array('id', $data['id']));
458:
459: } else {
460: $this->command = 'INSERT';
461: $this->update_on_duplicate = $update_on_duplicate;
462: }
463:
464: if (empty($this->current_table_columns) === FALSE) {
465: $this->data = array();
466: foreach ($this->current_table_columns as $column => $type) {
467: if (array_key_exists($column, $data) === TRUE) {
468: $this->data[$column] = $data[$column];
469: }
470: }
471:
472: } else {
473: $this->data = $data;
474: }
475:
476: $result = $this->_query();
477:
478: if ($result === FALSE) {
479: return FALSE;
480: }
481:
482: if (isset($data['id']) === TRUE) {
483: return $data['id'];
484:
485: } else {
486: return mysql_insert_id();
487: }
488: }
489:
490:
491: 492: 493: 494: 495: 496: 497: 498: 499: 500:
501: private function _insert($data)
502: {
503: $this->command = 'INSERT';
504: return $this->save($data, FALSE);
505: }
506:
507:
508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521:
522: private function _update($data, $where = NULL)
523: {
524: $this->command = 'UPDATE';
525:
526: if ($where !== NULL) {
527: if (is_numeric($where) === TRUE
528: OR Arrays::is_numeric($where) === TRUE) {
529:
530: $where = array(array('id', $where));
531: }
532:
533: $this->where = $where;
534: }
535:
536: if (empty($this->where) === TRUE) {
537: return FALSE;
538: }
539:
540: $this->data = $data;
541:
542: return $this->_query();
543: }
544:
545:
546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558:
559: private function _delete($where = NULL)
560: {
561: $this->command = 'DELETE';
562:
563: if ($where !== NULL) {
564: if (is_numeric($where) === TRUE
565: OR Arrays::is_numeric($where) === TRUE) {
566:
567: $where = array(array('id', $where));
568: }
569:
570: $this->where = $where;
571: }
572:
573: if (empty($this->where) === TRUE) {
574: return FALSE;
575: }
576:
577: return $this->_query();
578: }
579:
580:
581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592:
593: private function _count($where = NULL)
594: {
595: $this->command = 'COUNT';
596:
597: if ($where !== NULL) {
598: if (is_numeric($where) === TRUE
599: OR Arrays::is_numeric($where) === TRUE) {
600:
601: $where = array(array('id', $where));
602: }
603:
604: $this->where = $where;
605: }
606:
607: return $this->_query(FALSE);
608: }
609:
610:
611: 612: 613: 614: 615:
616: private function build_sql()
617: {
618: switch ($this->command) {
619: case 'SELECT':
620: return $this->build_select();
621: exit;
622:
623: case 'COUNT':
624: return $this->build_count();
625: exit;
626:
627: case 'INSERT':
628: return $this->build_insert();
629: exit;
630:
631: case 'UPDATE':
632: return $this->build_update();
633: exit;
634:
635: case 'DELETE':
636: return $this->build_delete();
637: exit;
638: }
639: }
640:
641:
642: 643: 644: 645: 646:
647: private function build_select()
648: {
649: $where = '';
650: if (empty($this->where) === FALSE) {
651: $where = ' WHERE ' . $this->build_where($this->where);
652: }
653:
654: $order = '';
655: if (empty($this->order) === FALSE) {
656: $order = ' ORDER BY ' . $this->build_order($this->order);
657: }
658:
659: $sql = 'SELECT ';
660:
661: if ($this->distinct === TRUE) {
662: $sql .= 'DISTINCT ';
663: }
664:
665: $sql .= $this->build_columns($this->columns);
666:
667: $sql .= ' FROM ';
668: $sql .= '`' . $this->prefix . $this->table . '` ';
669:
670: if (empty($this->joins) === FALSE) {
671: $sql .= $this->build_joins($this->joins);
672: }
673:
674: $sql .= $where;
675:
676: if (empty($this->group_by) === FALSE) {
677: $sql .= $this->build_group_by($this->group_by);
678: }
679:
680: $sql .= $order;
681:
682: if (empty($this->limit) === FALSE) {
683: $sql .= ' LIMIT ' . implode(',', $this->limit);;
684: }
685:
686: return $sql;
687: }
688:
689:
690: 691: 692: 693: 694:
695: private function build_count()
696: {
697: $where = '';
698: if (empty($this->where) === FALSE) {
699: $where = ' WHERE ' . $this->build_where($this->where);
700: }
701:
702: $distinct = '*';
703: if ($this->distinct === TRUE) {
704: $distinct = 'DISTINCT `' . $this->table . '`.`id`';
705: }
706: $sql = 'SELECT COUNT(' . $distinct . ') FROM ';
707: $sql .= '`' . $this->prefix . $this->table . '` ';
708:
709: if (empty($this->joins) === FALSE) {
710: $sql .= $this->build_joins($this->joins);
711: }
712:
713: $sql .= $where;
714:
715: if (empty($this->group_by) === FALSE) {
716: $sql .= $this->build_group_by($this->group_by);
717: }
718:
719: return $sql;
720: }
721:
722:
723: 724: 725: 726: 727:
728: private function build_insert()
729: {
730: $data = $this->data;
731:
732: if (empty($this->columns) === FALSE
733: AND empty($this->values) === FALSE) {
734:
735: $data_2 = array_combine($this->columns, $this->values);
736: $data = array_merge($data, $data_2);
737: }
738:
739: $sql = 'INSERT INTO ';
740: $sql .= '`' . $this->prefix . $this->table . '` ';
741:
742: $sql .= $this->build_values($data);
743:
744: if ($this->update_on_duplicate === TRUE) {
745: $sql .= ' ON DUPLICATE KEY UPDATE ';
746: $sql .= 'id=LAST_INSERT_ID(id), ';
747:
748: if (isset($data['id']) === TRUE) {
749: unset($data['id']);
750: }
751:
752: $sql .= $this->build_key_value($data);
753: }
754:
755: return $sql;
756: }
757:
758:
759: 760: 761: 762: 763:
764: private function build_update()
765: {
766: if (empty($this->where) === TRUE) {
767: return FALSE;
768: }
769:
770: $data = $this->data;
771:
772: if (empty($this->columns) === FALSE
773: AND empty($this->values) === FALSE) {
774:
775: $data_2 = array_combine($this->columns, $this->values);
776: $data = array_merge($data, $data_2);
777: }
778:
779: $sql = 'UPDATE ';
780: $sql .= '`' . $this->prefix . $this->table . '` SET ';
781:
782: $sql .= $this->build_key_value($data);
783:
784: $sql .= ' WHERE ';
785: $sql .= $this->build_where($this->where);
786:
787: return $sql;
788: }
789:
790:
791: 792: 793: 794: 795:
796: private function build_delete()
797: {
798: if (empty($this->where) === TRUE) {
799: return FALSE;
800: }
801:
802: $sql = 'DELETE FROM ';
803: $sql .= '`' . $this->prefix . $this->table . '` ';
804:
805: $sql .= ' WHERE ';
806: $sql .= $this->build_where($this->where);
807:
808: return $sql;
809: }
810:
811:
812: 813: 814: 815: 816:
817: public function build_columns($columns)
818: {
819: if (empty($columns) === TRUE
820: OR $columns === '*') {
821:
822: return '*';
823: }
824:
825: $columns_out = '';
826:
827: foreach ($columns as $key => $column) {
828: $column_tmp = preg_split('/(\.)|( as )/i', $column, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
829:
830: $c = count($column_tmp);
831:
832: $column = array();
833: if ($c === 1) {
834: $column[0] = $this->table;
835: $column[1] = $column_tmp[0];
836: $column[2] = $column_tmp[0];
837:
838: } elseif ($c === 3) {
839: if ($column_tmp[1] === '.') {
840:
841: $column[0] = $column_tmp[0];
842: $column[1] = $column_tmp[2];
843: $column[2] = $column_tmp[2];
844:
845: } else {
846:
847: $column[0] = $this->table;
848: $column[1] = $column_tmp[0];
849: $column[2] = $column_tmp[2];
850: }
851:
852: } elseif ($c === 5) {
853: $column[0] = $column_tmp[0];
854: $column[1] = $column_tmp[2];
855: $column[2] = $column_tmp[4];
856: }
857:
858: if (isset($this->current_table_relations[$column[0]]) === TRUE) {
859: $column[2] = $column_tmp[0] . '_' . $column_tmp[2];
860:
861: $join_table = $this->current_table_relations[$column[0]];
862: $as = ' AS ' . $this->prefix . $column[0];
863:
864: $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
865:
866: } else {
867: $join_table = $column[0];
868: $as = '';
869:
870: $join_column = FALSE;
871: if (isset(self::$relations[$column[0]]) === TRUE) {
872: $join_column = array_search($this->table, self::$relations[$column[0]]);
873: }
874:
875: if ($join_column === FALSE) {
876: $join_column = $column[1];
877: }
878:
879: $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
880: }
881:
882: if ($join_table !== $this->table
883: AND isset($this->joins[$join_table . $as]) == FALSE) {
884:
885: $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
886: }
887:
888: $columns_out .= '`';
889: $columns_out .= $this->prefix . $column[0];
890: $columns_out .= '`.`';
891: $columns_out .= $column[1];
892: $columns_out .= '` AS `';
893: $columns_out .= $column[2];
894: $columns_out .= '`, ';
895: }
896:
897: return trim($columns_out, ', ');
898: }
899:
900:
901: 902: 903: 904: 905: 906:
907: public function build_joins($joins)
908: {
909: $joins_out = '';
910:
911: foreach ($this->joins as $join) {
912: $type = 'LEFT';
913: if ($join[2] !== NULL) {
914: $type = $join[2];
915: }
916:
917: $table = '`' . $this->prefix;
918: $table .= str_ireplace(' AS ', '` AS `', $join[0]);
919: $table .= '`';
920:
921: $relation = '`';
922: $relation .= $this->prefix;
923: $relation .= str_ireplace(array('.', ' = '), array('`.`', '` = `' . $this->prefix), $join[1]);
924: $relation .= '`';
925:
926: $joins_out .= $type . ' JOIN ' . $table;
927: $joins_out .= ' ON ' . $relation . ' ';
928: }
929:
930: return $joins_out;
931: }
932:
933:
934: 935: 936: 937: 938: 939:
940: public function build_where($conditions)
941: {
942: $where = '';
943:
944: $conditions_count = count($conditions);
945: $conditions_count--;
946:
947: foreach ($conditions as $key => $condition) {
948: if (is_array($condition[0]) === TRUE) {
949: $where .= '(';
950: $where .= $this->build_where($condition);
951: $where .= ')';
952:
953: } else {
954: $column = explode('.', $condition[0]);
955:
956: if (count($column) === 1) {
957: $column = '`' . $column[0] . '`';
958:
959: } else {
960: if (isset($this->current_table_relations[$column[0]]) === TRUE) {
961: $join_table = $this->current_table_relations[$column[0]];
962: $as = ' AS ' . $this->prefix . $column[0];
963:
964: $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
965:
966: } else {
967: $join_table = $column[0];
968: $as = '';
969:
970: $join_column = FALSE;
971: if (isset(self::$relations[$column[0]]) === TRUE) {
972: $join_column = array_search($this->table, self::$relations[$column[0]]);
973: }
974:
975: if ($join_column === FALSE) {
976: $join_column = $column[1];
977: }
978:
979: $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
980: }
981:
982: if ($join_table !== $this->table
983: AND isset($this->joins[$join_table . $as]) == FALSE) {
984:
985: $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
986: }
987:
988: $column = '`' . $this->prefix . $column[0] . '`.`' . $column[1] . '`';
989: }
990:
991: $value = $condition[1];
992:
993: $operator = '=';
994: if (isset($condition[2]) === TRUE) {
995: $operator = $condition[2];
996: }
997:
998: $operator = strtoupper($operator);
999:
1000: if (is_array($value) === TRUE) {
1001: if ($operator === '=') {
1002: $operator = 'IN';
1003:
1004: } elseif ($operator === '<>'
1005: OR $operator === '!=') {
1006:
1007: $operator = 'NOT IN';
1008: }
1009:
1010: $where .= $column . ' ' . $operator . ' (';
1011:
1012: foreach ($value as $val) {
1013: if ($val === NULL) {
1014: $where .= 'NULL,';
1015:
1016: } else {
1017: $where .= '\'' . mysql_real_escape_string($val) . '\',';
1018: }
1019: }
1020:
1021: $where = substr($where, 0, -1);
1022: $where .= ')';
1023:
1024: } else {
1025:
1026: if ($value === NULL) {
1027: if ($operator === '=') {
1028: $operator = 'IS';
1029:
1030: } elseif ($operator === '!=' OR $operator === '<>') {
1031: $operator = 'IS NOT';
1032: }
1033:
1034: $value = 'NULL';
1035:
1036: } else {
1037: $value = '\'' . mysql_real_escape_string($value) . '\'';
1038: }
1039:
1040: $where .= $column . ' ' . $operator . ' ' .$value;
1041: }
1042:
1043: $where .= ' ';
1044: }
1045:
1046: $bool = 'AND';
1047: if (isset($conditions[$key + 1][3]) === TRUE) {
1048: $bool = $conditions[$key + 1][3];
1049: }
1050:
1051: if ($conditions_count !== $key) {
1052: $where .= ' ' . $bool . ' ';
1053: }
1054: }
1055:
1056: return $where;
1057: }
1058:
1059:
1060: 1061: 1062: 1063: 1064: 1065:
1066: public function build_group_by($group_by)
1067: {
1068: $group_by = str_replace('.', '`.`', $group_by);
1069: return ' GROUP BY `' . $group_by . '`';
1070: }
1071:
1072:
1073: 1074: 1075: 1076: 1077: 1078:
1079: public function build_order($orders)
1080: {
1081: $order_out = '';
1082:
1083: foreach ($orders as $order) {
1084: $column = explode('.', $order[0]);
1085:
1086: if (count($column) === 1) {
1087: $column = '`' . $column[0] . '`';
1088:
1089: } else {
1090: if (isset($this->current_table_relations[$column[0]]) === TRUE) {
1091: $join_table = $this->current_table_relations[$column[0]];
1092: $as = ' AS ' . $this->prefix . $column[0];
1093:
1094: $on = $this->table . '.' . $column[0] . ' = ' . $column[0] . '.id';
1095:
1096: } else {
1097: $join_table = $column[0];
1098: $as = '';
1099:
1100: $join_column = FALSE;
1101: if (isset(self::$relations[$column[0]]) === TRUE) {
1102: $join_column = array_search($this->table, self::$relations[$column[0]]);
1103: }
1104:
1105: if ($join_column === FALSE) {
1106: $join_column = $column[1];
1107: }
1108:
1109: $on = $this->table . '.id = ' . $join_table . '.' . $join_column;
1110: }
1111:
1112: if ($join_table !== $this->table
1113: AND isset($this->joins[$join_table . $as]) == FALSE) {
1114:
1115: $this->joins[$join_table . $as] = array($join_table . $as, $on, NULL);
1116: }
1117:
1118: $column = '`' . $this->prefix . $column[0] . '`.`' . $column[1] . '`';
1119: }
1120:
1121: $direction = 'ASC';
1122: if (isset($order[1]) === TRUE) {
1123: $direction = $order[1];
1124: }
1125:
1126: $order_out .= $column . ' ' . $direction . ' ,';
1127: }
1128:
1129: return substr($order_out, 0, -1);
1130: }
1131:
1132:
1133: 1134: 1135: 1136: 1137: 1138: 1139:
1140: public function build_values($data)
1141: {
1142: if (isset($data[0]) === FALSE
1143: OR is_array($data[0]) === FALSE) {
1144:
1145: $data = array($data);
1146: }
1147:
1148: $keys = array_keys($data[0]);
1149:
1150: $values_out = '(`';
1151: $values_out .= implode('`,`', $keys);
1152: $values_out .= '`) VALUES ';
1153:
1154: foreach ($data as $values) {
1155: $values_out .= $this->build_values_list($values) . ',';
1156: }
1157:
1158: return substr($values_out, 0, -1);
1159: }
1160:
1161:
1162: 1163: 1164: 1165: 1166: 1167: 1168:
1169: public function build_key_value($data)
1170: {
1171: $values_out = '';
1172:
1173: foreach ($data as $key => $value) {
1174: $values_out .= '`' . $key . '`=';
1175: if ($value === NULL) {
1176: $values_out .= 'NULL,';
1177:
1178: } else {
1179: $values_out .= '\'' . mysql_real_escape_string($value) . '\',';
1180: }
1181: }
1182:
1183: return substr($values_out, 0, -1);
1184: }
1185:
1186:
1187: 1188: 1189: 1190: 1191: 1192:
1193: public function build_values_list($data)
1194: {
1195: $values_out = '(';
1196:
1197: foreach ($data as $value) {
1198: if ($value === NULL) {
1199: $values_out .= 'NULL,';
1200:
1201: } else {
1202: $values_out .= '\'' . mysql_real_escape_string($value) . '\',';
1203: }
1204: }
1205:
1206: $values_out = substr($values_out, 0, -1);
1207: $values_out .= ')';
1208:
1209: return $values_out;
1210: }
1211:
1212:
1213: 1214: 1215: 1216: 1217:
1218: public function reset()
1219: {
1220: $this->data = array();
1221: $this->values = array();
1222: $this->fields = '*';
1223: $this->joins = array();
1224: $this->where = array();
1225: $this->order = array();
1226: $this->limit = array();
1227: $this->distinct = FALSE;
1228: }
1229:
1230:
1231: 1232: 1233: 1234: 1235: 1236:
1237: public function command($command)
1238: {
1239: $this->command = strtoupper($command);
1240: return $this;
1241: }
1242:
1243:
1244: 1245: 1246: 1247: 1248: 1249:
1250: public function columns()
1251: {
1252: $this->columns = func_get_args();
1253: }
1254:
1255:
1256: 1257: 1258: 1259: 1260: 1261:
1262: public function join($table, $on, $alias = NULL, $type = 'LEFT')
1263: {
1264: if ($alias !== NULL) {
1265: $table = $table . ' AS ' . $alias;
1266:
1267: } else {
1268: $alias = $table;
1269: }
1270:
1271: $this->joins[$alias] = array($table, $on, $type);
1272: return $this;
1273: }
1274:
1275:
1276: 1277: 1278: 1279: 1280: 1281:
1282: public function where()
1283: {
1284: $arguments = func_get_args();
1285:
1286: $this->where[] = $arguments;
1287:
1288: return $this;
1289: }
1290:
1291:
1292: 1293: 1294: 1295: 1296: 1297:
1298: public function group_by($group_by)
1299: {
1300: $this->group_by = $group_by;
1301:
1302: return $this;
1303: }
1304:
1305:
1306: 1307: 1308: 1309: 1310: 1311: 1312:
1313: public function order($column, $direction = 'ASC')
1314: {
1315: if (is_array($column) === TRUE) {
1316: $this->order = array($column);
1317:
1318: } else {
1319: $this->order[] = array($column, $direction);
1320: }
1321:
1322: return $this;
1323: }
1324:
1325:
1326: 1327: 1328: 1329: 1330: 1331:
1332: public function limit()
1333: {
1334: $limit = func_get_args();
1335:
1336: if (is_array($limit[0]) === TRUE) {
1337: $this->limit = $limit[0];
1338:
1339: } else {
1340: $this->limit = $limit;
1341: }
1342:
1343: return $this;
1344: }
1345:
1346:
1347: 1348: 1349: 1350: 1351:
1352: public function get_relations()
1353: {
1354: $query = 'SELECT TABLE_NAME as `table`, COLUMN_NAME as `column`, REFERENCED_TABLE_NAME as `ref_table`
1355: FROM information_schema.KEY_COLUMN_USAGE
1356: WHERE TABLE_SCHEMA = DATABASE()
1357: AND REFERENCED_TABLE_SCHEMA = DATABASE();';
1358:
1359: try {
1360: $result = $this->_query($query);
1361:
1362: } catch (Exception $e) {
1363: trigger_error('Could not get tables relations!', E_USER_ERROR);
1364: }
1365:
1366: $prefix_len = strlen($this->prefix);
1367:
1368: $relations = array();
1369: foreach ($result->fetch_all() as $relation) {
1370: $table = substr($relation['table'], $prefix_len);
1371: $ref_table = substr($relation['ref_table'], $prefix_len);
1372: $column = $relation['column'];
1373:
1374: $relations[$table][$column] = $ref_table;
1375: }
1376:
1377: return $relations;
1378: }
1379:
1380:
1381: 1382: 1383: 1384: 1385:
1386: public function get_tables_columns()
1387: {
1388: $query = 'SELECT `TABLE_NAME` AS `table`, `COLUMN_NAME` AS `column`, `DATA_TYPE` AS `type`
1389: FROM `information_schema`.`COLUMNS`
1390: WHERE `TABLE_SCHEMA` = DATABASE()';
1391:
1392: try {
1393: $result = $this->_query($query);
1394:
1395: } catch (Exception $e) {
1396: trigger_error('Could not get tables columns!', E_USER_ERROR);
1397: }
1398:
1399: $prefix_len = strlen($this->prefix);
1400:
1401: $tables = array();
1402: foreach ($result->fetch_all() as $row) {
1403: $table = substr($row['table'], $prefix_len);
1404: $column = $row['column'];
1405: $type = $row['type'];
1406:
1407: $tables[$table][$column] = $type;
1408: }
1409:
1410: return $tables;
1411: }
1412: }
1413:
1414:
1415: 1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423:
1424:
1425: class DatabaseResult implements Iterator, Countable
1426: {
1427: 1428: 1429:
1430: private $result;
1431:
1432:
1433: 1434: 1435:
1436: private $row;
1437:
1438:
1439: 1440: 1441:
1442: private $pointer;
1443:
1444:
1445: 1446: 1447: 1448: 1449: 1450:
1451: public function __construct($result)
1452: {
1453: $this->result = $result;
1454: }
1455:
1456:
1457: 1458: 1459:
1460: public function fetch()
1461: {
1462: $this->pointer++;
1463:
1464: return mysql_fetch_assoc($this->result, MYSQL_ASSOC);
1465: }
1466:
1467:
1468: 1469: 1470: 1471: 1472: 1473:
1474: public function fetch_single($column = NULL)
1475: {
1476: $row = mysql_fetch_assoc($this->result, MYSQL_ASSOC);
1477:
1478: if (is_array($row) === FALSE) {
1479: return FALSE;
1480: }
1481:
1482: $this->pointer++;
1483:
1484: if ($column === NULL) {
1485: return reset($row);
1486: }
1487:
1488: return $row[$column];
1489: }
1490:
1491:
1492: 1493: 1494: 1495: 1496:
1497: public function fetch_all()
1498: {
1499: $this->rewind(TRUE);
1500:
1501: $rows = array();
1502:
1503: while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1504: $rows[] = $row;
1505: }
1506:
1507: return $rows;
1508: }
1509:
1510:
1511: 1512: 1513: 1514: 1515: 1516:
1517: public function fetch_assoc($key)
1518: {
1519: $this->rewind(TRUE);
1520:
1521: $rows = array();
1522:
1523: while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1524: $rows[$row[$key]] = $row;
1525: }
1526:
1527: return $rows;
1528: }
1529:
1530:
1531: 1532: 1533: 1534: 1535: 1536: 1537: 1538: 1539:
1540: public function fetch_pairs($key, $value)
1541: {
1542: $this->rewind(TRUE);
1543:
1544: $rows = array();
1545:
1546: while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1547: $rows[$row[$key]] = $row[$value];
1548: }
1549:
1550: return $rows;
1551: }
1552:
1553:
1554: 1555: 1556: 1557: 1558: 1559:
1560: public function fetch_values($key)
1561: {
1562: $this->rewind(TRUE);
1563:
1564: $values = array();
1565:
1566: while ($row = mysql_fetch_assoc($this->result, MYSQL_ASSOC)) {
1567: $values[] = $row[$key];
1568: }
1569:
1570: return $values;
1571: }
1572:
1573:
1574: 1575: 1576: 1577: 1578: 1579: 1580:
1581: public function rewind($only_seek = FALSE)
1582: {
1583: $this->pointer = 0;
1584: if (mysql_num_rows($this->result) > 0) {
1585: mysql_data_seek($this->result, 0);
1586: }
1587:
1588: if ($only_seek === TRUE) {
1589: return;
1590: }
1591:
1592: $this->row = mysql_fetch_assoc($this->result);
1593: }
1594:
1595:
1596: 1597: 1598: 1599: 1600:
1601: public function key()
1602: {
1603: return $this->pointer;
1604: }
1605:
1606:
1607: 1608: 1609: 1610: 1611:
1612: public function current()
1613: {
1614: return $this->row;
1615: }
1616:
1617:
1618: 1619: 1620: 1621: 1622:
1623: public function next()
1624: {
1625: $this->row = mysql_fetch_assoc($this->result);
1626: $this->pointer++;
1627: }
1628:
1629:
1630: 1631: 1632: 1633: 1634:
1635: public function valid()
1636: {
1637: return empty($this->row) === FALSE;
1638: }
1639:
1640:
1641: 1642: 1643: 1644: 1645:
1646: public function count()
1647: {
1648: return mysql_num_rows($this->result);
1649: }
1650: }
1651: