IDBConnection.php 11 KB

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