Test Failed
Push — master ( 768736...0faf86 )
by Jean-Christophe
24:11 queued 13:40
created

DatabaseOperationsTrait::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Ubiquity\db\traits;
3
4
use Ubiquity\log\Logger;
5
use Ubiquity\cache\database\DbCache;
6
use Ubiquity\exceptions\CacheException;
7
use Ubiquity\db\SqlUtils;
8
9
/**
10
 * Ubiquity\db\traits$DatabaseOperationsTrait
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.4
15
 * @property mixed $cache
16
 * @property array $options
17
 * @property \Ubiquity\db\providers\AbstractDbWrapper $wrapperObject
18
 */
19
trait DatabaseOperationsTrait {
20
21
	abstract public function getDSN();
22
23
	public function getDbObject() {
24 3
		return $this->wrapperObject->getDbInstance();
25 3
	}
26
27
	public function _connect() {
28 94
		$this->wrapperObject->connect($this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options);
29 94
	}
30 94
31
	/**
32
	 * Executes an SQL statement, returning a result set as a statement object
33
	 *
34
	 * @param string $sql
35
	 * @return object|boolean
36
	 */
37
	public function query($sql) {
38 1
		return $this->wrapperObject->query($sql);
39 1
	}
40
41
	/**
42
	 *
43
	 * @param string $tableName
44
	 * @param string $condition
45
	 * @param array|string $fields
46
	 * @param array $parameters
47
	 * @param boolean|null $useCache
48
	 * @return array
49
	 */
50
	public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = false, $one = false) {
51 64
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
52 64
		$result = false;
53 64
		if ($cache) {
54 64
			$result = $this->getCacheValue($tableName, $condition, $parameters, $cKey);
55 18
		}
56 18
		if ($result === false) {
57 15
			$quote = SqlUtils::$quote;
58
			$result = $this->wrapperObject->_optPrepareAndExecute("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters, $one);
59
			if ($cache) {
60 18
				$this->cache->store($tableName, $cKey, $result);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $cKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
61 17
			}
62 1
		}
63
		return $result;
64
	}
65
66 64
	private function getCacheValue($tableName, $condition, $parameters, &$cKey) {
67 62
		$cKey = $condition;
68 62
		if (\is_array($parameters)) {
69 62
			$cKey .= \implode(',', $parameters);
70 15
		}
71
		try {
72
			$result = $this->cache->fetch($tableName, $cKey);
73 64
			Logger::info("Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters);
74
		} catch (\Exception $e) {
75
			throw new CacheException("Cache is not created in Database constructor");
76 24
		}
77 24
		return $result;
78
	}
79
80
	public function _optExecuteAndFetch($statement, $tableName, $condition, $parameters = null, $useCache = false, $one = false) {
81
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
82
		$result = false;
83
		if ($cache) {
84 1
			$result = $this->getCacheValue($tableName, $condition, $parameters, $cKey);
85 1
		}
86
		if ($result === false) {
87
			if ($one) {
88 22
				$result = $this->wrapperObject->_optExecuteAndFetchOne($statement, $parameters);
0 ignored issues
show
Bug introduced by
The method _optExecuteAndFetchOne() does not exist on Ubiquity\db\providers\AbstractDbWrapper. Did you maybe mean _optExecuteAndFetch()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
				/** @scrutinizer ignore-call */ 
89
    $result = $this->wrapperObject->_optExecuteAndFetchOne($statement, $parameters);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89 22
			} else {
90 22
				$result = $this->wrapperObject->_optExecuteAndFetch($statement, $parameters);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->wrapperObject->_o...statement, $parameters) targeting Ubiquity\db\providers\Ab...::_optExecuteAndFetch() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91 22
			}
92 22
			if ($cache) {
93
				$this->cache->store($tableName, $cKey, $result);
94
			}
95
		}
96
		return $result;
97
	}
98
99
	public function _optExecuteAndFetchNoCache($statement, $parameters = null, $one = false) {
100
		return $this->wrapperObject->_optExecuteAndFetch($statement, $parameters, $one);
1 ignored issue
show
Bug introduced by
Are you sure the usage of $this->wrapperObject->_o...ent, $parameters, $one) targeting Ubiquity\db\providers\Ab...::_optExecuteAndFetch() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
101
	}
102
103
	public function getDaoPreparedStatement($tableName, $condition, $fields) {
104
		$quote = SqlUtils::$quote;
105
		return $this->wrapperObject->prepareStatement("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}");
106
	}
107
108
	public function prepareAndExecuteNoCache($tableName, $condition, $fields, $parameters = null) {
109
		$quote = SqlUtils::$quote;
110
		return $this->wrapperObject->_optPrepareAndExecute("SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters);
111 12
	}
112 12
113
	public function storeCache() {
114
		$this->cache->storeDeferred();
115
	}
116
117
	public function prepareAndFetchAll($sql, $parameters = null, $mode = null) {
118
		return $this->wrapperObject->fetchAll($this->wrapperObject->_getStatement($sql), $parameters, $mode);
119
	}
120
121
	public function prepareAndFetchOne($sql, $parameters = null, $mode = null) {
122
		return $this->wrapperObject->fetchOne($this->wrapperObject->_getStatement($sql), $parameters, $mode);
123
	}
124
125
	public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) {
126
		return $this->wrapperObject->fetchAllColumn($this->wrapperObject->_getStatement($sql), $parameters, $column);
127
	}
128
129
	public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) {
130
		$statement = $this->wrapperObject->_getStatement($sql);
131
		if ($statement->execute($parameters)) {
132 2
			Logger::info("Database", $sql, "prepareAndFetchColumn", $parameters);
133 2
			return $statement->fetchColumn($columnNumber);
134
		}
135
		return false;
136
	}
137
138
	/**
139
	 *
140
	 * @param string $sql
141
	 * @return object statement
142 9
	 */
143 9
	private function getStatement($sql) {
144
		return $this->wrapperObject->_getStatement($sql);
145
	}
146
147
	/**
148
	 *
149
	 * @param string $sql
150
	 * @return object statement
151
	 */
152
	public function getUpdateStatement($sql) {
153
		return $this->wrapperObject->_getStatement($sql);
154 1
	}
155 1
156
	/**
157
	 * Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...)
158
	 *
159
	 * @param string $sql
160
	 * @param array|null $parameters
161
	 * @return boolean
162
	 */
163 11
	public function prepareAndExecuteUpdate($sql, $parameters = null) {
164 11
		return $this->getUpdateStatement($sql)->execute($parameters);
165
	}
166
167
	/**
168
	 * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE)
169
	 *
170
	 * @param string $sql
171
	 * @return int the number of rows that were modified or deleted by the SQL statement you issued
172
	 */
173 1
	public function execute($sql) {
174 1
		return $this->wrapperObject->execute($sql);
175 1
	}
176 1
177
	/**
178
	 * Prepares a statement for execution and returns a statement object
179 1
	 *
180 1
	 * @param String $sql
181
	 * @return object|boolean
182
	 */
183 1
	public function prepareStatement($sql) {
184 1
		return $this->wrapperObject->prepareStatement($sql);
185
	}
186
187 21
	/**
188 21
	 * Prepares and returns a statement for execution and gives it a name.
189
	 *
190
	 * @param
191 21
	 *        	$name
192 21
	 * @param String $sql
193
	 * @return mixed
194
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $name at position 0 could not be parsed: Unknown type name '$name' at position 0 in $name.
Loading history...
195
	public function prepareNamedStatement(string $name, string $sql) {
196
		return $this->wrapperObject->prepareNamedStatement($name, $sql);
197
	}
198
199
	/**
200
	 * Returns the statement corresponding to the name.
201
	 *
202
	 * @param string $name
203
	 * @param ?string $sql
204
	 * @return mixed
205
	 */
206
	public function getNamedStatement(string $name, ?string $sql = null) {
207
		return $this->wrapperObject->getNamedStatement($name, $sql);
208
	}
209
210
	/**
211
	 * Sets $value to $parameter
212
	 *
213
	 * @param mixed $statement
214
	 * @param String $parameter
215
	 * @param mixed $value
216
	 * @return boolean
217
	 */
218
	public function bindValueFromStatement($statement, $parameter, $value) {
219
		return $this->wrapperObject->bindValueFromStatement($statement, $parameter, $value);
220
	}
221
222
	/**
223
	 * Returns the last insert id
224
	 *
225
	 * @return string
226
	 */
227
	public function lastInserId($name = null) {
228
		return $this->wrapperObject->lastInsertId($name);
229
	}
230
231
	/**
232
	 * Returns the number of records in $tableName matching with the condition passed as a parameter
233
	 *
234
	 * @param string $tableName
235
	 * @param string $condition
236
	 *        	Part following the WHERE of an SQL statement
237
	 */
238
	public function count($tableName, $condition = '') {
239
		if ($condition != '')
240
			$condition = " WHERE " . $condition;
241
		return $this->wrapperObject->queryColumn("SELECT COUNT(*) FROM " . $tableName . $condition);
242
	}
243
244
	public function queryColumn($query, $columnNumber = null) {
245
		return $this->wrapperObject->queryColumn($query, $columnNumber);
246
	}
247
248
	public function fetchAll($query, $mode = null) {
249
		return $this->wrapperObject->queryAll($query, $mode);
250
	}
251
252
	public function isConnected() {
253
		return ($this->wrapperObject !== null && $this->ping());
254
	}
255
256
	public function ping() {
257
		return ($this->wrapperObject && $this->wrapperObject->ping());
258
	}
259
}
260