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
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 3cb42e4..8543085 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
};
@@ -130,6 +131,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 40209c5..c59daf7 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -511,6 +511,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.