1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\db\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\log\Logger; |
6
|
|
|
use Ubiquity\cache\database\DbCache; |
7
|
|
|
use Ubiquity\exceptions\CacheException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Ubiquity\db\traits$DatabaseOperationsTrait |
11
|
|
|
* This class is part of Ubiquity |
12
|
|
|
* |
13
|
|
|
* @author jcheron <[email protected]> |
14
|
|
|
* @version 1.0.2 |
15
|
|
|
* @property \PDO $pdoObject |
16
|
|
|
* @property mixed $cache |
17
|
|
|
* @property array $options |
18
|
|
|
*/ |
19
|
|
|
trait DatabaseOperationsTrait { |
20
|
|
|
private $statements = [ ]; |
21
|
|
|
private $updateStatements = [ ]; |
22
|
|
|
|
23
|
|
|
abstract public function getDSN(); |
24
|
2 |
|
|
25
|
2 |
|
public function getPdoObject() { |
26
|
|
|
return $this->pdoObject; |
27
|
|
|
} |
28
|
100 |
|
|
29
|
100 |
|
public function _connect() { |
30
|
100 |
|
$this->options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; |
31
|
100 |
|
$this->pdoObject = new \PDO ( $this->getDSN (), $this->user, $this->password, $this->options ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Executes an SQL statement, returning a result set as a PDOStatement object |
36
|
|
|
* |
37
|
|
|
* @param string $sql |
38
|
|
|
* @return \PDOStatement|boolean |
39
|
7 |
|
*/ |
40
|
7 |
|
public function query($sql) { |
41
|
|
|
return $this->pdoObject->query ( $sql ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @param string $tableName |
47
|
|
|
* @param string $condition |
48
|
|
|
* @param array|string $fields |
49
|
|
|
* @param array $parameters |
50
|
|
|
* @param boolean|null $useCache |
51
|
|
|
* @return array |
52
|
60 |
|
*/ |
53
|
60 |
|
public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = NULL) { |
54
|
60 |
|
$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true)); |
55
|
60 |
|
$result = false; |
56
|
18 |
|
if ($cache) { |
57
|
18 |
|
$cKey = $condition; |
58
|
15 |
|
if (is_array ( $parameters )) { |
59
|
|
|
$cKey .= implode ( ",", $parameters ); |
60
|
|
|
} |
61
|
18 |
|
try { |
62
|
17 |
|
$result = $this->cache->fetch ( $tableName, $cKey ); |
63
|
1 |
|
Logger::info ( "Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters ); |
64
|
|
|
} catch ( \Exception $e ) { |
65
|
|
|
throw new CacheException ( "Cache is not created in Database constructor" ); |
66
|
|
|
} |
67
|
60 |
|
} |
68
|
58 |
|
if ($result === false) { |
69
|
58 |
|
$result = $this->prepareAndFetchAll ( "SELECT {$fields} FROM `" . $tableName . "`" . $condition, $parameters ); |
70
|
15 |
|
if ($cache) { |
71
|
|
|
$this->cache->store ( $tableName, $cKey, $result ); |
72
|
|
|
} |
73
|
60 |
|
} |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
59 |
|
|
77
|
59 |
|
public function prepareAndFetchAll($sql, $parameters = null) { |
78
|
59 |
|
$statement = $this->getStatement ( $sql ); |
79
|
59 |
|
if ($statement->execute ( $parameters )) { |
80
|
59 |
|
Logger::info ( "Database", $sql, "prepareAndFetchAll", $parameters ); |
81
|
|
|
return $statement->fetchAll (); |
82
|
|
|
} |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
1 |
|
|
86
|
1 |
|
public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) { |
87
|
1 |
|
$statement = $this->getStatement ( $sql ); |
88
|
1 |
|
if ($statement->execute ( $parameters )) { |
89
|
1 |
|
Logger::info ( "Database", $sql, "prepareAndFetchAllColumn", $parameters ); |
90
|
|
|
return $statement->fetchAll ( \PDO::FETCH_COLUMN, $column ); |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
23 |
|
|
95
|
23 |
|
public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) { |
96
|
23 |
|
$statement = $this->getStatement ( $sql ); |
97
|
23 |
|
if ($statement->execute ( $parameters )) { |
98
|
23 |
|
Logger::info ( "Database", $sql, "prepareAndFetchColumn", $parameters ); |
99
|
|
|
return $statement->fetchColumn ( $columnNumber ); |
100
|
|
|
} |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @param string $sql |
107
|
|
|
* @return \PDOStatement |
108
|
69 |
|
*/ |
109
|
69 |
|
private function getStatement($sql) { |
110
|
69 |
|
if (! isset ( $this->statements [$sql] )) { |
111
|
69 |
|
$this->statements [$sql] = $this->pdoObject->prepare ( $sql ); |
112
|
|
|
$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC ); |
113
|
69 |
|
} |
114
|
|
|
return $this->statements [$sql]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* |
119
|
|
|
* @param string $sql |
120
|
|
|
* @return \PDOStatement |
121
|
|
|
*/ |
122
|
2 |
|
private function getUpdateStatement($sql) { |
123
|
2 |
|
if (! isset ( $this->updateStatements [$sql] )) { |
124
|
|
|
$this->updateStatements [$sql] = $this->pdoObject->prepare ( $sql ); |
125
|
|
|
} |
126
|
|
|
return $this->updateStatements [$sql]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...) |
131
|
|
|
* |
132
|
16 |
|
* @param string $sql |
133
|
16 |
|
* @param array|null $parameters |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
|
|
public function prepareAndExecuteUpdate($sql, $parameters = null) { |
137
|
|
|
$statement = $this->getUpdateStatement ( $sql ); |
138
|
|
|
return $statement->execute ( $parameters ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
143
|
|
|
* |
144
|
1 |
|
* @param string $sql |
145
|
1 |
|
* @return int the number of rows that were modified or deleted by the SQL statement you issued |
146
|
|
|
*/ |
147
|
|
|
public function execute($sql) { |
148
|
|
|
return $this->pdoObject->exec ( $sql ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Prepares a statement for execution and returns a statement object |
153
|
12 |
|
* |
154
|
12 |
|
* @param String $sql |
155
|
|
|
* @return \PDOStatement|boolean |
156
|
|
|
*/ |
157
|
3 |
|
public function prepareStatement($sql) { |
158
|
3 |
|
return $this->pdoObject->prepare ( $sql ); |
159
|
3 |
|
} |
160
|
3 |
|
|
161
|
|
|
/** |
162
|
|
|
* Sets $value to $parameter |
163
|
|
|
* |
164
|
|
|
* @param \PDOStatement $statement |
165
|
|
|
* @param String $parameter |
166
|
|
|
* @param mixed $value |
167
|
|
|
* @return boolean |
168
|
|
|
*/ |
169
|
1 |
|
public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) { |
170
|
1 |
|
return $statement->bindValue ( ":" . $parameter, $value ); |
171
|
1 |
|
} |
172
|
1 |
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the last insert id |
175
|
4 |
|
* |
176
|
4 |
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function lastInserId() { |
179
|
1 |
|
return $this->pdoObject->lastInsertId (); |
180
|
1 |
|
} |
181
|
|
|
|
182
|
|
|
public function getTablesName() { |
183
|
21 |
|
$sql = 'SHOW TABLES'; |
184
|
21 |
|
$query = $this->pdoObject->query ( $sql ); |
185
|
|
|
return $query->fetchAll ( \PDO::FETCH_COLUMN ); |
186
|
|
|
} |
187
|
7 |
|
|
188
|
7 |
|
/** |
189
|
|
|
* Returns the number of records in $tableName that respects the condition passed as a parameter |
190
|
|
|
* |
191
|
|
|
* @param string $tableName |
192
|
|
|
* @param string $condition Part following the WHERE of an SQL statement |
193
|
|
|
*/ |
194
|
|
|
public function count($tableName, $condition = '') { |
195
|
|
|
if ($condition != '') |
196
|
|
|
$condition = " WHERE " . $condition; |
197
|
|
|
return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn (); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function queryColumn($query, $columnNumber = null) { |
201
|
|
|
return $this->query ( $query )->fetchColumn ( $columnNumber ); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function fetchAll($query) { |
205
|
|
|
return $this->query ( $query )->fetchAll (); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function isConnected() { |
209
|
|
|
return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping ()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function ping() { |
213
|
|
|
return ($this->pdoObject && 1 === intval ( $this->pdoObject->query ( 'SELECT 1' )->fetchColumn ( 0 ) )); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|