functions to connect to libpq (PostGres) More...
Go to the source code of this file.
Functions | |
| static void | reset_connection (struct GNUNET_PQ_Context *db) |
| Close connection to db and mark it as uninitialized. | |
| static enum GNUNET_GenericReturnValue | prepare_check_patch (struct GNUNET_PQ_Context *db) |
| Prepare the "gnunet_pq_check_patch" statement. | |
| static enum GNUNET_GenericReturnValue | prepare_get_oid_by_name (struct GNUNET_PQ_Context *db) |
| Prepare the "gnunet_pq_get_oid_by_name" statement. | |
| 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). | |
| 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 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. | |
| static char * | get_sql_file_name (const struct GNUNET_PQ_Context *db, const char *infix) |
| Construct the name of the SQL file with the given filename infix in the load path of db. | |
| static enum GNUNET_GenericReturnValue | patch_file_exists (const struct GNUNET_PQ_Context *db, const char *load_suffix, unsigned int patch_number) |
| Check if the SQL file for the patch with the given patch_number exists in the load path of db. | |
| static bool | find_next_patch (const struct GNUNET_PQ_Context *db, const char *load_suffix, unsigned int from, unsigned int *found) |
| Find the lowest patch number of at least from for which an SQL file exists in the load path of db. | |
| enum GNUNET_GenericReturnValue | GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db, const char *buf) |
| Execute SQL statements from buf against db. | |
| 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. | |
| 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_suffix and ends with consecutive numbers from "[0000-9999].sql". | |
| void | GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db) |
| Reinitialize the database db if the connection is down. | |
| 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. | |
| static enum GNUNET_GenericReturnValue | load_initial_oids (struct GNUNET_PQ_Context *db) |
| Load the initial set of OIDs for the supported array-datatypes. | |
| 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. | |
| 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) |
| void | GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db) |
| Disconnect from the database, destroying the prepared statements and releasing other associated resources. | |
functions to connect to libpq (PostGres)
Definition in file pq_connect.c.
|
static |
Close connection to db and mark it as uninitialized.
| [in,out] | db | connection to close |
Definition at line 37 of file pq_connect.c.
References db.
Referenced by GNUNET_PQ_reconnect_(), prepare_check_patch(), and prepare_get_oid_by_name().
|
static |
Prepare the "gnunet_pq_check_patch" statement.
| [in,out] | db | database to prepare statement for |
Definition at line 56 of file pq_connect.c.
References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, res, and reset_connection().
Referenced by check_patch_applied().
|
static |
Prepare the "gnunet_pq_get_oid_by_name" statement.
| [in,out] | db | database to prepare statement for |
Definition at line 96 of file pq_connect.c.
References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, res, and reset_connection().
Referenced by GNUNET_PQ_reconnect_().
|
static |
Check if the "_v" versioning schema exists (and cache the result in db).
| [in,out] | db | connection to check |
Definition at line 135 of file pq_connect.c.
References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, and res.
Referenced by check_patch_applied(), and GNUNET_PQ_load_versioning().
|
static |
Check if the patch with patch_number from the given load_path was already applied on the db.
| [in] | db | database to check |
| load_path | file system path to database setup files | |
| patch_number | number of the patch to check |
Definition at line 184 of file pq_connect.c.
References check_versioning_ok(), db, GNUNET_assert, GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), 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_string(), GNUNET_snprintf(), GNUNET_SYSERR, and prepare_check_patch().
Referenced by GNUNET_PQ_check_current(), and GNUNET_PQ_run_sql().
|
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 265 of file pq_connect.c.
References 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 282 of file pq_connect.c.
References GNUNET_ERROR_TYPE_INFO, and GNUNET_log_from.
Referenced by GNUNET_PQ_reconnect_().
|
static |
Construct the name of the SQL file with the given filename infix in the load path of db.
This is the one place where the mapping from a filename infix to an actual file on disk is defined; all callers must use it, or they risk probing for files that are never loaded (and vice versa).
| db | database context to get the load path from |
| infix | filename infix (!) identifying the SQL file |
Definition at line 305 of file pq_connect.c.
References db, and GNUNET_asprintf().
Referenced by GNUNET_PQ_exec_sql(), and patch_file_exists().
|
static |
Check if the SQL file for the patch with the given patch_number exists in the load path of db.
| db | database context to get the load path from |
| load_suffix | suffix to append to the load path, usually "subsystem-" |
| patch_number | number of the patch to look for |
Definition at line 331 of file pq_connect.c.
References db, get_sql_file_name(), GNUNET_DISK_file_test_read(), GNUNET_free, GNUNET_snprintf(), and ret.
Referenced by find_next_patch().
|
static |
Find the lowest patch number of at least from for which an SQL file exists in the load path of db.
Used to tell "we ran out of patches" apart from "there is a hole in the numbering": the latter must never be treated as the end of the sequence, as we would then silently ignore all patches beyond the hole.
| db | database context to get the load path from | |
| load_suffix | suffix to append to the load path, usually "subsystem-" | |
| from | patch number to start searching at | |
| [out] | found | set to the number of the patch that was found |
Definition at line 368 of file pq_connect.c.
References db, GNUNET_YES, and patch_file_exists().
Referenced by GNUNET_PQ_check_current(), and GNUNET_PQ_run_sql().
| 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.
| [in,out] | db | database context to use |
| buf | filename infix (!) with the SQL code to run |
Definition at line 388 of file pq_connect.c.
References db, get_sql_file_name(), GNUNET_assert, GNUNET_DISK_file_test_read(), 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_EXITED, GNUNET_process_create(), GNUNET_process_destroy(), GNUNET_process_run_command_va(), GNUNET_process_wait(), GNUNET_SYSERR, GNUNET_YES, and type.
Referenced by GNUNET_PQ_load_versioning(), GNUNET_PQ_run_sql(), libgnunet_plugin_datacache_postgres_done(), and namestore_postgres_drop_tables().
| enum GNUNET_GenericReturnValue GNUNET_PQ_check_current | ( | struct GNUNET_PQ_Context * | db, |
| const char * | prefix | ||
| ) |
Check if the database is current with respect to database migrations using prefix.
| [in,out] | db | database to check versioning for |
| prefix | prefix to use to check migrations |
Definition at line 450 of file pq_connect.c.
References check_patch_applied(), db, find_next_patch(), GNUNET_break, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, and ret.
| 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_suffix and ends with consecutive numbers from "[0000-9999].sql".
The numbering must have no gaps: if a patch file is missing but a higher-numbered one exists, this is an error, as we must not skip patches.
| db | database context to use |
| load_suffix | suffix to append to the load path to find the XXXX.sql files, usually "subsystem-". |
Definition at line 486 of file pq_connect.c.
References check_patch_applied(), db, find_next_patch(), GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_exec_sql(), GNUNET_snprintf(), GNUNET_SYSERR, and ret.
Referenced by reconnect_cb(), reconnect_setup(), reconnect_setup(), and reconnect_setup().
| void GNUNET_PQ_reconnect_if_down | ( | struct GNUNET_PQ_Context * | db | ) |
Reinitialize the database db if the connection is down.
| [in,out] | db | database connection to reinitialize |
Definition at line 549 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 561 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_int16(), 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_int16(), 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 631 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 662 of file pq_connect.c.
References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_log_from, GNUNET_OK, GNUNET_PQ_event_reconnect_(), GNUNET_SYSERR, load_initial_oids(), pq_notice_processor_cb(), pq_notice_receiver_cb(), prepare_get_oid_by_name(), and reset_connection().
Referenced by do_scheduler_notify(), GNUNET_PQ_eval_result(), GNUNET_PQ_event_do_poll(), GNUNET_PQ_exec_prepared(), GNUNET_PQ_init(), and GNUNET_PQ_reconnect_if_down().
| enum GNUNET_GenericReturnValue GNUNET_PQ_load_versioning | ( | struct GNUNET_PQ_Context * | db | ) |
Setup database versioning.
| [in,out] | db | database to load versioning for |
Definition at line 712 of file pq_connect.c.
References check_versioning_ok(), db, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_exec_sql(), GNUNET_SYSERR, GNUNET_YES, and ret.
Referenced by reconnect_setup(), and reconnect_setup().
| 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 | ||
| ) |
Definition at line 741 of file pq_connect.c.
References cfg, db, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONTAINER_multishortmap_create(), GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_config_missing(), GNUNET_new, GNUNET_OK, GNUNET_PQ_reconnect_(), GNUNET_strdup, GNUNET_SYSERR, GNUNET_PQ_Context::load_path, GNUNET_PQ_Context::rc, and GNUNET_PQ_Context::rc_cls.
| 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 788 of file pq_connect.c.
References db, GNUNET_assert, GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_CONTAINER_multishortmap_size(), GNUNET_free, and GNUNET_SCHEDULER_cancel().
Referenced by database_shutdown(), database_shutdown(), libgnunet_plugin_datacache_postgres_done(), libgnunet_plugin_datastore_postgres_done(), namestore_postgres_create_tables(), and namestore_postgres_drop_tables().