gnunet_postgres_lib.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * Helper library to access a PostgreSQL database
  22. *
  23. * @defgroup postgres PostgreSQL library
  24. * Helper library to access a PostgreSQL database.
  25. * @{
  26. */
  27. #ifndef GNUNET_POSTGRES_LIB_H
  28. #define GNUNET_POSTGRES_LIB_H
  29. #include "gnunet_util_lib.h"
  30. #include <libpq-fe.h>
  31. #ifdef __cplusplus
  32. extern "C"
  33. {
  34. #if 0 /* keep Emacsens' auto-indent happy */
  35. }
  36. #endif
  37. #endif
  38. /**
  39. * Check if the result obtained from Postgres has
  40. * the desired status code. If not, log an error, clear the
  41. * result and return GNUNET_SYSERR.
  42. *
  43. * @param dbh database handle
  44. * @param ret return value from database operation to check
  45. * @param expected_status desired status
  46. * @param command description of the command that was run
  47. * @param args arguments given to the command
  48. * @param filename name of the source file where the command was run
  49. * @param line line number in the source file
  50. * @return GNUNET_OK if the result is acceptable
  51. */
  52. int
  53. GNUNET_POSTGRES_check_result_ (PGconn *dbh, PGresult * ret, int expected_status,
  54. const char *command, const char *args,
  55. const char *filename, int line);
  56. /**
  57. * Check if the result obtained from Postgres has
  58. * the desired status code. If not, log an error, clear the
  59. * result and return GNUNET_SYSERR.
  60. *
  61. * @param dbh database handle
  62. * @param ret return value from database operation to check
  63. * @param expected_status desired status
  64. * @param command description of the command that was run
  65. * @param args arguments given to the command
  66. * @return GNUNET_OK if the result is acceptable
  67. */
  68. #define GNUNET_POSTGRES_check_result(dbh,ret,expected_status,command,args) GNUNET_POSTGRES_check_result_(dbh,ret,expected_status,command,args,__FILE__,__LINE__)
  69. /**
  70. * Run simple SQL statement (without results).
  71. *
  72. * @param dbh database handle
  73. * @param sql statement to run
  74. * @param filename filename for error reporting
  75. * @param line code line for error reporting
  76. * @return GNUNET_OK on success
  77. */
  78. int
  79. GNUNET_POSTGRES_exec_ (PGconn *dbh, const char *sql, const char *filename, int line);
  80. /**
  81. * Run simple SQL statement (without results).
  82. *
  83. * @param dbh database handle
  84. * @param sql statement to run
  85. * @return GNUNET_OK on success
  86. */
  87. #define GNUNET_POSTGRES_exec(dbh,sql) GNUNET_POSTGRES_exec_(dbh,sql,__FILE__,__LINE__)
  88. /**
  89. * Prepare SQL statement.
  90. *
  91. * @param dbh database handle
  92. * @param name name for the prepared SQL statement
  93. * @param sql SQL code to prepare
  94. * @param nparams number of parameters in sql
  95. * @param filename filename for error reporting
  96. * @param line code line for error reporting
  97. * @return GNUNET_OK on success
  98. */
  99. int
  100. GNUNET_POSTGRES_prepare_ (PGconn *dbh, const char *name, const char *sql,
  101. int nparams,
  102. const char *filename, int line);
  103. /**
  104. * Prepare SQL statement.
  105. *
  106. * @param dbh database handle
  107. * @param name name for the prepared SQL statement
  108. * @param sql SQL code to prepare
  109. * @param nparams number of parameters in sql
  110. * @return GNUNET_OK on success
  111. */
  112. #define GNUNET_POSTGRES_prepare(dbh,name,sql,nparams) GNUNET_POSTGRES_prepare_(dbh,name,sql,nparams,__FILE__,__LINE__)
  113. /**
  114. * Connect to a postgres database
  115. *
  116. * @param cfg configuration
  117. * @param section configuration section to use to get Postgres configuration options
  118. * @return the postgres handle
  119. */
  120. PGconn *
  121. GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  122. const char *section);
  123. /**
  124. * Delete the row identified by the given rowid (qid
  125. * in postgres).
  126. *
  127. * @param dbh database handle
  128. * @param stmt name of the prepared statement
  129. * @param rowid which row to delete
  130. * @return GNUNET_OK on success
  131. */
  132. int
  133. GNUNET_POSTGRES_delete_by_rowid (PGconn *dbh,
  134. const char *stmt,
  135. uint32_t rowid);
  136. #if 0 /* keep Emacsens' auto-indent happy */
  137. {
  138. #endif
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif
  143. /** @} */ /* end of group */