mirror of
https://gitea.osmocom.org/cellular-infrastructure/osmo-hlr.git
synced 2025-11-01 20:53:46 +00:00
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
This commit is contained in:
committed by
Oliver Smith
parent
6b73fd9678
commit
c13599dc69
1
src/db.c
1
src/db.c
@@ -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)
|
||||
|
||||
2
src/db.h
2
src/db.h
@@ -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);
|
||||
|
||||
27
src/db_hlr.c
27
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.
|
||||
|
||||
@@ -294,6 +294,11 @@ static void test_subscr_create_update_sel_delete()
|
||||
ASSERT_SEL(imsi, imsi0, 0);
|
||||
ASSERT_SEL(msisdn, "5432101234567891", -ENOENT);
|
||||
|
||||
comment("Check if subscriber exists (by MSISDN)");
|
||||
|
||||
ASSERT_RC(db_subscr_exists_by_msisdn(dbc, "543210123456789"), 0);
|
||||
ASSERT_RC(db_subscr_exists_by_msisdn(dbc, "5432101234567891"), -ENOENT);
|
||||
|
||||
comment("Set MSISDN on non-existent / invalid IMSI");
|
||||
|
||||
ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, unknown_imsi, "99"), -ENOENT);
|
||||
|
||||
@@ -219,6 +219,13 @@ db_subscr_get_by_msisdn(dbc, "5432101234567891", &g_subscr) --> -ENOENT
|
||||
DAUC Cannot read subscriber from db: MSISDN='5432101234567891': No such subscriber
|
||||
|
||||
|
||||
--- Check if subscriber exists (by MSISDN)
|
||||
|
||||
db_subscr_exists_by_msisdn(dbc, "543210123456789") --> 0
|
||||
|
||||
db_subscr_exists_by_msisdn(dbc, "5432101234567891") --> -ENOENT
|
||||
|
||||
|
||||
--- Set MSISDN on non-existent / invalid IMSI
|
||||
|
||||
db_subscr_update_msisdn_by_imsi(dbc, unknown_imsi, "99") --> -ENOENT
|
||||
|
||||
Reference in New Issue
Block a user