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