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: | function db_findWithPager($table, array $condition = array(), array $orderBy = array(), array $pagerOptions = array())
|
1226: | {
|
1227: | if (db_tableHasTimestamps($table)) {
|
1228: | $condition['deleted'] = null;
|
1229: | }
|
1230: |
|
1231: |
|
1232: | $countQuery = db_count($table);
|
1233: | if (!empty($condition)) {
|
1234: | $countQuery->where($condition);
|
1235: | }
|
1236: | $rowCount = $countQuery->fetch();
|
1237: |
|
1238: |
|
1239: | $pagerOptions = array_merge(array(
|
1240: | 'itemsPerPage' => _cfg('itemsPerPage'),
|
1241: | 'pageNumLimit' => _cfg('pageNumLimit'),
|
1242: | 'ajax' => true
|
1243: | ), $pagerOptions);
|
1244: |
|
1245: | $pager = _pager();
|
1246: | $pager->set('total', $rowCount);
|
1247: | foreach ($pagerOptions as $name => $value) {
|
1248: | $pager->set($name, $value);
|
1249: | }
|
1250: | $pager->calculate();
|
1251: |
|
1252: |
|
1253: | $qb = db_select($table);
|
1254: | if (!empty($condition)) {
|
1255: | $qb->where($condition);
|
1256: | }
|
1257: | $qb->limit($pager->get('offset'), $pager->get('itemsPerPage'));
|
1258: |
|
1259: | foreach ($orderBy as $field => $sort) {
|
1260: | $qb->orderBy($field, $sort);
|
1261: | }
|
1262: |
|
1263: | return array($qb, $pager, $rowCount);
|
1264: | }
|
1265: |
|
1266: | |
1267: | |
1268: | |
1269: | |
1270: | |
1271: | |
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: | function db_findBy($table, array $condition, array $orderBy = array(), $limit = null)
|
1299: | {
|
1300: | if (db_tableHasTimestamps($table)) {
|
1301: | $condition['deleted'] = null;
|
1302: | }
|
1303: |
|
1304: | $qb = db_select($table)->where($condition);
|
1305: |
|
1306: | foreach ($orderBy as $field => $sort) {
|
1307: | $qb->orderBy($field, $sort);
|
1308: | }
|
1309: |
|
1310: | if ($limit) {
|
1311: | $qb->limit($limit);
|
1312: | }
|
1313: |
|
1314: | return $qb->getResult();
|
1315: | }
|
1316: |
|
1317: | |
1318: | |
1319: | |
1320: | |
1321: | |
1322: | |
1323: | |
1324: | |
1325: | |
1326: | |
1327: | |
1328: | |
1329: | |
1330: | |
1331: | |
1332: | |
1333: | |
1334: | |
1335: | |
1336: | |
1337: | |
1338: | |
1339: | |
1340: | |
1341: | |
1342: | |
1343: | |
1344: | |
1345: | |
1346: | |
1347: |
|
1348: | function db_findOneBy($table, array $condition, array $orderBy = array())
|
1349: | {
|
1350: | $result = db_findBy($table, $condition, $orderBy, 1);
|
1351: | if (!empty($result)) {
|
1352: | return $result[0];
|
1353: | }
|
1354: |
|
1355: | return null;
|
1356: | }
|
1357: |
|
1358: | |
1359: | |
1360: | |
1361: | |
1362: | |
1363: | |
1364: | |
1365: | |
1366: | |
1367: | |
1368: | |
1369: | |
1370: | |
1371: | |
1372: | |
1373: | |
1374: | |
1375: | |
1376: | |
1377: | |
1378: | |
1379: | |
1380: | |
1381: | |
1382: | |
1383: | |
1384: | |
1385: | |
1386: | |
1387: | |
1388: |
|
1389: | function db_findOneByOrFail($table, array $condition, array $orderBy = array())
|
1390: | {
|
1391: | $result = db_findOneBy($table, $condition, $orderBy);
|
1392: | if (empty($result)) {
|
1393: | _page404();
|
1394: | }
|
1395: |
|
1396: | return $result;
|
1397: | }
|
1398: |
|
1399: | |
1400: | |
1401: | |
1402: | |
1403: | |
1404: | |
1405: | |
1406: | |
1407: | |
1408: | |
1409: | |
1410: |
|
1411: | function db_findAll($table, $fields = array(), $orderBy = array())
|
1412: | {
|
1413: | $qb = db_select($table);
|
1414: |
|
1415: | if (db_tableHasTimestamps($table)) {
|
1416: | $qb->where()->condition('deleted', null);
|
1417: | }
|
1418: |
|
1419: | if (!empty($fields)) {
|
1420: | $qb->fields($table, $fields);
|
1421: | }
|
1422: |
|
1423: | if (!empty($orderBy)) {
|
1424: | foreach ($orderBy as $field => $sort) {
|
1425: | $qb->orderBy($field, $sort);
|
1426: | }
|
1427: | }
|
1428: |
|
1429: | return $qb->getResult();
|
1430: | }
|
1431: |
|
1432: | |
1433: | |
1434: | |
1435: | |
1436: | |
1437: | |
1438: | |
1439: |
|
1440: | function db_findColumn($table, $field, array $condition)
|
1441: | {
|
1442: | $entity = db_findOneBy($table, $condition);
|
1443: |
|
1444: | return $entity ? $entity->$field : null;
|
1445: | }
|
1446: | |