Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add our own DB exception abstraction #25091

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
'OCP\\Contacts\\ContactsMenu\\IProvider' => $baseDir . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => $baseDir . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => $baseDir . '/lib/public/Contacts/IManager.php',
'OCP\\DB\\Exception' => $baseDir . '/lib/public/DB/Exception.php',
'OCP\\DB\\IPreparedStatement' => $baseDir . '/lib/public/DB/IPreparedStatement.php',
'OCP\\DB\\IResult' => $baseDir . '/lib/public/DB/IResult.php',
'OCP\\DB\\ISchemaWrapper' => $baseDir . '/lib/public/DB/ISchemaWrapper.php',
Expand Down Expand Up @@ -952,6 +953,7 @@
'OC\\DB\\Connection' => $baseDir . '/lib/private/DB/Connection.php',
'OC\\DB\\ConnectionAdapter' => $baseDir . '/lib/private/DB/ConnectionAdapter.php',
'OC\\DB\\ConnectionFactory' => $baseDir . '/lib/private/DB/ConnectionFactory.php',
'OC\\DB\\Exceptions\\DbalException' => $baseDir . '/lib/private/DB/Exceptions/DbalException.php',
'OC\\DB\\MDB2SchemaManager' => $baseDir . '/lib/private/DB/MDB2SchemaManager.php',
'OC\\DB\\MDB2SchemaReader' => $baseDir . '/lib/private/DB/MDB2SchemaReader.php',
'OC\\DB\\MigrationException' => $baseDir . '/lib/private/DB/MigrationException.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Contacts\\ContactsMenu\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => __DIR__ . '/../../..' . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => __DIR__ . '/../../..' . '/lib/public/Contacts/IManager.php',
'OCP\\DB\\Exception' => __DIR__ . '/../../..' . '/lib/public/DB/Exception.php',
'OCP\\DB\\IPreparedStatement' => __DIR__ . '/../../..' . '/lib/public/DB/IPreparedStatement.php',
'OCP\\DB\\IResult' => __DIR__ . '/../../..' . '/lib/public/DB/IResult.php',
'OCP\\DB\\ISchemaWrapper' => __DIR__ . '/../../..' . '/lib/public/DB/ISchemaWrapper.php',
Expand Down Expand Up @@ -981,6 +982,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\DB\\Connection' => __DIR__ . '/../../..' . '/lib/private/DB/Connection.php',
'OC\\DB\\ConnectionAdapter' => __DIR__ . '/../../..' . '/lib/private/DB/ConnectionAdapter.php',
'OC\\DB\\ConnectionFactory' => __DIR__ . '/../../..' . '/lib/private/DB/ConnectionFactory.php',
'OC\\DB\\Exceptions\\DbalException' => __DIR__ . '/../../..' . '/lib/private/DB/Exceptions/DbalException.php',
'OC\\DB\\MDB2SchemaManager' => __DIR__ . '/../../..' . '/lib/private/DB/MDB2SchemaManager.php',
'OC\\DB\\MDB2SchemaReader' => __DIR__ . '/../../..' . '/lib/private/DB/MDB2SchemaReader.php',
'OC\\DB\\MigrationException' => __DIR__ . '/../../..' . '/lib/private/DB/MigrationException.php',
Expand Down
10 changes: 9 additions & 1 deletion lib/private/DB/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

namespace OC\DB;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;

/**
Expand All @@ -49,7 +50,9 @@ public function __construct($conn) {

/**
* @param string $table name
*
* @return int id of last insert statement
* @throws Exception
*/
public function lastInsertId($table) {
return (int) $this->conn->realLastInsertId($table);
Expand All @@ -67,6 +70,7 @@ public function fixupStatement($statement) {
* Create an exclusive read+write lock on a table
*
* @param string $tableName
* @throws Exception
* @since 9.1.0
*/
public function lockTable($tableName) {
Expand All @@ -77,6 +81,7 @@ public function lockTable($tableName) {
/**
* Release a previous acquired lock again
*
* @throws Exception
* @since 9.1.0
*/
public function unlockTable() {
Expand All @@ -94,7 +99,7 @@ public function unlockTable() {
* If this is null or an empty array, all keys of $input will be compared
* Please note: text fields (clob) must not be used in the compare array
* @return int number of inserted rows
* @throws \Doctrine\DBAL\Exception
* @throws Exception
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
*/
public function insertIfNotExist($table, $input, array $compare = null) {
Expand Down Expand Up @@ -130,6 +135,9 @@ public function insertIfNotExist($table, $input, array $compare = null) {
}
}

/**
* @throws \OCP\DB\Exception
*/
public function insertIgnoreConflict(string $table,array $values) : int {
try {
$builder = $this->conn->getQueryBuilder();
Expand Down
25 changes: 24 additions & 1 deletion lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class Connection extends ReconnectWrapper {
/** @var int */
protected $queriesExecuted = 0;

/**
* @throws Exception
*/
public function connect() {
try {
return parent::connect();
Expand Down Expand Up @@ -183,7 +186,9 @@ public function __construct(array $params, Driver $driver, Configuration $config
* @param string $statement The SQL statement to prepare.
* @param int $limit
* @param int $offset
*
* @return Statement The prepared statement.
* @throws Exception
*/
public function prepare($statement, $limit = null, $offset = null): Statement {
if ($limit === -1) {
Expand Down Expand Up @@ -221,6 +226,9 @@ public function executeQuery(string $sql, array $params = [], $types = [], Query
return parent::executeQuery($sql, $params, $types, $qcp);
}

/**
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
Expand Down Expand Up @@ -258,7 +266,9 @@ public function executeStatement($sql, array $params = [], array $types = []): i
* columns or sequences.
*
* @param string $seqName Name of the sequence object from which the ID should be returned.
*
* @return string the last inserted ID.
* @throws Exception
*/
public function lastInsertId($seqName = null) {
if ($seqName) {
Expand All @@ -267,7 +277,10 @@ public function lastInsertId($seqName = null) {
return $this->adapter->lastInsertId($seqName);
}

// internal use
/**
* @internal
* @throws Exception
*/
public function realLastInsertId($seqName = null) {
return parent::lastInsertId($seqName);
}
Expand Down Expand Up @@ -364,7 +377,9 @@ public function setValues($table, array $keys, array $values, array $updatePreco
* Create an exclusive read+write lock on a table
*
* @param string $tableName
*
* @throws \BadMethodCallException When trying to acquire a second lock
* @throws Exception
* @since 9.1.0
*/
public function lockTable($tableName) {
Expand All @@ -380,6 +395,7 @@ public function lockTable($tableName) {
/**
* Release a previous acquired lock again
*
* @throws Exception
* @since 9.1.0
*/
public function unlockTable() {
Expand Down Expand Up @@ -415,6 +431,8 @@ public function errorInfo() {
* Drop a table from the database if it exists
*
* @param string $table table name without the prefix
*
* @throws Exception
*/
public function dropTable($table) {
$table = $this->tablePrefix . trim($table);
Expand All @@ -428,7 +446,9 @@ public function dropTable($table) {
* Check if a table exists
*
* @param string $table table name without the prefix
*
* @return bool
* @throws Exception
*/
public function tableExists($table) {
$table = $this->tablePrefix . trim($table);
Expand Down Expand Up @@ -483,6 +503,7 @@ public function supports4ByteText() {
* Create the schema of the connected database
*
* @return Schema
* @throws Exception
*/
public function createSchema() {
$schemaManager = new MDB2SchemaManager($this);
Expand All @@ -494,6 +515,8 @@ public function createSchema() {
* Migrate the database to the given schema
*
* @param Schema $toSchema
*
* @throws Exception
*/
public function migrateToSchema(Schema $toSchema) {
$schemaManager = new MDB2SchemaManager($this);
Expand Down
118 changes: 96 additions & 22 deletions lib/private/DB/ConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

namespace OC\DB;

use Doctrine\DBAL\Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something complains on the imports

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was one unused below

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Schema;
use OC\DB\Exceptions\DbalException;
use OCP\DB\IPreparedStatement;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -49,63 +51,115 @@ public function getQueryBuilder(): IQueryBuilder {
}

public function prepare($sql, $limit = null, $offset = null): IPreparedStatement {
return new PreparedStatement(
$this->inner->prepare($sql, $limit, $offset)
);
try {
return new PreparedStatement(
$this->inner->prepare($sql, $limit, $offset)
);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function executeQuery(string $sql, array $params = [], $types = []): IResult {
return new ResultAdapter(
$this->inner->executeQuery($sql, $params, $types)
);
try {
return new ResultAdapter(
$this->inner->executeQuery($sql, $params, $types)
);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function executeUpdate(string $sql, array $params = [], array $types = []): int {
return $this->inner->executeUpdate($sql, $params, $types);
try {
return $this->inner->executeUpdate($sql, $params, $types);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function executeStatement($sql, array $params = [], array $types = []): int {
return $this->inner->executeStatement($sql, $params, $types);
try {
return $this->inner->executeStatement($sql, $params, $types);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function lastInsertId(string $table): int {
return (int) $this->inner->lastInsertId($table);
try {
return (int)$this->inner->lastInsertId($table);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function insertIfNotExist(string $table, array $input, array $compare = null) {
return $this->inner->insertIfNotExist($table, $input, $compare);
try {
return $this->inner->insertIfNotExist($table, $input, $compare);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function insertIgnoreConflict(string $table, array $values): int {
return $this->inner->insertIgnoreConflict($table, $values);
try {
return $this->inner->insertIgnoreConflict($table, $values);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int {
return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues);
try {
return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function lockTable($tableName): void {
$this->inner->lockTable($tableName);
try {
$this->inner->lockTable($tableName);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function unlockTable(): void {
$this->inner->unlockTable();
try {
$this->inner->unlockTable();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function beginTransaction(): void {
$this->inner->beginTransaction();
try {
$this->inner->beginTransaction();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function inTransaction(): bool {
return $this->inner->inTransaction();
}

public function commit(): void {
$this->inner->commit();
try {
$this->inner->commit();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function rollBack(): void {
$this->inner->rollBack();
try {
$this->inner->rollBack();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function getError(): string {
Expand All @@ -121,7 +175,11 @@ public function errorInfo() {
}

public function connect(): bool {
return $this->inner->connect();
try {
return $this->inner->connect();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function close(): void {
Expand All @@ -140,11 +198,19 @@ public function getDatabasePlatform(): AbstractPlatform {
}

public function dropTable(string $table): void {
$this->inner->dropTable($table);
try {
$this->inner->dropTable($table);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function tableExists(string $table): bool {
return $this->inner->tableExists($table);
try {
return $this->inner->tableExists($table);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function escapeLikeParameter(string $param): string {
Expand All @@ -159,11 +225,19 @@ public function supports4ByteText(): bool {
* @todo leaks a 3rdparty type
*/
public function createSchema(): Schema {
return $this->inner->createSchema();
try {
return $this->inner->createSchema();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function migrateToSchema(Schema $toSchema): void {
$this->inner->migrateToSchema($toSchema);
try {
$this->inner->migrateToSchema($toSchema);
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function getInner(): Connection {
Expand Down
Loading