IDBConnection.php 8.6 KB

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