diff options
Diffstat (limited to 'src/include/gnunet_postgres_lib.h')
-rw-r--r-- | src/include/gnunet_postgres_lib.h | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/include/gnunet_postgres_lib.h b/src/include/gnunet_postgres_lib.h new file mode 100644 index 000000000..12c0110b5 --- /dev/null +++ b/src/include/gnunet_postgres_lib.h | |||
@@ -0,0 +1,163 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2012 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file include/gnunet_postgres_lib.h | ||
22 | * @brief library to help with access to a Postgres database | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #ifndef GNUNET_POSTGRES_LIB_H | ||
26 | #define GNUNET_POSTGRES_LIB_H | ||
27 | |||
28 | #include "gnunet_util_lib.h" | ||
29 | #include <postgresql/libpq-fe.h> | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" | ||
33 | { | ||
34 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | |||
40 | /** | ||
41 | * Check if the result obtained from Postgres has | ||
42 | * the desired status code. If not, log an error, clear the | ||
43 | * result and return GNUNET_SYSERR. | ||
44 | * | ||
45 | * @param dbh database handle | ||
46 | * @param ret return value from database operation to check | ||
47 | * @param expected_status desired status | ||
48 | * @param command description of the command that was run | ||
49 | * @param args arguments given to the command | ||
50 | * @param filename name of the source file where the command was run | ||
51 | * @param line line number in the source file | ||
52 | * @return GNUNET_OK if the result is acceptable | ||
53 | */ | ||
54 | int | ||
55 | GNUNET_POSTGRES_check_result_ (PGconn *dbh, PGresult * ret, int expected_status, | ||
56 | const char *command, const char *args, | ||
57 | const char *filename, int line); | ||
58 | |||
59 | |||
60 | /** | ||
61 | * Check if the result obtained from Postgres has | ||
62 | * the desired status code. If not, log an error, clear the | ||
63 | * result and return GNUNET_SYSERR. | ||
64 | * | ||
65 | * @param dbh database handle | ||
66 | * @param ret return value from database operation to check | ||
67 | * @param expected_status desired status | ||
68 | * @param command description of the command that was run | ||
69 | * @param args arguments given to the command | ||
70 | * @return GNUNET_OK if the result is acceptable | ||
71 | */ | ||
72 | #define GNUNET_POSTGRES_check_result(dbh,ret,expected_status,command,args) GNUNET_POSTGRES_check_result_(dbh,ret,expected_status,command,args,__FILE__,__LINE__) | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Run simple SQL statement (without results). | ||
77 | * | ||
78 | * @param dbh database handle | ||
79 | * @param sql statement to run | ||
80 | * @param filename filename for error reporting | ||
81 | * @param line code line for error reporting | ||
82 | * @return GNUNET_OK on success | ||
83 | */ | ||
84 | int | ||
85 | GNUNET_POSTGRES_exec_ (PGconn *dbh, const char *sql, const char *filename, int line); | ||
86 | |||
87 | |||
88 | /** | ||
89 | * Run simple SQL statement (without results). | ||
90 | * | ||
91 | * @param dbh database handle | ||
92 | * @param sql statement to run | ||
93 | * @return GNUNET_OK on success | ||
94 | */ | ||
95 | #define GNUNET_POSTGRES_exec(dbh,sql) GNUNET_POSTGRES_exec_(dbh,sql,__FILE__,__LINE__) | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Prepare SQL statement. | ||
100 | * | ||
101 | * @param dbh database handle | ||
102 | * @param name name for the prepared SQL statement | ||
103 | * @param sql SQL code to prepare | ||
104 | * @param nparams number of parameters in sql | ||
105 | * @param filename filename for error reporting | ||
106 | * @param line code line for error reporting | ||
107 | * @return GNUNET_OK on success | ||
108 | */ | ||
109 | int | ||
110 | GNUNET_POSTGRES_prepare_ (PGconn *dbh, const char *name, const char *sql, | ||
111 | int nparms, | ||
112 | const char *filename, int line); | ||
113 | |||
114 | |||
115 | /** | ||
116 | * Prepare SQL statement. | ||
117 | * | ||
118 | * @param dbh database handle | ||
119 | * @param name name for the prepared SQL statement | ||
120 | * @param sql SQL code to prepare | ||
121 | * @param nparams number of parameters in sql | ||
122 | * @return GNUNET_OK on success | ||
123 | */ | ||
124 | #define GNUNET_POSTGRES_prepare(dbh,name,sql,nparams) GNUNET_POSTGRES_prepare_(dbh,name,sql,nparams,__FILE__,__LINE__) | ||
125 | |||
126 | |||
127 | /** | ||
128 | * Connect to a postgres database | ||
129 | * | ||
130 | * @param cfg configuration | ||
131 | * @param section configuration section to use to get Postgres configuration options | ||
132 | * @return the postgres handle | ||
133 | */ | ||
134 | PGconn * | ||
135 | GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
136 | const char *section); | ||
137 | |||
138 | |||
139 | /** | ||
140 | * Delete the row identified by the given rowid (qid | ||
141 | * in postgres). | ||
142 | * | ||
143 | * @param dbh database handle | ||
144 | * @param stmt name of the prepared statement | ||
145 | * @param rowid which row to delete | ||
146 | * @return GNUNET_OK on success | ||
147 | */ | ||
148 | int | ||
149 | GNUNET_POSTGRES_delete_by_rowid (PGconn *dbh, | ||
150 | const char *stmt, | ||
151 | uint32_t rowid); | ||
152 | |||
153 | |||
154 | |||
155 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
156 | { | ||
157 | #endif | ||
158 | #ifdef __cplusplus | ||
159 | } | ||
160 | #endif | ||
161 | |||
162 | /* end of gnunet_postgres_lib.h */ | ||
163 | #endif | ||