GNUnet  0.11.x
gnunet_pq_lib.h
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2016, 2017 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #ifndef GNUNET_PQ_LIB_H
26 #define GNUNET_PQ_LIB_H
27 
28 #include <libpq-fe.h>
29 #include "gnunet_util_lib.h"
30 #include "gnunet_db_lib.h"
31 
32 /* ************************* pq_query_helper.c functions ************************ */
33 
34 
49 typedef int
51  const void *data,
52  size_t data_len,
53  void *param_values[],
54  int param_lengths[],
55  int param_formats[],
56  unsigned int param_length,
57  void *scratch[],
58  unsigned int scratch_length);
59 
60 
65 {
70 
74  void *conv_cls;
75 
79  const void *data;
80 
84  size_t size;
85 
89  unsigned int num_params;
90 };
91 
92 
96 #define GNUNET_PQ_query_param_end \
97  { \
98  NULL, NULL, NULL, 0, 0 \
99  }
100 
101 
110 GNUNET_PQ_query_param_fixed_size (const void *ptr, size_t ptr_size);
111 
112 
119 GNUNET_PQ_query_param_string (const char *ptr);
120 
121 
128 #define GNUNET_PQ_query_param_auto_from_type(x) \
129  GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
130 
131 
140  const struct GNUNET_CRYPTO_RsaPublicKey *x);
141 
142 
151  const struct GNUNET_CRYPTO_RsaSignature *x);
152 
153 
162 
163 
172 
173 
182  const struct GNUNET_TIME_AbsoluteNBO *x);
183 
184 
191 GNUNET_PQ_query_param_uint16 (const uint16_t *x);
192 
193 
200 GNUNET_PQ_query_param_uint32 (const uint32_t *x);
201 
202 
209 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
210 
211 
212 /* ************************* pq_result_helper.c functions ************************ */
213 
214 
228 typedef int
230  PGresult *result,
231  int row,
232  const char *fname,
233  size_t *dst_size,
234  void *dst);
235 
236 
244 typedef void
246  void *rd);
247 
248 
253 {
258 
264 
268  void *cls;
269 
273  void *dst;
274 
280  size_t dst_size;
281 
285  const char *fname;
286 
290  size_t *result_size;
291 };
292 
293 
299 #define GNUNET_PQ_result_spec_end \
300  { \
301  NULL, NULL, NULL, NULL, 0, NULL, NULL \
302  }
303 
304 
315  void **dst,
316  size_t *sptr);
317 
318 
328 GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size);
329 
330 
338 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) \
339  GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
340 
341 
350 GNUNET_PQ_result_spec_string (const char *name, char **dst);
351 
352 
362  struct GNUNET_CRYPTO_RsaPublicKey **rsa);
363 
364 
374  struct GNUNET_CRYPTO_RsaSignature **sig);
375 
376 
386  struct GNUNET_TIME_Absolute *at);
387 
388 
398  struct GNUNET_TIME_Relative *rt);
399 
400 
410  struct GNUNET_TIME_AbsoluteNBO *at);
411 
412 
421 GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16);
422 
423 
432 GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32);
433 
434 
443 GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64);
444 
445 
446 /* ************************* pq.c functions ************************ */
447 
451 struct GNUNET_PQ_Context;
452 
453 
463 PGresult *
465  const char *name,
466  const struct GNUNET_PQ_QueryParam *params);
467 
468 
480 int
481 GNUNET_PQ_extract_result (PGresult *result,
482  struct GNUNET_PQ_ResultSpec *rs,
483  int row);
484 
485 
492 void
494 
495 
496 /* ******************** pq_eval.c functions ************** */
497 
498 
514  const char *statement_name,
515  PGresult *result);
516 
517 
536  const char *statement_name,
537  const struct GNUNET_PQ_QueryParam *params);
538 
539 
548 typedef void
550  PGresult *result,
551  unsigned int num_results);
552 
553 
571  const char *statement_name,
572  const struct GNUNET_PQ_QueryParam *params,
574  void *rh_cls);
575 
576 
594  const char *statement_name,
595  const struct
596  GNUNET_PQ_QueryParam *params,
597  struct GNUNET_PQ_ResultSpec *rs);
598 
599 
600 /* ******************** pq_prepare.c functions ************** */
601 
602 
608 {
612  const char *name;
613 
617  const char *sql;
618 
622  unsigned int num_arguments;
623 
624 };
625 
626 
630 #define GNUNET_PQ_PREPARED_STATEMENT_END \
631  { \
632  NULL, NULL, 0 \
633  }
634 
635 
645 GNUNET_PQ_make_prepare (const char *name,
646  const char *sql,
647  unsigned int num_args);
648 
649 
659 int
661  const struct GNUNET_PQ_PreparedStatement *ps);
662 
663 
664 /* ******************** pq_exec.c functions ************** */
665 
666 
672 {
676  const char *sql;
677 
682 };
683 
684 
688 #define GNUNET_PQ_EXECUTE_STATEMENT_END \
689  { \
690  NULL, GNUNET_SYSERR \
691  }
692 
693 
701 GNUNET_PQ_make_execute (const char *sql);
702 
703 
712 GNUNET_PQ_make_try_execute (const char *sql);
713 
714 
724 int
726  const struct GNUNET_PQ_ExecuteStatement *es);
727 
728 
729 /* ******************** pq_connect.c functions ************** */
730 
731 
754 struct GNUNET_PQ_Context *
755 GNUNET_PQ_connect (const char *config_str,
756  const char *load_path,
757  const struct GNUNET_PQ_ExecuteStatement *es,
758  const struct GNUNET_PQ_PreparedStatement *ps);
759 
760 
778 struct GNUNET_PQ_Context *
780  const char *section,
781  const char *load_path_suffix,
782  const struct GNUNET_PQ_ExecuteStatement *es,
783  const struct GNUNET_PQ_PreparedStatement *ps);
784 
785 
791 void
793 
794 
800 void
802 
803 
813 int
815  const char *load_path);
816 
817 
824 void
826 
827 
828 #endif /* GNUNET_PQ_LIB_H_ */
829 
830 /* end of include/gnunet_pq_lib.h */
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition: pq_connect.c:513
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:59
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
an RSA signature
Definition: crypto_rsa.c:63
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
int(* GNUNET_PQ_QueryConverter)(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
Definition: gnunet_pq_lib.h:50
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal.
Definition: pq_exec.c:36
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
GNUNET_PQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_pq_lib.h:69
char * load_path
Path to load SQL files from.
Definition: pq.h:59
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_public_key(const char *name, struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:324
const char * fname
Field name of the desired result.
Description of a DB result cell.
int GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:62
PGresult * GNUNET_PQ_exec_prepared(struct GNUNET_PQ_Context *db, const char *name, const struct GNUNET_PQ_QueryParam *params)
Execute a prepared statement.
Definition: pq.c:39
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
int ignore_errors
Should we ignore errors?
const char * name
Name of the statement.
const void * data
Data or NULL.
Definition: gnunet_pq_lib.h:79
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_relative_time(const struct GNUNET_TIME_Relative *x)
Generate query parameter for a relative time value.
static char * section
Name of the section.
Definition: gnunet-config.c:33
int GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_path)
Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the fi...
Definition: pq_connect.c:213
struct GNUNET_PQ_Context * GNUNET_PQ_connect(const char *config_str, const char *load_path, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
Create a connection to the Postgres database using config_str for the configuration.
Definition: pq_connect.c:89
void * cls
Closure for conv and cleaner.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition: pq_eval.c:258
shared defintions for transactional databases
int GNUNET_PQ_exec_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:76
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:54
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_fixed_size(const char *name, void *dst, size_t dst_size)
Fixed-size result expected.
void * conv_cls
Closure for conv.
Definition: gnunet_pq_lib.h:74
static int result
Global testing status.
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
const char * sql
Actual SQL statement.
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:466
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_relative_time(const char *name, struct GNUNET_TIME_Relative *rt)
Relative time expected.
GNUNET_PQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_signature(const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_pq_lib.h:89
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
Definition: pq_connect.c:310
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint16(const char *name, uint16_t *u16)
uint16_t expected.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:164
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
configuration data
Definition: configuration.c:84
const char * name
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time_nbo(const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
unsigned int num_arguments
Number of arguments included in sql.
void(* GNUNET_PQ_PostgresResultHandler)(void *cls, PGresult *result, unsigned int num_results)
Function to be called with the results of a SELECT statement that has returned num_results results...
Time for absolute times used by GNUnet, in microseconds.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
void * dst
Destination for the data.
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated.
Definition: pq_exec.c:55
size_t size
Size of data.
Definition: gnunet_pq_lib.h:84
void(* GNUNET_PQ_ResultCleanup)(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
size_t * result_size
Where to store actual size of the result.
GNUNET_PQ_ResultConverter conv
What is the format of the result?
uint32_t data
The data value.
const char * sql
Actual SQL statement.
Handle to Postgres database.
Definition: pq.h:34
int(* GNUNET_PQ_ResultConverter)(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql, unsigned int num_args)
Create a struct GNUNET_PQ_PreparedStatement.
Definition: pq_prepare.c:38
int GNUNET_PQ_extract_result(PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
Extract results from a query result according to the given specification.
Definition: pq.c:151
Time for relative time used by GNUnet, in microseconds.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition: pq.c:130