Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
SqlUtils.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\db;
4
6
13class SqlUtils {
14 public static $quote = '`';
15
16 private static function getParameters($keyAndValues) {
17 $ret = array();
18 foreach ($keyAndValues as $key => $value) {
19 $ret [] = ':' . $key;
20 }
21 return $ret;
22 }
23
24 private static function getQuotedKeys($keyAndValues) {
25 $ret = array();
26 foreach ($keyAndValues as $key => $value) {
27 $ret [] = self::$quote . $key . self::$quote;
28 }
29 return $ret;
30 }
31
32 public static function getWhere($keyAndValues) {
33 $ret = array();
34 foreach ($keyAndValues as $key => $value) {
35 $ret [] = self::$quote . $key . self::$quote . '= :' . $key;
36 }
37 return \implode(' AND ', $ret);
38 }
39
40 public static function getWherePK($pkKeyAndValues) {
41 $ret = array();
42 foreach ($pkKeyAndValues as $key => $value) {
43 $ret [] = self::$quote . \trim($key, '___') . self::$quote . '= :' . $key;
44 }
45 return \implode(' AND ', $ret);
46 }
47
48 public static function getMultiWhere($values, $field) {
49 $ret = array();
50 foreach ($values as $value) {
51 $ret [] = self::$quote . $field . self::$quote . "='" . $value . "'";
52 }
53 return \implode(' OR ', $ret);
54 }
55
56 public static function getSearchWhere($likeOp, $fields, $value, $jokerBefore = '%', $jokerAfter = '%') {
57 $ret = array();
58 foreach ($fields as $field) {
59 $ret [] = self::$quote . $field . self::$quote . $likeOp . $jokerBefore . $value . $jokerAfter;
60 }
61 return \implode(' OR ', $ret);
62 }
63
64 public static function getInsertFields($keyAndValues) {
65 return \implode(',', self::getQuotedKeys($keyAndValues));
66 }
67
68 public static function getInsertFieldsValues($keyAndValues) {
69 return \implode(',', self::getParameters($keyAndValues));
70 }
71
72 public static function getUpdateFieldsKeyAndParams($keyAndValues) {
73 $ret = array();
74 foreach ($keyAndValues as $key => $value) {
75 $ret [] = self::$quote . $key . self::$quote . '= :' . $key;
76 }
77 return \implode(',', $ret);
78 }
79
80 public static function getUpdateFieldsKeyAndValues($keyAndValues) {
81 $ret = array();
82 foreach ($keyAndValues as $key => $value) {
83 $ret [] = self::$quote . $key . self::$quote . '= :' . $key;
84 }
85 return \implode(',', $ret);
86 }
87
88 public static function checkWhere($condition) {
89 if ($condition != null && \stristr($condition, ' join ') === false) {
90 $condition = ' WHERE ' . $condition;
91 }
92 return $condition;
93 }
94
95 public static function checkWhereParams($condition, &$params = []) {
96 if (\strpos($condition, '?') !== -1) {
97 foreach ($params as $k => $v) {
98 if (\is_int($k)) {
99 $params["_$k"] = $v;
100 unset($params[$k]);
101 $k = "_$k";
102 }
103 $condition = \str_replace('?', ":$k", $condition);
104 }
105 }
106 return self::checkWhere($condition);
107 }
108
109 public static function getCondition($keyValues, $classname = null, $separator = ' AND ') {
110 if (!\is_array($keyValues)) {
111 return $keyValues;
112 } else {
113 if ((\array_keys($keyValues) === \range(0, \count($keyValues) - 1))) {//Not associative array
114 if (isset ($classname)) {
115 $keys = OrmUtils::getKeyFields($classname);
116 if (\is_array($keys)) {
117 $keyValues = \array_combine($keys, $keyValues);
118 }
119 }
120 }
121 $retArray = array();
122 foreach ($keyValues as $key => $value) {
123 $retArray [] = self::$quote . $key . self::$quote . " = '" . $value . "'";
124 }
125 return \implode($separator, $retArray);
126 }
127 }
128
135 public static function getFieldList($fields, $tableName = false) {
136 if (!\is_array($fields)) {
137 return $fields;
138 }
139 $result = [];
140 $prefix = '';
141 if (\is_string($tableName)) {
142 $prefix = self::$quote . $tableName . self::$quote . '.';
143 }
144 foreach ($fields as $field) {
145 $result [] = $prefix . self::$quote . $field . self::$quote;
146 }
147 return \implode(',', $result);
148 }
149}
SQL utilities.
Definition SqlUtils.php:13
static getInsertFields($keyAndValues)
Definition SqlUtils.php:64
static getCondition($keyValues, $classname=null, $separator=' AND ')
Definition SqlUtils.php:109
static getParameters($keyAndValues)
Definition SqlUtils.php:16
static getUpdateFieldsKeyAndValues($keyAndValues)
Definition SqlUtils.php:80
static getUpdateFieldsKeyAndParams($keyAndValues)
Definition SqlUtils.php:72
static checkWhereParams($condition, &$params=[])
Definition SqlUtils.php:95
static getFieldList($fields, $tableName=false)
Definition SqlUtils.php:135
static checkWhere($condition)
Definition SqlUtils.php:88
static getQuotedKeys($keyAndValues)
Definition SqlUtils.php:24
static getSearchWhere($likeOp, $fields, $value, $jokerBefore='%', $jokerAfter='%')
Definition SqlUtils.php:56
static getWherePK($pkKeyAndValues)
Definition SqlUtils.php:40
static getWhere($keyAndValues)
Definition SqlUtils.php:32
static getMultiWhere($values, $field)
Definition SqlUtils.php:48
static getInsertFieldsValues($keyAndValues)
Definition SqlUtils.php:68
Object/relational mapping utilities.
Definition OrmUtils.php:17
Database implementation.