IDBConnection.php 11 KB

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