SqlString.class.php
6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* SQL字符串操作
*
*/
class Util_Utils_SqlString
{
/**
* 合并一个整数型的sql in 字符串
*
* @param $list array
* @return string
*/
public static function mergeSqlIntegerInString ($list)
{
$return = '';
foreach ($list as $v)
{
$return .= (int) $v . ',';
}
return substr($return, 0, - 1);
}
/**
* 合并一个字符串型的sql in 字符串
*
* @param $list array
* @return string
*/
public static function mergeSqlInString ($list)
{
$ret = '';
foreach ($list as $v)
{
$v = addslashes($v);
$ret .= "'$v',";
}
return substr($ret, 0, - 1);
}
/**
* 在数组每个元素前头增加一个前缀
*
* @param $prefix string
* @param $list array
* @param $pairs &array 如果输入返回增加了前缀的数组和没有增加前缀数组的对应
* @return array
*/
public static function prefixValues ($prefix, $list, &$pairs = null)
{
$return = array();
foreach ($list as $v)
{
$return[$v] = $prefix . $v;
if (isset($pairs))
{
$pairs[$v] = $prefix . $v;
}
}
return $return;
}
/**
* 计算两个数组的差集
*
* @param $list1 array
* @param $list2 array
* @param $pairs array $list2 与 $list的对应 key(list1) value(list2)
* @return array
*/
public static function diffByPairs ($list1, $list2, $pairs)
{
$return = array();
foreach ($list1 as $v)
{
if (! in_array($pairs[$v], $list2))
{
$return[] = $v;
}
}
return $return;
}
/**
* 从缓存中取DAO已存的数据,根据前缀,在getByList功能性函数中使用
*
* @param $idList array
* @param $prefix string
* @param $cacheObj Q_Cache_Adapter_Memcache
* @param $listNotHit &array 如果存在则返回没有命中的List
* @return array false
*/
public static function getListFromCache ($idList, $prefix, $cacheObj, &$listNotHit = null)
{
$pairs = array();
$keys = self::prefixValues($prefix, $idList, $pairs);
$return = $cacheObj->get(array_values($keys));
$return = is_array($return) ? array_filter($return) : $return;
$realReturn = array();
if ($return)
{
$pairs1 = array_flip($pairs);
foreach ($return as $k => $v)
{
$realReturn[$pairs1[$k]] = $v;
}
if (isset($listNotHit))
{
$listNotHit = self::diffByPairs($idList, array_keys($return), $pairs);
}
}
else
{
$realReturn = false;
if (isset($listNotHit))
{
$listNotHit = $idList;
}
}
return $realReturn;
}
/**
* 将列表结果存入缓存
*
* @param $result array
* @param $prefix string
* @param $cacheObj Q_Cache_Adapter_Memcache
* @param $expire integer
* @return void
*/
public static function setListToCache ($result, $prefix, $cacheObj, $expire = 60)
{
foreach ($result as $k => $v)
{
$cacheObj->set($prefix . $k, $v, $expire);
}
}
/**
* 组合SQL更新语句
*
* @param $params array
* @return string
*/
public static function mergeSqlUpstring ($params)
{
$return = '';
foreach ($params as $k => $v)
{
$return .= $k . ' = :' . $k . ',';
}
return substr($return, 0, - 1);
}
/**
* 合并整数字串
*
* @param $params array
* @return string
*/
public static function mergeIntegerString ($params)
{
return self::mergeSqlIntegerInString($params);
}
/**
* 组合INSERT INTO table (InsertString) VALUES (InsertFields)的参数
*
* @param $params array
* @return string
*/
public static function mergeInsertString ($params)
{
$return = '';
foreach ($params as $k => $v)
{
$return .= ':' . $k . ',';
}
return substr($return, 0, - 1);
}
/**
* 组合INSERT INTO table (InsertString) VALUES (InsertFields)的参数
*
* @param $params array
* @return string
*/
public static function mergeInsertFields ($params)
{
$return = '';
foreach ($params as $k => $v)
{
$return .= $k . ',';
}
return substr($return, 0, - 1);
}
/**
* 组合SQL条件WHERE语句
*
* @example 'field1 =: field1 AND field2 = :field2 '
* Util_Utils_SqlString::mergeSqlWhereString ( array ( 'field1' => '***',
* ) );
* @example 'field1 <= :field AND '
* Util_Utils_SqlString::mergeSqlWhereString ( array ( 'field1' => '***',
* ), ' <= :', ' AND ', false, null );
* @example 'field2 LIKE ":field1%" OR field2 LIKE ":field2%" '
* Util_Utils_SqlString::mergeSqlWhereString ( array ( 'field1' => '***',
* ), ' LIKE ":', '%" OR ', true, -3 );
*
* @param $params array
* @param $sign string 连接符号 eg: '=: != >: <: <=: >=:'
* @param $separator string 条件之间分隔符 eg: ' AND OR '
* @param $section boolean 控制返回的结果是否需要截取掉分隔符 (true: 是, false: 否 )
* @param $length int 截取的长度
* @return string
*/
public static function mergeSqlWhereString ($params, $sign = '=:', $separator = ' AND ', $section = true, $length = null)
{
$return = '';
foreach ($params as $k => $v)
{
$return .= $k . $sign . $k . $separator;
}
if ($section)
{
if (! isset($length))
{
$length = 0 - strlen($separator);
}
$return = substr($return, 0, $length);
}
return $return;
}
}