1
0

db.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Dan Bartram <daneybartram@gmail.com>
  5. * @author Felix Moeller <mail@felixmoeller.de>
  6. * @author Frank Karlitschek <frank@owncloud.org>
  7. * @author Joas Schilling <nickvergessen@owncloud.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <icewind@owncloud.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. * DB Class
  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. /**
  39. * This class provides access to the internal database system. Use this class exlusively if you want to access databases
  40. */
  41. class DB {
  42. /**
  43. * Prepare a SQL query
  44. * @param string $query Query string
  45. * @param int $limit Limit of the SQL statement
  46. * @param int $offset Offset of the SQL statement
  47. * @return \OC_DB_StatementWrapper prepared SQL query
  48. *
  49. * SQL query via Doctrine prepare(), needs to be execute()'d!
  50. */
  51. static public function prepare( $query, $limit=null, $offset=null ) {
  52. return(\OC_DB::prepare($query, $limit, $offset));
  53. }
  54. /**
  55. * Insert a row if the matching row does not exists.
  56. *
  57. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  58. * @param array $input data that should be inserted into the table (column name => value)
  59. * @param array|null $compare List of values that should be checked for "if not exists"
  60. * If this is null or an empty array, all keys of $input will be compared
  61. * @return int number of inserted rows
  62. * @throws \Doctrine\DBAL\DBALException
  63. *
  64. */
  65. public static function insertIfNotExist($table, $input, array $compare = null) {
  66. return \OC::$server->getDatabaseConnection()->insertIfNotExist($table, $input, $compare);
  67. }
  68. /**
  69. * Gets last value of autoincrement
  70. * @param string $table The optional table name (will replace *PREFIX*) and add sequence suffix
  71. * @return string
  72. *
  73. * \Doctrine\DBAL\Connection lastInsertID()
  74. *
  75. * Call this method right after the insert command or other functions may
  76. * cause trouble!
  77. */
  78. public static function insertid($table=null) {
  79. return(\OC_DB::insertid($table));
  80. }
  81. /**
  82. * Start a transaction
  83. */
  84. public static function beginTransaction() {
  85. \OC_DB::beginTransaction();
  86. }
  87. /**
  88. * Commit the database changes done during a transaction that is in progress
  89. */
  90. public static function commit() {
  91. \OC_DB::commit();
  92. }
  93. /**
  94. * Rollback the database changes done during a transaction that is in progress
  95. */
  96. public static function rollback() {
  97. \OC_DB::rollback();
  98. }
  99. /**
  100. * Check if a result is an error, works with Doctrine
  101. * @param mixed $result
  102. * @return bool
  103. */
  104. public static function isError($result) {
  105. return(\OC_DB::isError($result));
  106. }
  107. /**
  108. * returns the error code and message as a string for logging
  109. * works with DoctrineException
  110. * @param mixed $error
  111. * @return string
  112. */
  113. public static function getErrorMessage($error) {
  114. return(\OC_DB::getErrorMessage($error));
  115. }
  116. }