2 This file is part of GNUnet
3 Copyright (C) 2009, 2010, 2012 GNUnet e.V.
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.
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.
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.
21 * @file postgres/postgres.c
22 * @brief library to help with access to a Postgres database
23 * @author Christian Grothoff
26 #include "gnunet_postgres_lib.h"
30 * Check if the result obtained from Postgres has
31 * the desired status code. If not, log an error, clear the
32 * result and return GNUNET_SYSERR.
34 * @param dbh database handle
35 * @param ret return value from database operation to check
36 * @param expected_status desired status
37 * @param command description of the command that was run
38 * @param args arguments given to the command
39 * @param filename name of the source file where the command was run
40 * @param line line number in the source file
41 * @return #GNUNET_OK if the result is acceptable
44 GNUNET_POSTGRES_check_result_ (PGconn *dbh,
54 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
56 "Postgres failed to allocate result for `%s:%s' at %s:%d\n",
63 if (PQresultStatus (ret) != expected_status)
65 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
67 _("`%s:%s' failed at %s:%d with error: %s\n"),
72 PQerrorMessage (dbh));
81 * Run simple SQL statement (without results).
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
90 GNUNET_POSTGRES_exec_ (PGconn * dbh,
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98 "Executing SQL statement `%s' at %s:%d\n",
105 GNUNET_POSTGRES_check_result_ (dbh,
112 return GNUNET_SYSERR;
119 * Prepare SQL statement.
121 * @param dbh database handle
122 * @param name name for the prepared SQL statement
123 * @param sql SQL code to prepare
124 * @param nparams number of parameters in sql
125 * @param filename filename for error reporting
126 * @param line code line for error reporting
127 * @return #GNUNET_OK on success
130 GNUNET_POSTGRES_prepare_ (PGconn *dbh,
134 const char *filename,
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Preparing SQL statement `%s' as `%s' at %s:%d\n",
145 ret = PQprepare (dbh,
150 GNUNET_POSTGRES_check_result_ (dbh,
157 return GNUNET_SYSERR;
164 * Connect to a postgres database
166 * @param cfg configuration
167 * @param section configuration section to use to get Postgres configuration options
168 * @return the postgres handle
171 GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle * cfg,
177 /* Open database and precompile statements */
179 GNUNET_CONFIGURATION_get_value_string (cfg,
184 dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
185 GNUNET_free_non_null (conninfo);
188 /* FIXME: warn about out-of-memory? */
191 if (PQstatus (dbh) != CONNECTION_OK)
193 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
195 _("Unable to connect to Postgres database '%s': %s\n"),
197 PQerrorMessage (dbh));
206 * Delete the row identified by the given rowid (qid
209 * @param dbh database handle
210 * @param stmt name of the prepared statement
211 * @param rowid which row to delete
212 * @return #GNUNET_OK on success
215 GNUNET_POSTGRES_delete_by_rowid (PGconn * dbh,
219 uint32_t brow = htonl (rowid);
220 const char *paramValues[] = { (const char *) &brow };
221 int paramLengths[] = { sizeof (brow) };
222 const int paramFormats[] = { 1 };
225 ret = PQexecPrepared (dbh,
233 GNUNET_POSTGRES_check_result_ (dbh, ret,
240 return GNUNET_SYSERR;
247 /* end of postgres.c */