1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | use LucidFrame\Core\Database; |
17: | use LucidFrame\Core\QueryBuilder; |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | function db_namespace($namespace = null) |
31: | { |
32: | return _app('db')->getNamespace($namespace); |
33: | } |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | function db_config($namespace = null) |
44: | { |
45: | return _app('db')->getConfig($namespace); |
46: | } |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | function db_engine($namespace = null) |
54: | { |
55: | return _app('db')->getDriver($namespace); |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | function db_host($namespace = null) |
64: | { |
65: | return _app('db')->getHost($namespace); |
66: | } |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | function db_name($namespace = null) |
74: | { |
75: | return _app('db')->getName($namespace); |
76: | } |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | function db_user($namespace = null) |
84: | { |
85: | return _app('db')->getUser($namespace); |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | function db_prefix($namespace = null) |
94: | { |
95: | return _app('db')->getPrefix($namespace); |
96: | } |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | function db_collation($namespace = null) |
104: | { |
105: | return _app('db')->getCollation($namespace); |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | function db_prerequisite($namespace = null) |
116: | { |
117: | $db = _app('db'); |
118: | $namespace = $db->getNamespace($namespace); |
119: | |
120: | if ($db->getHost($namespace) && $db->getUser($namespace) && $db->getName($namespace)) { |
121: | return $db->getConfig($namespace); |
122: | } else { |
123: | _header(400); |
124: | throw new \InvalidArgumentException('Required to configure <code class="inline">db</code> in <code class="inline">/inc/parameters/'._cfg('env').'.php</code>.'); |
125: | } |
126: | } |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | function db_switch($namespace = null) |
134: | { |
135: | _app('db', new Database($namespace)); |
136: | } |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | function db_close() |
143: | { |
144: | _app('db')->close(); |
145: | } |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | function db_prq($enable = true) |
156: | { |
157: | _g('db_printQuery', $enable); |
158: | } |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | function db_query($sql, $args = array()) |
174: | { |
175: | return _app('db')->query($sql, $args); |
176: | } |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | function db_queryStr() |
185: | { |
186: | return _app('db')->getQueryStr(); |
187: | } |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | function db_error() |
194: | { |
195: | return _app('db')->getError(); |
196: | } |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | function db_errorNo() |
203: | { |
204: | return _app('db')->getErrorCode(); |
205: | } |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | function db_numRows($result) |
213: | { |
214: | return _app('db')->getNumRows($result); |
215: | } |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | function db_fetchArray($result) |
224: | { |
225: | return _app('db')->fetchArray($result); |
226: | } |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: | function db_fetchAssoc($result) |
234: | { |
235: | return _app('db')->fetchAssoc($result); |
236: | } |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | function db_fetchObject($result) |
244: | { |
245: | return _app('db')->fetchObject($result); |
246: | } |
247: | |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | function db_insertId() |
254: | { |
255: | return _app('db')->getInsertId(); |
256: | } |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | function db_insertSlug() |
263: | { |
264: | return session_get('lastInsertSlug'); |
265: | } |
266: | |
267: | |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | |
274: | |
275: | function db_select($table, $alias = null) |
276: | { |
277: | return new QueryBuilder($table, $alias); |
278: | } |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: | |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | function db_count($arg1, $arg2 = null, $arg3 = null) |
298: | { |
299: | return _app('db')->getCount($arg1, $arg2, $arg3); |
300: | } |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | function db_max($table, $field, $alias = null) |
312: | { |
313: | $qb = new QueryBuilder($table); |
314: | |
315: | return $qb->max($field, $alias ? $alias : 'max'); |
316: | } |
317: | |
318: | |
319: | |
320: | |
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | function db_min($table, $field, $alias = null) |
328: | { |
329: | $qb = new QueryBuilder($table); |
330: | |
331: | return $qb->min($field, $alias ? $alias : 'min'); |
332: | } |
333: | |
334: | |
335: | |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | function db_sum($table, $field, $alias = null) |
344: | { |
345: | $qb = new QueryBuilder($table); |
346: | |
347: | return $qb->sum($field, $alias ? $alias : 'sum'); |
348: | } |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | function db_avg($table, $field, $alias = null) |
360: | { |
361: | $qb = new QueryBuilder($table); |
362: | |
363: | return $qb->avg($field, $alias ? $alias : 'avg'); |
364: | } |
365: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | |
373: | |
374: | |
375: | |
376: | |
377: | |
378: | |
379: | |
380: | |
381: | |
382: | function db_fetch($sql, $args = array()) |
383: | { |
384: | return _app('db')->fetchColumn($sql, $args); |
385: | } |
386: | |
387: | |
388: | |
389: | |
390: | |
391: | |
392: | |
393: | |
394: | |
395: | |
396: | |
397: | |
398: | |
399: | |
400: | |
401: | |
402: | |
403: | function db_fetchResult($sql, $args = array()) |
404: | { |
405: | return _app('db')->fetchResult($sql, $args); |
406: | } |
407: | |
408: | |
409: | |
410: | |
411: | |
412: | |
413: | |
414: | |
415: | |
416: | |
417: | |
418: | |
419: | |
420: | |
421: | function db_extract($sql, $args = array(), $resultType = LC_FETCH_OBJECT) |
422: | { |
423: | return _app('db')->fetchAll($sql, $args, $resultType); |
424: | } |
425: | |
426: | |
427: | |
428: | |
429: | |
430: | |
431: | function db_table($table) |
432: | { |
433: | return _app('db')->getTable($table); |
434: | } |
435: | |
436: | |
437: | |
438: | |
439: | |
440: | |
441: | |
442: | |
443: | function db_tableHasSlug($table, $useSlug = true) |
444: | { |
445: | return _app('db')->hasSlug($table, $useSlug); |
446: | } |
447: | |
448: | |
449: | |
450: | |
451: | |
452: | |
453: | |
454: | function db_tableHasTimestamps($table) |
455: | { |
456: | return _app('db')->hasTimestamps($table); |
457: | } |
458: | |
459: | |
460: | |
461: | |
462: | |
463: | |
464: | |
465: | |
466: | |
467: | |
468: | |
469: | |
470: | |
471: | |
472: | |
473: | |
474: | |
475: | |
476: | |
477: | |
478: | |
479: | |
480: | |
481: | |
482: | |
483: | |
484: | |
485: | |
486: | function db_save($table, $data = array(), $id = 0, $useSlug = true, array $condition = array()) |
487: | { |
488: | if ($id) { |
489: | $data = array_merge(array('id' => $id), $data); |
490: | |
491: | if (db_update($table, $data, $useSlug, $condition)) { |
492: | return $id; |
493: | } |
494: | |
495: | return false; |
496: | } else { |
497: | return db_insert($table, $data, $useSlug); |
498: | } |
499: | } |
500: | |
501: | if (!function_exists('db_insert')) { |
502: | |
503: | |
504: | |
505: | |
506: | |
507: | |
508: | |
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: | |
516: | |
517: | function db_insert($table, $data = array(), $useSlug = true) |
518: | { |
519: | QueryBuilder::clearBindValues(); |
520: | |
521: | if (count($data) == 0) { |
522: | return false; |
523: | } |
524: | |
525: | $db = _app('db'); |
526: | |
527: | $table = db_table($table); |
528: | |
529: | |
530: | $hook = 'db_insert_' . strtolower($table); |
531: | if (function_exists($hook)) { |
532: | return call_user_func_array($hook, array($table, $data, $useSlug)); |
533: | } |
534: | |
535: | |
536: | if (array_key_exists('slug', $data)) { |
537: | $slug = _slug($data['slug']); |
538: | $data['slug'] = $slug; |
539: | session_set('lastInsertSlug', $slug); |
540: | $useSlug = false; |
541: | } |
542: | |
543: | $dsm = $db->schemaManager; |
544: | if (is_object($dsm) && $dsm->isLoaded()) { |
545: | foreach ($data as $field => $value) { |
546: | $fieldType = $db->schemaManager->getFieldType($table, $field); |
547: | if (is_array($value) && $fieldType == 'array') { |
548: | $data[$field] = serialize($value); |
549: | } elseif (is_array($value) && $fieldType == 'json') { |
550: | $jsonValue = json_encode($value); |
551: | $data[$field] = $jsonValue ? $jsonValue : null; |
552: | } elseif ($fieldType == 'boolean') { |
553: | $data[$field] = $value ? 1 : 0; |
554: | } |
555: | } |
556: | } |
557: | |
558: | $fields = array_keys($data); |
559: | $dataValues = array_values($data); |
560: | |
561: | if (db_tableHasSlug($table, $useSlug)) { |
562: | $fields[] = 'slug'; |
563: | } |
564: | |
565: | if (db_tableHasTimestamps($table)) { |
566: | if (!array_key_exists('created', $data)) { |
567: | $fields[] = 'created'; |
568: | } |
569: | if (!array_key_exists('updated', $data)) { |
570: | $fields[] = 'updated'; |
571: | } |
572: | } |
573: | |
574: | $fields = array_unique($fields); |
575: | |
576: | $sqlFields = implode(', ', $fields); |
577: | $placeHolders = implode(', ', array_fill(0, count($fields), '?')); |
578: | $values = array(); |
579: | $i = 0; |
580: | |
581: | |
582: | foreach ($dataValues as $val) { |
583: | if ($i == 0 && $useSlug) { |
584: | $slug = $val; |
585: | } |
586: | |
587: | $values[] = is_null($val) ? null : $val; |
588: | |
589: | $i++; |
590: | } |
591: | |
592: | if (db_tableHasSlug($table, $useSlug)) { |
593: | $slug = _slug($slug, $table); |
594: | session_set('lastInsertSlug', $slug); |
595: | $values[] = $slug; |
596: | } |
597: | |
598: | if (db_tableHasTimestamps($table)) { |
599: | if (!array_key_exists('created', $data)) { |
600: | $values[] = date('Y-m-d H:i:s'); |
601: | } |
602: | if (!array_key_exists('updated', $data)) { |
603: | $values[] = date('Y-m-d H:i:s'); |
604: | } |
605: | } |
606: | |
607: | $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', QueryBuilder::quote($table), $sqlFields, $placeHolders); |
608: | |
609: | return db_query($sql, $values) ? db_insertId() : false; |
610: | } |
611: | } |
612: | |
613: | if (!function_exists('db_update')) { |
614: | |
615: | |
616: | |
617: | |
618: | |
619: | |
620: | |
621: | |
622: | |
623: | |
624: | |
625: | |
626: | |
627: | |
628: | |
629: | |
630: | |
631: | |
632: | |
633: | |
634: | |
635: | |
636: | |
637: | |
638: | |
639: | |
640: | |
641: | |
642: | |
643: | |
644: | |
645: | |
646: | |
647: | |
648: | |
649: | |
650: | |
651: | |
652: | |
653: | function db_update($table, $data = array(), $useSlug = true, array $condition = array()) |
654: | { |
655: | QueryBuilder::clearBindValues(); |
656: | |
657: | if (count($data) == 0) { |
658: | return false; |
659: | } |
660: | |
661: | $db = _app('db'); |
662: | |
663: | if (func_num_args() === 3 && (gettype($useSlug) === 'string' || is_array($useSlug))) { |
664: | $condition = $useSlug; |
665: | $useSlug = true; |
666: | } |
667: | |
668: | $table = db_table($table); |
669: | |
670: | |
671: | $hook = 'db_update_' . strtolower($table); |
672: | if (function_exists($hook)) { |
673: | return call_user_func_array($hook, array($table, $data, $useSlug, $condition)); |
674: | } |
675: | |
676: | |
677: | if (array_key_exists('slug', $data)) { |
678: | $slug = _slug($data['slug']); |
679: | $data['slug'] = $slug; |
680: | session_set('lastInsertSlug', $slug); |
681: | $useSlug = false; |
682: | } |
683: | |
684: | $fields = array(); |
685: | $slug = ''; |
686: | $cond = ''; |
687: | $i = 0; |
688: | $slugIndex = 1; |
689: | |
690: | if ($condition) { |
691: | $slugIndex = 0; |
692: | } |
693: | |
694: | $dsm = $db->schemaManager; |
695: | foreach ($data as $field => $value) { |
696: | if ($i === 0 && !$condition) { |
697: | |
698: | $cond = array($field => $value); |
699: | $i++; |
700: | continue; |
701: | } |
702: | |
703: | if (is_object($dsm) && $dsm->isLoaded()) { |
704: | $fieldType = $dsm->getFieldType($table, $field); |
705: | if (is_array($value) && $fieldType == 'array') { |
706: | $value = serialize($value); |
707: | } elseif (is_array($value) && $fieldType == 'json') { |
708: | $jsonValue = json_encode($value); |
709: | $value = $jsonValue ? $jsonValue : null; |
710: | } elseif ($fieldType == 'boolean') { |
711: | $value = $value ? 1 : 0; |
712: | } |
713: | } |
714: | |
715: | $fields[$field] = is_null($value) ? null : $value; |
716: | |
717: | if ($i === $slugIndex && $useSlug === true) { |
718: | |
719: | $slug = $value; |
720: | } |
721: | |
722: | $i++; |
723: | } |
724: | |
725: | |
726: | |
727: | if ($cond || $condition) { |
728: | $clause = ''; |
729: | $notCond = array(); |
730: | $values = array(); |
731: | |
732: | if ($cond && is_array($cond) && count($cond)) { |
733: | QueryBuilder::clearBindValues(); |
734: | list($clause, $values) = db_condition($cond); |
735: | $notCond = array( |
736: | '$not' => $cond |
737: | ); |
738: | } elseif ($condition && is_array($condition) && count($condition)) { |
739: | QueryBuilder::clearBindValues(); |
740: | list($clause, $values) = db_condition($condition); |
741: | $notCond = array( |
742: | '$not' => $condition |
743: | ); |
744: | } |
745: | |
746: | if (empty($clause)) { |
747: | return false; |
748: | } |
749: | |
750: | if (db_tableHasSlug($table, $useSlug)) { |
751: | $slug = _slug($slug, $table, $notCond); |
752: | session_set('lastInsertSlug', $slug); |
753: | $fields['slug'] = $slug; |
754: | } |
755: | |
756: | if (db_tableHasTimestamps($table)) { |
757: | $fields['updated'] = date('Y-m-d H:i:s'); |
758: | } |
759: | |
760: | $sql = 'UPDATE ' . QueryBuilder::quote($table) . ' SET '; |
761: | foreach ($fields as $key => $value) { |
762: | $placeholder = ':upd_' . $key; |
763: | $sql .= sprintf('`%s` = %s, ', $key, $placeholder); |
764: | $values[$placeholder] = $value; |
765: | } |
766: | $sql = rtrim($sql, ', '); |
767: | $sql .= ' WHERE ' . $clause; |
768: | |
769: | return db_query($sql, $values) ? true : false; |
770: | } |
771: | |
772: | return false; |
773: | } |
774: | } |
775: | |
776: | if (!function_exists('db_delete')) { |
777: | |
778: | |
779: | |
780: | |
781: | |
782: | |
783: | |
784: | |
785: | |
786: | |
787: | |
788: | |
789: | |
790: | |
791: | |
792: | |
793: | |
794: | |
795: | |
796: | |
797: | |
798: | |
799: | |
800: | |
801: | |
802: | |
803: | |
804: | |
805: | function db_delete($table, array $condition = array(), $softDelete = false) |
806: | { |
807: | QueryBuilder::clearBindValues(); |
808: | |
809: | $table = db_table($table); |
810: | |
811: | |
812: | $hook = 'db_delete_' . strtolower($table); |
813: | if (function_exists($hook)) { |
814: | return call_user_func_array($hook, array($table, $condition)); |
815: | } |
816: | |
817: | $values = array(); |
818: | |
819: | if (is_array($condition)) { |
820: | list($condition, $values) = db_condition($condition); |
821: | } |
822: | |
823: | if ($condition) { |
824: | $condition = ' WHERE '.$condition; |
825: | } |
826: | |
827: | if ($softDelete) { |
828: | $sql = 'UPDATE '. QueryBuilder::quote($table) . ' |
829: | SET `deleted` = :deleted ' . $condition . ' |
830: | LIMIT 1'; |
831: | $values[':deleted'] = date('Y-m-d H:i:s'); |
832: | if (_g('db_printQuery')) { |
833: | return $sql; |
834: | } |
835: | |
836: | return db_query($sql, $values) ? true : false; |
837: | } |
838: | |
839: | $sql = 'DELETE FROM ' . QueryBuilder::quote($table) . $condition . ' LIMIT 1'; |
840: | if (_g('db_printQuery')) { |
841: | return $sql; |
842: | } |
843: | |
844: | ob_start(); |
845: | db_query($sql, $values); |
846: | $return = ob_get_clean(); |
847: | if ($return) { |
848: | |
849: | if (db_errorNo() == 1451) { |
850: | if (db_tableHasTimestamps($table)) { |
851: | $sql = 'UPDATE '. QueryBuilder::quote($table) . ' |
852: | SET `deleted` = :deleted ' . $condition . ' |
853: | LIMIT 1'; |
854: | $values[':deleted'] = date('Y-m-d H:i:s'); |
855: | |
856: | return db_query($sql, $values); |
857: | } |
858: | |
859: | return false; |
860: | } else { |
861: | echo $return; |
862: | |
863: | return false; |
864: | } |
865: | } |
866: | |
867: | return db_errorNo() == 0; |
868: | } |
869: | } |
870: | |
871: | if (!function_exists('db_delete_multi')) { |
872: | |
873: | |
874: | |
875: | |
876: | |
877: | |
878: | |
879: | |
880: | |
881: | |
882: | |
883: | |
884: | |
885: | |
886: | |
887: | |
888: | |
889: | |
890: | |
891: | |
892: | |
893: | |
894: | |
895: | |
896: | |
897: | |
898: | |
899: | function db_delete_multi($table, $condition = null, $softDelete = false) |
900: | { |
901: | QueryBuilder::clearBindValues(); |
902: | |
903: | $table = db_table($table); |
904: | |
905: | |
906: | $hook = 'db_delete_multi_' . strtolower($table); |
907: | if (function_exists($hook)) { |
908: | return call_user_func_array($hook, array($table, $condition)); |
909: | } |
910: | |
911: | $values = array(); |
912: | if (is_array($condition)) { |
913: | list($condition, $values) = db_condition($condition); |
914: | } |
915: | |
916: | if ($condition) { |
917: | $condition = ' WHERE '. $condition; |
918: | } |
919: | |
920: | if ($softDelete) { |
921: | $sql = 'UPDATE '. QueryBuilder::quote($table) . ' |
922: | SET `deleted` = :deleted ' . $condition; |
923: | $values[':deleted'] = date('Y-m-d H:i:s'); |
924: | if (_g('db_printQuery')) { |
925: | return $sql; |
926: | } |
927: | |
928: | return db_query($sql, $values); |
929: | } |
930: | |
931: | $sql = 'DELETE FROM ' . QueryBuilder::quote($table) . $condition; |
932: | if (_g('db_printQuery')) { |
933: | return $sql; |
934: | } |
935: | |
936: | ob_start(); |
937: | db_query($sql, $values); |
938: | $return = ob_get_clean(); |
939: | |
940: | if ($return && db_errorNo() > 0) { |
941: | |
942: | return false; |
943: | } |
944: | |
945: | return db_errorNo() == 0; |
946: | } |
947: | } |
948: | |
949: | if (!function_exists('db_truncate')) { |
950: | |
951: | |
952: | |
953: | |
954: | function db_truncate($table) |
955: | { |
956: | $table = db_table($table); |
957: | |
958: | db_query('TRUNCATE ' . QueryBuilder::quote($table)); |
959: | } |
960: | } |
961: | |
962: | |
963: | |
964: | |
965: | |
966: | function db_setForeignKeyCheck($flag) |
967: | { |
968: | db_query('SET FOREIGN_KEY_CHECKS =' . $flag); |
969: | } |
970: | |
971: | |
972: | |
973: | |
974: | function db_enableForeignKeyCheck() |
975: | { |
976: | db_setForeignKeyCheck(1); |
977: | } |
978: | |
979: | |
980: | |
981: | |
982: | function db_disableForeignKeyCheck() |
983: | { |
984: | db_setForeignKeyCheck(0); |
985: | } |
986: | |
987: | |
988: | |
989: | |
990: | |
991: | |
992: | |
993: | |
994: | |
995: | |
996: | |
997: | |
998: | |
999: | |
1000: | |
1001: | |
1002: | |
1003: | |
1004: | |
1005: | |
1006: | |
1007: | function db_condition($cond = array(), $type = 'AND') |
1008: | { |
1009: | return QueryBuilder::buildCondition($cond, $type); |
1010: | } |
1011: | |
1012: | |
1013: | |
1014: | |
1015: | |
1016: | |
1017: | |
1018: | |
1019: | |
1020: | |
1021: | |
1022: | |
1023: | |
1024: | |
1025: | |
1026: | |
1027: | |
1028: | |
1029: | |
1030: | function db_and($condition = array()) |
1031: | { |
1032: | return db_condition($condition, 'AND'); |
1033: | } |
1034: | |
1035: | |
1036: | |
1037: | |
1038: | |
1039: | |
1040: | |
1041: | |
1042: | |
1043: | |
1044: | |
1045: | |
1046: | |
1047: | |
1048: | |
1049: | |
1050: | |
1051: | |
1052: | |
1053: | function db_or($condition = array()) |
1054: | { |
1055: | return db_condition($condition, 'OR'); |
1056: | } |
1057: | |
1058: | |
1059: | |
1060: | |
1061: | function db_transaction() |
1062: | { |
1063: | db_query('SET AUTOCOMMIT=0'); |
1064: | db_query('START TRANSACTION'); |
1065: | } |
1066: | |
1067: | |
1068: | |
1069: | |
1070: | function db_commit() |
1071: | { |
1072: | db_query('COMMIT'); |
1073: | db_query('SET AUTOCOMMIT=1'); |
1074: | } |
1075: | |
1076: | |
1077: | |
1078: | |
1079: | function db_rollback() |
1080: | { |
1081: | db_query('ROLLBACK'); |
1082: | db_query('SET AUTOCOMMIT=1'); |
1083: | } |
1084: | |
1085: | |
1086: | |
1087: | |
1088: | |
1089: | |
1090: | |
1091: | function db_raw($exp, array $values = array()) |
1092: | { |
1093: | return QueryBuilder::raw($exp, $values); |
1094: | } |
1095: | |
1096: | |
1097: | |
1098: | |
1099: | |
1100: | |
1101: | |
1102: | |
1103: | |
1104: | |
1105: | |
1106: | |
1107: | |
1108: | |
1109: | |
1110: | |
1111: | |
1112: | |
1113: | |
1114: | function db_exp($field, $value, $exp = '') |
1115: | { |
1116: | return _app('db')->exp($field, $value, $exp); |
1117: | } |
1118: | |
1119: | |
1120: | |
1121: | |
1122: | |
1123: | |
1124: | |
1125: | |
1126: | function db_find($table, $id, $fields = []) |
1127: | { |
1128: | $qb = db_select($table)->where()->condition('id', $id); |
1129: | |
1130: | if (!empty($fields)) { |
1131: | $qb->fields($table, $fields); |
1132: | } |
1133: | |
1134: | if (db_tableHasTimestamps($table)) { |
1135: | $qb->condition('deleted', null); |
1136: | } |
1137: | |
1138: | $entity = $qb->getSingleResult(); |
1139: | |
1140: | if ($entity) { |
1141: | $schema = _schema(_cfg('defaultDbSource'), true); |
1142: | $entity = (array) $entity; |
1143: | if ($schema && isset($schema[$table])) { |
1144: | foreach ($entity as $field => $value) { |
1145: | switch ($schema[$table][$field]['type']) { |
1146: | case 'int': |
1147: | case 'integer': |
1148: | case 'smallint': |
1149: | case 'mediumint': |
1150: | case 'bigint': |
1151: | $entity[$field] = is_numeric($value) ? (int) $value : $value; |
1152: | break; |
1153: | |
1154: | case 'boolean': |
1155: | $entity[$field] = (bool) $value; |
1156: | break; |
1157: | |
1158: | case 'array': |
1159: | $entity[$field] = $value ? unserialize($value) : array(); |
1160: | break; |
1161: | |
1162: | case 'json': |
1163: | $entity[$field] = $value ? json_decode($value, true) : array(); |
1164: | break; |
1165: | } |
1166: | } |
1167: | } |
1168: | $entity = (object) $entity; |
1169: | } |
1170: | |
1171: | return $entity ?: null; |
1172: | } |
1173: | |
1174: | |
1175: | |
1176: | |
1177: | |
1178: | |
1179: | |
1180: | |
1181: | |
1182: | function db_findOrFail($table, $id, $fields = []) |
1183: | { |
1184: | $result = db_find($table, $id, $fields); |
1185: | |
1186: | if (!$result) { |
1187: | _page404(_t('The entity %s not found.', $table), $table); |
1188: | } |
1189: | |
1190: | return $result; |
1191: | } |
1192: | |
1193: | |
1194: | |
1195: | |
1196: | |
1197: | |
1198: | |
1199: | |
1200: | |
1201: | |
1202: | |
1203: | |
1204: | |
1205: | |
1206: | |
1207: | |
1208: | |
1209: | |
1210: | |
1211: | |
1212: | |
1213: | |
1214: | |
1215: | |
1216: | |
1217: | |
1218: | |
1219: | |
1220: | |
1221: | |
1222: | |
1223: | |
1224: | |
1225: | |
1226: | |
1227: | |
1228: | |
1229: | function db_findWithPager($table, array $condition = array(), array $orderBy = array(), array $pagerOptions = array()) |
1230: | { |
1231: | if (db_tableHasTimestamps($table)) { |
1232: | $condition['deleted'] = null; |
1233: | } |
1234: | |
1235: | |
1236: | $countQuery = db_count($table); |
1237: | if (!empty($condition)) { |
1238: | $countQuery->where($condition); |
1239: | } |
1240: | $rowCount = $countQuery->fetch(); |
1241: | |
1242: | |
1243: | $pagerOptions = array_merge(array( |
1244: | 'itemsPerPage' => _cfg('itemsPerPage'), |
1245: | 'pageNumLimit' => _cfg('pageNumLimit'), |
1246: | 'ajax' => true |
1247: | ), $pagerOptions); |
1248: | |
1249: | $pager = _pager(); |
1250: | $pager->set('total', $rowCount); |
1251: | foreach ($pagerOptions as $name => $value) { |
1252: | $pager->set($name, $value); |
1253: | } |
1254: | $pager->calculate(); |
1255: | |
1256: | |
1257: | $qb = db_select($table); |
1258: | if (!empty($condition)) { |
1259: | $qb->where($condition); |
1260: | } |
1261: | $qb->limit($pager->get('offset'), $pager->get('itemsPerPage')); |
1262: | |
1263: | foreach ($orderBy as $field => $sort) { |
1264: | if (is_numeric($field)) { |
1265: | $qb->orderBy(db_raw($sort)); |
1266: | } else { |
1267: | $qb->orderBy($field, $sort); |
1268: | } |
1269: | } |
1270: | |
1271: | return array($qb, $pager, $rowCount); |
1272: | } |
1273: | |
1274: | |
1275: | |
1276: | |
1277: | |
1278: | |
1279: | |
1280: | |
1281: | |
1282: | |
1283: | |
1284: | |
1285: | |
1286: | |
1287: | |
1288: | |
1289: | |
1290: | |
1291: | |
1292: | |
1293: | |
1294: | |
1295: | |
1296: | |
1297: | |
1298: | |
1299: | |
1300: | |
1301: | |
1302: | |
1303: | |
1304: | |
1305: | |
1306: | |
1307: | |
1308: | |
1309: | |
1310: | |
1311: | function db_findBy($table, array $condition, array $orderBy = array(), $limit = null) |
1312: | { |
1313: | if (db_tableHasTimestamps($table)) { |
1314: | $condition['deleted'] = null; |
1315: | } |
1316: | |
1317: | $qb = db_select($table)->where($condition); |
1318: | |
1319: | foreach ($orderBy as $field => $sort) { |
1320: | if (is_numeric($field)) { |
1321: | $qb->orderBy(db_raw($sort)); |
1322: | } else { |
1323: | $qb->orderBy($field, $sort); |
1324: | } |
1325: | } |
1326: | |
1327: | if ($limit) { |
1328: | $qb->limit($limit); |
1329: | } |
1330: | |
1331: | return $qb->getResult(); |
1332: | } |
1333: | |
1334: | |
1335: | |
1336: | |
1337: | |
1338: | |
1339: | |
1340: | |
1341: | |
1342: | |
1343: | |
1344: | |
1345: | |
1346: | |
1347: | |
1348: | |
1349: | |
1350: | |
1351: | |
1352: | |
1353: | |
1354: | |
1355: | |
1356: | |
1357: | |
1358: | |
1359: | |
1360: | |
1361: | |
1362: | |
1363: | |
1364: | |
1365: | function db_findOneBy($table, array $condition, array $orderBy = array()) |
1366: | { |
1367: | $result = db_findBy($table, $condition, $orderBy, 1); |
1368: | if (!empty($result)) { |
1369: | return $result[0]; |
1370: | } |
1371: | |
1372: | return null; |
1373: | } |
1374: | |
1375: | |
1376: | |
1377: | |
1378: | |
1379: | |
1380: | |
1381: | |
1382: | |
1383: | |
1384: | |
1385: | |
1386: | |
1387: | |
1388: | |
1389: | |
1390: | |
1391: | |
1392: | |
1393: | |
1394: | |
1395: | |
1396: | |
1397: | |
1398: | |
1399: | |
1400: | |
1401: | |
1402: | |
1403: | |
1404: | |
1405: | |
1406: | function db_findOneByOrFail($table, array $condition, array $orderBy = array()) |
1407: | { |
1408: | $result = db_findOneBy($table, $condition, $orderBy); |
1409: | if (empty($result)) { |
1410: | _page404(); |
1411: | } |
1412: | |
1413: | return $result; |
1414: | } |
1415: | |
1416: | |
1417: | |
1418: | |
1419: | |
1420: | |
1421: | |
1422: | |
1423: | |
1424: | |
1425: | |
1426: | |
1427: | |
1428: | |
1429: | |
1430: | |
1431: | |
1432: | function db_findAll($table, $fields = array(), $orderBy = array()) |
1433: | { |
1434: | $qb = db_select($table); |
1435: | |
1436: | if (db_tableHasTimestamps($table)) { |
1437: | $qb->where()->condition('deleted', null); |
1438: | } |
1439: | |
1440: | if (!empty($fields)) { |
1441: | $qb->fields($table, $fields); |
1442: | } |
1443: | |
1444: | if (!empty($orderBy)) { |
1445: | foreach ($orderBy as $field => $sort) { |
1446: | if (is_numeric($field)) { |
1447: | $qb->orderBy(db_raw($sort)); |
1448: | } else { |
1449: | $qb->orderBy($field, $sort); |
1450: | } |
1451: | } |
1452: | } |
1453: | |
1454: | return $qb->getResult(); |
1455: | } |
1456: | |
1457: | |
1458: | |
1459: | |
1460: | |
1461: | |
1462: | |
1463: | |
1464: | |
1465: | function db_findColumn($table, $field, array $condition) |
1466: | { |
1467: | $entity = db_findOneBy($table, $condition); |
1468: | |
1469: | return $entity ? $entity->$field : null; |
1470: | } |
1471: | |