GNUnet 0.22.2
pq_connect.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2017, 2019, 2020, 2021, 2023 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 */
26#include "platform.h"
27#include "pq.h"
28#include <pthread.h>
29
30
36static void
38{
39 if (NULL == db->conn)
40 return;
41 PQfinish (db->conn);
42 db->conn = NULL;
43 db->prepared_check_patch = false;
44 db->prepared_get_oid_by_name = false;
45}
46
47
57{
58 PGresult *res;
59
60 if (db->prepared_check_patch)
61 return GNUNET_OK;
62 res = PQprepare (db->conn,
63 "gnunet_pq_check_patch",
64 "SELECT"
65 " applied_by"
66 " FROM _v.patches"
67 " WHERE patch_name = $1"
68 " LIMIT 1",
69 1,
70 NULL);
71 if (PGRES_COMMAND_OK !=
72 PQresultStatus (res))
73 {
75 "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
76 PQresultErrorMessage (res),
77 PQerrorMessage (db->conn));
78 PQclear (res);
80 return GNUNET_SYSERR;
81 }
82 PQclear (res);
83 db->prepared_check_patch = true;
84 return GNUNET_OK;
85}
86
87
97{
98 PGresult *res;
99
100 if (db->prepared_get_oid_by_name)
101 return GNUNET_OK;
102 res = PQprepare (db->conn,
103 "gnunet_pq_get_oid_by_name",
104 "SELECT"
105 " typname, oid"
106 " FROM pg_type"
107 " WHERE typname = $1"
108 " LIMIT 1",
109 1,
110 NULL);
111 if (PGRES_COMMAND_OK != PQresultStatus (res))
112 {
114 "Failed to run SQL statement prepare OID lookups: %s/%s\n",
115 PQresultErrorMessage (res),
116 PQerrorMessage (db->conn));
117 PQclear (res);
119 return GNUNET_SYSERR;
120 }
121 PQclear (res);
122 db->prepared_get_oid_by_name = true;
123 return GNUNET_OK;
124}
125
126
140 const char *load_path,
141 unsigned int patch_number)
142{
143 const char *load_path_suffix;
144 size_t slen = strlen (load_path) + 10;
145 char patch_name[slen];
146
147 load_path_suffix = strrchr (load_path,
148 '/');
149 if (NULL == load_path_suffix)
150 load_path_suffix = load_path;
151 else
152 load_path_suffix++; /* skip '/' */
153 GNUNET_snprintf (patch_name,
154 sizeof (patch_name),
155 "%s%04u",
156 load_path_suffix,
157 patch_number);
158 {
159 struct GNUNET_PQ_QueryParam params[] = {
160 GNUNET_PQ_query_param_string (patch_name),
162 };
163 char *applied_by;
164 struct GNUNET_PQ_ResultSpec rs[] = {
165 GNUNET_PQ_result_spec_string ("applied_by",
166 &applied_by),
168 };
169 enum GNUNET_DB_QueryStatus qs;
170
171 if (GNUNET_OK !=
173 {
174 GNUNET_break (0);
175 return GNUNET_SYSERR;
176 }
178 "gnunet_pq_check_patch",
179 params,
180 rs);
181 switch (qs)
182 {
185 "Database version %s already applied by %s\n",
186 patch_name,
187 applied_by);
189 return GNUNET_OK;
191 return GNUNET_NO;
193 GNUNET_break (0);
194 return GNUNET_SYSERR;
196 GNUNET_break (0);
197 return GNUNET_SYSERR;
198 }
199 GNUNET_assert (0);
200 return GNUNET_SYSERR;
201 }
202}
203
204
213static void
215 const PGresult *res)
216{
217 /* do nothing, intentionally */
218 (void) arg;
219 (void) res;
220}
221
222
230static void
232 const char *message)
233{
234 (void) arg;
236 "pq",
237 "%s",
238 message);
239}
240
241
242struct GNUNET_PQ_Context *
244 const char *load_path,
245 const struct GNUNET_PQ_ExecuteStatement *es,
246 const struct GNUNET_PQ_PreparedStatement *ps)
247{
249 load_path,
250 NULL == load_path
251 ? NULL
252 : "",
253 es,
254 ps,
256}
257
258
259struct GNUNET_PQ_Context *
261 const char *load_path,
262 const char *auto_suffix,
263 const struct GNUNET_PQ_ExecuteStatement *es,
264 const struct GNUNET_PQ_PreparedStatement *ps,
266{
267 struct GNUNET_PQ_Context *db;
268 unsigned int elen = 0;
269 unsigned int plen = 0;
270
271 if (NULL != es)
272 while (NULL != es[elen].sql)
273 elen++;
274 if (NULL != ps)
275 while (NULL != ps[plen].name)
276 plen++;
277
279 db->flags = flags;
280 db->config_str = GNUNET_strdup (config_str);
281 if (NULL != load_path)
282 db->load_path = GNUNET_strdup (load_path);
283 if (NULL != auto_suffix)
284 db->auto_suffix = GNUNET_strdup (auto_suffix);
285 if (0 != elen)
286 {
287 db->es = GNUNET_new_array (elen + 1,
289 memcpy (db->es,
290 es,
291 elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
292 }
293 if (0 != plen)
294 {
295 db->ps = GNUNET_new_array (plen + 1,
297 memcpy (db->ps,
298 ps,
299 plen * sizeof (struct GNUNET_PQ_PreparedStatement));
300 }
301 db->channel_map = GNUNET_CONTAINER_multishortmap_create (16,
302 GNUNET_YES);
304 if (NULL == db->conn)
305 {
307 GNUNET_free (db->load_path);
308 GNUNET_free (db->auto_suffix);
309 GNUNET_free (db->config_str);
310 GNUNET_free (db);
311 return NULL;
312 }
313 return db;
314}
315
316
319 const char *buf)
320{
321 struct GNUNET_OS_Process *psql;
323 unsigned long code;
325 char *fn;
326
327 GNUNET_asprintf (&fn,
328 "%s%s.sql",
329 db->load_path,
330 buf);
331 if (GNUNET_YES !=
333 {
335 "SQL resource `%s' does not exist\n",
336 fn);
337 GNUNET_free (fn);
338 return GNUNET_NO;
339 }
340 if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
341 return GNUNET_SYSERR;
343 "Applying SQL file `%s' on database %s\n",
344 fn,
345 db->config_str);
347 NULL,
348 NULL,
349 NULL,
350 "psql",
351 "psql",
352 db->config_str,
353 "-f",
354 fn,
355 "-q",
356 "--set",
357 "ON_ERROR_STOP=1",
358 NULL);
359 if (NULL == psql)
360 {
362 "exec",
363 "psql");
364 GNUNET_free (fn);
365 return GNUNET_SYSERR;
366 }
368 &type,
369 &code);
370 if (GNUNET_OK != ret)
371 {
373 "psql on file %s did not finish, killed it!\n",
374 fn);
375 /* can happen if we got a signal, like CTRL-C, before
376 psql was complete */
377 (void) GNUNET_OS_process_kill (psql,
378 SIGKILL);
380 GNUNET_free (fn);
381 return GNUNET_SYSERR;
382 }
384 if ( (GNUNET_OS_PROCESS_EXITED != type) ||
385 (0 != code) )
386 {
388 "Could not run PSQL on file %s: psql exit code was %d\n",
389 fn,
390 (int) code);
391 GNUNET_free (fn);
392 return GNUNET_SYSERR;
393 }
394 GNUNET_free (fn);
395 return GNUNET_OK;
396}
397
398
401 const char *load_suffix)
402{
403 size_t slen = strlen (load_suffix) + 10;
404 char patch_name[slen];
405
407 "Loading SQL resources from `%s'\n",
408 load_suffix);
409 for (unsigned int i = 1; i<10000; i++)
410 {
412
414 load_suffix,
415 i);
416 if (GNUNET_SYSERR == ret)
417 {
418 GNUNET_break (0);
419 return GNUNET_SYSERR;
420 }
421 if (GNUNET_OK == ret)
422 continue; /* patch already applied, skip it */
423
424 GNUNET_snprintf (patch_name,
425 sizeof (patch_name),
426 "%s%04u",
427 load_suffix,
428 i);
430 patch_name);
431 if (GNUNET_NO == ret)
432 break;
433 if ( (GNUNET_SYSERR == ret) &&
434 (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags)) )
435 {
436 /* We are only checking, found unapplied patch, bad! */
438 "Database outdated, patch %s missing. Aborting!\n",
439 patch_name);
440 }
441 if (GNUNET_SYSERR == ret)
442 return GNUNET_SYSERR;
443 }
444 return GNUNET_OK;
445}
446
447
448void
450{
451 if (1 ==
452 PQconsumeInput (db->conn))
453 return;
454 if (CONNECTION_BAD != PQstatus (db->conn))
455 return;
457}
458
459
462 struct GNUNET_PQ_Context *db,
463 const char *name,
464 Oid *oid)
465{
466 /* Check if the entry is in the cache already */
467 for (unsigned int i = 0; i < db->oids.num; i++)
468 {
469 /* Pointer comparison */
470 if (name == db->oids.table[i].name)
471 {
472 *oid = db->oids.table[i].oid;
473 return GNUNET_OK;
474 }
475 }
476
477 /* No entry found in cache, ask database */
478 {
479 enum GNUNET_DB_QueryStatus qs;
480 struct GNUNET_PQ_QueryParam params[] = {
483 };
484 struct GNUNET_PQ_ResultSpec spec[] = {
486 oid),
488 };
489
490 GNUNET_assert (NULL != db);
491
493 "gnunet_pq_get_oid_by_name",
494 params,
495 spec);
497 return GNUNET_SYSERR;
498 }
499
500 /* Add the entry to the cache */
501 if (NULL == db->oids.table)
502 {
503 db->oids.table = GNUNET_new_array (8,
504 typeof(*db->oids.table));
505 db->oids.cap = 8;
506 db->oids.num = 0;
507 }
508
509 if (db->oids.cap <= db->oids.num)
510 GNUNET_array_grow (db->oids.table,
511 db->oids.cap,
512 db->oids.cap + 8);
513
514 db->oids.table[db->oids.num].name = name;
515 db->oids.table[db->oids.num].oid = *oid;
516 db->oids.num++;
517
518 return GNUNET_OK;
519}
520
521
532{
533 static const char *typnames[] = {
534 "bool",
535 "int2",
536 "int4",
537 "int8",
538 "bytea",
539 "varchar"
540 };
541 Oid oid;
542
543 for (size_t i = 0; i< sizeof(typnames) / sizeof(*typnames); i++)
544 {
545 if (GNUNET_OK !=
547 typnames[i],
548 &oid))
549 {
551 "pq",
552 "Couldn't retrieve OID for type %s\n",
553 typnames[i]);
554 return GNUNET_SYSERR;
555 }
556 }
557 return GNUNET_OK;
558}
559
560
561void
563{
565 -1);
567 db->conn = PQconnectdb (db->config_str);
568 if ( (NULL == db->conn) ||
569 (CONNECTION_OK != PQstatus (db->conn)) )
570 {
572 "pq",
573 "Database connection to '%s' failed: %s\n",
574 db->config_str,
575 (NULL != db->conn)
576 ? PQerrorMessage (db->conn)
577 : "PQconnectdb returned NULL");
579 return;
580 }
581 PQsetNoticeReceiver (db->conn,
583 db);
584 PQsetNoticeProcessor (db->conn,
586 db);
587 if ( (NULL != db->load_path) &&
588 (NULL != db->auto_suffix) )
589 {
590 PGresult *res;
591 ExecStatusType est;
592
593 res = PQexec (db->conn,
594 "SELECT"
595 " schema_name"
596 " FROM information_schema.schemata"
597 " WHERE schema_name='_v';");
598 est = PQresultStatus (res);
599 if ( (PGRES_COMMAND_OK != est) &&
600 (PGRES_TUPLES_OK != est) )
601 {
603 "Failed to run statement to check versioning schema. Bad!\n");
604 PQclear (res);
606 return;
607 }
608 if (0 == PQntuples (res))
609 {
611
612 PQclear (res);
613 if (0 != (db->flags & GNUNET_PQ_FLAG_DROP))
614 {
616 "Versioning schema does not exist yet. Not attempting drop!\n");
618 return;
619 }
621 "versioning");
622 if (GNUNET_NO == ret)
623 {
625 "Failed to find SQL file to load database versioning logic\n");
627 return;
628 }
629 if (GNUNET_SYSERR == ret)
630 {
632 "Failed to run SQL logic to setup database versioning logic\n");
634 return;
635 }
636 }
637 else
638 {
639 PQclear (res);
640 }
641 }
642
643 /* Prepare statement for OID lookup by name */
644 if (GNUNET_OK !=
646 return;
647
648 /* Reset the OID-cache and retrieve the OIDs for the supported Array types */
649 db->oids.num = 0;
651 {
653 "Failed to retrieve OID information for array types!\n");
655 return;
656 }
657
658 if (NULL != db->auto_suffix)
659 {
660 GNUNET_assert (NULL != db->load_path);
661 if (GNUNET_OK !=
663 return;
664
665 if (GNUNET_SYSERR ==
667 db->auto_suffix))
668 {
669 if (0 == (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
671 "Failed to load SQL statements from `%s*'\n",
672 db->auto_suffix);
674 return;
675 }
676 }
677
678 if ( (NULL != db->es) &&
679 (GNUNET_OK !=
681 db->es)) )
682 {
684 return;
685 }
686 if ( (NULL != db->ps) &&
687 (GNUNET_OK !=
689 db->ps)) )
690 {
692 return;
693 }
695 PQsocket (db->conn));
696}
697
698
699struct GNUNET_PQ_Context *
701 const char *section,
702 const char *load_path_suffix,
703 const struct GNUNET_PQ_ExecuteStatement *es,
704 const struct GNUNET_PQ_PreparedStatement *ps)
705{
707 section,
708 load_path_suffix,
709 es,
710 ps,
712}
713
714
715struct GNUNET_PQ_Context *
717 const char *section,
718 const char *load_path_suffix,
719 const struct GNUNET_PQ_ExecuteStatement *es,
720 const struct GNUNET_PQ_PreparedStatement *ps,
722{
723 struct GNUNET_PQ_Context *db;
724 char *conninfo;
725 char *load_path;
726
727 if (GNUNET_OK !=
729 section,
730 "CONFIG",
731 &conninfo))
732 conninfo = NULL;
733 load_path = NULL;
734 if (GNUNET_OK !=
736 section,
737 "SQL_DIR",
738 &load_path))
739 {
741 section,
742 "SQL_DIR");
743 }
744 if ( (NULL != load_path_suffix) &&
745 (NULL == load_path) )
746 {
748 section,
749 "SQL_DIR");
750 return NULL;
751 }
752 db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
753 load_path,
754 load_path_suffix,
755 es,
756 ps,
757 flags);
759 GNUNET_free (conninfo);
760 return db;
761}
762
763
764void
766{
767 if (NULL == db)
768 return;
769 GNUNET_assert (0 ==
772 GNUNET_free (db->es);
773 GNUNET_free (db->ps);
774 GNUNET_free (db->load_path);
775 GNUNET_free (db->auto_suffix);
776 GNUNET_free (db->config_str);
777 GNUNET_free (db->oids.table);
778 db->oids.table = NULL;
779 db->oids.num = 0;
780 db->oids.cap = 0;
781 PQfinish (db->conn);
782 GNUNET_free (db);
783}
784
785
786/* end of pq/pq_connect.c */
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
static char * name
Name (label) of the records to list.
static char * res
Currently read line or NULL on EOF.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_FS_DirectoryBuilder * db
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:37
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:60
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:41
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:55
@ GNUNET_DB_STATUS_SOFT_ERROR
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:47
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:199
enum GNUNET_GenericReturnValue 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:54
enum GNUNET_GenericReturnValue 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:88
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
GNUNET_PQ_Options
Flags to control PQ operation.
@ GNUNET_PQ_FLAG_DROP
Dropping database.
@ GNUNET_PQ_FLAG_CHECK_CURRENT
Check database version is current.
@ GNUNET_PQ_FLAG_NONE
Traditional default.
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:139
uint32_t oid
Definition: gnunet_pq_lib.h:2
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
Definition: disk.c:513
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
unsigned int GNUNET_CONTAINER_multishortmap_size(const struct GNUNET_CONTAINER_MultiShortmap *map)
Get the number of key-value pairs in the map.
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_OS_Process * GNUNET_OS_start_process(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename,...)
Start a process.
Definition: os_priority.c:620
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
Definition: os_priority.c:862
GNUNET_OS_ProcessStatusType
Process status types.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:260
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
Definition: gnunet_os_lib.h:77
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
shared internal data structures of libgnunetpq
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
Definition: pq_event.c:412
static enum GNUNET_GenericReturnValue check_patch_applied(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int patch_number)
Check if the patch with patch_number from the given load_path was already applied on the db.
Definition: pq_connect.c:139
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:562
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:700
static enum GNUNET_GenericReturnValue prepare_check_patch(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_check_patch" statement.
Definition: pq_connect.c:56
struct GNUNET_PQ_Context * GNUNET_PQ_connect2(const char *config_str, const char *load_path, const char *auto_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
Create a connection to the Postgres database using config_str for the configuration.
Definition: pq_connect.c:260
static void pq_notice_receiver_cb(void *arg, const PGresult *res)
Function called by libpq whenever it wants to log something.
Definition: pq_connect.c:214
static void reset_connection(struct GNUNET_PQ_Context *db)
Close connection to db and mark it as uninitialized.
Definition: pq_connect.c:37
static enum GNUNET_GenericReturnValue prepare_get_oid_by_name(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_get_oid_by_name" statement.
Definition: pq_connect.c:96
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
Definition: pq_connect.c:449
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:765
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg2(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, enum GNUNET_PQ_Options flags)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:716
static void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
Definition: pq_connect.c:231
enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name(struct GNUNET_PQ_Context *db, const char *name, Oid *oid)
Returns the oid for a given datatype by name.
Definition: pq_connect.c:461
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql(struct GNUNET_PQ_Context *db, const char *buf)
Execute SQL statements from buf against db.
Definition: pq_connect.c:318
static enum GNUNET_GenericReturnValue load_initial_oids(struct GNUNET_PQ_Context *db)
Load the initial set of OIDs for the supported array-datatypes.
Definition: pq_connect.c:531
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:243
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_suffix)
Within the db context, run all the SQL files in the load path where the name starts with the load_suf...
Definition: pq_connect.c:400
Handle to Postgres database.
Definition: pq.h:36
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:45
enum GNUNET_PQ_Options flags
Flags controlling the connection.
Definition: pq.h:100
char * load_path
Path to load SQL files from.
Definition: pq.h:70
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:65
char * auto_suffix
Suffix to append to path to load on startup.
Definition: pq.h:75
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:83
Description of a DB result cell.