Passed
Pull Request — master (#59)
by
unknown
17:09
created

prepareAndFetchAllColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 6
cp 0.8333
rs 10
cc 2
nc 2
nop 3
crap 2.0185
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
25 2
	public function getPdoObject() {
26 2
		return $this->pdoObject;
27
	}
28
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 100
	}
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
	 */
40 7
	public function query($sql) {
41 7
		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
	 */
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 60
		if ($cache) {
57 18
			$cKey = $condition;
58 18
			if (is_array ( $parameters )) {
59 15
				$cKey .= implode ( ",", $parameters );
60
			}
61
			try {
62 18
				$result = $this->cache->fetch ( $tableName, $cKey );
63 17
				Logger::info ( "Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters );
64 1
			} catch ( \Exception $e ) {
65
				throw new CacheException ( "Cache is not created in Database constructor" );
66
			}
67
		}
68 60
		if ($result === false) {
69 58
			$result = $this->prepareAndFetchAll ( "SELECT {$fields} FROM `" . $tableName . "`" . $condition, $parameters );
70 58
			if ($cache) {
71 15
				$this->cache->store ( $tableName, $cKey, $result );
72
			}
73
		}
74 60
		return $result;
75
	}
76
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 59
			return $statement->fetchAll ();
82
		}
83
		return false;
84
	}
85
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 1
			return $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
91
		}
92
		return false;
93
	}
94
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 23
			return $statement->fetchColumn ( $columnNumber );
100
		}
101
		return false;
102
	}
103
104
	/**
105
	 *
106
	 * @param string $sql
107
	 * @return \PDOStatement
108
	 */
109 69
	private function getStatement($sql) {
110 69
		if (! isset ( $this->statements [$sql] )) {
111 69
			$this->statements [$sql] = $this->pdoObject->prepare ( $sql );
112 69
			$this->statements [$sql]->setFetchMode ( \PDO::FETCH_ASSOC );
113
		}
114 69
		return $this->statements [$sql];
115
	}
116
117
	/**
118
	 *
119
	 * @param string $sql
120
	 * @return \PDOStatement
121
	 */
122
	private function getUpdateStatement($sql) {
123
		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
	 * @param string $sql
133
	 * @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
	 * @param string $sql
145
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
146
	 */
147 2
	public function execute($sql) {
148 2
		return $this->pdoObject->exec ( $sql );
149
	}
150
151
	/**
152
	 * Prepares a statement for execution and returns a statement object
153
	 *
154
	 * @param String $sql
155
	 * @return \PDOStatement|boolean
156
	 */
157 16
	public function prepareStatement($sql) {
158 16
		return $this->pdoObject->prepare ( $sql );
159
	}
160
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
	}
172
173
	/**
174
	 * Returns the last insert id
175
	 *
176
	 * @return string
177
	 */
178 12
	public function lastInserId() {
179 12
		return $this->pdoObject->lastInsertId ();
180
	}
181
182 3
	public function getTablesName() {
183 3
		$sql = 'SHOW TABLES';
184 3
		$query = $this->pdoObject->query ( $sql );
185 3
		return $query->fetchAll ( \PDO::FETCH_COLUMN );
186
	}
187
188
	/**
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 1
	public function count($tableName, $condition = '') {
195 1
		if ($condition != '')
196 1
			$condition = " WHERE " . $condition;
197 1
		return $this->query ( "SELECT COUNT(*) FROM " . $tableName . $condition )->fetchColumn ();
198
	}
199
200 4
	public function queryColumn($query, $columnNumber = null) {
201 4
		return $this->query ( $query )->fetchColumn ( $columnNumber );
202
	}
203
204 1
	public function fetchAll($query) {
205 1
		return $this->query ( $query )->fetchAll ();
206
	}
207
208 21
	public function isConnected() {
209 21
		return ($this->pdoObject !== null && $this->pdoObject instanceof \PDO && $this->ping ());
210
	}
211
212 7
	public function ping() {
213 7
		return ($this->pdoObject && 1 === intval ( $this->pdoObject->query ( 'SELECT 1' )->fetchColumn ( 0 ) ));
214
	}
215
}
216
217