Change in osmo-hlr[master]: db_hlr.c: add db_subscr_exists_by_imsi()

Vadim Yanitskiy gerrit-no-reply at lists.osmocom.org
Sat Apr 20 09:35:16 UTC 2019


Vadim Yanitskiy has uploaded this change for review. ( https://gerrit.osmocom.org/13712


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

db_hlr.c: add db_subscr_exists_by_imsi()

Check if a subscriber exists without generating an error log entry if
it does not. This is cheaper than db_subscr_get_by_imsi(), as it does
not fetch the subscriber entry. subscriber-create-on-demand will use
this function.

Related: OS#2542
Change-Id: I63818c0dd4fd22b41dadeeba2a07a651b5454c54
---
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, 40 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/12/13712/1

diff --git a/src/db.c b/src/db.c
index 770c3a4..9cad263 100644
--- a/src/db.c
+++ b/src/db.c
@@ -79,6 +79,7 @@
 		" VALUES($subscriber_id, $algo_id_3g, $k, $op, $opc, $ind_bitlen)",
 	[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",
 };
 
 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 7187bb3..1ebdaeb 100644
--- a/src/db.h
+++ b/src/db.h
@@ -28,6 +28,7 @@
 	DB_STMT_AUC_3G_INSERT,
 	DB_STMT_AUC_3G_DELETE,
 	DB_STMT_SET_LAST_LU_SEEN,
+	DB_STMT_EXISTS_BY_IMSI,
 	_NUM_DB_STMT
 };
 
@@ -127,6 +128,8 @@
 			       const struct sub_auth_data_str *aud);
 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_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,
diff --git a/src/db_hlr.c b/src/db_hlr.c
index e879ae8..10f6287 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -512,6 +512,31 @@
 	return ret;
 }
 
+/*! Check if a subscriber exists in the HLR database.
+ * \param[in, out] dbc  database context.
+ * \param[in] imsi  ASCII string of IMSI digits.
+ * \returns 0 if it exists, -ENOENT if it does not exist, -EIO on database error.
+ */
+int db_subscr_exists_by_imsi(struct db_context *dbc, const char *imsi) {
+	sqlite3_stmt *stmt = dbc->stmt[DB_STMT_EXISTS_BY_IMSI];
+	const char *err;
+	int rc;
+
+	if (!db_bind_text(stmt, NULL, imsi))
+		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 IMSI='%s': %s\n", imsi, err);
+	return rc;
+}
+
 /*! Retrieve subscriber data from the HLR database.
  * \param[in,out] dbc  database context.
  * \param[in] imsi  ASCII string of IMSI digits.
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index 0ec366b..2bd7819 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -255,6 +255,10 @@
 	ASSERT_SEL(imsi, short_imsi, 0);
 	id_short = g_subscr.id;
 
+	comment("Check if subscriber exists");
+
+	ASSERT_RC(db_subscr_exists_by_imsi(dbc, imsi0), 0);
+	ASSERT_RC(db_subscr_exists_by_imsi(dbc, unknown_imsi), -ENOENT);
 
 	comment("Set valid / invalid MSISDN");
 
diff --git a/tests/db/db_test.err b/tests/db/db_test.err
index bfab345..39f3e54 100644
--- a/tests/db/db_test.err
+++ b/tests/db/db_test.err
@@ -93,6 +93,13 @@
 }
 
 
+--- Check if subscriber exists
+
+db_subscr_exists_by_imsi(dbc, imsi0) --> 0
+
+db_subscr_exists_by_imsi(dbc, unknown_imsi) --> -ENOENT
+
+
 --- Set valid / invalid MSISDN
 
 db_subscr_get_by_imsi(dbc, imsi0, &g_subscr) --> 0

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

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I63818c0dd4fd22b41dadeeba2a07a651b5454c54
Gerrit-Change-Number: 13712
Gerrit-PatchSet: 1
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190420/7e12c09e/attachment.html>


More information about the gerrit-log mailing list