functions to connect to libpq (PostGres) More...
Go to the source code of this file.
Functions | |
static void | pq_notice_receiver_cb (void *arg, const PGresult *res) |
Function called by libpq whenever it wants to log something. More... | |
static void | pq_notice_processor_cb (void *arg, const char *message) |
Function called by libpq whenever it wants to log something. More... | |
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. More... | |
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. More... | |
enum GNUNET_GenericReturnValue | GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db, const char *buf) |
Execute SQL statements from buf against db. More... | |
enum GNUNET_GenericReturnValue | 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 files exist contiguously). More... | |
void | GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db) |
Reinitialize the database db if the connection is down. More... | |
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. More... | |
static enum GNUNET_GenericReturnValue | load_initial_oids (struct GNUNET_PQ_Context *db) |
Load the initial set of OIDs for the supported array-datatypes. More... | |
void | GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db) |
Reinitialize the database db. More... | |
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. More... | |
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. More... | |
void | GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db) |
Disconnect from the database, destroying the prepared statements and releasing other associated resources. More... | |
functions to connect to libpq (PostGres)
Definition in file pq_connect.c.
|
static |
Function called by libpq whenever it wants to log something.
We already log whenever we care, so this function does nothing and merely exists to silence the libpq logging.
arg | the SQL connection that was used |
res | information about some libpq event |
Definition at line 40 of file pq_connect.c.
References find_typedefs::arg, and res.
Referenced by GNUNET_PQ_reconnect().
|
static |
Function called by libpq whenever it wants to log something.
We log those using the GNUnet logger.
arg | the SQL connection that was used |
message | information about some libpq event |
Definition at line 57 of file pq_connect.c.
References find_typedefs::arg, GNUNET_ERROR_TYPE_INFO, and GNUNET_log_from.
Referenced by GNUNET_PQ_reconnect().
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.
Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.
In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.
config_str | configuration to use |
load_path | path to directory with SQL transactions to run, can be NULL |
es | GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL |
ps | array of prepared statements to prepare, can be NULL |
Definition at line 69 of file pq_connect.c.
References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::es, GNUNET_PQ_connect2(), GNUNET_PQ_FLAG_NONE, GNUNET_PQ_Context::load_path, and ps.
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.
Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.
In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.
config_str | configuration to use |
load_path | path to directory with SQL transactions to run, can be NULL |
auto_suffix | infix of SQL series to run on every reconnect; runs multiple (!) files, of the form auto_suffix-XXXX where XXXX is from 0 to 9999 (consecutive). |
es | GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL |
ps | array of prepared statements to prepare, can be NULL |
flags | connection flags |
Definition at line 86 of file pq_connect.c.
References GNUNET_PQ_Context::auto_suffix, GNUNET_PQ_Context::config_str, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONTAINER_multishortmap_create(), GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_free, GNUNET_new, GNUNET_new_array, GNUNET_PQ_reconnect(), GNUNET_strdup, GNUNET_YES, GNUNET_PQ_Context::load_path, name, and ps.
Referenced by GNUNET_PQ_connect(), and GNUNET_PQ_connect_with_cfg2().
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql | ( | struct GNUNET_PQ_Context * | db, |
const char * | buf | ||
) |
Execute SQL statements from buf against db.
The given filename infix in buf is prefixed with the "load_path" and ".sql" is appended to construct the full filename.
db | database context to use |
buf | filename infix (!) with the SQL code to run |
Definition at line 144 of file pq_connect.c.
References db, GNUNET_asprintf(), GNUNET_DISK_file_test(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror_file, GNUNET_NO, GNUNET_OK, GNUNET_OS_INHERIT_STD_NONE, GNUNET_OS_process_destroy(), GNUNET_OS_PROCESS_EXITED, GNUNET_OS_process_kill(), GNUNET_OS_process_wait_status(), GNUNET_OS_start_process(), GNUNET_SYSERR, GNUNET_YES, ret, and type.
Referenced by GNUNET_PQ_reconnect(), GNUNET_PQ_run_sql(), libgnunet_plugin_datacache_postgres_done(), and namestore_postgres_drop_tables().
enum GNUNET_GenericReturnValue 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 files exist contiguously).
db | database context to use |
load_path | where to find the XXXX.sql files |
Definition at line 224 of file pq_connect.c.
References db, GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_exec_sql(), GNUNET_PQ_FLAG_CHECK_CURRENT, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_string(), GNUNET_snprintf(), GNUNET_SYSERR, and ret.
Referenced by GNUNET_PQ_reconnect().
void GNUNET_PQ_reconnect_if_down | ( | struct GNUNET_PQ_Context * | db | ) |
Reinitialize the database db if the connection is down.
db | database connection to reinitialize |
Definition at line 314 of file pq_connect.c.
References db, and GNUNET_PQ_reconnect().
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.
db | The db-connection | |
name | The name of the datatype | |
[out] | oid | The OID of the datatype. |
Definition at line 326 of file pq_connect.c.
References db, GNUNET_array_grow, GNUNET_assert, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_new_array, GNUNET_OK, GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_SYSERR, name, and oid.
Referenced by GNUNET_PQ_query_param_array_abs_time(), GNUNET_PQ_query_param_array_bool(), GNUNET_PQ_query_param_array_bytes(), GNUNET_PQ_query_param_array_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_abs_time(), GNUNET_PQ_query_param_array_ptrs_bytes(), GNUNET_PQ_query_param_array_ptrs_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_rel_time(), GNUNET_PQ_query_param_array_ptrs_string(), GNUNET_PQ_query_param_array_ptrs_timestamp(), GNUNET_PQ_query_param_array_rel_time(), GNUNET_PQ_query_param_array_string(), GNUNET_PQ_query_param_array_timestamp(), GNUNET_PQ_query_param_array_uint16(), GNUNET_PQ_query_param_array_uint32(), GNUNET_PQ_query_param_array_uint64(), GNUNET_PQ_result_spec_array_abs_time(), GNUNET_PQ_result_spec_array_bool(), GNUNET_PQ_result_spec_array_fixed_size(), GNUNET_PQ_result_spec_array_rel_time(), GNUNET_PQ_result_spec_array_string(), GNUNET_PQ_result_spec_array_timestamp(), GNUNET_PQ_result_spec_array_uint16(), GNUNET_PQ_result_spec_array_uint32(), GNUNET_PQ_result_spec_array_uint64(), GNUNET_PQ_result_spec_array_variable_size(), and load_initial_oids().
|
static |
Load the initial set of OIDs for the supported array-datatypes.
db | The database context |
Definition at line 396 of file pq_connect.c.
References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_OK, GNUNET_PQ_get_oid_by_name(), GNUNET_SYSERR, and oid.
Referenced by GNUNET_PQ_reconnect().
void GNUNET_PQ_reconnect | ( | struct GNUNET_PQ_Context * | db | ) |
Reinitialize the database db.
db | database connection to reinitialize |
Definition at line 427 of file pq_connect.c.
References db, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_event_reconnect_(), GNUNET_PQ_exec_sql(), GNUNET_PQ_exec_statements(), GNUNET_PQ_FLAG_DROP, GNUNET_PQ_prepare_statements(), GNUNET_PQ_run_sql(), GNUNET_SYSERR, load_initial_oids(), pq_notice_processor_cb(), pq_notice_receiver_cb(), res, and ret.
Referenced by do_scheduler_notify(), GNUNET_PQ_connect2(), GNUNET_PQ_eval_result(), GNUNET_PQ_event_do_poll(), GNUNET_PQ_exec_prepared(), and GNUNET_PQ_reconnect_if_down().
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.
Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".
The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.
cfg | configuration |
section | configuration section to use to get Postgres configuration options |
load_path_suffix | suffix to append to the SQL_DIR in the configuration |
es | GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL |
ps | array of prepared statements to prepare, can be NULL |
Definition at line 619 of file pq_connect.c.
References cfg, GNUNET_PQ_Context::es, GNUNET_PQ_connect_with_cfg2(), GNUNET_PQ_FLAG_NONE, and ps.
Referenced by database_connect(), database_setup(), init_connection(), namestore_postgres_create_tables(), and namestore_postgres_drop_tables().
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.
Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".
The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.
cfg | configuration |
section | configuration section to use to get Postgres configuration options |
load_path_suffix | suffix to append to the SQL_DIR in the configuration |
es | GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL |
ps | array of prepared statements to prepare, can be NULL |
flags | connection flags |
Definition at line 635 of file pq_connect.c.
References cfg, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_PQ_connect2(), GNUNET_PQ_Context::load_path, and ps.
Referenced by GNUNET_PQ_connect_with_cfg().
void GNUNET_PQ_disconnect | ( | struct GNUNET_PQ_Context * | db | ) |
Disconnect from the database, destroying the prepared statements and releasing other associated resources.
db | database handle to disconnect (will be free'd) |
Definition at line 684 of file pq_connect.c.
References db, GNUNET_assert, GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_CONTAINER_multishortmap_size(), and GNUNET_free.
Referenced by database_shutdown(), libgnunet_plugin_datacache_postgres_done(), libgnunet_plugin_datastore_postgres_done(), namestore_postgres_create_tables(), and namestore_postgres_drop_tables().