Completed
Push — master ( 7524e4...6d5841 )
by Alexander
06:07 queued 03:07
created

PdoAdapter::rows()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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