From e50121ec96c2457c66501d7b6e1fcd539ee3c5e1 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 9 Oct 2017 17:48:51 +0200 Subject: refactor db_subscr_purge Use named parameters in the SQL statements. Use db_bind_* functions to drop some code dup. Adopt error handling (rc and logging) to match the other db functions: return -ENOENT for unknown subscriber, -EIO for SQL failures. Change-Id: Iad49d29b90a708c6cf55bfb3bcc02d9e29001a15 --- src/db.c | 4 ++-- src/db.h | 4 ++-- src/db_hlr.c | 46 ++++++++++++++++++++++++++++++++++------------ src/hlr.c | 2 +- 4 files changed, 39 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/db.c b/src/db.c index eb29434..2b2c2c4 100644 --- a/src/db.c +++ b/src/db.c @@ -54,8 +54,8 @@ static const char *stmt_sql[] = { " LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id" " WHERE imsi = ?", [DB_STMT_AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?", - [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?", - [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?", + [DB_STMT_UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs = $val WHERE imsi = $imsi", + [DB_STMT_UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps = $val WHERE imsi = $imsi", [DB_STMT_UPD_NAM_CS_BY_IMSI] = "UPDATE subscriber SET nam_cs = $val WHERE imsi = $imsi", [DB_STMT_UPD_NAM_PS_BY_IMSI] = "UPDATE subscriber SET nam_ps = $val WHERE imsi = $imsi", [DB_STMT_SUBSCR_CREATE] = "INSERT INTO subscriber (imsi) VALUES ($imsi)", diff --git a/src/db.h b/src/db.h index 9d4274d..2e6cc9b 100644 --- a/src/db.h +++ b/src/db.h @@ -94,5 +94,5 @@ int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool i int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, const char *vlr_or_sgsn_number, bool is_ps); -int db_subscr_purge(struct db_context *dbc, - const char *imsi, bool is_ps); +int db_subscr_purge(struct db_context *dbc, const char *by_imsi, + bool purge_val, bool is_ps); diff --git a/src/db_hlr.c b/src/db_hlr.c index ac3c730..b6d224b 100644 --- a/src/db_hlr.c +++ b/src/db_hlr.c @@ -356,27 +356,49 @@ out: return ret; } -int db_subscr_purge(struct db_context *dbc, const char *imsi, bool is_ps) +int db_subscr_purge(struct db_context *dbc, const char *by_imsi, + bool purge_val, bool is_ps) { - sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; - int rc, ret = 1; + sqlite3_stmt *stmt; + int rc, ret = 0; - if (is_ps) - stmt = dbc->stmt[DB_STMT_UPD_PURGE_PS_BY_IMSI]; - else - stmt = dbc->stmt[DB_STMT_UPD_PURGE_CS_BY_IMSI]; + stmt = dbc->stmt[is_ps ? DB_STMT_UPD_PURGE_PS_BY_IMSI + : DB_STMT_UPD_PURGE_CS_BY_IMSI]; - if (!db_bind_text(stmt, NULL, imsi)) - return -EINVAL; + if (!db_bind_text(stmt, "$imsi", by_imsi)) + return -EIO; + if (!db_bind_int(stmt, "$val", purge_val ? 1 : 0)) + return -EIO; /* execute the statement */ rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { - LOGP(DAUC, LOGL_ERROR, "Error setting Purged: %d\n", rc); - ret = -ENOEXEC; + LOGP(DAUC, LOGL_ERROR, "%s %s: SQL error: %s\n", + purge_val ? "purge" : "un-purge", + is_ps ? "PS" : "CS", + sqlite3_errmsg(dbc->db)); + ret = -EIO; + goto out; } - /* FIXME: return 0 in case IMSI not known */ + /* verify execution result */ + rc = sqlite3_changes(dbc->db); + if (!rc) { + LOGP(DAUC, LOGL_ERROR, "Cannot %s %s: no such subscriber: IMSI='%s'\n", + purge_val ? "purge" : "un-purge", + is_ps ? "PS" : "CS", + by_imsi); + ret = -ENOENT; + goto out; + } else if (rc != 1) { + LOGHLR(by_imsi, LOGL_ERROR, "%s %s: SQL modified %d rows (expected 1)\n", + purge_val ? "purge" : "un-purge", + is_ps ? "PS" : "CS", + rc); + ret = -EIO; + } + +out: db_remove_reset(stmt); return ret; diff --git a/src/hlr.c b/src/hlr.c index 9e8b699..b32f709 100644 --- a/src/hlr.c +++ b/src/hlr.c @@ -221,7 +221,7 @@ static int rx_purge_ms_req(struct osmo_gsup_conn *conn, * we have on record. Only update if yes */ /* Perform the actual update of the DB */ - rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, is_ps); + rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps); if (rc == 1) gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; -- cgit v1.2.3