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",
107 " WHERE typname = $1"
111 if (PGRES_COMMAND_OK != PQresultStatus (
res))
114 "Failed to run SQL statement prepare OID lookups: %s/%s\n",
115 PQresultErrorMessage (
res),
116 PQerrorMessage (
db->conn));
122 db->prepared_get_oid_by_name =
true;
140 const char *load_path,
141 unsigned int patch_number)
143 const char *load_path_suffix;
144 size_t slen = strlen (load_path) + 10;
145 char patch_name[slen];
147 load_path_suffix = strrchr (load_path,
149 if (NULL == load_path_suffix)
150 load_path_suffix = load_path;
178 "gnunet_pq_check_patch",
185 "Database version %s already applied by %s\n",
268 unsigned int elen = 0;
269 unsigned int plen = 0;
272 while (NULL !=
es[elen].sql)
275 while (NULL !=
ps[plen].
name)
304 if (NULL ==
db->conn)
335 "SQL resource `%s' does not exist\n",
343 "Applying SQL file `%s' on database %s\n",
373 "psql on file %s did not finish, killed it!\n",
388 "Could not run PSQL on file %s: psql exit code was %d\n",
401 const char *load_suffix)
403 size_t slen = strlen (load_suffix) + 10;
404 char patch_name[slen];
407 "Loading SQL resources from `%s'\n",
409 for (
unsigned int i = 1; i<10000; i++)
438 "Database outdated, patch %s missing. Aborting!\n",
452 PQconsumeInput (
db->conn))
454 if (CONNECTION_BAD != PQstatus (
db->conn))
467 for (
unsigned int i = 0; i <
db->oids.num; i++)
470 if (
name ==
db->oids.table[i].name)
472 *
oid =
db->oids.table[i].oid;
493 "gnunet_pq_get_oid_by_name",
501 if (NULL ==
db->oids.table)
504 typeof(*
db->oids.table));
509 if (
db->oids.cap <=
db->oids.num)
514 db->oids.table[
db->oids.num].name =
name;
515 db->oids.table[
db->oids.num].oid = *
oid;
533 static const char *typnames[] = {
543 for (
size_t i = 0; i<
sizeof(typnames) /
sizeof(*typnames); i++)
552 "Couldn't retrieve OID for type %s\n",
567 db->conn = PQconnectdb (
db->config_str);
568 if ( (NULL ==
db->conn) ||
569 (CONNECTION_OK != PQstatus (
db->conn)) )
573 "Database connection to '%s' failed: %s\n",
576 ? PQerrorMessage (
db->conn)
577 :
"PQconnectdb returned NULL");
581 PQsetNoticeReceiver (
db->conn,
584 PQsetNoticeProcessor (
db->conn,
587 if ( (NULL !=
db->load_path) &&
588 (NULL !=
db->auto_suffix) )
593 res = PQexec (
db->conn,
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) )
603 "Failed to run statement to check versioning schema. Bad!\n");
608 if (0 == PQntuples (
res))
616 "Versioning schema does not exist yet. Not attempting drop!\n");
625 "Failed to find SQL file to load database versioning logic\n");
632 "Failed to run SQL logic to setup database versioning logic\n");
653 "Failed to retrieve OID information for array types!\n");
658 if (NULL !=
db->auto_suffix)
671 "Failed to load SQL statements from `%s*'\n",
678 if ( (NULL !=
db->es) &&
686 if ( (NULL !=
db->ps) &&
695 PQsocket (
db->conn));
702 const char *load_path_suffix,
718 const char *load_path_suffix,
744 if ( (NULL != load_path_suffix) &&
778 db->oids.table = NULL;
static int ret
Final status code.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
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.
@ 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...
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.
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.
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().
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.
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.
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.
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.
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.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
@ 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.
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.
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
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.
static enum GNUNET_GenericReturnValue prepare_check_patch(struct GNUNET_PQ_Context *db)
Prepare the "gnunet_pq_check_patch" statement.
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.
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_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.
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.
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.
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.
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
enum GNUNET_PQ_Options flags
Flags controlling the connection.
char * load_path
Path to load SQL files from.
char * config_str
Configuration to use to connect to the DB.
char * auto_suffix
Suffix to append to path to load on startup.
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.
Description of a DB result cell.