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.

248 lines
6.8 KiB

  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework\Db;
  23. use OCP\IDb;
  24. use OCP\IDBConnection;
  25. /**
  26. * @deprecated use IDBConnection directly, will be removed in ownCloud 10
  27. * Small Facade for being able to inject the database connection for tests
  28. */
  29. class Db implements IDb {
  30. /**
  31. * @var IDBConnection
  32. */
  33. protected $connection;
  34. /**
  35. * @param IDBConnection $connection
  36. */
  37. public function __construct(IDBConnection $connection) {
  38. $this->connection = $connection;
  39. }
  40. /**
  41. * Used to abstract the ownCloud database access away
  42. *
  43. * @param string $sql the sql query with ? placeholder for params
  44. * @param int $limit the maximum number of rows
  45. * @param int $offset from which row we want to start
  46. * @deprecated use prepare instead, will be removed in ownCloud 10
  47. * @return \OC_DB_StatementWrapper prepared SQL query
  48. */
  49. public function prepareQuery($sql, $limit = null, $offset = null) {
  50. $isManipulation = \OC_DB::isManipulation($sql);
  51. $statement = $this->connection->prepare($sql, $limit, $offset);
  52. return new \OC_DB_StatementWrapper($statement, $isManipulation);
  53. }
  54. /**
  55. * Used to get the id of the just inserted element
  56. *
  57. * @deprecated use lastInsertId instead, will be removed in ownCloud 10
  58. * @param string $tableName the name of the table where we inserted the item
  59. * @return int the id of the inserted element
  60. */
  61. public function getInsertId($tableName) {
  62. return $this->connection->lastInsertId($tableName);
  63. }
  64. /**
  65. * Used to abstract the ownCloud database access away
  66. * @param string $sql the sql query with ? placeholder for params
  67. * @param int $limit the maximum number of rows
  68. * @param int $offset from which row we want to start
  69. * @return \Doctrine\DBAL\Driver\Statement The prepared statement.
  70. */
  71. public function prepare($sql, $limit=null, $offset=null) {
  72. return $this->connection->prepare($sql, $limit, $offset);
  73. }
  74. /**
  75. * Executes an, optionally parameterized, SQL query.
  76. *
  77. * If the query is parameterized, a prepared statement is used.
  78. * If an SQLLogger is configured, the execution is logged.
  79. *
  80. * @param string $query The SQL query to execute.
  81. * @param string[] $params The parameters to bind to the query, if any.
  82. * @param array $types The types the previous parameters are in.
  83. * @return \Doctrine\DBAL\Driver\Statement The executed statement.
  84. */
  85. public function executeQuery($query, array $params = array(), $types = array()) {
  86. return $this->connection->executeQuery($query, $params, $types);
  87. }
  88. /**
  89. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  90. * and returns the number of affected rows.
  91. *
  92. * This method supports PDO binding types as well as DBAL mapping types.
  93. *
  94. * @param string $query The SQL query.
  95. * @param array $params The query parameters.
  96. * @param array $types The parameter types.
  97. * @return integer The number of affected rows.
  98. */
  99. public function executeUpdate($query, array $params = array(), array $types = array()) {
  100. return $this->connection->executeUpdate($query, $params, $types);
  101. }
  102. /**
  103. * Used to get the id of the just inserted element
  104. * @param string $table the name of the table where we inserted the item
  105. * @return int the id of the inserted element
  106. */
  107. public function lastInsertId($table = null) {
  108. return $this->connection->lastInsertId($table);
  109. }
  110. /**
  111. * Insert a row if a matching row doesn't exists.
  112. * @param string $table The table name (will replace *PREFIX*) to perform the replace on.
  113. * @param array $input
  114. * @throws \OC\HintException
  115. *
  116. * The input array if in the form:
  117. *
  118. * array ( 'id' => array ( 'value' => 6,
  119. * 'key' => true
  120. * ),
  121. * 'name' => array ('value' => 'Stoyan'),
  122. * 'family' => array ('value' => 'Stefanov'),
  123. * 'birth_date' => array ('value' => '1975-06-20')
  124. * );
  125. * @return bool
  126. *
  127. */
  128. public function insertIfNotExist($table, $input, $compare = null) {
  129. return $this->connection->insertIfNotExist($table, $input, $compare);
  130. }
  131. /**
  132. * Start a transaction
  133. */
  134. public function beginTransaction() {
  135. $this->connection->beginTransaction();
  136. }
  137. /**
  138. * Commit the database changes done during a transaction that is in progress
  139. */
  140. public function commit() {
  141. $this->connection->commit();
  142. }
  143. /**
  144. * Rollback the database changes done during a transaction that is in progress
  145. */
  146. public function rollBack() {
  147. $this->connection->rollBack();
  148. }
  149. /**
  150. * Gets the error code and message as a string for logging
  151. * @return string
  152. */
  153. public function getError() {
  154. return $this->connection->getError();
  155. }
  156. /**
  157. * Fetch the SQLSTATE associated with the last database operation.
  158. *
  159. * @return integer The last error code.
  160. */
  161. public function errorCode() {
  162. return $this->connection->errorCode();
  163. }
  164. /**
  165. * Fetch extended error information associated with the last database operation.
  166. *
  167. * @return array The last error information.
  168. */
  169. public function errorInfo() {
  170. return $this->connection->errorInfo();
  171. }
  172. /**
  173. * Establishes the connection with the database.
  174. *
  175. * @return bool
  176. */
  177. public function connect() {
  178. return $this->connection->connect();
  179. }
  180. /**
  181. * Close the database connection
  182. */
  183. public function close() {
  184. $this->connection->close();
  185. }
  186. /**
  187. * Quotes a given input parameter.
  188. *
  189. * @param mixed $input Parameter to be quoted.
  190. * @param int $type Type of the parameter.
  191. * @return string The quoted parameter.
  192. */
  193. public function quote($input, $type = \PDO::PARAM_STR) {
  194. return $this->connection->quote($input, $type);
  195. }
  196. /**
  197. * Gets the DatabasePlatform instance that provides all the metadata about
  198. * the platform this driver connects to.
  199. *
  200. * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  201. */
  202. public function getDatabasePlatform() {
  203. return $this->connection->getDatabasePlatform();
  204. }
  205. /**
  206. * Drop a table from the database if it exists
  207. *
  208. * @param string $table table name without the prefix
  209. */
  210. public function dropTable($table) {
  211. $this->connection->dropTable($table);
  212. }
  213. /**
  214. * Check if a table exists
  215. *
  216. * @param string $table table name without the prefix
  217. * @return bool
  218. */
  219. public function tableExists($table) {
  220. return $this->connection->tableExists($table);
  221. }
  222. }