1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DbSqlAware { |
4
|
|
|
/** |
5
|
|
|
* @var db_mysql $db |
6
|
|
|
*/ |
7
|
|
|
protected $db; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* DbSqlAware constructor. |
11
|
|
|
* |
12
|
|
|
* @param db_mysql|null $db |
13
|
|
|
*/ |
14
|
2 |
|
public function __construct($db = null) { |
15
|
2 |
|
$this->db = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db; |
16
|
2 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param db_mysql|null $db |
20
|
|
|
* |
21
|
|
|
* @return static |
22
|
|
|
*/ |
23
|
2 |
|
public static function build($db = null) { |
24
|
2 |
|
return new static($db); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return db_mysql|null |
29
|
|
|
*/ |
30
|
|
|
public function getDb() { |
31
|
|
|
return $this->db; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param db_mysql|null $db |
36
|
|
|
*/ |
37
|
|
|
public function setDb($db) { |
38
|
|
|
$this->db = $db; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Just escapes string used DB string escape method - or a drop-in replacement addslashes() if not DB specified |
43
|
|
|
* |
44
|
|
|
* @param string $string |
45
|
|
|
* |
46
|
|
|
* @return mixed|string |
47
|
|
|
*/ |
48
|
1 |
|
protected function escapeString($string) { |
49
|
|
|
return |
50
|
1 |
|
method_exists($this->db, 'db_escape') |
51
|
1 |
|
? $this->db->db_escape($string) |
52
|
1 |
|
: addslashes($string); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Quotes string by ref as DDL ID (i.e. field or table name) - except '*' which not quoted |
57
|
|
|
* |
58
|
|
|
* @param string &$string |
59
|
|
|
*/ |
60
|
7 |
|
protected function quoteFieldSimpleByRef(&$string) { |
61
|
7 |
|
$string = $this->escapeString($string); |
62
|
7 |
|
if ((string)$string && '*' != $string) { |
63
|
4 |
|
$string = '`' . $string . '`'; |
64
|
4 |
|
} |
65
|
7 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Escapes field - include fully qualified field like table.field, table.* and mask '*' |
69
|
|
|
* |
70
|
|
|
* @param string $string |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
5 |
|
protected function quoteField($string) { |
75
|
|
|
// Checking if string is just a '*' - to skip some code |
76
|
5 |
|
if ($string != '*') { |
77
|
4 |
|
$temp = explode('.', $string); |
78
|
4 |
|
array_walk($temp, array($this, 'quoteFieldSimpleByRef')); |
79
|
4 |
|
$string = implode('.', $temp); |
80
|
4 |
|
} |
81
|
|
|
|
82
|
5 |
|
return $string; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Making and alias from fully qualified field name prepending with function name |
87
|
|
|
* |
88
|
|
|
* @param string $functionName |
89
|
|
|
* @param string $field |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
4 |
|
protected function aliasFromField($functionName, $field) { |
94
|
4 |
|
$alias = strtolower($functionName); |
95
|
|
|
|
96
|
|
|
// Checking if string is just a '*' - to skip some code |
97
|
4 |
|
if ($field != '*') { |
98
|
3 |
|
$temp = explode('.', $field); |
99
|
3 |
|
array_walk($temp, 'DbSqlHelper::UCFirstByRef'); |
100
|
3 |
|
if (!empty($temp[1]) && $temp[1] == '*') { |
101
|
1 |
|
unset($temp[1]); |
102
|
1 |
|
} |
103
|
3 |
|
$temp = implode('', $temp); |
104
|
3 |
|
$alias .= ucfirst($temp); |
105
|
3 |
|
} else { |
106
|
1 |
|
$alias .= 'Value'; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
return $alias; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|