diff options
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/DB/IPreparedStatement.php | 127 | ||||
-rw-r--r-- | lib/public/DB/IResult.php | 83 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IQueryBuilder.php | 7 | ||||
-rw-r--r-- | lib/public/IDBConnection.php | 71 |
4 files changed, 258 insertions, 30 deletions
diff --git a/lib/public/DB/IPreparedStatement.php b/lib/public/DB/IPreparedStatement.php new file mode 100644 index 00000000000..6b6617ba5ea --- /dev/null +++ b/lib/public/DB/IPreparedStatement.php @@ -0,0 +1,127 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCP\DB; + +use Doctrine\DBAL\Exception; +use Doctrine\DBAL\ParameterType; +use PDO; + +/** + * @since 21.0.0 + */ +interface IPreparedStatement { + + /** + * @return true + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::closeCursor on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function closeCursor(): bool; + + /** + * @param int $fetchMode + * + * @return mixed + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::fetch on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function fetch(int $fetchMode = PDO::FETCH_ASSOC); + + /** + * @param int $fetchMode + * + * @return mixed[] + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::fetchAll on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC); + + /** + * @return mixed + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::fetchColumn on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function fetchColumn(); + + /** + * @return mixed + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::fetchOne on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function fetchOne(); + + /** + * @param int|string $param + * @param mixed $value + * @param int $type + * + * @return bool + * + * @throws Exception + * + * @since 21.0.0 + */ + public function bindValue($param, $value, $type = ParameterType::STRING): bool; + + /** + * @param int|string $param + * @param mixed $variable + * @param int $type + * @param int|null $length + * + * @return bool + * + * @throws Exception + * + * @since 21.0.0 + */ + public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool; + + /** + * @param mixed[]|null $params + * + * @return IResult + * + * @since 21.0.0 + * @throws Exception + */ + public function execute($params = null): IResult; + + /** + * @return int + * + * @since 21.0.0 + * + * @throws Exception + * @deprecated 21.0.0 use \OCP\DB\IResult::rowCount on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare + */ + public function rowCount(): int; +} diff --git a/lib/public/DB/IResult.php b/lib/public/DB/IResult.php new file mode 100644 index 00000000000..10c788ebbf6 --- /dev/null +++ b/lib/public/DB/IResult.php @@ -0,0 +1,83 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCP\DB; + +use PDO; + +/** + * @since 21.0.0 + */ +interface IResult { + + /** + * @return true + * + * @since 21.0.0 + */ + public function closeCursor(): bool; + + /** + * @param int $fetchMode + * + * @return mixed + * + * @since 21.0.0 + */ + public function fetch(int $fetchMode = PDO::FETCH_ASSOC); + + /** + * @param int $fetchMode (one of PDO::FETCH_ASSOC, PDO::FETCH_NUM or PDO::FETCH_COLUMN (2, 3 or 7) + * + * @return mixed[] + * + * @since 21.0.0 + */ + public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array; + + /** + * @return mixed + * + * @since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\IResult::fetchOne + */ + public function fetchColumn(); + + /** + * @param int $columnIndex + * + * @return false|mixed + * + * @since 21.0.0 + */ + public function fetchOne(); + + /** + * @return int + * + * @since 21.0.0 + */ + public function rowCount(): int; +} diff --git a/lib/public/DB/QueryBuilder/IQueryBuilder.php b/lib/public/DB/QueryBuilder/IQueryBuilder.php index b4255f95834..8fcbd6c3276 100644 --- a/lib/public/DB/QueryBuilder/IQueryBuilder.php +++ b/lib/public/DB/QueryBuilder/IQueryBuilder.php @@ -29,6 +29,7 @@ namespace OCP\DB\QueryBuilder; use Doctrine\DBAL\Connection; +use OCP\DB\IResult; /** * This class provides a wrapper around Doctrine's QueryBuilder @@ -148,7 +149,11 @@ interface IQueryBuilder { * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} * for insert, update and delete statements. * - * @return \Doctrine\DBAL\Driver\Statement|int + * Warning: until Nextcloud 20, this method could return a \Doctrine\DBAL\Driver\Statement but since + * that interface changed in a breaking way the adapter \OCP\DB\QueryBuilder\IStatement is returned + * to bridge old code to the new API + * + * @return IResult|int * @since 8.2.0 */ public function execute(); diff --git a/lib/public/IDBConnection.php b/lib/public/IDBConnection.php index 11fa301ab86..16a5f998fde 100644 --- a/lib/public/IDBConnection.php +++ b/lib/public/IDBConnection.php @@ -39,7 +39,10 @@ namespace OCP; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\Schema; +use OCP\DB\IPreparedStatement; +use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; /** @@ -68,10 +71,11 @@ interface IDBConnection { * @param string $sql the sql query with ? placeholder for params * @param int $limit the maximum number of rows * @param int $offset from which row we want to start - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. + * @return IPreparedStatement The prepared statement. * @since 6.0.0 + * @throws Exception since 21.0.0 */ - public function prepare($sql, $limit = null, $offset = null); + public function prepare($sql, $limit = null, $offset = null): IPreparedStatement; /** * Executes an, optionally parameterized, SQL query. @@ -82,10 +86,11 @@ interface IDBConnection { * @param string $sql The SQL query to execute. * @param string[] $params The parameters to bind to the query, if any. * @param array $types The types the previous parameters are in. - * @return \Doctrine\DBAL\Driver\Statement The executed statement. + * @return IResult The executed statement. * @since 8.0.0 + * @throws Exception since 21.0.0 */ - public function executeQuery($sql, array $params = [], $types = []); + public function executeQuery(string $sql, array $params = [], $types = []): IResult; /** * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters @@ -96,12 +101,12 @@ interface IDBConnection { * @param string $sql The SQL query. * @param array $params The query parameters. * @param array $types The parameter types. - * @return integer The number of affected rows. + * @return int The number of affected rows. * @since 8.0.0 * * @deprecated 21.0.0 use executeStatement */ - public function executeUpdate($sql, array $params = [], array $types = []); + public function executeUpdate(string $sql, array $params = [], array $types = []): int; /** * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters @@ -112,18 +117,20 @@ interface IDBConnection { * @param string $sql The SQL query. * @param array $params The query parameters. * @param array $types The parameter types. - * @return integer The number of affected rows. + * @return int The number of affected rows. * @since 21.0.0 */ - public function executeStatement($sql, array $params = [], array $types = []); + public function executeStatement($sql, array $params = [], array $types = []): int; /** * Used to get the id of the just inserted element * @param string $table the name of the table where we inserted the item * @return int the id of the inserted element * @since 6.0.0 + * @throws Exception since 21.0.0 + * @deprecated 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId */ - public function lastInsertId($table = null); + public function lastInsertId(string $table): int; /** * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance @@ -136,11 +143,11 @@ interface IDBConnection { * 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\DBALException + * @throws Exception * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0 * @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); + public function insertIfNotExist(string $table, array $input, array $compare = null); /** @@ -164,11 +171,11 @@ interface IDBConnection { * @param array $values (column name => value) * @param array $updatePreconditionValues ensure values match preconditions (column name => value) * @return int number of new rows - * @throws \Doctrine\DBAL\DBALException + * @throws Exception * @throws PreconditionNotMetException * @since 9.0.0 */ - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []); + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int; /** * Create an exclusive read+write lock on a table @@ -180,20 +187,21 @@ interface IDBConnection { * @param string $tableName * @since 9.1.0 */ - public function lockTable($tableName); + public function lockTable($tableName): void; /** * Release a previous acquired lock again * * @since 9.1.0 */ - public function unlockTable(); + public function unlockTable(): void; /** * Start a transaction * @since 6.0.0 + * @throws Exception since 21.0.0 */ - public function beginTransaction(); + public function beginTransaction(): void; /** * Check if a transaction is active @@ -201,32 +209,36 @@ interface IDBConnection { * @return bool * @since 8.2.0 */ - public function inTransaction(); + public function inTransaction(): bool; /** * Commit the database changes done during a transaction that is in progress * @since 6.0.0 + * @throws Exception since 21.0.0 */ - public function commit(); + public function commit(): void; /** * Rollback the database changes done during a transaction that is in progress * @since 6.0.0 + * @throws Exception since 21.0.0 */ - public function rollBack(); + public function rollBack(): void; /** * Gets the error code and message as a string for logging * @return string * @since 6.0.0 + * @deprecated 21.0.0 doesn't return anything meaningful */ - public function getError(); + public function getError(): string; /** * Fetch the SQLSTATE associated with the last database operation. * * @return integer The last error code. * @since 8.0.0 + * @deprecated 21.0.0 doesn't return anything anymore */ public function errorCode(); @@ -235,6 +247,7 @@ interface IDBConnection { * * @return array The last error information. * @since 8.0.0 + * @deprecated 21.0.0 doesn't return anything anymore */ public function errorInfo(); @@ -244,20 +257,20 @@ interface IDBConnection { * @return bool * @since 8.0.0 */ - public function connect(); + public function connect(): bool; /** * Close the database connection * @since 8.0.0 */ - public function close(); + public function close(): void; /** * Quotes a given input parameter. * * @param mixed $input Parameter to be quoted. * @param int $type Type of the parameter. - * @return string The quoted parameter. + * @return mixed The quoted parameter. * @since 8.0.0 */ public function quote($input, $type = IQueryBuilder::PARAM_STR); @@ -277,7 +290,7 @@ interface IDBConnection { * @param string $table table name without the prefix * @since 8.0.0 */ - public function dropTable($table); + public function dropTable(string $table): void; /** * Check if a table exists @@ -286,7 +299,7 @@ interface IDBConnection { * @return bool * @since 8.0.0 */ - public function tableExists($table); + public function tableExists(string $table): bool; /** * Escape a parameter to be used in a LIKE query @@ -295,7 +308,7 @@ interface IDBConnection { * @return string * @since 9.0.0 */ - public function escapeLikeParameter($param); + public function escapeLikeParameter(string $param): string; /** * Check whether or not the current database support 4byte wide unicode @@ -303,7 +316,7 @@ interface IDBConnection { * @return bool * @since 11.0.0 */ - public function supports4ByteText(); + public function supports4ByteText(): bool; /** * Create the schema of the connected database @@ -311,7 +324,7 @@ interface IDBConnection { * @return Schema * @since 13.0.0 */ - public function createSchema(); + public function createSchema(): Schema; /** * Migrate the database to the given schema @@ -319,5 +332,5 @@ interface IDBConnection { * @param Schema $toSchema * @since 13.0.0 */ - public function migrateToSchema(Schema $toSchema); + public function migrateToSchema(Schema $toSchema): void; } |