|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\db\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\database\DbCache; |
|
6
|
|
|
use Ubiquity\db\SqlUtils; |
|
7
|
|
|
use Ubiquity\exceptions\CacheException; |
|
8
|
|
|
use Ubiquity\log\Logger; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Ubiquity\db\traits$DatabaseOperationsTrait |
|
12
|
|
|
* This class is part of Ubiquity |
|
13
|
|
|
* |
|
14
|
|
|
* @author jcheron <[email protected]> |
|
15
|
|
|
* @version 1.0.5 |
|
16
|
|
|
* @property mixed $cache |
|
17
|
|
|
* @property array $options |
|
18
|
|
|
* @property \Ubiquity\db\providers\AbstractDbWrapper $wrapperObject |
|
19
|
|
|
*/ |
|
20
|
|
|
trait DatabaseOperationsTrait { |
|
21
|
|
|
|
|
22
|
|
|
abstract public function getDSN(); |
|
23
|
|
|
|
|
24
|
5 |
|
public function getDbObject() { |
|
25
|
5 |
|
return $this->wrapperObject->getDbInstance (); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
176 |
|
public function _connect() { |
|
29
|
176 |
|
$this->wrapperObject->connect ( $this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Executes an SQL statement, returning a result set as a statement object |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $sql |
|
36
|
|
|
* @return object|boolean |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function query($sql) { |
|
39
|
1 |
|
return $this->wrapperObject->query ( $sql ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $tableName |
|
45
|
|
|
* @param string $condition |
|
46
|
|
|
* @param array|string $fields |
|
47
|
|
|
* @param array $parameters |
|
48
|
|
|
* @param boolean|null $useCache |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
128 |
|
public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = false, $one = false) { |
|
52
|
128 |
|
$result = false; |
|
53
|
128 |
|
if ($cache = ($useCache!==false && (DbCache::$active || $useCache))) { |
|
54
|
18 |
|
$result = $this->getCacheValue ( $tableName, $condition, $parameters, $cKey ); |
|
55
|
|
|
} |
|
56
|
128 |
|
if ($result === false) { |
|
57
|
127 |
|
$quote = SqlUtils::$quote; |
|
58
|
127 |
|
$sql="SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}"; |
|
59
|
127 |
|
Logger::info ( 'Database', $sql, 'prepareAndExecute', $parameters ); |
|
60
|
127 |
|
$result = $this->wrapperObject->_optPrepareAndExecute ( $sql, $parameters, $one ); |
|
61
|
127 |
|
if ($cache) { |
|
62
|
16 |
|
$this->cache->store ( $tableName, $cKey, $result ); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
128 |
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
18 |
|
private function getCacheValue($tableName, $condition, $parameters, &$cKey) { |
|
69
|
18 |
|
$cKey = $condition; |
|
70
|
18 |
|
if (\is_array ( $parameters )) { |
|
71
|
15 |
|
$cKey .= \implode ( ',', $parameters ); |
|
72
|
|
|
} |
|
73
|
|
|
try { |
|
74
|
18 |
|
$result = $this->cache->fetch ( $tableName, $cKey ); |
|
75
|
17 |
|
Logger::info ( 'Cache', "fetching cache for table {$tableName} with condition : {$condition}", 'Database::prepareAndExecute', $parameters ); |
|
76
|
1 |
|
} catch ( \Exception $e ) { |
|
77
|
|
|
throw new CacheException ( "Cache is not created in Database constructor" ); |
|
78
|
|
|
} |
|
79
|
17 |
|
return $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function _optExecuteAndFetch($statement, $tableName, $condition, $parameters = null, $useCache = false, $one = false) { |
|
83
|
|
|
$result = false; |
|
84
|
|
|
if ($cache = ($useCache!==false && (DbCache::$active || $useCache))) { |
|
85
|
|
|
$result = $this->getCacheValue ( $tableName, $condition, $parameters, $cKey ); |
|
86
|
|
|
} |
|
87
|
|
|
if ($result === false) { |
|
88
|
|
|
$result = $this->wrapperObject->_optExecuteAndFetch ( $statement, $parameters, $one ); |
|
89
|
|
|
if ($cache) { |
|
90
|
|
|
$this->cache->store ( $tableName, $cKey, $result ); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
public function _optExecuteAndFetchNoCache($statement, $parameters = null, $one = false) { |
|
97
|
6 |
|
return $this->wrapperObject->_optExecuteAndFetch ( $statement, $parameters, $one ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
65 |
|
public function getDaoPreparedStatement($tableName, $condition, $fields) { |
|
101
|
65 |
|
$quote = SqlUtils::$quote; |
|
102
|
65 |
|
return $this->wrapperObject->prepareStatement ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}" ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function prepareAndExecuteNoCache($tableName, $condition, $fields, $parameters = null) { |
|
106
|
|
|
$quote = SqlUtils::$quote; |
|
107
|
|
|
return $this->wrapperObject->_optPrepareAndExecute ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function storeCache() { |
|
111
|
|
|
$this->cache->storeDeferred (); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
37 |
|
public function prepareAndFetchAll($sql, $parameters = null, $mode = null) { |
|
115
|
37 |
|
return $this->wrapperObject->fetchAll ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function prepareAndFetchOne($sql, $parameters = null, $mode = null) { |
|
119
|
|
|
return $this->wrapperObject->fetchOne ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) { |
|
123
|
1 |
|
return $this->wrapperObject->fetchAllColumn ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $column ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
36 |
|
public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = 0) { |
|
127
|
36 |
|
$statement = $this->wrapperObject->_getStatement ( $sql ); |
|
128
|
36 |
|
if ($statement->execute ( $parameters )) { |
|
129
|
36 |
|
Logger::info ( 'Database', $sql, 'prepareAndFetchColumn', $parameters ); |
|
130
|
36 |
|
return $statement->fetchColumn ( $columnNumber ); |
|
131
|
|
|
} |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $sql |
|
138
|
|
|
* @return object statement |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getStatement($sql) { |
|
141
|
|
|
return $this->wrapperObject->_getStatement ( $sql ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $sql |
|
147
|
|
|
* @return object statement |
|
148
|
|
|
*/ |
|
149
|
26 |
|
public function getUpdateStatement($sql) { |
|
150
|
26 |
|
return $this->wrapperObject->_getStatement ( $sql ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...) |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $sql |
|
157
|
|
|
* @param array|null $parameters |
|
158
|
|
|
* @return boolean |
|
159
|
|
|
*/ |
|
160
|
|
|
public function prepareAndExecuteUpdate($sql, $parameters = null) { |
|
161
|
|
|
return $this->getUpdateStatement ( $sql )->execute ( $parameters ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $sql |
|
168
|
|
|
* @return int the number of rows that were modified or deleted by the SQL statement you issued |
|
169
|
|
|
*/ |
|
170
|
5 |
|
public function execute($sql) { |
|
171
|
5 |
|
return $this->wrapperObject->execute ( $sql ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Prepares a statement for execution and returns a statement object |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $sql |
|
178
|
|
|
* @return object|boolean |
|
179
|
|
|
*/ |
|
180
|
15 |
|
public function prepareStatement($sql) { |
|
181
|
15 |
|
return $this->wrapperObject->prepareStatement ( $sql ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Prepares and returns a statement for execution and gives it a name. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $name |
|
188
|
|
|
* @param string $sql |
|
189
|
|
|
* @return mixed |
|
190
|
|
|
*/ |
|
191
|
|
|
public function prepareNamedStatement(string $name, string $sql) { |
|
192
|
|
|
return $this->wrapperObject->prepareNamedStatement ( $name, $sql ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Returns the statement corresponding to the name. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
* @param ?string $sql |
|
200
|
|
|
* @return mixed |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getNamedStatement(string $name, ?string $sql = null) { |
|
203
|
|
|
return $this->wrapperObject->getNamedStatement ( $name, $sql ); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Sets $value to $parameter |
|
208
|
|
|
* |
|
209
|
|
|
* @param mixed $statement |
|
210
|
|
|
* @param string $parameter |
|
211
|
|
|
* @param mixed $value |
|
212
|
|
|
* @return boolean |
|
213
|
|
|
*/ |
|
214
|
1 |
|
public function bindValueFromStatement($statement, $parameter, $value) { |
|
215
|
1 |
|
return $this->wrapperObject->bindValueFromStatement ( $statement, $parameter, $value ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Returns the last insert id |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
21 |
|
public function lastInserId($name = null) { |
|
224
|
21 |
|
return $this->wrapperObject->lastInsertId ( $name ); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Returns the number of records in $tableName matching with the condition passed as a parameter |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $tableName |
|
231
|
|
|
* @param string $condition Part following the WHERE of an SQL statement |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function count($tableName, $condition = '') { |
|
234
|
1 |
|
if ($condition != ''){ |
|
235
|
1 |
|
$condition = ' WHERE ' . $condition; |
|
236
|
|
|
} |
|
237
|
1 |
|
return $this->wrapperObject->queryColumn ( 'SELECT COUNT(*) FROM ' . $tableName . $condition ); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
1 |
|
public function queryColumn($query, $columnNumber = null) { |
|
241
|
1 |
|
return $this->wrapperObject->queryColumn ( $query, $columnNumber ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
public function fetchAll($query, $mode = null) { |
|
245
|
1 |
|
return $this->wrapperObject->queryAll ( $query, $mode ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
25 |
|
public function isConnected() { |
|
249
|
25 |
|
return ($this->wrapperObject !== null && $this->ping ()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
25 |
|
public function ping() { |
|
253
|
25 |
|
return ($this->wrapperObject && $this->wrapperObject->ping ()); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|