Passed
Push — master ( aeb32e...81302f )
by Christoph
15:20 queued 10s
created

PreparedStatement   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 56
rs 10
c 1
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 2 1
A bindValue() 0 2 1
A __construct() 0 2 1
A rowCount() 0 2 1
A getResult() 0 6 2
A closeCursor() 0 4 1
A fetchOne() 0 2 1
A fetchColumn() 0 2 1
A fetch() 0 2 1
A bindParam() 0 2 1
A fetchAll() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2021 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2021 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\DB;
27
28
use Doctrine\DBAL\Exception;
29
use Doctrine\DBAL\ParameterType;
30
use Doctrine\DBAL\Statement;
31
use OCP\DB\IPreparedStatement;
32
use OCP\DB\IResult;
33
use PDO;
34
35
/**
36
 * Adapts our public API to what doctrine/dbal exposed with 2.6
37
 *
38
 * The old dbal statement had stateful methods e.g. to fetch data from an executed
39
 * prepared statement. To provide backwards compatibility to apps we need to make
40
 * this class stateful. As soon as those now deprecated exposed methods are gone,
41
 * we can limit the API of this adapter to the methods that map to the direct dbal
42
 * methods without much magic.
43
 */
44
class PreparedStatement implements IPreparedStatement {
45
46
	/** @var Statement */
47
	private $statement;
48
49
	/** @var IResult|null */
50
	private $result;
51
52
	public function __construct(Statement $statement) {
53
		$this->statement = $statement;
54
	}
55
56
	public function closeCursor(): bool {
57
		$this->getResult()->closeCursor();
58
59
		return true;
60
	}
61
62
	public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
63
		return $this->getResult()->fetch($fetchMode);
64
	}
65
66
	public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
67
		return $this->getResult()->fetchAll($fetchMode);
68
	}
69
70
	public function fetchColumn() {
71
		return $this->getResult()->fetchOne();
72
	}
73
74
	public function fetchOne() {
75
		return $this->getResult()->fetchOne();
76
	}
77
78
	public function bindValue($param, $value, $type = ParameterType::STRING): bool {
79
		return $this->statement->bindValue($param, $value, $type);
80
	}
81
82
	public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool {
83
		return $this->statement->bindParam($param, $variable, $type, $length);
84
	}
85
86
	public function execute($params = null): IResult {
87
		return ($this->result = new ResultAdapter($this->statement->execute($params)));
88
	}
89
90
	public function rowCount(): int {
91
		return $this->getResult()->rowCount();
92
	}
93
94
	private function getResult(): IResult {
95
		if ($this->result !== null) {
96
			return $this->result;
97
		}
98
99
		throw new Exception("You have to execute the prepared statement before accessing the results");
100
	}
101
}
102