static udb_query_t **queries = NULL;
static size_t queries_num = 0;
-static c_psql_database_t *databases = NULL;
-static int databases_num = 0;
-
static c_psql_database_t *c_psql_database_new (const char *name)
{
c_psql_database_t *db;
- ++databases_num;
- if (NULL == (databases = (c_psql_database_t *)realloc (databases,
- databases_num * sizeof (*databases)))) {
+ db = (c_psql_database_t *)malloc (sizeof (*db));
+ if (NULL == db) {
log_err ("Out of memory.");
exit (5);
}
- db = databases + (databases_num - 1);
-
db->conn = NULL;
C_COMPLAIN_INIT (&db->conn_complaint);
return db;
} /* c_psql_database_new */
-static void c_psql_database_delete (c_psql_database_t *db)
+static void c_psql_database_delete (void *data)
{
+ c_psql_database_t *db = data;
+
PQfinish (db->conn);
db->conn = NULL;
#undef BAIL_OUT
} /* c_psql_exec_query */
-static int c_psql_read (void)
+static int c_psql_read (user_data_t *ud)
{
- int success = 0;
+ c_psql_database_t *db;
int i;
- for (i = 0; i < databases_num; ++i) {
- c_psql_database_t *db = databases + i;
-
- int j;
+ if ((ud == NULL) || (ud->data == NULL)) {
+ log_err ("c_psql_read: Invalid user data.");
+ return -1;
+ }
- assert (NULL != db->database);
+ db = ud->data;
- if (0 != c_psql_check_connection (db))
- continue;
+ assert (NULL != db->database);
- for (j = 0; j < db->queries_num; ++j)
- {
- udb_query_t *q;
+ if (0 != c_psql_check_connection (db))
+ return -1;
- q = db->queries[j];
+ for (i = 0; i < db->queries_num; ++i)
+ {
+ udb_query_t *q;
- if ((0 != db->server_version)
- && (udb_query_check_version (q, db->server_version) <= 0))
- continue;
+ q = db->queries[i];
- c_psql_exec_query (db, q);
- }
+ if ((0 != db->server_version)
+ && (udb_query_check_version (q, db->server_version) <= 0))
+ return -1;
- ++success;
+ c_psql_exec_query (db, q);
}
-
- if (! success)
- return -1;
return 0;
} /* c_psql_read */
static int c_psql_shutdown (void)
{
- int i;
-
- if ((NULL == databases) || (0 == databases_num))
- return 0;
-
- plugin_unregister_read ("postgresql");
- plugin_unregister_shutdown ("postgresql");
-
- for (i = 0; i < databases_num; ++i)
- c_psql_database_delete (databases + i);
-
- sfree (databases);
- databases_num = 0;
+ plugin_unregister_read_group ("postgresql");
udb_query_free (queries, queries_num);
queries = NULL;
return 0;
} /* c_psql_shutdown */
-static int c_psql_init (void)
-{
- if ((NULL == databases) || (0 == databases_num))
- return 0;
-
- plugin_register_read ("postgresql", c_psql_read);
- plugin_register_shutdown ("postgresql", c_psql_shutdown);
- return 0;
-} /* c_psql_init */
-
static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
{
if ((0 != ci->children_num) || (1 != ci->values_num)
{
c_psql_database_t *db;
+ char cb_name[DATA_MAX_NAME_LEN];
+ user_data_t ud;
+
int i;
if ((1 != ci->values_num)
return 1;
}
+ memset (&ud, 0, sizeof (ud));
+
db = c_psql_database_new (ci->values[0].value.string);
for (i = 0; i < ci->children_num; ++i) {
if ((data != NULL) && (data->params_num > db->max_params_num))
db->max_params_num = data->params_num;
}
+
+ ud.data = db;
+ ud.free_func = c_psql_database_delete;
+
+ ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database);
+
+ plugin_register_complex_read ("postgresql", cb_name, c_psql_read,
+ /* interval = */ NULL, &ud);
return 0;
} /* c_psql_config_database */
void module_register (void)
{
plugin_register_complex_config ("postgresql", c_psql_config);
- plugin_register_init ("postgresql", c_psql_init);
+ plugin_register_shutdown ("postgresql", c_psql_shutdown);
} /* module_register */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */