db.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @return \MDB2_Statement_Common prepared SQL query
  38. *
  39. * SQL query via MDB2 prepare(), needs to be execute()'d!
  40. */
  41. static public function prepare( $query, $limit=null, $offset=null ) {
  42. return(\OC_DB::prepare($query, $limit, $offset));
  43. }
  44. /**
  45. * Insert a row if a matching row doesn't exists.
  46. * @param $table string The table name (will replace *PREFIX*) to perform the replace on.
  47. * @param $input array
  48. *
  49. * The input array if in the form:
  50. *
  51. * array ( 'id' => array ( 'value' => 6,
  52. * 'key' => true
  53. * ),
  54. * 'name' => array ('value' => 'Stoyan'),
  55. * 'family' => array ('value' => 'Stefanov'),
  56. * 'birth_date' => array ('value' => '1975-06-20')
  57. * );
  58. * @return bool
  59. *
  60. */
  61. public static function insertIfNotExist($table, $input) {
  62. return(\OC_DB::insertIfNotExist($table, $input));
  63. }
  64. /**
  65. * Gets last value of autoincrement
  66. * @param $table string The optional table name (will replace *PREFIX*) and add sequence suffix
  67. * @return int
  68. *
  69. * MDB2 lastInsertID()
  70. *
  71. * Call this method right after the insert command or other functions may
  72. * cause trouble!
  73. */
  74. public static function insertid($table=null) {
  75. return(\OC_DB::insertid($table));
  76. }
  77. /**
  78. * Start a transaction
  79. */
  80. public static function beginTransaction() {
  81. return(\OC_DB::beginTransaction());
  82. }
  83. /**
  84. * Commit the database changes done during a transaction that is in progress
  85. */
  86. public static function commit() {
  87. return(\OC_DB::commit());
  88. }
  89. /**
  90. * Check if a result is an error, works with MDB2 and PDOException
  91. * @param mixed $result
  92. * @return bool
  93. */
  94. public static function isError($result) {
  95. return(\OC_DB::isError($result));
  96. }
  97. /**
  98. * returns the error code and message as a string for logging
  99. * works with DoctrineException
  100. * @param mixed $error
  101. * @return string
  102. */
  103. public static function getErrorMessage($error) {
  104. return(\OC_DB::getErrorMessage($error));
  105. }
  106. }