summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax <msuraev@sysmocom.de>2017-02-20 11:09:27 +0100
committerMax <msuraev@sysmocom.de>2017-02-20 13:37:08 +0100
commit00b37157231b98cfb9e940c0f488320f42d3eca8 (patch)
tree9da863cede8f53799be46d2a913f65cf8714a46a /src
parentea8b0d46ebab1ed5bf7df024a69f349249296738 (diff)
db: move duplicated code into helper functions
* move common cleanup code into separate function * add helper function for IMSI binding * use errno.h instead of numbers Change-Id: Iec81b56ab1ccc948807854a3947b04355a555c10
Diffstat (limited to 'src')
-rw-r--r--src/db.c31
-rw-r--r--src/db.h2
-rw-r--r--src/db_hlr.c60
3 files changed, 47 insertions, 46 deletions
diff --git a/src/db.c b/src/db.c
index 1385502..92983f9 100644
--- a/src/db.c
+++ b/src/db.c
@@ -19,6 +19,7 @@
#include <osmocom/core/utils.h>
+#include <stdbool.h>
#include <sqlite3.h>
#include "logging.h"
@@ -57,6 +58,36 @@ static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
}
}
+/* remove bindings and reset statement to be re-executed */
+bool db_remove_reset(sqlite3_stmt *stmt)
+{
+ int rc = sqlite3_clear_bindings(stmt);
+ if (rc != SQLITE_OK) {
+ LOGP(DDB, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
+ return false;
+ }
+
+ rc = sqlite3_reset(stmt);
+ if (rc != SQLITE_OK) {
+ LOGP(DDB, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
+ return false;
+ }
+ return true;
+}
+
+/* bind IMSI and do proper cleanup in case of failure */
+bool db_bind_imsi(sqlite3_stmt *stmt, const char *imsi)
+{
+ int rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
+ if (rc != SQLITE_OK) {
+ LOGP(DDB, LOGL_ERROR, "Error binding IMSI %s: %d\n", imsi, rc);
+ db_remove_reset(stmt);
+ return false;
+ }
+
+ return true;
+}
+
void db_close(struct db_context *dbc)
{
unsigned int i;
diff --git a/src/db.h b/src/db.h
index d569fb0..0fb76a0 100644
--- a/src/db.h
+++ b/src/db.h
@@ -20,6 +20,8 @@ struct db_context {
sqlite3_stmt *stmt[_NUM_STMT];
};
+bool db_remove_reset(sqlite3_stmt *stmt);
+bool db_bind_imsi(sqlite3_stmt *stmt, const char *imsi);
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 1b95556..b7485f0 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -18,6 +18,7 @@
*/
#include <string.h>
+#include <errno.h>
#include <osmocom/core/utils.h>
#include <osmocom/crypt/auth.h>
@@ -42,17 +43,14 @@ int db_subscr_get(struct db_context *dbc, const char *imsi,
sqlite3_stmt *stmt = dbc->stmt[SEL_BY_IMSI];
int rc, ret = 0;
- rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
- if (rc != SQLITE_OK) {
- LOGHLR(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc);
- return -1;
- }
+ if (!db_bind_imsi(stmt, imsi))
+ return -EINVAL;
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) {
LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
- ret = -2;
+ ret = -ENOEXEC;
goto out;
}
@@ -73,15 +71,7 @@ int db_subscr_get(struct db_context *dbc, const char *imsi,
subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
out:
- /* remove bindings and reset statement to be re-executed */
- rc = sqlite3_clear_bindings(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
- }
- rc = sqlite3_reset(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
- }
+ db_remove_reset(stmt);
return ret;
}
@@ -105,13 +95,13 @@ int db_subscr_lu(struct db_context *dbc,
rc = sqlite3_bind_int64(stmt, 1, subscr->id);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc);
- return -1;
+ return -EINVAL;
}
rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc);
- ret = -2;
+ ret = -EBADMSG;
goto out;
}
@@ -119,19 +109,10 @@ int db_subscr_lu(struct db_context *dbc,
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
- ret = -3;
- goto out;
+ ret = -ENOEXEC;
}
out:
- /* remove bindings and reset statement to be re-executed */
- rc = sqlite3_clear_bindings(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
- }
- rc = sqlite3_reset(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
- }
+ db_remove_reset(stmt);
return ret;
}
@@ -146,31 +127,18 @@ int db_subscr_purge(struct db_context *dbc, const char *imsi, bool is_ps)
else
stmt = dbc->stmt[UPD_PURGE_CS_BY_IMSI];
- rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error binding IMSI %s: %d\n", imsi, rc);
- ret = -1;
- goto out;
- }
+ if (!db_bind_imsi(stmt, imsi))
+ return -EINVAL;
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
LOGP(DAUC, LOGL_ERROR, "Error setting Purged: %d\n", rc);
- ret = -2;
- goto out;
+ ret = -ENOEXEC;
}
/* FIXME: return 0 in case IMSI not known */
-out:
- /* remove bindings and reset statement to be re-executed */
- rc = sqlite3_clear_bindings(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error clearing bindings: %d\n", rc);
- }
- rc = sqlite3_reset(stmt);
- if (rc != SQLITE_OK) {
- LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
- }
+
+ db_remove_reset(stmt);
return ret;
}