[PATCH] osmo-hlr[master]: cosmetic: refactor db_bind_imsi() as db_bind_text()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Tue Oct 10 01:31:57 UTC 2017


Review at  https://gerrit.osmocom.org/4172

cosmetic: refactor db_bind_imsi() as db_bind_text()

There are more uses for a generalized db_bind_text(), and in an upcoming patch
there will be similar functions like db_bind_int().

Also, add argument param_name, optionally indicating a named SQL parameter to
bind to, which will be used in subsequent patches. So far, all callers pass
NULL to yield previous db_bind_imsi() behavior of binding to the first param.

Change-Id: I87bc46a23a724677e8319d6a4b032976b7ba9394
---
M src/db.c
M src/db.h
M src/db_hlr.c
3 files changed, 18 insertions(+), 9 deletions(-)


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

diff --git a/src/db.c b/src/db.c
index 6566527..4bba2f0 100644
--- a/src/db.c
+++ b/src/db.c
@@ -77,16 +77,25 @@
 	return true;
 }
 
-/* bind IMSI and do proper cleanup in case of failure */
-bool db_bind_imsi(sqlite3_stmt *stmt, const char *imsi)
+/** bind text arg and do proper cleanup in case of failure. If param_name is
+ * NULL, bind to the first parameter (useful for SQL statements that have only
+ * one parameter). */
+bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
 {
-	int rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
+	int rc;
+	int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
+	if (idx < 1) {
+		LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
+		     param_name);
+		return false;
+	}
+	rc = sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
 	if (rc != SQLITE_OK) {
-		LOGP(DDB, LOGL_ERROR, "Error binding IMSI %s: %d\n", imsi, rc);
+		LOGP(DDB, LOGL_ERROR, "Error binding text to SQL parameter %s: %d\n",
+		     param_name ? param_name : "#1", rc);
 		db_remove_reset(stmt);
 		return false;
 	}
-
 	return true;
 }
 
diff --git a/src/db.h b/src/db.h
index 0064a4d..6d6723a 100644
--- a/src/db.h
+++ b/src/db.h
@@ -23,7 +23,7 @@
 };
 
 bool db_remove_reset(sqlite3_stmt *stmt);
-bool db_bind_imsi(sqlite3_stmt *stmt, const char *imsi);
+bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
 void db_close(struct db_context *dbc);
 struct db_context *db_open(void *ctx, const char *fname);
 
diff --git a/src/db_hlr.c b/src/db_hlr.c
index fa962f3..3bf3912 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -43,7 +43,7 @@
 	sqlite3_stmt *stmt = dbc->stmt[DB_STMT_SEL_BY_IMSI];
 	int rc;
 
-	if (!db_bind_imsi(stmt, imsi))
+	if (!db_bind_text(stmt, NULL, imsi))
 		return -EINVAL;
 
 	/* execute the statement */
@@ -86,7 +86,7 @@
 		dbc->stmt[enable ? DB_STMT_SET_NAM_PS_BY_IMSI : DB_STMT_UNSET_NAM_PS_BY_IMSI];
 	int rc;
 
-	if (!db_bind_imsi(stmt, imsi))
+	if (!db_bind_text(stmt, NULL, imsi))
 		return -EINVAL;
 
 	rc = sqlite3_step(stmt); /* execute the statement */
@@ -158,7 +158,7 @@
 	else
 		stmt = dbc->stmt[DB_STMT_UPD_PURGE_CS_BY_IMSI];
 
-	if (!db_bind_imsi(stmt, imsi))
+	if (!db_bind_text(stmt, NULL, imsi))
 		return -EINVAL;
 
 	/* execute the statement */

-- 
To view, visit https://gerrit.osmocom.org/4172
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I87bc46a23a724677e8319d6a4b032976b7ba9394
Gerrit-PatchSet: 1
Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list