db.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  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. /**
  23. * Public interface of ownCloud for apps to use.
  24. * DB Class
  25. *
  26. */
  27. // use OCP namespace for all classes that are considered public.
  28. // This means that they should be used by apps instead of the internal ownCloud classes
  29. namespace OCP;
  30. /**
  31. * This class provides access to the internal database system. Use this class exlusively if you want to access databases
  32. */
  33. class DB {
  34. /**
  35. * Prepare a SQL query
  36. * @param string $query Query string
  37. * @param int $limit Limit of the SQL statement
  38. * @param int $offset Offset of the SQL statement
  39. * @return \OC_DB_StatementWrapper prepared SQL query
  40. *
  41. * SQL query via Doctrine prepare(), needs to be execute()'d!
  42. */
  43. static public function prepare( $query, $limit=null, $offset=null ) {
  44. return(\OC_DB::prepare($query, $limit, $offset));
  45. }
  46. /**
  47. * Insert a row if a matching row doesn't exists.
  48. * @param string $table The optional table name (will replace *PREFIX*) and add sequence suffix
  49. * @param array $input
  50. *
  51. * The input array if in the form:
  52. *
  53. * array ( 'id' => array ( 'value' => 6,
  54. * 'key' => true
  55. * ),
  56. * 'name' => array ('value' => 'Stoyan'),
  57. * 'family' => array ('value' => 'Stefanov'),
  58. * 'birth_date' => array ('value' => '1975-06-20')
  59. * );
  60. * @return bool
  61. *
  62. */
  63. public static function insertIfNotExist($table, $input) {
  64. return(\OC_DB::insertIfNotExist($table, $input));
  65. }
  66. /**
  67. * Gets last value of autoincrement
  68. * @param string $table The optional table name (will replace *PREFIX*) and add sequence suffix
  69. * @return string
  70. *
  71. * \Doctrine\DBAL\Connection lastInsertID()
  72. *
  73. * Call this method right after the insert command or other functions may
  74. * cause trouble!
  75. */
  76. public static function insertid($table=null) {
  77. return(\OC_DB::insertid($table));
  78. }
  79. /**
  80. * Start a transaction
  81. */
  82. public static function beginTransaction() {
  83. \OC_DB::beginTransaction();
  84. }
  85. /**
  86. * Commit the database changes done during a transaction that is in progress
  87. */
  88. public static function commit() {
  89. \OC_DB::commit();
  90. }
  91. /**
  92. * Rollback the database changes done during a transaction that is in progress
  93. */
  94. public static function rollback() {
  95. \OC_DB::rollback();
  96. }
  97. /**
  98. * Check if a result is an error, works with Doctrine
  99. * @param mixed $result
  100. * @return bool
  101. */
  102. public static function isError($result) {
  103. return(\OC_DB::isError($result));
  104. }
  105. /**
  106. * returns the error code and message as a string for logging
  107. * works with DoctrineException
  108. * @param mixed $error
  109. * @return string
  110. */
  111. public static function getErrorMessage($error) {
  112. return(\OC_DB::getErrorMessage($error));
  113. }
  114. }