95 unsigned int elen = 0;
96 unsigned int plen = 0;
99 while (NULL != es[elen].sql)
102 while (NULL != ps[plen].
name)
107 if (NULL != load_path)
126 if (NULL == db->
conn)
153 size_t slen = strlen (load_path) + 10;
162 "Applying SQL file `%s' on database %s\n",
194 "Could not run PSQL on file %s: %d\n",
214 const char *load_path)
216 const char *load_path_suffix;
217 size_t slen = strlen (load_path) + 10;
219 load_path_suffix = strrchr (load_path,
'/');
220 if (NULL == load_path_suffix)
227 "Loading SQL resources from `%s'\n",
229 for (
unsigned int i = 1; i<10000; i++)
249 char patch_name[slen];
269 "gnunet_pq_check_patch",
275 "Database version %s already applied by %s, skipping\n",
315 if (CONNECTION_BAD != PQstatus (db->
conn))
329 if (NULL != db->
conn)
332 if ( (NULL == db->
conn) ||
333 (CONNECTION_OK != PQstatus (db->
conn)) )
337 "Database connection to '%s' failed: %s\n",
340 PQerrorMessage (db->
conn)
341 :
"PQconnectdb returned NULL");
342 if (NULL != db->
conn)
349 PQsetNoticeReceiver (db->
conn,
352 PQsetNoticeProcessor (db->
conn,
359 res = PQprepare (db->
conn,
360 "gnunet_pq_check_patch",
364 " WHERE patch_name = $1" 368 if (PGRES_COMMAND_OK != PQresultStatus (res))
374 "Failed to prepare statement to check patch level. Likely versioning schema does not exist yet, loading patch level 0000!\n");
381 "Failed to find SQL file to load database versioning logic\n");
389 "Failed to run SQL logic to setup database versioning logic\n");
395 res = PQprepare (db->
conn,
396 "gnunet_pq_check_patch",
400 " WHERE patch_name = $1" 404 if (PGRES_COMMAND_OK != PQresultStatus (res))
407 "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
408 PQresultErrorMessage (res),
409 PQerrorMessage (db->
conn));
423 "Failed to load SQL statements from `%s*'\n",
430 if ( (NULL != db->
es) &&
439 if ( (NULL != db->
ps) &&
471 const char *load_path_suffix,
488 if ( (NULL != load_path_suffix) &&
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
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.
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
char * load_path
Path to load SQL files from.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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().
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
static int apply_patch(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int i)
Apply patch number from path load_path.
Description of a DB result cell.
static int ret
Return value of the commandline.
int GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.
A hard error occurred, retrying will not help.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char * section
Name of the section.
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.
PGconn * conn
Actual connection.
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.
shared internal data structures of libgnunetpq
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
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...
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.
char * config_str
Configuration to use to connect to the DB.
#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...
No standard streams should be inherited.
GNUNET_OS_ProcessStatusType
Process status types.
int 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.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
The process exited with a return code.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
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.
The transaction succeeded, and yielded one result.
static struct GNUNET_FS_DirectoryBuilder * db
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
Description of a DB query parameter.
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static void pq_notice_receiver_cb(void *arg, const PGresult *res)
Function called by libpq whenever it wants to log something.
static void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
#define GNUNET_log_from(kind, comp,...)
Handle to Postgres database.
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...
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().