IDBConnection.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. // use OCP namespace for all classes that are considered public.
  8. // This means that they should be used by apps instead of the internal Nextcloud classes
  9. namespace OCP;
  10. use Doctrine\DBAL\Schema\Schema;
  11. use OC\DB\QueryBuilder\Sharded\CrossShardMoveHelper;
  12. use OC\DB\QueryBuilder\Sharded\ShardDefinition;
  13. use OCP\DB\Exception;
  14. use OCP\DB\IPreparedStatement;
  15. use OCP\DB\IResult;
  16. use OCP\DB\QueryBuilder\IQueryBuilder;
  17. /**
  18. * Interface IDBConnection
  19. *
  20. * @since 6.0.0
  21. */
  22. interface IDBConnection {
  23. /**
  24. * @since 28.0.0
  25. */
  26. public const PLATFORM_MYSQL = 'mysql';
  27. /**
  28. * @since 28.0.0
  29. */
  30. public const PLATFORM_ORACLE = 'oracle';
  31. /**
  32. * @since 28.0.0
  33. */
  34. public const PLATFORM_POSTGRES = 'postgres';
  35. /**
  36. * @since 28.0.0
  37. */
  38. public const PLATFORM_SQLITE = 'sqlite';
  39. /**
  40. * Gets the QueryBuilder for the connection.
  41. *
  42. * @return \OCP\DB\QueryBuilder\IQueryBuilder
  43. * @since 8.2.0
  44. */
  45. public function getQueryBuilder();
  46. /**
  47. * Used to abstract the Nextcloud database access away
  48. * @param string $sql the sql query with ? placeholder for params
  49. * @param int|null $limit the maximum number of rows
  50. * @param int|null $offset from which row we want to start
  51. * @return IPreparedStatement The prepared statement.
  52. * @since 6.0.0
  53. * @throws Exception since 21.0.0
  54. *
  55. * @psalm-taint-sink sql $sql
  56. */
  57. public function prepare($sql, $limit = null, $offset = null): IPreparedStatement;
  58. /**
  59. * Executes an, optionally parameterized, SQL query.
  60. *
  61. * If the query is parameterized, a prepared statement is used.
  62. * If an SQLLogger is configured, the execution is logged.
  63. *
  64. * @param string $sql The SQL query to execute.
  65. * @param string[] $params The parameters to bind to the query, if any.
  66. * @param array $types The types the previous parameters are in.
  67. * @return IResult The executed statement.
  68. * @since 8.0.0
  69. * @throws Exception since 21.0.0
  70. *
  71. * @psalm-taint-sink sql $sql
  72. */
  73. public function executeQuery(string $sql, array $params = [], $types = []): IResult;
  74. /**
  75. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  76. * and returns the number of affected rows.
  77. *
  78. * This method supports PDO binding types as well as DBAL mapping types.
  79. *
  80. * @param string $sql The SQL query.
  81. * @param array $params The query parameters.
  82. * @param array $types The parameter types.
  83. * @return int The number of affected rows.
  84. * @since 8.0.0
  85. * @throws Exception since 21.0.0
  86. *
  87. * @deprecated 21.0.0 use executeStatement
  88. *
  89. * @psalm-taint-sink sql $sql
  90. */
  91. public function executeUpdate(string $sql, array $params = [], array $types = []): int;
  92. /**
  93. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  94. * and returns the number of affected rows.
  95. *
  96. * This method supports PDO binding types as well as DBAL mapping types.
  97. *
  98. * @param string $sql The SQL query.
  99. * @param array $params The query parameters.
  100. * @param array $types The parameter types.
  101. * @return int The number of affected rows.
  102. * @since 21.0.0
  103. * @throws Exception since 21.0.0
  104. *
  105. * @psalm-taint-sink sql $sql
  106. */
  107. public function executeStatement($sql, array $params = [], array $types = []): int;
  108. /**
  109. * Used to get the id of the just inserted element
  110. * @param string $table the name of the table where we inserted the item
  111. * @return int the id of the inserted element
  112. * @since 6.0.0
  113. * @throws Exception since 21.0.0
  114. * @deprecated 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId
  115. */
  116. public function lastInsertId(string $table): int;
  117. /**
  118. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  119. * it is needed that there is also a unique constraint on the values. Then this method will
  120. * catch the exception and return 0.
  121. *
  122. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  123. * @param array $input data that should be inserted into the table (column name => value)
  124. * @param array|null $compare List of values that should be checked for "if not exists"
  125. * If this is null or an empty array, all keys of $input will be compared
  126. * Please note: text fields (clob) must not be used in the compare array
  127. * @return int number of inserted rows
  128. * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception
  129. * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0
  130. * @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
  131. */
  132. public function insertIfNotExist(string $table, array $input, ?array $compare = null);
  133. /**
  134. *
  135. * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored.
  136. *
  137. * Implementation is not fully finished and should not be used!
  138. *
  139. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  140. * @param array $values data that should be inserted into the table (column name => value)
  141. * @return int number of inserted rows
  142. * @since 16.0.0
  143. */
  144. public function insertIgnoreConflict(string $table, array $values) : int;
  145. /**
  146. * Insert or update a row value
  147. *
  148. * @param string $table
  149. * @param array $keys (column name => value)
  150. * @param array $values (column name => value)
  151. * @param array $updatePreconditionValues ensure values match preconditions (column name => value)
  152. * @return int number of new rows
  153. * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception
  154. * @throws PreConditionNotMetException
  155. * @since 9.0.0
  156. */
  157. public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int;
  158. /**
  159. * Create an exclusive read+write lock on a table
  160. *
  161. * Important Note: Due to the nature how locks work on different DBs, it is
  162. * only possible to lock one table at a time. You should also NOT start a
  163. * transaction while holding a lock.
  164. *
  165. * @param string $tableName
  166. * @throws Exception since 21.0.0
  167. * @since 9.1.0
  168. */
  169. public function lockTable($tableName): void;
  170. /**
  171. * Release a previous acquired lock again
  172. *
  173. * @throws Exception since 21.0.0
  174. * @since 9.1.0
  175. */
  176. public function unlockTable(): void;
  177. /**
  178. * Start a transaction
  179. * @since 6.0.0
  180. * @throws Exception since 21.0.0
  181. */
  182. public function beginTransaction(): void;
  183. /**
  184. * Check if a transaction is active
  185. *
  186. * @return bool
  187. * @since 8.2.0
  188. */
  189. public function inTransaction(): bool;
  190. /**
  191. * Commit the database changes done during a transaction that is in progress
  192. * @since 6.0.0
  193. * @throws Exception since 21.0.0
  194. */
  195. public function commit(): void;
  196. /**
  197. * Rollback the database changes done during a transaction that is in progress
  198. * @since 6.0.0
  199. * @throws Exception since 21.0.0
  200. */
  201. public function rollBack(): void;
  202. /**
  203. * Gets the error code and message as a string for logging
  204. * @return string
  205. * @since 6.0.0
  206. * @deprecated 21.0.0 doesn't return anything meaningful
  207. */
  208. public function getError(): string;
  209. /**
  210. * Fetch the SQLSTATE associated with the last database operation.
  211. *
  212. * @return integer The last error code.
  213. * @since 8.0.0
  214. * @deprecated 21.0.0 doesn't return anything anymore
  215. */
  216. public function errorCode();
  217. /**
  218. * Fetch extended error information associated with the last database operation.
  219. *
  220. * @return array The last error information.
  221. * @since 8.0.0
  222. * @deprecated 21.0.0 doesn't return anything anymore
  223. */
  224. public function errorInfo();
  225. /**
  226. * Establishes the connection with the database.
  227. *
  228. * @return bool
  229. * @throws Exception since 21.0.0
  230. * @since 8.0.0
  231. */
  232. public function connect(): bool;
  233. /**
  234. * Close the database connection
  235. * @since 8.0.0
  236. */
  237. public function close(): void;
  238. /**
  239. * Quotes a given input parameter.
  240. *
  241. * @param mixed $input Parameter to be quoted.
  242. * @param int $type Type of the parameter.
  243. * @return mixed The quoted parameter.
  244. * @since 8.0.0
  245. */
  246. public function quote($input, $type = IQueryBuilder::PARAM_STR);
  247. /**
  248. * Gets the DatabasePlatform instance that provides all the metadata about
  249. * the platform this driver connects to.
  250. *
  251. * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  252. * @since 8.0.0
  253. * @deprecated 30.0.0 Please use {@see self::getDatabaseProvider()} and compare to self::PLATFORM_* constants
  254. */
  255. public function getDatabasePlatform();
  256. /**
  257. * Drop a table from the database if it exists
  258. *
  259. * @param string $table table name without the prefix
  260. * @throws Exception since 21.0.0
  261. * @since 8.0.0
  262. *
  263. * @psalm-taint-sink sql $table
  264. */
  265. public function dropTable(string $table): void;
  266. /**
  267. * Check if a table exists
  268. *
  269. * @param string $table table name without the prefix
  270. * @return bool
  271. * @throws Exception since 21.0.0
  272. * @since 8.0.0
  273. */
  274. public function tableExists(string $table): bool;
  275. /**
  276. * Escape a parameter to be used in a LIKE query
  277. *
  278. * @param string $param
  279. * @return string
  280. * @since 9.0.0
  281. */
  282. public function escapeLikeParameter(string $param): string;
  283. /**
  284. * Check whether or not the current database support 4byte wide unicode
  285. *
  286. * @return bool
  287. * @since 11.0.0
  288. */
  289. public function supports4ByteText(): bool;
  290. /**
  291. * Create the schema of the connected database
  292. *
  293. * @return Schema
  294. * @throws Exception since 21.0.0
  295. * @since 13.0.0
  296. */
  297. public function createSchema(): Schema;
  298. /**
  299. * Migrate the database to the given schema
  300. *
  301. * @param Schema $toSchema
  302. * @throws Exception since 21.0.0
  303. * @since 13.0.0
  304. */
  305. public function migrateToSchema(Schema $toSchema): void;
  306. /**
  307. * Returns the database provider name
  308. * @link https://github.com/nextcloud/server/issues/30877
  309. * @since 28.0.0
  310. * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE
  311. */
  312. public function getDatabaseProvider(): string;
  313. /**
  314. * Get the shard definition by name, if configured
  315. *
  316. * @param string $name
  317. * @return ShardDefinition|null
  318. * @since 30.0.0
  319. */
  320. public function getShardDefinition(string $name): ?ShardDefinition;
  321. /**
  322. * Get a helper class for implementing cross-shard moves
  323. *
  324. * @return CrossShardMoveHelper
  325. * @since 30.0.0
  326. */
  327. public function getCrossShardMoveHelper(): CrossShardMoveHelper;
  328. }