aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmsc/db.c')
-rw-r--r--src/libmsc/db.c98
1 files changed, 64 insertions, 34 deletions
diff --git a/src/libmsc/db.c b/src/libmsc/db.c
index 0104bce6e..7f14bf7ef 100644
--- a/src/libmsc/db.c
+++ b/src/libmsc/db.c
@@ -188,6 +188,36 @@ static inline int next_row(dbi_result result)
return dbi_result_next_row(result);
}
+dbi_result query(dbi_conn conn, const char *sql)
+{
+ dbi_result result;
+
+ LOGP(DDB, LOGL_DEBUG, "Running query: %s\n", sql);
+ result = dbi_conn_query(conn, sql);
+
+ if (!result)
+ LOGP(DDB, LOGL_ERROR, "Failed to query: %s\n", sql);
+
+ return result;
+}
+
+dbi_result queryf(dbi_conn conn, const char *format, ...)
+{
+ va_list args;
+ char *sql;
+ dbi_result result;
+
+ va_start(args, format);
+ sql = talloc_vasprintf(NULL, format, args);
+ va_end(args);
+
+ result = query(conn, sql);
+
+ talloc_free(sql);
+
+ return result;
+}
+
void db_error_func(dbi_conn conn, void *data)
{
const char *msg;
@@ -200,7 +230,7 @@ static int update_db_revision_2(void)
{
dbi_result result;
- result = dbi_conn_query(conn,
+ result = query(conn,
"ALTER TABLE Subscriber "
"ADD COLUMN expire_lu "
"TIMESTAMP DEFAULT NULL");
@@ -211,7 +241,7 @@ static int update_db_revision_2(void)
}
dbi_result_free(result);
- result = dbi_conn_query(conn,
+ result = query(conn,
"UPDATE Meta "
"SET value = '3' "
"WHERE key = 'revision'");
@@ -252,7 +282,7 @@ static struct gsm_sms *sms_from_result_v3(dbi_result result)
snprintf(buf, sizeof(buf), "%llu", sender_id);
dbi_conn_quote_string_copy(conn, buf, &quoted);
- result2 = dbi_conn_queryf(conn,
+ result2 = queryf(conn,
"SELECT extension FROM Subscriber "
"WHERE id = %s ", quoted);
free(quoted);
@@ -292,7 +322,7 @@ static int update_db_revision_3(void)
LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
- result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
+ result = query(conn, "BEGIN EXCLUSIVE TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to begin transaction (upgrade from rev 3)\n");
@@ -301,7 +331,7 @@ static int update_db_revision_3(void)
dbi_result_free(result);
/* Rename old SMS table to be able create a new one */
- result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
+ result = query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to rename the old SMS table (upgrade from rev 3).\n");
@@ -310,7 +340,7 @@ static int update_db_revision_3(void)
dbi_result_free(result);
/* Create new SMS table with all the bells and whistles! */
- result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
+ result = query(conn, create_stmts[SCHEMA_SMS]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create a new SMS table (upgrade from rev 3).\n");
@@ -319,7 +349,7 @@ static int update_db_revision_3(void)
dbi_result_free(result);
/* Cycle through old messages and convert them to the new format */
- result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
+ result = query(conn, "SELECT * FROM SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
@@ -338,7 +368,7 @@ static int update_db_revision_3(void)
dbi_result_free(result);
/* Remove the temporary table */
- result = dbi_conn_query(conn, "DROP TABLE SMS_3");
+ result = query(conn, "DROP TABLE SMS_3");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to drop the old SMS table (upgrade from rev 3).\n");
@@ -347,7 +377,7 @@ static int update_db_revision_3(void)
dbi_result_free(result);
/* We're done. Bump DB Meta revision to 4 */
- result = dbi_conn_query(conn,
+ result = query(conn,
"UPDATE Meta "
"SET value = '4' "
"WHERE key = 'revision'");
@@ -358,7 +388,7 @@ static int update_db_revision_3(void)
}
dbi_result_free(result);
- result = dbi_conn_query(conn, "COMMIT TRANSACTION");
+ result = query(conn, "COMMIT TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to commit the transaction (upgrade from rev 3)\n");
@@ -368,7 +398,7 @@ static int update_db_revision_3(void)
}
/* Shrink DB file size by actually wiping out SMS_3 table data */
- result = dbi_conn_query(conn, "VACUUM");
+ result = query(conn, "VACUUM");
if (!result)
LOGP(DDB, LOGL_ERROR,
"VACUUM failed. Ignoring it (upgrade from rev 3).\n");
@@ -378,7 +408,7 @@ static int update_db_revision_3(void)
return 0;
rollback:
- result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
+ result = query(conn, "ROLLBACK TRANSACTION");
if (!result)
LOGP(DDB, LOGL_ERROR,
"Rollback failed (upgrade from rev 3).\n");
@@ -438,7 +468,7 @@ static int update_db_revision_4(void)
LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 4\n");
- result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
+ result = query(conn, "BEGIN EXCLUSIVE TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to begin transaction (upgrade from rev 4)\n");
@@ -447,7 +477,7 @@ static int update_db_revision_4(void)
dbi_result_free(result);
/* Rename old SMS table to be able create a new one */
- result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_4");
+ result = query(conn, "ALTER TABLE SMS RENAME TO SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to rename the old SMS table (upgrade from rev 4).\n");
@@ -456,7 +486,7 @@ static int update_db_revision_4(void)
dbi_result_free(result);
/* Create new SMS table with all the bells and whistles! */
- result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
+ result = query(conn, create_stmts[SCHEMA_SMS]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create a new SMS table (upgrade from rev 4).\n");
@@ -465,7 +495,7 @@ static int update_db_revision_4(void)
dbi_result_free(result);
/* Cycle through old messages and convert them to the new format */
- result = dbi_conn_query(conn, "SELECT * FROM SMS_4");
+ result = query(conn, "SELECT * FROM SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed fetch messages from the old SMS table (upgrade from rev 4).\n");
@@ -484,7 +514,7 @@ static int update_db_revision_4(void)
dbi_result_free(result);
/* Remove the temporary table */
- result = dbi_conn_query(conn, "DROP TABLE SMS_4");
+ result = query(conn, "DROP TABLE SMS_4");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to drop the old SMS table (upgrade from rev 4).\n");
@@ -493,7 +523,7 @@ static int update_db_revision_4(void)
dbi_result_free(result);
/* We're done. Bump DB Meta revision to 4 */
- result = dbi_conn_query(conn,
+ result = query(conn,
"UPDATE Meta "
"SET value = '5' "
"WHERE key = 'revision'");
@@ -504,7 +534,7 @@ static int update_db_revision_4(void)
}
dbi_result_free(result);
- result = dbi_conn_query(conn, "COMMIT TRANSACTION");
+ result = query(conn, "COMMIT TRANSACTION");
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to commit the transaction (upgrade from rev 4)\n");
@@ -514,7 +544,7 @@ static int update_db_revision_4(void)
}
/* Shrink DB file size by actually wiping out SMS_4 table data */
- result = dbi_conn_query(conn, "VACUUM");
+ result = query(conn, "VACUUM");
if (!result)
LOGP(DDB, LOGL_ERROR,
"VACUUM failed. Ignoring it (upgrade from rev 4).\n");
@@ -524,7 +554,7 @@ static int update_db_revision_4(void)
return 0;
rollback:
- result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
+ result = query(conn, "ROLLBACK TRANSACTION");
if (!result)
LOGP(DDB, LOGL_ERROR,
"Rollback failed (upgrade from rev 4).\n");
@@ -540,7 +570,7 @@ static int check_db_revision(void)
int db_rev = 0;
/* Make a query */
- result = dbi_conn_query(conn,
+ result = query(conn,
"SELECT value FROM Meta "
"WHERE key = 'revision'");
@@ -600,7 +630,7 @@ static int db_configure(void)
{
dbi_result result;
- result = dbi_conn_query(conn,
+ result = query(conn,
"PRAGMA synchronous = FULL");
if (!result)
return -EINVAL;
@@ -654,7 +684,7 @@ int db_prepare(void)
int i;
for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
- result = dbi_conn_query(conn, create_stmts[i]);
+ result = query(conn, create_stmts[i]);
if (!result) {
LOGP(DDB, LOGL_ERROR,
"Failed to create some table.\n");
@@ -701,7 +731,7 @@ int db_sms_store(struct gsm_sms *sms)
&q_udata);
/* FIXME: correct validity period */
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"INSERT INTO SMS "
"(created, valid_until, "
"reply_path_req, status_rep_req, is_report, "
@@ -789,7 +819,7 @@ struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
dbi_result result;
struct gsm_sms *sms;
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"SELECT * FROM SMS WHERE SMS.id = %llu", id);
if (!result)
return NULL;
@@ -813,7 +843,7 @@ struct gsm_sms *db_sms_get_next_unsent(struct gsm_network *net,
dbi_result result;
struct gsm_sms *sms;
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"SELECT * FROM SMS"
" WHERE sent IS NULL"
" AND id >= %llu"
@@ -853,7 +883,7 @@ struct gsm_sms *db_sms_get_unsent_for_subscr(struct vlr_subscr *vsub,
return NULL;
dbi_conn_quote_string_copy(conn, vsub->msisdn, &q_msisdn);
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"SELECT * FROM SMS"
" WHERE sent IS NULL"
" AND dest_addr = %s"
@@ -886,7 +916,7 @@ struct gsm_sms *db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
char *q_last_msisdn;
dbi_conn_quote_string_copy(conn, last_msisdn, &q_last_msisdn);
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"SELECT * FROM SMS"
" WHERE sent IS NULL"
" AND dest_addr > %s"
@@ -915,7 +945,7 @@ int db_sms_mark_delivered(struct gsm_sms *sms)
{
dbi_result result;
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"UPDATE SMS "
"SET sent = datetime('now') "
"WHERE id = %llu", sms->id);
@@ -933,7 +963,7 @@ int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
{
dbi_result result;
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"UPDATE SMS "
"SET deliver_attempts = deliver_attempts + 1 "
"WHERE id = %llu", sms->id);
@@ -956,7 +986,7 @@ int db_sms_delete_by_msisdn(const char *msisdn)
return 0;
dbi_conn_quote_string_copy(conn, msisdn, &q_msisdn);
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
q_msisdn, q_msisdn);
free(q_msisdn);
@@ -977,7 +1007,7 @@ int db_store_counter(struct osmo_counter *ctr)
dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"INSERT INTO Counters "
"(timestamp,name,value) VALUES "
"(datetime('now'),%s,%lu)", q_name, ctr->value);
@@ -1000,7 +1030,7 @@ static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
&q_name);
- result = dbi_conn_queryf(conn,
+ result = queryf(conn,
"Insert INTO RateCounters "
"(timestamp,name,idx,value) VALUES "
"(datetime('now'),%s.%s,%u,%"PRIu64")",