IDBConnection.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. *
  114. * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored.
  115. *
  116. * Implementation is not fully finished and should not be used!
  117. *
  118. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  119. * @param array $values data that should be inserted into the table (column name => value)
  120. * @return int number of inserted rows
  121. * @since 16.0.0
  122. */
  123. public function insertIgnoreConflict(string $table,array $values) : int;
  124. /**
  125. * Insert or update a row value
  126. *
  127. * @param string $table
  128. * @param array $keys (column name => value)
  129. * @param array $values (column name => value)
  130. * @param array $updatePreconditionValues ensure values match preconditions (column name => value)
  131. * @return int number of new rows
  132. * @throws \Doctrine\DBAL\DBALException
  133. * @throws PreconditionNotMetException
  134. * @since 9.0.0
  135. */
  136. public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []);
  137. /**
  138. * Create an exclusive read+write lock on a table
  139. *
  140. * Important Note: Due to the nature how locks work on different DBs, it is
  141. * only possible to lock one table at a time. You should also NOT start a
  142. * transaction while holding a lock.
  143. *
  144. * @param string $tableName
  145. * @since 9.1.0
  146. */
  147. public function lockTable($tableName);
  148. /**
  149. * Release a previous acquired lock again
  150. *
  151. * @since 9.1.0
  152. */
  153. public function unlockTable();
  154. /**
  155. * Start a transaction
  156. * @since 6.0.0
  157. */
  158. public function beginTransaction();
  159. /**
  160. * Check if a transaction is active
  161. *
  162. * @return bool
  163. * @since 8.2.0
  164. */
  165. public function inTransaction();
  166. /**
  167. * Commit the database changes done during a transaction that is in progress
  168. * @since 6.0.0
  169. */
  170. public function commit();
  171. /**
  172. * Rollback the database changes done during a transaction that is in progress
  173. * @since 6.0.0
  174. */
  175. public function rollBack();
  176. /**
  177. * Gets the error code and message as a string for logging
  178. * @return string
  179. * @since 6.0.0
  180. */
  181. public function getError();
  182. /**
  183. * Fetch the SQLSTATE associated with the last database operation.
  184. *
  185. * @return integer The last error code.
  186. * @since 8.0.0
  187. */
  188. public function errorCode();
  189. /**
  190. * Fetch extended error information associated with the last database operation.
  191. *
  192. * @return array The last error information.
  193. * @since 8.0.0
  194. */
  195. public function errorInfo();
  196. /**
  197. * Establishes the connection with the database.
  198. *
  199. * @return bool
  200. * @since 8.0.0
  201. */
  202. public function connect();
  203. /**
  204. * Close the database connection
  205. * @since 8.0.0
  206. */
  207. public function close();
  208. /**
  209. * Quotes a given input parameter.
  210. *
  211. * @param mixed $input Parameter to be quoted.
  212. * @param int $type Type of the parameter.
  213. * @return string The quoted parameter.
  214. * @since 8.0.0
  215. */
  216. public function quote($input, $type = IQueryBuilder::PARAM_STR);
  217. /**
  218. * Gets the DatabasePlatform instance that provides all the metadata about
  219. * the platform this driver connects to.
  220. *
  221. * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  222. * @since 8.0.0
  223. */
  224. public function getDatabasePlatform();
  225. /**
  226. * Drop a table from the database if it exists
  227. *
  228. * @param string $table table name without the prefix
  229. * @since 8.0.0
  230. */
  231. public function dropTable($table);
  232. /**
  233. * Check if a table exists
  234. *
  235. * @param string $table table name without the prefix
  236. * @return bool
  237. * @since 8.0.0
  238. */
  239. public function tableExists($table);
  240. /**
  241. * Escape a parameter to be used in a LIKE query
  242. *
  243. * @param string $param
  244. * @return string
  245. * @since 9.0.0
  246. */
  247. public function escapeLikeParameter($param);
  248. /**
  249. * Check whether or not the current database support 4byte wide unicode
  250. *
  251. * @return bool
  252. * @since 11.0.0
  253. */
  254. public function supports4ByteText();
  255. /**
  256. * Create the schema of the connected database
  257. *
  258. * @return Schema
  259. * @since 13.0.0
  260. */
  261. public function createSchema();
  262. /**
  263. * Migrate the database to the given schema
  264. *
  265. * @param Schema $toSchema
  266. * @since 13.0.0
  267. */
  268. public function migrateToSchema(Schema $toSchema);
  269. }