Passed
Push — master ( 749218...64fe1c )
by Jean-Christophe
09:18
created

DatabaseOperationsTrait::prepareAndFetchColumn()   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 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
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
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.4
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 3
	public function getDbObject() {
25 3
		return $this->wrapperObject->getDbInstance ();
26
	}
27
28 94
	public function _connect() {
29 94
		$this->wrapperObject->connect ( $this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options );
30 94
	}
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 63
	public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = false, $one = false) {
52 63
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
53 63
		$result = false;
54 63
		if ($cache) {
55 18
			$result = $this->getCacheValue ( $tableName, $condition, $parameters, $cKey );
56
		}
57 63
		if ($result === false) {
58 62
			$quote = SqlUtils::$quote;
59 62
			$result = $this->wrapperObject->_optPrepareAndExecute ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters, $one );
60 62
			if ($cache) {
61 16
				$this->cache->store ( $tableName, $cKey, $result );
62
			}
63
		}
64 63
		return $result;
65
	}
66
67 18
	private function getCacheValue($tableName, $condition, $parameters, &$cKey) {
68 18
		$cKey = $condition;
69 18
		if (\is_array ( $parameters )) {
70 15
			$cKey .= \implode ( ',', $parameters );
71
		}
72
		try {
73 18
			$result = $this->cache->fetch ( $tableName, $cKey );
74 17
			Logger::info ( "Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters );
75 1
		} catch ( \Exception $e ) {
76
			throw new CacheException ( "Cache is not created in Database constructor" );
77
		}
78 17
		return $result;
79
	}
80
81
	public function _optExecuteAndFetch($statement, $tableName, $condition, $parameters = null, $useCache = false, $one = false) {
82
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
83
		$result = false;
84
		if ($cache) {
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 1
	public function _optExecuteAndFetchNoCache($statement, $parameters = null, $one = false) {
97 1
		return $this->wrapperObject->_optExecuteAndFetch ( $statement, $parameters, $one );
98
	}
99
100 28
	public function getDaoPreparedStatement($tableName, $condition, $fields) {
101 28
		$quote = SqlUtils::$quote;
102 28
		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 24
	public function prepareAndFetchAll($sql, $parameters = null, $mode = null) {
115 24
		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 22
	public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) {
127 22
		$statement = $this->wrapperObject->_getStatement ( $sql );
128 22
		if ($statement->execute ( $parameters )) {
129 22
			Logger::info ( "Database", $sql, "prepareAndFetchColumn", $parameters );
130 22
			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 12
	public function getUpdateStatement($sql) {
150 12
		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 2
	public function execute($sql) {
171 2
		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 9
	public function prepareStatement($sql) {
181 9
		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 11
	public function lastInserId($name = null) {
224 11
		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 1
		return $this->wrapperObject->queryColumn ( "SELECT COUNT(*) FROM " . $tableName . $condition );
237
	}
238
239 1
	public function queryColumn($query, $columnNumber = null) {
240 1
		return $this->wrapperObject->queryColumn ( $query, $columnNumber );
241
	}
242
243 1
	public function fetchAll($query, $mode = null) {
244 1
		return $this->wrapperObject->queryAll ( $query, $mode );
245
	}
246
247 21
	public function isConnected() {
248 21
		return ($this->wrapperObject !== null && $this->ping ());
249
	}
250
251 21
	public function ping() {
252 21
		return ($this->wrapperObject && $this->wrapperObject->ping ());
253
	}
254
}
255