Passed
Push — master ( 4fc3fe...855968 )
by Jean-Christophe
02:25
created

Database::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\db;
4
5
use Ubiquity\cache\database\DbCache;
6
use Ubiquity\exceptions\CacheException;
7
use Ubiquity\log\Logger;
8
9
/**
10
 * PDO database class
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.3
14
 */
15
class Database {
16
	private $dbType;
17
	private $serverName;
18
	private $port;
19
	private $dbName;
20
	private $user;
21
	private $password;
22
	private $pdoObject;
23
	private $statements = [ ];
24
	private $cache;
25
	private $options;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param string $dbName
31
	 * @param string $serverName
32
	 * @param string $port
33
	 * @param string $user
34
	 * @param string $password
35
	 * @param array $options
36
	 * @param boolean|string $cache
37
	 */
38 22
	public function __construct($dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) {
39 22
		$this->dbType = $dbType;
40 22
		$this->dbName = $dbName;
41 22
		$this->serverName = $serverName;
42 22
		$this->port = $port;
43 22
		$this->user = $user;
44 22
		$this->password = $password;
45 22
		if (isset ( $options ["quote"] ))
46
			SqlUtils::$quote = $options ["quote"];
47 22
		$this->options = $options;
48 22
		if ($cache !== false) {
49
			if (\is_callable ( $cache )) {
50
				$this->cache = $cache ();
51
			} else {
52
				if (\class_exists ( $cache )) {
53
					$this->cache = new $cache ();
54
				} else {
55
					throw new CacheException ( $cache . " is not a valid value for database cache" );
56
				}
57
			}
58
		}
59 22
	}
60
61
	/**
62
	 * Creates the PDO instance and realize a safe connection
63
	 *
64
	 * @return boolean true if connection is established
65
	 */
66 17
	public function connect() {
67
		try {
68 17
			$this->_connect ();
69 17
			return true;
70
		} catch ( \PDOException $e ) {
71
			echo $e->getMessage ();
72
			return false;
73
		}
74
	}
75
76 17
	public function _connect() {
77 17
		$this->options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
78 17
		$this->pdoObject = new \PDO ( $this->getDSN (), $this->user, $this->password, $this->options );
79 17
	}
80
81 18
	public function getDSN() {
82 18
		return $this->dbType . ':dbname=' . $this->dbName . ';host=' . $this->serverName . ';charset=UTF8;port=' . $this->port;
83
	}
84
85
	/**
86
	 * Executes an SQL statement, returning a result set as a PDOStatement object
87
	 *
88
	 * @param string $sql
89
	 * @return \PDOStatement
90
	 */
91 4
	public function query($sql) {
92 4
		return $this->pdoObject->query ( $sql );
93
	}
94
95
	/**
96
	 *
97
	 * @param string $tableName
98
	 * @param string $condition
99
	 * @param array|string $fields
100
	 * @param array $parameters
101
	 * @param boolean|null $useCache
102
	 * @return array
103
	 */
104 1
	public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = NULL) {
105 1
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
106 1
		$result = false;
107 1
		if ($cache) {
108
			$cKey = $condition;
109
			if (is_array ( $parameters )) {
110
				$cKey .= implode ( ",", $parameters );
111
			}
112
			$result = $this->cache->fetch ( $tableName, $cKey );
113
			Logger::info ( "Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters );
114
		}
115 1
		if ($result === false) {
116 1
			$fields = SqlUtils::getFieldList ( $fields, $tableName );
117 1
			$result = $this->prepareAndFetchAll ( "SELECT {$fields} FROM `" . $tableName . "`" . $condition, $parameters );
118 1
			if ($cache) {
119
				$this->cache->store ( $tableName, $cKey, $result );
120
			}
121
		}
122 1
		return $result;
123
	}
124
125 2
	public function prepareAndFetchAll($sql, $parameters = null) {
126 2
		$result = false;
127 2
		$statement = $this->getStatement ( $sql );
128 2
		if ($statement->execute ( $parameters )) {
129 2
			Logger::info ( "Database", $sql, "prepareAndFetchAll", $parameters );
130 2
			$result = $statement->fetchAll ();
131
		}
132 2
		$statement->closeCursor ();
133 2
		return $result;
134
	}
135
136 1
	public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) {
137 1
		$result = false;
138 1
		$statement = $this->getStatement ( $sql );
139 1
		if ($statement->execute ( $parameters )) {
140 1
			Logger::info ( "Database", $sql, "prepareAndFetchAllColumn", $parameters );
141 1
			$result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
142
		}
143 1
		$statement->closeCursor ();
144 1
		return $result;
145
	}
146
147 1
	public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) {
148 1
		$statement = $this->getStatement ( $sql );
149 1
		if ($statement->execute ( $parameters )) {
150 1
			Logger::info ( "Database", $sql, "prepareAndFetchColumn", $parameters );
151 1
			return $statement->fetchColumn ( $columnNumber );
152
		}
153
		return false;
154
	}
155
156
	/**
157
	 *
158
	 * @param string $sql
159
	 * @return \PDOStatement
160
	 */
161 4
	private function getStatement($sql) {
162 4
		if (! isset ( $this->statements [$sql] )) {
163 4
			$this->statements [$sql] = $this->pdoObject->prepare ( $sql );
164 4
			$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
165
		}
166 4
		return $this->statements [$sql];
167
	}
168
169
	/**
170
	 * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
171
	 *
172
	 * @param string $sql
173
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
174
	 */
175 2
	public function execute($sql) {
176 2
		return $this->pdoObject->exec ( $sql );
177
	}
178
179
	/**
180
	 *
181
	 * @return string
182
	 * @codeCoverageIgnore
183
	 */
184
	public function getServerName() {
185
		return $this->serverName;
186
	}
187
188 2
	public function setServerName($serverName) {
189 2
		$this->serverName = $serverName;
190 2
	}
191
192
	/**
193
	 * Prepares a statement for execution and returns a statement object
194
	 *
195
	 * @param String $sql
196
	 * @return \PDOStatement
197
	 */
198 2
	public function prepareStatement($sql) {
199 2
		return $this->pdoObject->prepare ( $sql );
200
	}
201
202
	/**
203
	 * Sets $value to $parameter
204
	 *
205
	 * @param \PDOStatement $statement
206
	 * @param String $parameter
207
	 * @param mixed $value
208
	 * @return boolean
209
	 */
210 1
	public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) {
211 1
		return $statement->bindValue ( ":" . $parameter, $value );
212
	}
213
214
	/**
215
	 * Returns the last insert id
216
	 *
217
	 * @return integer
218
	 */
219 1
	public function lastInserId() {
220 1
		return $this->pdoObject->lastInsertId ();
221
	}
222
223 1
	public function getTablesName() {
224 1
		$sql = 'SHOW TABLES';
225 1
		$query = $this->pdoObject->query ( $sql );
226 1
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
227
	}
228
229
	/**
230
	 * Returns the number of records in $tableName that respects the condition passed as a parameter
231
	 *
232
	 * @param string $tableName
233
	 * @param string $condition
234
	 *        	Partie suivant le WHERE d'une instruction SQL
235
	 */
236 1
	public function count($tableName, $condition = '') {
237 1
		if ($condition != '')
238 1
			$condition = " WHERE " . $condition;
239 1
		return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
240
	}
241
242 1
	public function queryColumn($query, $columnNumber = null) {
243 1
		return $this->query ( $query )->fetchColumn ( $columnNumber );
244
	}
245
246 1
	public function fetchAll($query) {
247 1
		return $this->query ( $query )->fetchAll ();
248
	}
249
250 17
	public function isConnected() {
251 17
		return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping ());
252
	}
253
254 2
	public function setDbType($dbType) {
255 2
		$this->dbType = $dbType;
256 2
		return $this;
257
	}
258
259 3
	public function ping() {
260 3
		return ($this->pdoObject && 1 === intval ( $this->pdoObject->query ( 'SELECT 1' )->fetchColumn ( 0 ) ));
261
	}
262
263
	/**
264
	 *
265
	 * @return string
266
	 * @codeCoverageIgnore
267
	 */
268
	public function getPort() {
269
		return $this->port;
270
	}
271
272
	/**
273
	 *
274
	 * @return string
275
	 * @codeCoverageIgnore
276
	 */
277
	public function getDbName() {
278
		return $this->dbName;
279
	}
280
281
	/**
282
	 *
283
	 * @return string
284
	 * @codeCoverageIgnore
285
	 */
286
	public function getUser() {
287
		return $this->user;
288
	}
289
290 1
	public function getPdoObject() {
291 1
		return $this->pdoObject;
292
	}
293
294 1
	public static function getAvailableDrivers() {
295 1
		return \PDO::getAvailableDrivers ();
296
	}
297
298
	/**
299
	 *
300
	 * @return mixed
301
	 * @codeCoverageIgnore
302
	 */
303
	public function getDbType() {
304
		return $this->dbType;
305
	}
306
307
	/**
308
	 *
309
	 * @return string
310
	 * @codeCoverageIgnore
311
	 */
312
	public function getPassword() {
313
		return $this->password;
314
	}
315
316
	/**
317
	 *
318
	 * @return array
319
	 * @codeCoverageIgnore
320
	 */
321
	public function getOptions() {
322
		return $this->options;
323
	}
324
325
	/**
326
	 *
327
	 * @param string $port
328
	 */
329 1
	public function setPort($port) {
330 1
		$this->port = $port;
331 1
	}
332
333
	/**
334
	 *
335
	 * @param string $dbName
336
	 */
337
	public function setDbName($dbName) {
338
		$this->dbName = $dbName;
339
	}
340
341
	/**
342
	 *
343
	 * @param string $user
344
	 */
345
	public function setUser($user) {
346
		$this->user = $user;
347
	}
348
349
	/**
350
	 *
351
	 * @param string $password
352
	 */
353
	public function setPassword($password) {
354
		$this->password = $password;
355
	}
356
357
	/**
358
	 *
359
	 * @param array $options
360
	 */
361
	public function setOptions($options) {
362
		$this->options = $options;
363
	}
364
}
365