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
This commit is contained in:
Max 2017-02-20 11:09:27 +01:00
parent ea8b0d46eb
commit 00b3715723
3 changed files with 47 additions and 46 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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;
}