From dd783056f7ede461dd27347fa86743a24c4c4eed Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 9 Oct 2017 17:36:08 +0200 Subject: refactor db_subscr_lu() Use named parameters in the SQL statement. Use db_bind_* functions to drop some code dup. Use explicit subscriber id arg instead of subscriber struct. Match return values and error logging to other db functions. Change-Id: I35665e84ddbe54a6f218b24033df969ad2e669a0 --- src/db.c | 4 ++-- src/db.h | 6 ++---- src/db_hlr.c | 54 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 32 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/db.c b/src/db.c index 8286ba8..eb29434 100644 --- a/src/db.c +++ b/src/db.c @@ -45,8 +45,8 @@ static const char *stmt_sql[] = { [DB_STMT_SEL_BY_IMSI] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE imsi = ?", [DB_STMT_SEL_BY_MSISDN] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE msisdn = ?", [DB_STMT_SEL_BY_ID] = "SELECT " SEL_COLUMNS " FROM subscriber WHERE id = ?", - [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?", - [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?", + [DB_STMT_UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = $number WHERE id = $subscriber_id", + [DB_STMT_UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = $number WHERE id = $subscriber_id", [DB_STMT_AUC_BY_IMSI] = "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn, ind_bitlen" " FROM subscriber" diff --git a/src/db.h b/src/db.h index 5e234ff..9d4274d 100644 --- a/src/db.h +++ b/src/db.h @@ -91,10 +91,8 @@ int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn, int db_subscr_get_by_id(struct db_context *dbc, int64_t id, struct hlr_subscriber *subscr); int db_subscr_nam(struct db_context *dbc, const char *imsi, bool nam_val, bool is_ps); -int db_subscr_lu(struct db_context *dbc, - const struct hlr_subscriber *subscr, - const char *vlr_or_sgsn_number, - bool lu_is_ps); +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); diff --git a/src/db_hlr.c b/src/db_hlr.c index 9bdb372..ac3c730 100644 --- a/src/db_hlr.c +++ b/src/db_hlr.c @@ -313,44 +313,46 @@ out: return ret; } -int db_subscr_lu(struct db_context *dbc, - const struct hlr_subscriber *subscr, - const char *vlr_or_sgsn_number, bool lu_is_ps) +int db_subscr_lu(struct db_context *dbc, int64_t subscr_id, + const char *vlr_or_sgsn_number, bool is_ps) { - sqlite3_stmt *stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; - const char *txt; + sqlite3_stmt *stmt; int rc, ret = 0; - if (lu_is_ps) { - stmt = dbc->stmt[DB_STMT_UPD_SGSN_BY_ID]; - txt = subscr->sgsn_number; - } else { - stmt = dbc->stmt[DB_STMT_UPD_VLR_BY_ID]; - txt = subscr->vlr_number; - } + stmt = dbc->stmt[is_ps ? DB_STMT_UPD_SGSN_BY_ID + : DB_STMT_UPD_VLR_BY_ID]; - rc = sqlite3_bind_int64(stmt, 1, subscr->id); - if (rc != SQLITE_OK) { - LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc); - return -EINVAL; - } + if (!db_bind_int64(stmt, "$subscriber_id", subscr_id)) + return -EIO; - rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC); - if (rc != SQLITE_OK) { - LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc); - ret = -EBADMSG; - goto out; - } + if (!db_bind_text(stmt, "$number", vlr_or_sgsn_number)) + return -EIO; /* execute the statement */ rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { - LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc); - ret = -ENOEXEC; + LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64": SQL Error: %s\n", + is_ps? "SGSN" : "VLR", subscr_id, sqlite3_errmsg(dbc->db)); + ret = -EIO; + goto out; } + + /* verify execution result */ + rc = sqlite3_changes(dbc->db); + if (!rc) { + LOGP(DAUC, LOGL_ERROR, "Cannot update %s number for subscriber ID=%"PRId64 + ": no such subscriber\n", + is_ps? "SGSN" : "VLR", subscr_id); + ret = -ENOENT; + } else if (rc != 1) { + LOGP(DAUC, LOGL_ERROR, "Update %s number for subscriber ID=%"PRId64 + ": SQL modified %d rows (expected 1)\n", + is_ps? "SGSN" : "VLR", subscr_id, rc); + ret = -EIO; + } + out: db_remove_reset(stmt); - return ret; } -- cgit v1.2.3