IDBConnection.php 9.9 KB

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