1
0

IDBConnection.php 11 KB

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