convert fs publish to MQ
[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, 
61                                PGresult *ret,
62                                int expected_status,
63                                const char *command, 
64                                const char *args,
65                                const char *filename, 
66                                int line);
67
68
69 /**
70  * Check if the result obtained from Postgres has
71  * the desired status code.  If not, log an error, clear the
72  * result and return #GNUNET_SYSERR.
73  *
74  * @param dbh database handle
75  * @param ret return value from database operation to check
76  * @param expected_status desired status
77  * @param command description of the command that was run
78  * @param args arguments given to the command
79  * @return #GNUNET_OK if the result is acceptable
80  */
81 #define GNUNET_POSTGRES_check_result(dbh,ret,expected_status,command,args) GNUNET_POSTGRES_check_result_(dbh,ret,expected_status,command,args,__FILE__,__LINE__)
82
83
84 /**
85  * Run simple SQL statement (without results).
86  *
87  * @param dbh database handle
88  * @param sql statement to run
89  * @param filename filename for error reporting
90  * @param line code line for error reporting
91  * @return #GNUNET_OK on success
92  */
93 int
94 GNUNET_POSTGRES_exec_ (PGconn *dbh, 
95                        const char *sql, 
96                        const char *filename, 
97                        int line);
98
99
100 /**
101  * Run simple SQL statement (without results).
102  *
103  * @param dbh database handle
104  * @param sql statement to run
105  * @return #GNUNET_OK on success
106  */
107 #define GNUNET_POSTGRES_exec(dbh,sql) GNUNET_POSTGRES_exec_(dbh,sql,__FILE__,__LINE__)
108
109
110 /**
111  * Prepare SQL statement.
112  *
113  * @param dbh database handle
114  * @param name name for the prepared SQL statement
115  * @param sql SQL code to prepare
116  * @param nparams number of parameters in sql
117  * @param filename filename for error reporting
118  * @param line code line for error reporting
119  * @return #GNUNET_OK on success
120  */
121 int
122 GNUNET_POSTGRES_prepare_ (PGconn *dbh, 
123                           const char *name, 
124                           const char *sql,
125                           int nparams,
126                           const char *filename, 
127                           int line);
128
129
130 /**
131  * Prepare SQL statement.
132  *
133  * @param dbh database handle
134  * @param name name for the prepared SQL statement
135  * @param sql SQL code to prepare
136  * @param nparams number of parameters in sql
137  * @return #GNUNET_OK on success
138  */
139 #define GNUNET_POSTGRES_prepare(dbh,name,sql,nparams) GNUNET_POSTGRES_prepare_(dbh,name,sql,nparams,__FILE__,__LINE__)
140
141
142 /**
143  * Connect to a postgres database
144  *
145  * @param cfg configuration
146  * @param section configuration section to use to get Postgres configuration options
147  * @return the postgres handle
148  */
149 PGconn *
150 GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
151                          const char *section);
152
153
154 /**
155  * Delete the row identified by the given rowid (qid
156  * in postgres).
157  *
158  * @param dbh database handle
159  * @param stmt name of the prepared statement
160  * @param rowid which row to delete
161  * @return #GNUNET_OK on success
162  */
163 int
164 GNUNET_POSTGRES_delete_by_rowid (PGconn *dbh,
165                                  const char *stmt,
166                                  uint32_t rowid);
167
168
169 #if 0                           /* keep Emacsens' auto-indent happy */
170 {
171 #endif
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif
177
178 /** @} */  /* end of group */