Completed
Push — master ( eb3c6f...097036 )
by Alexander
13:10 queued 09:24
created

PdoAdapter::query()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PDO adapter class
4
 *
5
 * @file      PdoAdapter.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2018 Alexander Yancharuk
11
 * @date      2013-12-31 15:44
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\DataBase\Adapters;
17
18
use PDO;
19
use Veles\DataBase\Exceptions\DbException;
20
21
/**
22
 * Class PdoAdapter
23
 *
24
 * Adapter PDO extension
25
 *
26
 * @author  Alexander Yancharuk <alex at itvault dot info>
27
 */
28
class PdoAdapter implements DbAdapterInterface
29
{
30
	// Save statement for ability to get error information
31
	/** @var  \PDOStatement */
32
	protected $stmt;
33
34
	protected $type = [
35
	   'i' => PDO::PARAM_INT,
36
	   'd' => PDO::PARAM_STR,
37
	   's' => PDO::PARAM_STR,
38
	   'b' => PDO::PARAM_LOB
39
	];
40
41
	use DbConnectionTrait;
42
43 2
	protected function bindParams(array $params, $types)
44
	{
45 2
		foreach ($params as $key => $param) {
46 2
			$type = isset($this->type[$types[$key]])
47 2
				? $this->type[$types[$key]]
48 2
				: PDO::PARAM_STR;
49
			// Placeholder numbers begins from 1
50 2
			$this->stmt->bindValue($key + 1, $param, $type);
51
		}
52 2
	}
53
54 6
	protected function prepare($sql, array $params, $types)
55
	{
56 6
		$this->stmt = $this->getResource()->prepare($sql);
57
58 4
		if (null === $types) {
59 2
			$this->stmt->execute($params);
60
		} else {
61 2
			$this->bindParams($params, $types);
62 2
			$this->stmt->execute();
63
		}
64 4
	}
65
66 4
	protected function execute($sql, array $params, $types)
67
	{
68 4
		$this->stmt = $this->getResource()->prepare($sql);
69
70 4
		if (null !== $types) {
71 2
			$this->bindParams($params, $types);
72
		}
73
74 4
		return null === $types
75 2
			? $this->stmt->execute($params)
76 4
			: $this->stmt->execute();
77
	}
78
79
	/**
80
	 * Throw DbException with query info
81
	 *
82
	 * @param string        $sql
83
	 * @param array         $params
84
	 * @param \PDOException $e
85
	 *
86
	 * @throws DbException
87
	 */
88 2
	protected function throwExceptionWithInfo($sql, array $params, \PDOException $e)
89
	{
90 2
		$exception = new DbException($e->getMessage(), (int) $e->getCode(), $e);
91 2
		$exception->setSql($sql);
92 2
		$exception->setParams($params);
93
94 2
		throw $exception;
95
	}
96
97
	/**
98
	 * Get value from table row
99
	 *
100
	 * @param string      $sql    SQL-query
101
	 * @param array       $params Query values
102
	 * @param string|null $types  Placeholders types
103
	 *
104
	 * @return mixed
105
	 * @throws DbException
106
	 */
107 6
	public function value($sql, array $params, $types)
108
	{
109 6
		$result = '';
110
111
		try {
112 6
			$this->prepare($sql, $params, $types);
113 4
			$result = $this->stmt->fetchColumn();
114 2
		} catch (\PDOException $e) {
115 2
			$this->throwExceptionWithInfo($sql, $params, $e);
116
		}
117
118 4
		return $result;
119
	}
120
121
	/**
122
	 * Get table row
123
	 *
124
	 * @param string      $sql    SQL-query
125
	 * @param array       $params Query values
126
	 * @param string|null $types  Placeholders types
127
	 *
128
	 * @return mixed              Depends on fetch type
129
	 * @throws DbException
130
	 */
131 4
	public function row($sql, array $params, $types)
132
	{
133 4
		$result = [];
134
135
		try {
136 4
			$this->prepare($sql, $params, $types);
137 2
			$result = $this->stmt->fetch();
138 2
		} catch (\PDOException $e) {
139 2
			$this->throwExceptionWithInfo($sql, $params, $e);
140
		}
141
142 2
		return $result;
143
	}
144
145
	/**
146
	 * Get result collection
147
	 *
148
	 * @param string      $sql    SQL-query
149
	 * @param array       $params Query values
150
	 * @param string|null $types  Placeholders types
151
	 *
152
	 * @return mixed              Depends on fetch type
153
	 * @throws DbException
154
	 */
155 4
	public function rows($sql, array $params, $types)
156
	{
157 4
		$result = [];
158
159
		try {
160 4
			$this->prepare($sql, $params, $types);
161 2
			$result = $this->stmt->fetchAll();
162 2
		} catch (\PDOException $e) {
163 2
			$this->throwExceptionWithInfo($sql, $params, $e);
164
		}
165
166 2
		return $result;
167
	}
168
169
	/**
170
	 * Transaction initialization
171
	 *
172
	 * @return bool
173
	 * @throws DbException
174
	 */
175 4
	public function begin()
176
	{
177
		try {
178 4
			$result = $this->getResource()->beginTransaction();
179 2
		} catch (\PDOException $e) {
180 2
			throw new DbException($e->getMessage(), (int) $e->getCode(), $e);
181
		}
182 2
		return $result;
183
	}
184
185
	/**
186
	 * Transaction rollback
187
	 *
188
	 * @return bool
189
	 * @throws DbException
190
	 */
191 4
	public function rollback()
192
	{
193
		try {
194 4
			$result = $this->getResource()->rollBack();
195 2
		} catch (\PDOException $e) {
196 2
			throw new DbException($e->getMessage(), (int) $e->getCode(), $e);
197
		}
198 2
		return $result;
199
	}
200
201
	/**
202
	 * Commit transaction
203
	 *
204
	 * @return bool
205
	 * @throws DbException
206
	 */
207 4
	public function commit()
208
	{
209
		try {
210 4
			$result = $this->getResource()->commit();
211 2
		} catch (\PDOException $e) {
212 2
			throw new DbException($e->getMessage(), (int) $e->getCode(), $e);
213
		}
214 2
		return $result;
215
	}
216
217
	/**
218
	 * Launch non-SELECT query
219
	 *
220
	 * @param string      $sql    Non-SELECT SQL-query
221
	 * @param array       $params Query values
222
	 * @param string|null $types  Placeholders types
223
	 *
224
	 * @return bool
225
	 * @throws DbException
226
	 */
227 8
	public function query($sql, array $params, $types)
228
	{
229 8
		$result = false;
230
231
		try {
232 8
			if (empty($params)) {
233 4
				return (bool) $this->getResource()->query($sql);
234
			}
235
236 4
			$result = $this->execute($sql, $params, $types);
237 2
		} catch (\PDOException $e) {
238 2
			$this->throwExceptionWithInfo($sql, $params, $e);
239
		}
240
241 4
		return $result;
242
	}
243
244
	/**
245
	 * Get last saved ID
246
	 *
247
	 * @return int
248
	 * @throws DbException
249
	 */
250 4
	public function getLastInsertId()
251
	{
252
		try {
253 4
			$result = (int) $this->getResource()->lastInsertId();
254 2
		} catch (\PDOException $e) {
255 2
			throw new DbException($e->getMessage(), (int) $e->getCode(), $e);
256
		}
257 2
		return $result;
258
	}
259
260
	/**
261
	 * Get found rows quantity
262
	 *
263
	 * @return int
264
	 * @throws DbException
265
	 */
266 2
	public function getFoundRows()
267
	{
268 2
		return (int) $this->value('SELECT FOUND_ROWS()', [], null);
269
	}
270
271
	/**
272
	 * Get PDOStatement
273
	 *
274
	 * Used in subscribers for getting error information
275
	 *
276
	 * @return mixed
277
	 */
278 2
	public function getStmt()
279
	{
280 2
		return $this->stmt;
281
	}
282
283
	/**
284
	 * Escape variable
285
	 *
286
	 * @param string $var
287
	 *
288
	 * @return string
289
	 * @throws DbException
290
	 */
291 4
	public function escape($var)
292
	{
293
		try {
294 4
			$result = $this->getResource()->quote($var);
295 2
		} catch (\PDOException $e) {
296 2
			throw new DbException($e->getMessage(), (int) $e->getCode(), $e);
297
		}
298 2
		return $result;
299
	}
300
}
301