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