@@ -26,27 +26,27 @@ |
||
26 | 26 | use Doctrine\DBAL\Driver; |
27 | 27 | |
28 | 28 | class ReconnectWrapper extends \Doctrine\DBAL\Connection { |
29 | - const CHECK_CONNECTION_INTERVAL = 60; |
|
29 | + const CHECK_CONNECTION_INTERVAL = 60; |
|
30 | 30 | |
31 | - private $lastConnectionCheck = null; |
|
31 | + private $lastConnectionCheck = null; |
|
32 | 32 | |
33 | - public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { |
|
34 | - parent::__construct($params, $driver, $config, $eventManager); |
|
35 | - $this->lastConnectionCheck = time(); |
|
36 | - } |
|
33 | + public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { |
|
34 | + parent::__construct($params, $driver, $config, $eventManager); |
|
35 | + $this->lastConnectionCheck = time(); |
|
36 | + } |
|
37 | 37 | |
38 | - public function connect() { |
|
39 | - $now = time(); |
|
40 | - $checkTime = $now - self::CHECK_CONNECTION_INTERVAL; |
|
38 | + public function connect() { |
|
39 | + $now = time(); |
|
40 | + $checkTime = $now - self::CHECK_CONNECTION_INTERVAL; |
|
41 | 41 | |
42 | - if ($this->lastConnectionCheck > $checkTime || $this->isTransactionActive()) { |
|
43 | - return parent::connect(); |
|
44 | - } else { |
|
45 | - $this->lastConnectionCheck = $now; |
|
46 | - if (!$this->ping()) { |
|
47 | - $this->close(); |
|
48 | - } |
|
49 | - return parent::connect(); |
|
50 | - } |
|
51 | - } |
|
42 | + if ($this->lastConnectionCheck > $checkTime || $this->isTransactionActive()) { |
|
43 | + return parent::connect(); |
|
44 | + } else { |
|
45 | + $this->lastConnectionCheck = $now; |
|
46 | + if (!$this->ping()) { |
|
47 | + $this->close(); |
|
48 | + } |
|
49 | + return parent::connect(); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | } |
@@ -44,405 +44,405 @@ |
||
44 | 44 | use OCP\PreConditionNotMetException; |
45 | 45 | |
46 | 46 | class Connection extends ReconnectWrapper implements IDBConnection { |
47 | - /** |
|
48 | - * @var string $tablePrefix |
|
49 | - */ |
|
50 | - protected $tablePrefix; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var \OC\DB\Adapter $adapter |
|
54 | - */ |
|
55 | - protected $adapter; |
|
56 | - |
|
57 | - protected $lockedTable = null; |
|
58 | - |
|
59 | - public function connect() { |
|
60 | - try { |
|
61 | - return parent::connect(); |
|
62 | - } catch (DBALException $e) { |
|
63 | - // throw a new exception to prevent leaking info from the stacktrace |
|
64 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Returns a QueryBuilder for the connection. |
|
70 | - * |
|
71 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | - */ |
|
73 | - public function getQueryBuilder() { |
|
74 | - return new QueryBuilder( |
|
75 | - $this, |
|
76 | - \OC::$server->getSystemConfig(), |
|
77 | - \OC::$server->getLogger() |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Gets the QueryBuilder for the connection. |
|
83 | - * |
|
84 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | - * @deprecated please use $this->getQueryBuilder() instead |
|
86 | - */ |
|
87 | - public function createQueryBuilder() { |
|
88 | - $backtrace = $this->getCallerBacktrace(); |
|
89 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | - return parent::createQueryBuilder(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Gets the ExpressionBuilder for the connection. |
|
95 | - * |
|
96 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | - */ |
|
99 | - public function getExpressionBuilder() { |
|
100 | - $backtrace = $this->getCallerBacktrace(); |
|
101 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | - return parent::getExpressionBuilder(); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - protected function getCallerBacktrace() { |
|
111 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | - |
|
113 | - // 0 is the method where we use `getCallerBacktrace` |
|
114 | - // 1 is the target method which uses the method we want to log |
|
115 | - if (isset($traces[1])) { |
|
116 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | - } |
|
118 | - |
|
119 | - return ''; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function getPrefix() { |
|
126 | - return $this->tablePrefix; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Initializes a new instance of the Connection class. |
|
131 | - * |
|
132 | - * @param array $params The connection parameters. |
|
133 | - * @param \Doctrine\DBAL\Driver $driver |
|
134 | - * @param \Doctrine\DBAL\Configuration $config |
|
135 | - * @param \Doctrine\Common\EventManager $eventManager |
|
136 | - * @throws \Exception |
|
137 | - */ |
|
138 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | - EventManager $eventManager = null) |
|
140 | - { |
|
141 | - if (!isset($params['adapter'])) { |
|
142 | - throw new \Exception('adapter not set'); |
|
143 | - } |
|
144 | - if (!isset($params['tablePrefix'])) { |
|
145 | - throw new \Exception('tablePrefix not set'); |
|
146 | - } |
|
147 | - parent::__construct($params, $driver, $config, $eventManager); |
|
148 | - $this->adapter = new $params['adapter']($this); |
|
149 | - $this->tablePrefix = $params['tablePrefix']; |
|
150 | - |
|
151 | - parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Prepares an SQL statement. |
|
156 | - * |
|
157 | - * @param string $statement The SQL statement to prepare. |
|
158 | - * @param int $limit |
|
159 | - * @param int $offset |
|
160 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | - */ |
|
162 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | - if ($limit === -1) { |
|
164 | - $limit = null; |
|
165 | - } |
|
166 | - if (!is_null($limit)) { |
|
167 | - $platform = $this->getDatabasePlatform(); |
|
168 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | - } |
|
170 | - $statement = $this->replaceTablePrefix($statement); |
|
171 | - $statement = $this->adapter->fixupStatement($statement); |
|
172 | - |
|
173 | - return parent::prepare($statement); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Executes an, optionally parametrized, SQL query. |
|
178 | - * |
|
179 | - * If the query is parametrized, a prepared statement is used. |
|
180 | - * If an SQLLogger is configured, the execution is logged. |
|
181 | - * |
|
182 | - * @param string $query The SQL query to execute. |
|
183 | - * @param array $params The parameters to bind to the query, if any. |
|
184 | - * @param array $types The types the previous parameters are in. |
|
185 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | - * |
|
187 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | - * |
|
189 | - * @throws \Doctrine\DBAL\DBALException |
|
190 | - */ |
|
191 | - public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | - { |
|
193 | - $query = $this->replaceTablePrefix($query); |
|
194 | - $query = $this->adapter->fixupStatement($query); |
|
195 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | - * and returns the number of affected rows. |
|
201 | - * |
|
202 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | - * |
|
204 | - * @param string $query The SQL query. |
|
205 | - * @param array $params The query parameters. |
|
206 | - * @param array $types The parameter types. |
|
207 | - * |
|
208 | - * @return integer The number of affected rows. |
|
209 | - * |
|
210 | - * @throws \Doctrine\DBAL\DBALException |
|
211 | - */ |
|
212 | - public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | - { |
|
214 | - $query = $this->replaceTablePrefix($query); |
|
215 | - $query = $this->adapter->fixupStatement($query); |
|
216 | - return parent::executeUpdate($query, $params, $types); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | - * depending on the underlying driver. |
|
222 | - * |
|
223 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | - * columns or sequences. |
|
226 | - * |
|
227 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | - * @return string A string representation of the last inserted ID. |
|
229 | - */ |
|
230 | - public function lastInsertId($seqName = null) { |
|
231 | - if ($seqName) { |
|
232 | - $seqName = $this->replaceTablePrefix($seqName); |
|
233 | - } |
|
234 | - return $this->adapter->lastInsertId($seqName); |
|
235 | - } |
|
236 | - |
|
237 | - // internal use |
|
238 | - public function realLastInsertId($seqName = null) { |
|
239 | - return parent::lastInsertId($seqName); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Insert a row if the matching row does not exists. |
|
244 | - * |
|
245 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
246 | - * @param array $input data that should be inserted into the table (column name => value) |
|
247 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
248 | - * If this is null or an empty array, all keys of $input will be compared |
|
249 | - * Please note: text fields (clob) must not be used in the compare array |
|
250 | - * @return int number of inserted rows |
|
251 | - * @throws \Doctrine\DBAL\DBALException |
|
252 | - */ |
|
253 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
254 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
255 | - } |
|
256 | - |
|
257 | - private function getType($value) { |
|
258 | - if (is_bool($value)) { |
|
259 | - return IQueryBuilder::PARAM_BOOL; |
|
260 | - } else if (is_int($value)) { |
|
261 | - return IQueryBuilder::PARAM_INT; |
|
262 | - } else { |
|
263 | - return IQueryBuilder::PARAM_STR; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Insert or update a row value |
|
269 | - * |
|
270 | - * @param string $table |
|
271 | - * @param array $keys (column name => value) |
|
272 | - * @param array $values (column name => value) |
|
273 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
274 | - * @return int number of new rows |
|
275 | - * @throws \Doctrine\DBAL\DBALException |
|
276 | - * @throws PreConditionNotMetException |
|
277 | - * @suppress SqlInjectionChecker |
|
278 | - */ |
|
279 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
280 | - try { |
|
281 | - $insertQb = $this->getQueryBuilder(); |
|
282 | - $insertQb->insert($table) |
|
283 | - ->values( |
|
284 | - array_map(function($value) use ($insertQb) { |
|
285 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
286 | - }, array_merge($keys, $values)) |
|
287 | - ); |
|
288 | - return $insertQb->execute(); |
|
289 | - } catch (ConstraintViolationException $e) { |
|
290 | - // value already exists, try update |
|
291 | - $updateQb = $this->getQueryBuilder(); |
|
292 | - $updateQb->update($table); |
|
293 | - foreach ($values as $name => $value) { |
|
294 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
295 | - } |
|
296 | - $where = $updateQb->expr()->andX(); |
|
297 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
298 | - foreach ($whereValues as $name => $value) { |
|
299 | - $where->add($updateQb->expr()->eq( |
|
300 | - $name, |
|
301 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
302 | - $this->getType($value) |
|
303 | - )); |
|
304 | - } |
|
305 | - $updateQb->where($where); |
|
306 | - $affected = $updateQb->execute(); |
|
307 | - |
|
308 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
309 | - throw new PreConditionNotMetException(); |
|
310 | - } |
|
311 | - |
|
312 | - return 0; |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Create an exclusive read+write lock on a table |
|
318 | - * |
|
319 | - * @param string $tableName |
|
320 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
321 | - * @since 9.1.0 |
|
322 | - */ |
|
323 | - public function lockTable($tableName) { |
|
324 | - if ($this->lockedTable !== null) { |
|
325 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
326 | - } |
|
327 | - |
|
328 | - $tableName = $this->tablePrefix . $tableName; |
|
329 | - $this->lockedTable = $tableName; |
|
330 | - $this->adapter->lockTable($tableName); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Release a previous acquired lock again |
|
335 | - * |
|
336 | - * @since 9.1.0 |
|
337 | - */ |
|
338 | - public function unlockTable() { |
|
339 | - $this->adapter->unlockTable(); |
|
340 | - $this->lockedTable = null; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * returns the error code and message as a string for logging |
|
345 | - * works with DoctrineException |
|
346 | - * @return string |
|
347 | - */ |
|
348 | - public function getError() { |
|
349 | - $msg = $this->errorCode() . ': '; |
|
350 | - $errorInfo = $this->errorInfo(); |
|
351 | - if (is_array($errorInfo)) { |
|
352 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
353 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
354 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
355 | - } |
|
356 | - return $msg; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Drop a table from the database if it exists |
|
361 | - * |
|
362 | - * @param string $table table name without the prefix |
|
363 | - */ |
|
364 | - public function dropTable($table) { |
|
365 | - $table = $this->tablePrefix . trim($table); |
|
366 | - $schema = $this->getSchemaManager(); |
|
367 | - if($schema->tablesExist(array($table))) { |
|
368 | - $schema->dropTable($table); |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Check if a table exists |
|
374 | - * |
|
375 | - * @param string $table table name without the prefix |
|
376 | - * @return bool |
|
377 | - */ |
|
378 | - public function tableExists($table){ |
|
379 | - $table = $this->tablePrefix . trim($table); |
|
380 | - $schema = $this->getSchemaManager(); |
|
381 | - return $schema->tablesExist(array($table)); |
|
382 | - } |
|
383 | - |
|
384 | - // internal use |
|
385 | - /** |
|
386 | - * @param string $statement |
|
387 | - * @return string |
|
388 | - */ |
|
389 | - protected function replaceTablePrefix($statement) { |
|
390 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Check if a transaction is active |
|
395 | - * |
|
396 | - * @return bool |
|
397 | - * @since 8.2.0 |
|
398 | - */ |
|
399 | - public function inTransaction() { |
|
400 | - return $this->getTransactionNestingLevel() > 0; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Escape a parameter to be used in a LIKE query |
|
405 | - * |
|
406 | - * @param string $param |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - public function escapeLikeParameter($param) { |
|
410 | - return addcslashes($param, '\\_%'); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Check whether or not the current database support 4byte wide unicode |
|
415 | - * |
|
416 | - * @return bool |
|
417 | - * @since 11.0.0 |
|
418 | - */ |
|
419 | - public function supports4ByteText() { |
|
420 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
421 | - return true; |
|
422 | - } |
|
423 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Create the schema of the connected database |
|
429 | - * |
|
430 | - * @return Schema |
|
431 | - */ |
|
432 | - public function createSchema() { |
|
433 | - $schemaManager = new MDB2SchemaManager($this); |
|
434 | - $migrator = $schemaManager->getMigrator(); |
|
435 | - return $migrator->createSchema(); |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Migrate the database to the given schema |
|
440 | - * |
|
441 | - * @param Schema $toSchema |
|
442 | - */ |
|
443 | - public function migrateToSchema(Schema $toSchema) { |
|
444 | - $schemaManager = new MDB2SchemaManager($this); |
|
445 | - $migrator = $schemaManager->getMigrator(); |
|
446 | - $migrator->migrate($toSchema); |
|
447 | - } |
|
47 | + /** |
|
48 | + * @var string $tablePrefix |
|
49 | + */ |
|
50 | + protected $tablePrefix; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var \OC\DB\Adapter $adapter |
|
54 | + */ |
|
55 | + protected $adapter; |
|
56 | + |
|
57 | + protected $lockedTable = null; |
|
58 | + |
|
59 | + public function connect() { |
|
60 | + try { |
|
61 | + return parent::connect(); |
|
62 | + } catch (DBALException $e) { |
|
63 | + // throw a new exception to prevent leaking info from the stacktrace |
|
64 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Returns a QueryBuilder for the connection. |
|
70 | + * |
|
71 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | + */ |
|
73 | + public function getQueryBuilder() { |
|
74 | + return new QueryBuilder( |
|
75 | + $this, |
|
76 | + \OC::$server->getSystemConfig(), |
|
77 | + \OC::$server->getLogger() |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Gets the QueryBuilder for the connection. |
|
83 | + * |
|
84 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | + * @deprecated please use $this->getQueryBuilder() instead |
|
86 | + */ |
|
87 | + public function createQueryBuilder() { |
|
88 | + $backtrace = $this->getCallerBacktrace(); |
|
89 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | + return parent::createQueryBuilder(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Gets the ExpressionBuilder for the connection. |
|
95 | + * |
|
96 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | + */ |
|
99 | + public function getExpressionBuilder() { |
|
100 | + $backtrace = $this->getCallerBacktrace(); |
|
101 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | + return parent::getExpressionBuilder(); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + protected function getCallerBacktrace() { |
|
111 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | + |
|
113 | + // 0 is the method where we use `getCallerBacktrace` |
|
114 | + // 1 is the target method which uses the method we want to log |
|
115 | + if (isset($traces[1])) { |
|
116 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | + } |
|
118 | + |
|
119 | + return ''; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function getPrefix() { |
|
126 | + return $this->tablePrefix; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Initializes a new instance of the Connection class. |
|
131 | + * |
|
132 | + * @param array $params The connection parameters. |
|
133 | + * @param \Doctrine\DBAL\Driver $driver |
|
134 | + * @param \Doctrine\DBAL\Configuration $config |
|
135 | + * @param \Doctrine\Common\EventManager $eventManager |
|
136 | + * @throws \Exception |
|
137 | + */ |
|
138 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | + EventManager $eventManager = null) |
|
140 | + { |
|
141 | + if (!isset($params['adapter'])) { |
|
142 | + throw new \Exception('adapter not set'); |
|
143 | + } |
|
144 | + if (!isset($params['tablePrefix'])) { |
|
145 | + throw new \Exception('tablePrefix not set'); |
|
146 | + } |
|
147 | + parent::__construct($params, $driver, $config, $eventManager); |
|
148 | + $this->adapter = new $params['adapter']($this); |
|
149 | + $this->tablePrefix = $params['tablePrefix']; |
|
150 | + |
|
151 | + parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Prepares an SQL statement. |
|
156 | + * |
|
157 | + * @param string $statement The SQL statement to prepare. |
|
158 | + * @param int $limit |
|
159 | + * @param int $offset |
|
160 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | + */ |
|
162 | + public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | + if ($limit === -1) { |
|
164 | + $limit = null; |
|
165 | + } |
|
166 | + if (!is_null($limit)) { |
|
167 | + $platform = $this->getDatabasePlatform(); |
|
168 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | + } |
|
170 | + $statement = $this->replaceTablePrefix($statement); |
|
171 | + $statement = $this->adapter->fixupStatement($statement); |
|
172 | + |
|
173 | + return parent::prepare($statement); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Executes an, optionally parametrized, SQL query. |
|
178 | + * |
|
179 | + * If the query is parametrized, a prepared statement is used. |
|
180 | + * If an SQLLogger is configured, the execution is logged. |
|
181 | + * |
|
182 | + * @param string $query The SQL query to execute. |
|
183 | + * @param array $params The parameters to bind to the query, if any. |
|
184 | + * @param array $types The types the previous parameters are in. |
|
185 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | + * |
|
187 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | + * |
|
189 | + * @throws \Doctrine\DBAL\DBALException |
|
190 | + */ |
|
191 | + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | + { |
|
193 | + $query = $this->replaceTablePrefix($query); |
|
194 | + $query = $this->adapter->fixupStatement($query); |
|
195 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | + * and returns the number of affected rows. |
|
201 | + * |
|
202 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | + * |
|
204 | + * @param string $query The SQL query. |
|
205 | + * @param array $params The query parameters. |
|
206 | + * @param array $types The parameter types. |
|
207 | + * |
|
208 | + * @return integer The number of affected rows. |
|
209 | + * |
|
210 | + * @throws \Doctrine\DBAL\DBALException |
|
211 | + */ |
|
212 | + public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | + { |
|
214 | + $query = $this->replaceTablePrefix($query); |
|
215 | + $query = $this->adapter->fixupStatement($query); |
|
216 | + return parent::executeUpdate($query, $params, $types); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | + * depending on the underlying driver. |
|
222 | + * |
|
223 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | + * columns or sequences. |
|
226 | + * |
|
227 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | + * @return string A string representation of the last inserted ID. |
|
229 | + */ |
|
230 | + public function lastInsertId($seqName = null) { |
|
231 | + if ($seqName) { |
|
232 | + $seqName = $this->replaceTablePrefix($seqName); |
|
233 | + } |
|
234 | + return $this->adapter->lastInsertId($seqName); |
|
235 | + } |
|
236 | + |
|
237 | + // internal use |
|
238 | + public function realLastInsertId($seqName = null) { |
|
239 | + return parent::lastInsertId($seqName); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Insert a row if the matching row does not exists. |
|
244 | + * |
|
245 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
246 | + * @param array $input data that should be inserted into the table (column name => value) |
|
247 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
248 | + * If this is null or an empty array, all keys of $input will be compared |
|
249 | + * Please note: text fields (clob) must not be used in the compare array |
|
250 | + * @return int number of inserted rows |
|
251 | + * @throws \Doctrine\DBAL\DBALException |
|
252 | + */ |
|
253 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
254 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
255 | + } |
|
256 | + |
|
257 | + private function getType($value) { |
|
258 | + if (is_bool($value)) { |
|
259 | + return IQueryBuilder::PARAM_BOOL; |
|
260 | + } else if (is_int($value)) { |
|
261 | + return IQueryBuilder::PARAM_INT; |
|
262 | + } else { |
|
263 | + return IQueryBuilder::PARAM_STR; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Insert or update a row value |
|
269 | + * |
|
270 | + * @param string $table |
|
271 | + * @param array $keys (column name => value) |
|
272 | + * @param array $values (column name => value) |
|
273 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
274 | + * @return int number of new rows |
|
275 | + * @throws \Doctrine\DBAL\DBALException |
|
276 | + * @throws PreConditionNotMetException |
|
277 | + * @suppress SqlInjectionChecker |
|
278 | + */ |
|
279 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
280 | + try { |
|
281 | + $insertQb = $this->getQueryBuilder(); |
|
282 | + $insertQb->insert($table) |
|
283 | + ->values( |
|
284 | + array_map(function($value) use ($insertQb) { |
|
285 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
286 | + }, array_merge($keys, $values)) |
|
287 | + ); |
|
288 | + return $insertQb->execute(); |
|
289 | + } catch (ConstraintViolationException $e) { |
|
290 | + // value already exists, try update |
|
291 | + $updateQb = $this->getQueryBuilder(); |
|
292 | + $updateQb->update($table); |
|
293 | + foreach ($values as $name => $value) { |
|
294 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
295 | + } |
|
296 | + $where = $updateQb->expr()->andX(); |
|
297 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
298 | + foreach ($whereValues as $name => $value) { |
|
299 | + $where->add($updateQb->expr()->eq( |
|
300 | + $name, |
|
301 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
302 | + $this->getType($value) |
|
303 | + )); |
|
304 | + } |
|
305 | + $updateQb->where($where); |
|
306 | + $affected = $updateQb->execute(); |
|
307 | + |
|
308 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
309 | + throw new PreConditionNotMetException(); |
|
310 | + } |
|
311 | + |
|
312 | + return 0; |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Create an exclusive read+write lock on a table |
|
318 | + * |
|
319 | + * @param string $tableName |
|
320 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
321 | + * @since 9.1.0 |
|
322 | + */ |
|
323 | + public function lockTable($tableName) { |
|
324 | + if ($this->lockedTable !== null) { |
|
325 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
326 | + } |
|
327 | + |
|
328 | + $tableName = $this->tablePrefix . $tableName; |
|
329 | + $this->lockedTable = $tableName; |
|
330 | + $this->adapter->lockTable($tableName); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Release a previous acquired lock again |
|
335 | + * |
|
336 | + * @since 9.1.0 |
|
337 | + */ |
|
338 | + public function unlockTable() { |
|
339 | + $this->adapter->unlockTable(); |
|
340 | + $this->lockedTable = null; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * returns the error code and message as a string for logging |
|
345 | + * works with DoctrineException |
|
346 | + * @return string |
|
347 | + */ |
|
348 | + public function getError() { |
|
349 | + $msg = $this->errorCode() . ': '; |
|
350 | + $errorInfo = $this->errorInfo(); |
|
351 | + if (is_array($errorInfo)) { |
|
352 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
353 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
354 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
355 | + } |
|
356 | + return $msg; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Drop a table from the database if it exists |
|
361 | + * |
|
362 | + * @param string $table table name without the prefix |
|
363 | + */ |
|
364 | + public function dropTable($table) { |
|
365 | + $table = $this->tablePrefix . trim($table); |
|
366 | + $schema = $this->getSchemaManager(); |
|
367 | + if($schema->tablesExist(array($table))) { |
|
368 | + $schema->dropTable($table); |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Check if a table exists |
|
374 | + * |
|
375 | + * @param string $table table name without the prefix |
|
376 | + * @return bool |
|
377 | + */ |
|
378 | + public function tableExists($table){ |
|
379 | + $table = $this->tablePrefix . trim($table); |
|
380 | + $schema = $this->getSchemaManager(); |
|
381 | + return $schema->tablesExist(array($table)); |
|
382 | + } |
|
383 | + |
|
384 | + // internal use |
|
385 | + /** |
|
386 | + * @param string $statement |
|
387 | + * @return string |
|
388 | + */ |
|
389 | + protected function replaceTablePrefix($statement) { |
|
390 | + return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Check if a transaction is active |
|
395 | + * |
|
396 | + * @return bool |
|
397 | + * @since 8.2.0 |
|
398 | + */ |
|
399 | + public function inTransaction() { |
|
400 | + return $this->getTransactionNestingLevel() > 0; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Escape a parameter to be used in a LIKE query |
|
405 | + * |
|
406 | + * @param string $param |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + public function escapeLikeParameter($param) { |
|
410 | + return addcslashes($param, '\\_%'); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Check whether or not the current database support 4byte wide unicode |
|
415 | + * |
|
416 | + * @return bool |
|
417 | + * @since 11.0.0 |
|
418 | + */ |
|
419 | + public function supports4ByteText() { |
|
420 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
421 | + return true; |
|
422 | + } |
|
423 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Create the schema of the connected database |
|
429 | + * |
|
430 | + * @return Schema |
|
431 | + */ |
|
432 | + public function createSchema() { |
|
433 | + $schemaManager = new MDB2SchemaManager($this); |
|
434 | + $migrator = $schemaManager->getMigrator(); |
|
435 | + return $migrator->createSchema(); |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Migrate the database to the given schema |
|
440 | + * |
|
441 | + * @param Schema $toSchema |
|
442 | + */ |
|
443 | + public function migrateToSchema(Schema $toSchema) { |
|
444 | + $schemaManager = new MDB2SchemaManager($this); |
|
445 | + $migrator = $schemaManager->getMigrator(); |
|
446 | + $migrator->migrate($toSchema); |
|
447 | + } |
|
448 | 448 | } |