IDBConnection.php 8.1 KB

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