43 db->prepared_check_patch =
false;
44 db->prepared_get_oid_by_name =
false;
60 if (
db->prepared_check_patch)
62 res = PQprepare (
db->conn,
63 "gnunet_pq_check_patch",
67 " WHERE patch_name = $1"
71 if (PGRES_COMMAND_OK !=
75 "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
76 PQresultErrorMessage (
res),
77 PQerrorMessage (
db->conn));
83 db->prepared_check_patch =
true;
100 if (
db->prepared_get_oid_by_name)
102 res = PQprepare (
db->conn,
103 "gnunet_pq_get_oid_by_name",
104 "SELECT typname, oid"
106 " WHERE oid = to_regtype($1)",
109 if (PGRES_COMMAND_OK != PQresultStatus (
res))
112 "Failed to run SQL statement prepare OID lookups: %s/%s\n",
113 PQresultErrorMessage (
res),
114 PQerrorMessage (
db->conn));
120 db->prepared_get_oid_by_name =
true;
144 res = PQexec (
db->conn,
147 " FROM information_schema.schemata"
148 " WHERE schema_name='_v';");
149 est = PQresultStatus (
res);
150 if ( (PGRES_COMMAND_OK != est) &&
151 (PGRES_TUPLES_OK != est) )
154 "Failed to run statement to check versioning schema. Bad!\n");
158 if (0 == PQntuples (
res))
163 "_v schema not found\n");
185 const char *load_path,
186 unsigned int patch_number)
188 const char *load_path_suffix;
189 size_t slen = strlen (load_path) + 10;
190 char patch_name[slen];
198 load_path_suffix = strrchr (load_path,
200 if (NULL == load_path_suffix)
201 load_path_suffix = load_path;
229 "gnunet_pq_check_patch",
236 "Database version %s already applied by %s\n",
313 "Applying SQL file `%s' on database %s\n",
346 "Could not run PSQL on file %s: psql exit code was %d\n",
359 const char *load_suffix)
364 "Loading SQL resources from `%s'\n",
366 for (
unsigned int i = 1; i<10000; i++)
399 const char *load_suffix)
401 size_t slen = strlen (load_suffix) + 10;
402 char patch_name[slen];
405 "Loading SQL resources from `%s'\n",
407 for (
unsigned int i = 1; i<10000; i++)
442 PQconsumeInput (
db->conn))
444 if (CONNECTION_BAD != PQstatus (
db->conn))
457 for (
unsigned int i = 0; i <
db->oids.num; i++)
460 if (
name ==
db->oids.table[i].name)
462 *
oid =
db->oids.table[i].oid;
483 "gnunet_pq_get_oid_by_name",
491 if (NULL ==
db->oids.table)
494 typeof(*
db->oids.table));
499 if (
db->oids.cap <=
db->oids.num)
504 db->oids.table[
db->oids.num].name =
name;
505 db->oids.table[
db->oids.num].oid = *
oid;
523 static const char *typnames[] = {
533 for (
size_t i = 0; i<
sizeof(typnames) /
sizeof(*typnames); i++)
542 "Couldn't retrieve OID for type %s\n",
558 db->conn = PQconnectdb (
db->config_str);
559 if ( (NULL ==
db->conn) ||
560 (CONNECTION_OK != PQstatus (
db->conn)) )
564 "Database connection to '%s' failed: %s\n",
567 ? PQerrorMessage (
db->conn)
568 :
"PQconnectdb returned NULL");
572 PQsetNoticeReceiver (
db->conn,
575 PQsetNoticeProcessor (
db->conn,
592 "Failed to retrieve OID information for array types!\n");
597 PQsocket (
db->conn));
616 "Failed to find SQL file to load database versioning logic\n");
622 "Failed to run SQL logic to setup database versioning logic\n");
659 db->config_str = conninfo;
666 if (NULL ==
db->conn)
685 if (NULL !=
db->poller_task)
688 db->poller_task = NULL;
static int ret
Final status code.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
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.
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
@ GNUNET_DB_STATUS_SOFT_ERROR
A soft error occurred, retrying the transaction may succeed.
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...
void(* GNUNET_PQ_ReconnectCallback)(void *cls, struct GNUNET_PQ_Context *pq)
Function called each time we connect or reconnect to the database.
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.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
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.
#define GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE
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.
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.
#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
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.
enum GNUNET_GenericReturnValue GNUNET_process_run_command_va(struct GNUNET_Process *p, const char *filename,...)
Set the command and start a process.
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
GNUNET_OS_ProcessStatusType
Process status types.
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
shared internal data structures of libgnunetpq
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
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.
static enum GNUNET_GenericReturnValue prepare_check_patch(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_check_patch" statement.
enum GNUNET_GenericReturnValue GNUNET_PQ_check_current(struct GNUNET_PQ_Context *db, const char *load_suffix)
Check if the database is current with respect to database migrations using prefix.
static void pq_notice_receiver_cb(void *arg, const PGresult *res)
Function called by libpq whenever it wants to log something.
static void reset_connection(struct GNUNET_PQ_Context *db)
Close connection to db and mark it as uninitialized.
static enum GNUNET_GenericReturnValue prepare_get_oid_by_name(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_get_oid_by_name" statement.
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
struct GNUNET_PQ_Context * GNUNET_PQ_init(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_PQ_ReconnectCallback rc, GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE *rc_cls)
static enum GNUNET_GenericReturnValue check_versioning_ok(struct GNUNET_PQ_Context *db)
Check if the "_v" versioning schema exists (and cache the result in db).
void GNUNET_PQ_reconnect_(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
enum GNUNET_GenericReturnValue GNUNET_PQ_load_versioning(struct GNUNET_PQ_Context *db)
Setup database versioning.
static void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
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.
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql(struct GNUNET_PQ_Context *db, const char *buf)
Execute SQL statements from buf against db.
static enum GNUNET_GenericReturnValue load_initial_oids(struct GNUNET_PQ_Context *db)
Load the initial set of OIDs for the supported array-datatypes.
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...
Handle to Postgres database.
GNUNET_PQ_ReconnectCallback rc
Function to call whenever we needed to reconnect conn.
char * load_path
Path to load SQL files from.
GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE * rc_cls
Closure for rc.
Description of a DB query parameter.
Description of a DB result cell.