|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class sqlHelper |
|
5
|
|
|
*/ |
|
6
|
|
|
class sqlHelper |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @param $field |
|
11
|
|
|
* @param string $table |
|
12
|
|
|
* @return bool|float|int|string |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function tildeField($field, $table = '') |
|
15
|
|
|
{ |
|
16
|
|
|
$out = ''; |
|
17
|
|
|
if (!empty($field) && is_scalar($field)) { |
|
18
|
|
|
if (!empty($table) && is_scalar($table) && $tmp = strpos($field, $table)) { |
|
19
|
|
|
$tmp = substr($field, $tmp + strlen($table), 1); |
|
20
|
|
|
if ($tmp != '.' && $tmp != '`') { |
|
21
|
|
|
$field = $table . "." . $field; |
|
22
|
|
|
} else { |
|
23
|
|
|
$out = $field; |
|
24
|
|
|
} |
|
25
|
|
|
} elseif (empty($table) && strpos($field, "`")) { |
|
26
|
|
|
$out = $field; |
|
27
|
|
|
} |
|
28
|
|
|
if (empty($out)) { |
|
29
|
|
|
$field = explode(".", $field); |
|
30
|
|
|
foreach ($field as &$f) { |
|
31
|
|
|
$f = "`" . str_replace("`", "", $f) . "`"; |
|
32
|
|
|
} |
|
33
|
|
|
$out = implode(".", $field); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $out; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $string |
|
42
|
|
|
* @param string $mode |
|
43
|
|
|
* @return mixed|string |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function trimLogicalOp($string, $mode = '') |
|
46
|
|
|
{ |
|
47
|
|
|
$regex = 'AND|and|OR|or|\&\&|\|\||NOT|not|\!'; |
|
48
|
|
|
switch ($mode) { |
|
49
|
|
|
case 'right': |
|
50
|
|
|
$regex = '\s+(' . $regex . ')\s*$'; |
|
51
|
|
|
break; |
|
52
|
|
|
case 'left': |
|
53
|
|
|
$regex = '^\s*(' . $regex . ')\s+'; |
|
54
|
|
|
break; |
|
55
|
|
|
default: |
|
56
|
|
|
$regex = '(^\s*(' . $regex . ')\s+)|(\s+(' . $regex . ')\s*$)'; |
|
57
|
|
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return is_scalar($string) ? preg_replace("/{$regex}/", "", $string) : ""; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Экранирование строки в SQL запросе LIKE |
|
65
|
|
|
* @see: http://stackoverflow.com/a/3683868/2323306 |
|
66
|
|
|
* |
|
67
|
|
|
* @param DocumentParser $modx |
|
68
|
|
|
* @param string $field поле по которому осуществляется поиск |
|
69
|
|
|
* @param string $value искомое значение |
|
70
|
|
|
* @param string $escape экранирующий символ |
|
71
|
|
|
* @param string $tpl шаблон подстановки значения в SQL запрос |
|
72
|
|
|
* @return string строка для подстановки в SQL запрос |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function LikeEscape(DocumentParser $modx, $field, $value, $escape = '=', $tpl = '%[+value+]%') |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$str = ''; |
|
77
|
|
|
$escaped = false; |
|
78
|
|
|
if (!empty($field) && is_string($field) && is_scalar($value) && $value !== '') { |
|
79
|
|
|
$field = sqlHelper::tildeField($field); |
|
80
|
|
|
if (is_scalar($escape) && !empty($escape) && !in_array($escape, array("_", "%", "'"))) { |
|
81
|
|
|
$str = str_replace( |
|
82
|
|
|
array($escape, '_', '%'), |
|
83
|
|
|
array($escape . $escape, $escape . '_', $escape . '%'), |
|
84
|
|
|
$value |
|
85
|
|
|
); |
|
86
|
|
|
$escaped = true; |
|
87
|
|
|
} |
|
88
|
|
|
$str = $modx->db->escape($str); |
|
89
|
|
|
$str = str_replace('[+value+]', $str, $tpl); |
|
90
|
|
|
|
|
91
|
|
|
if ($escaped) { |
|
92
|
|
|
$str = "{$field} LIKE '{$str}' ESCAPE '{$escape}'"; |
|
93
|
|
|
} else { |
|
94
|
|
|
$str = "{$field} LIKE '{$str}'"; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $str; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.