Passed
Push — master ( caa32e...4f267a )
by Jean-Christophe
09:15
created

DatabaseOperationsTrait::getUpdateStatement()   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 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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.2
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 2
	public function getDbObject() {
25 2
		return $this->wrapperObject->getDbInstance ();
26
	}
27
28 84
	public function _connect() {
29 84
		$this->wrapperObject->connect ( $this->dbType, $this->dbName, $this->serverName, $this->port, $this->user, $this->password, $this->options );
30 84
	}
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 59
	public function prepareAndExecute($tableName, $condition, $fields, $parameters = null, $useCache = NULL) {
52 59
		$cache = ((DbCache::$active && $useCache !== false) || (! DbCache::$active && $useCache === true));
53 59
		$result = false;
54 59
		if ($cache) {
55 18
			$cKey = $condition;
56 18
			if (is_array ( $parameters )) {
57 15
				$cKey .= implode ( ",", $parameters );
58
			}
59
			try {
60 18
				$result = $this->cache->fetch ( $tableName, $cKey );
61 17
				Logger::info ( "Cache", "fetching cache for table {$tableName} with condition : {$condition}", "Database::prepareAndExecute", $parameters );
62 1
			} catch ( \Exception $e ) {
63
				throw new CacheException ( "Cache is not created in Database constructor" );
64
			}
65
		}
66 59
		if ($result === false) {
67 57
			$quote = SqlUtils::$quote;
68 57
			$result = $this->wrapperObject->_optPrepareAndExecute ( "SELECT {$fields} FROM {$quote}{$tableName}{$quote} {$condition}", $parameters );
69 57
			if ($cache) {
70 15
				$this->cache->store ( $tableName, $cKey, $result );
71
			}
72
		}
73 59
		return $result;
74
	}
75
76 24
	public function prepareAndFetchAll($sql, $parameters = null, $mode = null) {
77 24
		return $this->wrapperObject->fetchAll ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode );
78
	}
79
80
	public function prepareAndFetchOne($sql, $parameters = null, $mode = null) {
81
		return $this->wrapperObject->fetchOne ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $mode );
82
	}
83
84 1
	public function prepareAndFetchAllColumn($sql, $parameters = null, $column = null) {
85 1
		return $this->wrapperObject->fetchAllColumn ( $this->wrapperObject->_getStatement ( $sql ), $parameters, $column );
86
	}
87
88 23
	public function prepareAndFetchColumn($sql, $parameters = null, $columnNumber = null) {
89 23
		$statement = $this->wrapperObject->_getStatement ( $sql );
90 23
		if ($statement->execute ( $parameters )) {
91 23
			Logger::info ( "Database", $sql, "prepareAndFetchColumn", $parameters );
92 23
			return $statement->fetchColumn ( $columnNumber );
93
		}
94
		return false;
95
	}
96
97
	/**
98
	 *
99
	 * @param string $sql
100
	 * @return object statement
101
	 */
102
	private function getStatement($sql) {
103
		return $this->wrapperObject->_getStatement ( $sql );
104
	}
105
106
	/**
107
	 *
108
	 * @param string $sql
109
	 * @return object statement
110
	 */
111 13
	public function getUpdateStatement($sql) {
112 13
		return $this->wrapperObject->_getStatement ( $sql );
113
	}
114
115
	/**
116
	 * Prepares a statement and execute a query for update (INSERT, UPDATE, DELETE...)
117
	 *
118
	 * @param string $sql
119
	 * @param array|null $parameters
120
	 * @return boolean
121
	 */
122
	public function prepareAndExecuteUpdate($sql, $parameters = null) {
123
		return $this->getUpdateStatement ( $sql )->execute ( $parameters );
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 2
	public function execute($sql) {
133 2
		return $this->wrapperObject->execute ( $sql );
134
	}
135
136
	/**
137
	 * Prepares a statement for execution and returns a statement object
138
	 *
139
	 * @param String $sql
140
	 * @return object|boolean
141
	 */
142 10
	public function prepareStatement($sql) {
143 10
		return $this->wrapperObject->prepareStatement ( $sql );
144
	}
145
146
	/**
147
	 * Sets $value to $parameter
148
	 *
149
	 * @param mixed $statement
150
	 * @param String $parameter
151
	 * @param mixed $value
152
	 * @return boolean
153
	 */
154 1
	public function bindValueFromStatement($statement, $parameter, $value) {
155 1
		return $this->wrapperObject->bindValueFromStatement ( $statement, $parameter, $value );
156
	}
157
158
	/**
159
	 * Returns the last insert id
160
	 *
161
	 * @return string
162
	 */
163 12
	public function lastInserId() {
164 12
		return $this->wrapperObject->lastInsertId ();
165
	}
166
167
	/**
168
	 * Returns the number of records in $tableName matching with the condition passed as a parameter
169
	 *
170
	 * @param string $tableName
171
	 * @param string $condition Part following the WHERE of an SQL statement
172
	 */
173 1
	public function count($tableName, $condition = '') {
174 1
		if ($condition != '')
175 1
			$condition = " WHERE " . $condition;
176 1
		return $this->wrapperObject->queryColumn ( "SELECT COUNT(*) FROM " . $tableName . $condition );
177
	}
178
179 4
	public function queryColumn($query, $columnNumber = null) {
180 4
		return $this->wrapperObject->queryColumn ( $query, $columnNumber );
181
	}
182
183 1
	public function fetchAll($query, $mode = null) {
184 1
		return $this->wrapperObject->queryAll ( $query, $mode );
185
	}
186
187 19
	public function isConnected() {
188 19
		return ($this->wrapperObject !== null && $this->ping ());
189
	}
190
191 19
	public function ping() {
192 19
		return ($this->wrapperObject && $this->wrapperObject->ping ());
193
	}
194
}
195
196