IDBConnection.php 10 KB

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