You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IDBConnection.php 11KB

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