diff --git a/src/modules/db_postgres/db_postgres.c b/src/modules/db_postgres/db_postgres.c index 079ea6c7f3a..dded53b43af 100644 --- a/src/modules/db_postgres/db_postgres.c +++ b/src/modules/db_postgres/db_postgres.c @@ -96,6 +96,8 @@ int db_postgres_bind_api(db_func_t *dbb) dbb->end_transaction = db_postgres_end_transaction; dbb->abort_transaction = db_postgres_abort_transaction; dbb->query_lock = db_postgres_query_lock; + dbb->raw_query_async = db_postgres_raw_query_async; + dbb->insert_async = db_postgres_insert_async; return 0; } diff --git a/src/modules/db_postgres/km_dbase.c b/src/modules/db_postgres/km_dbase.c index 0ae7dff54d4..f7e533ee412 100644 --- a/src/modules/db_postgres/km_dbase.c +++ b/src/modules/db_postgres/km_dbase.c @@ -39,6 +39,7 @@ #include "../../lib/srdb1/db.h" #include "../../lib/srdb1/db_ut.h" #include "../../lib/srdb1/db_query.h" +#include "../../core/async_task.h" #include "../../core/locking.h" #include "../../core/hashes.h" #include "../../core/clist.h" @@ -269,6 +270,65 @@ static int db_postgres_submit_query(const db1_con_t *_con, const str *_s) return -1; } +void db_postgres_async_exec_task(void *param) +{ + str *p; + db1_con_t* dbc; + + p = (str*)param; + + dbc = db_postgres_init(&p[0]); + + if(dbc==NULL) { + LM_ERR("failed to open connection for [%.*s]\n", p[0].len, p[0].s); + return; + } + if(db_postgres_submit_query(dbc, &p[1])<0) { + LM_ERR("failed to execute query [%.*s] on async worker\n", p[1].len, p[1].s); + } + db_postgres_close(dbc); +} +/** + * Execute a raw SQL query via core async framework. + * \param _h handle for the database + * \param _s raw query string + * \return zero on success, negative value on failure + */ +int db_postgres_submit_query_async(const db1_con_t* _h, const str* _s) +{ + struct db_id* di; + async_task_t *atask; + int asize; + str *p; + + di = ((struct pool_con*)_h->tail)->id; + + asize = sizeof(async_task_t) + 2*sizeof(str) + di->url.len + _s->len + 2; + atask = shm_malloc(asize); + if(atask==NULL) { + LM_ERR("no more shared memory to allocate %d\n", asize); + return -1; + } + + atask->exec = db_postgres_async_exec_task; + atask->param = (char*)atask + sizeof(async_task_t); + + p = (str*)((char*)atask + sizeof(async_task_t)); + p[0].s = (char*)p + 2*sizeof(str); + p[0].len = di->url.len; + strncpy(p[0].s, di->url.s, di->url.len); + p[1].s = p[0].s + p[0].len + 1; + p[1].len = _s->len; + strncpy(p[1].s, _s->s, _s->len); + + + if (async_task_push(atask)<0) { + shm_free(atask); + return -1; + } + + return 0; +} /*! * \brief Gets a partial result set, fetch rows from a result @@ -499,6 +559,17 @@ int db_postgres_raw_query(const db1_con_t *_h, const str *_s, db1_res_t **_r) _h, _s, _r, db_postgres_submit_query, db_postgres_store_result); } +/** + * Execute a raw SQL query via core async framework. + * \param _h handle for the database + * \param _s raw query string + * \return zero on success, negative value on failure + */ +int db_postgres_raw_query_async(const db1_con_t* _h, const str* _s) +{ + return db_postgres_submit_query_async(_h, _s); + +} /*! * \brief Retrieve result set @@ -616,6 +687,27 @@ int db_postgres_insert(const db1_con_t *_h, const db_key_t *_k, return ret; } +/** + * Insert a row into a specified table via core async framework. + * \param _h structure representing database connection + * \param _k key names + * \param _v values of the keys + * \param _n number of key=value pairs + * \return zero on success, negative value on failure + */ +int db_postgres_insert_async(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) +{ + return db_do_insert(_h, _k, _v, _n, db_postgres_val2str, + db_postgres_submit_query_async); +} +/* + * Delete a row from the specified table + * _h: structure representing database connection + * _k: key names + * _o: operators + * _v: values of the keys that must match + * _n: number of key=value pairs + */ /*! * \brief Delete a row from the specified table @@ -1179,3 +1271,7 @@ int db_postgres_replace(const db1_con_t *_h, const db_key_t *_k, } return 0; } + +/** + * + */ diff --git a/src/modules/db_postgres/km_dbase.h b/src/modules/db_postgres/km_dbase.h index a8b50b11117..6dea9d91e65 100644 --- a/src/modules/db_postgres/km_dbase.h +++ b/src/modules/db_postgres/km_dbase.h @@ -96,12 +96,21 @@ int db_postgres_query_lock(const db1_con_t *_h, const db_key_t *_k, int db_postgres_raw_query(const db1_con_t *_h, const str *_s, db1_res_t **_r); +/* + * Raw SQL query via async framework + */ +int db_postgres_raw_query_async(const db1_con_t* _h, const str* _s); + /* * Insert a row into table */ int db_postgres_insert(const db1_con_t *_h, const db_key_t *_k, const db_val_t *_v, const int _n); +/* + * * Insert a row into table via async framework + */ +int db_postgres_insert_async(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n); /* * Insert and update ON CONFLICT */