Harald Welte has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/13719 )

Change subject: db_hlr.c: add db_subscr_exists_by_msisdn()
......................................................................

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
---
M src/db.c
M src/db.h
M src/db_hlr.c
M tests/db/db_test.c
M tests/db/db_test.err
5 files changed, 42 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved
  Vadim Yanitskiy: Looks good to me, but someone else must approve



diff --git a/src/db.c b/src/db.c
index 9cad263..7de61a2 100644
--- a/src/db.c
+++ b/src/db.c
@@ -80,6 +80,7 @@
        [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)
diff --git a/src/db.h b/src/db.h
index 8543085..12e74f8 100644
--- a/src/db.h
+++ b/src/db.h
@@ -29,6 +29,7 @@
        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_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 @@
        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.
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index 217a8c5..fdd62c5 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -294,6 +294,11 @@
        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);
diff --git a/tests/db/db_test.err b/tests/db/db_test.err
index 0701089..4dc77e8 100644
--- a/tests/db/db_test.err
+++ b/tests/db/db_test.err
@@ -219,6 +219,13 @@
 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

--
To view, visit https://gerrit.osmocom.org/13719
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ibfbc408c966197682ba2b12d166ade4bfeb7abc2
Gerrit-Change-Number: 13719
Gerrit-PatchSet: 5
Gerrit-Owner: Vadim Yanitskiy <[email protected]>
Gerrit-Reviewer: Harald Welte <[email protected]>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Vadim Yanitskiy <[email protected]>
Gerrit-Reviewer: osmith <[email protected]>

Reply via email to