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.

305 lines
8.8 KiB

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