diff options
author | Vadim Yanitskiy <axilirator@gmail.com> | 2019-03-30 17:03:42 +0700 |
---|---|---|
committer | Oliver Smith <osmith@sysmocom.de> | 2019-05-13 08:55:24 +0200 |
commit | c13599dc696fcab64219eba99afd0a2da9d0eec5 (patch) | |
tree | 9c8ac1ff6ecc794cc7dc0e82a71e2ca90738099e /src | |
parent | 6b73fd9678b5b4570edc3722fda467a82b3c7ccb (diff) |
db_hlr.c: add db_subscr_exists_by_msisdn()
Check if a subscriber exists without generating an error log entry if
it does not. This is cheaper than db_subscr_get_by_msisdn(), as it
does not fetch the subscriber entry.
subscriber-create-on-demand will use this function to generate
a random unique MSISDN for new subscribers.
Related: OS#2542
Change-Id: Ibfbc408c966197682ba2b12d166ade4bfeb7abc2
Diffstat (limited to 'src')
-rw-r--r-- | src/db.c | 1 | ||||
-rw-r--r-- | src/db.h | 2 | ||||
-rw-r--r-- | src/db_hlr.c | 27 |
3 files changed, 30 insertions, 0 deletions
@@ -80,6 +80,7 @@ static const char *stmt_sql[] = { [DB_STMT_AUC_3G_DELETE] = "DELETE FROM auc_3g WHERE subscriber_id = $subscriber_id", [DB_STMT_SET_LAST_LU_SEEN] = "UPDATE subscriber SET last_lu_seen = datetime($val, 'unixepoch') WHERE id = $subscriber_id", [DB_STMT_EXISTS_BY_IMSI] = "SELECT 1 FROM subscriber WHERE imsi = $imsi", + [DB_STMT_EXISTS_BY_MSISDN] = "SELECT 1 FROM subscriber WHERE msisdn = $msisdn", }; static void sql3_error_log_cb(void *arg, int err_code, const char *msg) @@ -29,6 +29,7 @@ enum stmt_idx { DB_STMT_AUC_3G_DELETE, DB_STMT_SET_LAST_LU_SEEN, DB_STMT_EXISTS_BY_IMSI, + DB_STMT_EXISTS_BY_MSISDN, _NUM_DB_STMT }; @@ -132,6 +133,7 @@ int db_subscr_update_aud_by_id(struct db_context *dbc, int64_t subscr_id, int db_subscr_update_imei_by_imsi(struct db_context *dbc, const char* imsi, const char *imei); int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi); +int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn); int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, struct hlr_subscriber *subscr); diff --git a/src/db_hlr.c b/src/db_hlr.c index c59daf7..362dcf2 100644 --- a/src/db_hlr.c +++ b/src/db_hlr.c @@ -560,6 +560,33 @@ int db_subscr_get_by_imsi(struct db_context *dbc, const char *imsi, return rc; } +/*! Check if a subscriber exists in the HLR database. + * \param[in, out] dbc database context. + * \param[in] msisdn ASCII string of MSISDN digits. + * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error. + */ +int db_subscr_exists_by_msisdn(struct db_context *dbc, const char *msisdn) +{ + sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_MSISDN]; + const char *err; + int rc; + + if (!db_bind_text(stmt, NULL, msisdn)) + return -EIO; + + rc = sqlite3_step(stmt); + db_remove_reset(stmt); + if (rc == SQLITE_ROW) + return 0; /* exists */ + if (rc == SQLITE_DONE) + return -ENOENT; /* does not exist */ + + err = sqlite3_errmsg(dbc->db); + LOGP(DAUC, LOGL_ERROR, "Failed to check if subscriber exists " + "by MSISDN='%s': %s\n", msisdn, err); + return rc; +} + /*! Retrieve subscriber data from the HLR database. * \param[in,out] dbc database context. * \param[in] msisdn ASCII string of MSISDN digits. |