IDBConnection.php 11 KB

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