add db_subscr_get_by_msisdn() and db_subscr_get_by_id()
Factor out the selected SQL columns as SEL_COLUMNS macro, so that each of the
new DB_STMTs will select identical columns: the old DB_STMT_SEL_BY_IMSI as well
as the new DB_STMT_SEL_BY_MSISDN and DB_STMT_SEL_BY_ID.
Add the new functions db_subscr_get_by_msisdn() and db_subscr_get_by_id() and
factor out common parts with db_subscr_get_by_imsi() to static db_sel().
Change-Id: I6d0ddd1b7e3f6b180b4b1b2663c5725d2a4a9428
diff --git a/src/db.c b/src/db.c
index 179eba8..5a38d55 100644
--- a/src/db.c
+++ b/src/db.c
@@ -26,8 +26,25 @@
#include "logging.h"
#include "db.h"
+#define SEL_COLUMNS \
+ "id," \
+ "imsi," \
+ "msisdn," \
+ "vlr_number," \
+ "sgsn_number," \
+ "sgsn_address," \
+ "periodic_lu_tmr," \
+ "periodic_rau_tau_tmr," \
+ "nam_cs," \
+ "nam_ps," \
+ "lmsi," \
+ "ms_purged_cs," \
+ "ms_purged_ps"
+
static const char *stmt_sql[] = {
- [DB_STMT_SEL_BY_IMSI] = "SELECT id,imsi,msisdn,vlr_number,sgsn_number,sgsn_address,periodic_lu_tmr,periodic_rau_tau_tmr,nam_cs,nam_ps,lmsi,ms_purged_cs,ms_purged_ps FROM subscriber WHERE imsi = ?",
+ [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_AUC_BY_IMSI] =
diff --git a/src/db.h b/src/db.h
index 761d88e..5ec7b72 100644
--- a/src/db.h
+++ b/src/db.h
@@ -5,6 +5,8 @@
enum stmt_idx {
DB_STMT_SEL_BY_IMSI,
+ DB_STMT_SEL_BY_MSISDN,
+ DB_STMT_SEL_BY_ID,
DB_STMT_UPD_VLR_BY_ID,
DB_STMT_UPD_SGSN_BY_ID,
DB_STMT_AUC_BY_IMSI,
@@ -84,6 +86,10 @@
int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
struct hlr_subscriber *subscr);
+int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
+ struct hlr_subscriber *subscr);
+int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
+ struct hlr_subscriber *subscr);
int db_subscr_ps(struct db_context *dbc, const char *imsi, bool enable);
int db_subscr_lu(struct db_context *dbc,
const struct hlr_subscriber *subscr,
diff --git a/src/db_hlr.c b/src/db_hlr.c
index b232dfd..8df4bcb 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -154,27 +154,26 @@
}
-int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
- struct hlr_subscriber *subscr)
+/* Common code for db_subscr_get_by_*() functions. */
+static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscriber *subscr,
+ const char **err)
{
- sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
int rc;
-
- if (!db_bind_text(stmt, NULL, imsi))
- return -EINVAL;
+ int ret = 0;
/* execute the statement */
rc = sqlite3_step(stmt);
+ if (rc == SQLITE_DONE) {
+ ret = -ENOENT;
+ goto out;
+ }
if (rc != SQLITE_ROW) {
- LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
- db_remove_reset(stmt);
- return -ENOEXEC;
+ ret = -EIO;
+ goto out;
}
- if (!subscr) {
- db_remove_reset(stmt);
- return 0;
- }
+ if (!subscr)
+ goto out;
/* obtain the various columns */
subscr->id = sqlite3_column_int64(stmt, 0);
@@ -192,9 +191,72 @@
subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
+out:
db_remove_reset(stmt);
- return 0;
+ switch (ret) {
+ case 0:
+ *err = NULL;
+ break;
+ case -ENOENT:
+ *err = "No such subscriber";
+ break;
+ default:
+ *err = sqlite3_errmsg(dbc->db);
+ break;
+ }
+ return ret;
+}
+
+int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi,
+ struct hlr_subscriber *subscr)
+{
+ sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
+ const char *err;
+ int rc;
+
+ if (!db_bind_text(stmt, NULL, imsi))
+ return -EIO;
+
+ rc = db_sel(dbc, stmt, subscr, &err);
+ if (rc)
+ LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: IMSI='%s': %s\n",
+ imsi, err);
+ return rc;
+}
+
+int db_subscr_get_by_msisdn(struct db_context *dbc, const char *msisdn,
+ struct hlr_subscriber *subscr)
+{
+ sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_MSISDN];
+ const char *err;
+ int rc;
+
+ if (!db_bind_text(stmt, NULL, msisdn))
+ return -EIO;
+
+ rc = db_sel(dbc, stmt, subscr, &err);
+ if (rc)
+ LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: MSISDN='%s': %s\n",
+ msisdn, err);
+ return rc;
+}
+
+int db_subscr_get_by_id(struct db_context *dbc, int64_t id,
+ struct hlr_subscriber *subscr)
+{
+ sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_ID];
+ const char *err;
+ int rc;
+
+ if (!db_bind_int64(stmt, NULL, id))
+ return -EIO;
+
+ rc = db_sel(dbc, stmt, subscr, &err);
+ if (rc)
+ LOGP(DAUC, LOGL_ERROR, "Cannot read subscriber from db: ID=%"PRId64": %s\n",
+ id, err);
+ return rc;
}
int db_subscr_ps(struct db_context *dbc, const char *imsi, bool enable)