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",
57 command, args, filename, line);
60 if (PQresultStatus (ret) != expected_status)
62 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
63 "postgres", _("`%s:%s' failed at %s:%d with error: %s"),
64 command, args, filename, line, PQerrorMessage (dbh));
73 * Run simple SQL statement (without results).
75 * @param dbh database handle
76 * @param sql statement to run
77 * @param filename filename for error reporting
78 * @param line code line for error reporting
79 * @return #GNUNET_OK on success
82 GNUNET_POSTGRES_exec_ (PGconn * dbh,
89 ret = PQexec (dbh, sql);
91 GNUNET_POSTGRES_check_result_ (dbh, ret, PGRES_COMMAND_OK, "PQexec", sql,
100 * Prepare SQL statement.
102 * @param dbh database handle
103 * @param name name for the prepared SQL statement
104 * @param sql SQL code to prepare
105 * @param nparams number of parameters in sql
106 * @param filename filename for error reporting
107 * @param line code line for error reporting
108 * @return #GNUNET_OK on success
111 GNUNET_POSTGRES_prepare_ (PGconn *dbh,
115 const char *filename,
120 ret = PQprepare (dbh,
125 GNUNET_POSTGRES_check_result_ (dbh,
132 return GNUNET_SYSERR;
139 * Connect to a postgres database
141 * @param cfg configuration
142 * @param section configuration section to use to get Postgres configuration options
143 * @return the postgres handle
146 GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle * cfg,
152 /* Open database and precompile statements */
154 GNUNET_CONFIGURATION_get_value_string (cfg, section, "CONFIG", &conninfo))
156 dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
157 GNUNET_free_non_null (conninfo);
160 /* FIXME: warn about out-of-memory? */
163 if (PQstatus (dbh) != CONNECTION_OK)
165 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "postgres",
166 _("Unable to initialize Postgres: %s"),
167 PQerrorMessage (dbh));
176 * Delete the row identified by the given rowid (qid
179 * @param dbh database handle
180 * @param stmt name of the prepared statement
181 * @param rowid which row to delete
182 * @return #GNUNET_OK on success
185 GNUNET_POSTGRES_delete_by_rowid (PGconn * dbh,
189 uint32_t brow = htonl (rowid);
190 const char *paramValues[] = { (const char *) &brow };
191 int paramLengths[] = { sizeof (brow) };
192 const int paramFormats[] = { 1 };
196 PQexecPrepared (dbh, stmt, 1,
202 GNUNET_POSTGRES_check_result_ (dbh, ret,
209 return GNUNET_SYSERR;
216 /* end of postgres.c */