PdoAdapter::rollback()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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