Passed
Push — master ( 5a141b...b9287f )
by Christoph
23:29 queued 08:32
created

ConnectionAdapter::inTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 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\Platforms\AbstractPlatform;
30
use Doctrine\DBAL\Schema\Schema;
31
use OC\DB\Exceptions\DbalException;
32
use OCP\DB\IPreparedStatement;
33
use OCP\DB\IResult;
34
use OCP\DB\QueryBuilder\IQueryBuilder;
35
use OCP\IDBConnection;
36
37
/**
38
 * Adapts the public API to our internal DBAL connection wrapper
39
 */
40
class ConnectionAdapter implements IDBConnection {
41
42
	/** @var Connection */
43
	private $inner;
44
45
	public function __construct(Connection $inner) {
46
		$this->inner = $inner;
47
	}
48
49
	public function getQueryBuilder(): IQueryBuilder {
50
		return $this->inner->getQueryBuilder();
51
	}
52
53
	public function prepare($sql, $limit = null, $offset = null): IPreparedStatement {
54
		try {
55
			return new PreparedStatement(
56
				$this->inner->prepare($sql, $limit, $offset)
57
			);
58
		} catch (Exception $e) {
59
			throw DbalException::wrap($e);
60
		}
61
	}
62
63
	public function executeQuery(string $sql, array $params = [], $types = []): IResult {
64
		try {
65
			return new ResultAdapter(
66
				$this->inner->executeQuery($sql, $params, $types)
67
			);
68
		} catch (Exception $e) {
69
			throw DbalException::wrap($e);
70
		}
71
	}
72
73
	public function executeUpdate(string $sql, array $params = [], array $types = []): int {
74
		try {
75
			return $this->inner->executeUpdate($sql, $params, $types);
76
		} catch (Exception $e) {
77
			throw DbalException::wrap($e);
78
		}
79
	}
80
81
	public function executeStatement($sql, array $params = [], array $types = []): int {
82
		try {
83
			return $this->inner->executeStatement($sql, $params, $types);
84
		} catch (Exception $e) {
85
			throw DbalException::wrap($e);
86
		}
87
	}
88
89
	public function lastInsertId(string $table): int {
90
		try {
91
			return (int)$this->inner->lastInsertId($table);
92
		} catch (Exception $e) {
93
			throw DbalException::wrap($e);
94
		}
95
	}
96
97
	public function insertIfNotExist(string $table, array $input, array $compare = null) {
98
		try {
99
			return $this->inner->insertIfNotExist($table, $input, $compare);
100
		} catch (Exception $e) {
101
			throw DbalException::wrap($e);
102
		}
103
	}
104
105
	public function insertIgnoreConflict(string $table, array $values): int {
106
		try {
107
			return $this->inner->insertIgnoreConflict($table, $values);
108
		} catch (Exception $e) {
109
			throw DbalException::wrap($e);
110
		}
111
	}
112
113
	public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int {
114
		try {
115
			return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues);
116
		} catch (Exception $e) {
117
			throw DbalException::wrap($e);
118
		}
119
	}
120
121
	public function lockTable($tableName): void {
122
		try {
123
			$this->inner->lockTable($tableName);
124
		} catch (Exception $e) {
125
			throw DbalException::wrap($e);
126
		}
127
	}
128
129
	public function unlockTable(): void {
130
		try {
131
			$this->inner->unlockTable();
132
		} catch (Exception $e) {
133
			throw DbalException::wrap($e);
134
		}
135
	}
136
137
	public function beginTransaction(): void {
138
		try {
139
			$this->inner->beginTransaction();
140
		} catch (Exception $e) {
141
			throw DbalException::wrap($e);
142
		}
143
	}
144
145
	public function inTransaction(): bool {
146
		return $this->inner->inTransaction();
147
	}
148
149
	public function commit(): void {
150
		try {
151
			$this->inner->commit();
152
		} catch (Exception $e) {
153
			throw DbalException::wrap($e);
154
		}
155
	}
156
157
	public function rollBack(): void {
158
		try {
159
			$this->inner->rollBack();
160
		} catch (Exception $e) {
161
			throw DbalException::wrap($e);
162
		}
163
	}
164
165
	public function getError(): string {
166
		return $this->inner->getError();
167
	}
168
169
	public function errorCode() {
170
		return $this->inner->errorCode();
171
	}
172
173
	public function errorInfo() {
174
		return $this->inner->errorInfo();
175
	}
176
177
	public function connect(): bool {
178
		try {
179
			return $this->inner->connect();
180
		} catch (Exception $e) {
181
			throw DbalException::wrap($e);
182
		}
183
	}
184
185
	public function close(): void {
186
		$this->inner->close();
187
	}
188
189
	public function quote($input, $type = IQueryBuilder::PARAM_STR) {
190
		return $this->inner->quote($input, $type);
191
	}
192
193
	/**
194
	 * @todo we are leaking a 3rdparty type here
195
	 */
196
	public function getDatabasePlatform(): AbstractPlatform {
197
		return $this->inner->getDatabasePlatform();
198
	}
199
200
	public function dropTable(string $table): void {
201
		try {
202
			$this->inner->dropTable($table);
203
		} catch (Exception $e) {
204
			throw DbalException::wrap($e);
205
		}
206
	}
207
208
	public function tableExists(string $table): bool {
209
		try {
210
			return $this->inner->tableExists($table);
211
		} catch (Exception $e) {
212
			throw DbalException::wrap($e);
213
		}
214
	}
215
216
	public function escapeLikeParameter(string $param): string {
217
		return $this->inner->escapeLikeParameter($param);
218
	}
219
220
	public function supports4ByteText(): bool {
221
		return $this->inner->supports4ByteText();
222
	}
223
224
	/**
225
	 * @todo leaks a 3rdparty type
226
	 */
227
	public function createSchema(): Schema {
228
		try {
229
			return $this->inner->createSchema();
230
		} catch (Exception $e) {
231
			throw DbalException::wrap($e);
232
		}
233
	}
234
235
	public function migrateToSchema(Schema $toSchema): void {
236
		try {
237
			$this->inner->migrateToSchema($toSchema);
238
		} catch (Exception $e) {
239
			throw DbalException::wrap($e);
240
		}
241
	}
242
243
	public function getInner(): Connection {
244
		return $this->inner;
245
	}
246
}
247