diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 18 | ||||
-rw-r--r-- | src/db.c | 32 | ||||
-rw-r--r-- | src/db_bootstrap.sed | 25 |
3 files changed, 75 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index fc7c653..3b09b7b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,8 +10,14 @@ AM_CFLAGS = \ EXTRA_DIST = \ populate_hlr_db.pl \ + db_bootstrap.sed \ $(NULL) +BUILT_SOURCES = \ + db_bootstrap.h \ + $(NULL) +CLEANFILES = $(BUILT_SOURCES) + noinst_HEADERS = \ auc.h \ db.h \ @@ -24,6 +30,7 @@ noinst_HEADERS = \ ctrl.h \ hlr_vty.h \ hlr_vty_subscr.h \ + db_bootstrap.h \ $(NULL) bin_PROGRAMS = \ @@ -73,3 +80,14 @@ db_test_LDADD = \ $(LIBOSMOGSM_LIBS) \ $(SQLITE3_LIBS) \ $(NULL) + +BOOTSTRAP_SQL = $(top_srcdir)/sql/hlr.sql + +db_bootstrap.h: $(BOOTSTRAP_SQL) $(srcdir)/db_bootstrap.sed + echo "/* DO NOT EDIT THIS FILE. It is generated from osmo-hlr.git/sql/hlr.sql */" > "$@" + echo "#pragma once" >> "$@" + echo "static const char *stmt_bootstrap_sql[] = {" >> "$@" + cat "$(BOOTSTRAP_SQL)" \ + | sed -f "$(srcdir)/db_bootstrap.sed" \ + >> "$@" + echo "};" >> "$@" @@ -25,6 +25,7 @@ #include "logging.h" #include "db.h" +#include "db_bootstrap.h" #define SEL_COLUMNS \ "id," \ @@ -179,6 +180,35 @@ void db_close(struct db_context *dbc) talloc_free(dbc); } +static int db_bootstrap(struct db_context *dbc) +{ + int i; + for (i = 0; i < ARRAY_SIZE(stmt_bootstrap_sql); i++) { + int rc; + sqlite3_stmt *stmt; + + rc = sqlite3_prepare_v2(dbc->db, stmt_bootstrap_sql[i], -1, + &stmt, NULL); + if (rc != SQLITE_OK) { + LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", + stmt_bootstrap_sql[i]); + return -1; + } + + /* execute the statement */ + rc = sqlite3_step(stmt); + db_remove_reset(stmt); + if (rc != SQLITE_DONE) { + LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s," + " during stmt '%s'", + rc, sqlite3_errmsg(dbc->db), + stmt_bootstrap_sql[i]); + return -1; + } + } + return 0; +} + struct db_context *db_open(void *ctx, const char *fname) { struct db_context *dbc = talloc_zero(ctx, struct db_context); @@ -231,6 +261,8 @@ struct db_context *db_open(void *ctx, const char *fname) LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n", err_msg); + db_bootstrap(dbc); + /* prepare all SQL statements */ for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, diff --git a/src/db_bootstrap.sed b/src/db_bootstrap.sed new file mode 100644 index 0000000..60b8243 --- /dev/null +++ b/src/db_bootstrap.sed @@ -0,0 +1,25 @@ +# Input to this is sql/hlr.sql. +# +# We want each SQL statement line wrapped in "...\n", and each end (";") to +# become a comma: +# +# SOME SQL COMMAND ( +# that may span ) +# MULTIPLE LINES; +# MORE; +# +# --> +# +# "SOME SQL COMMAND (\n" +# " that may span )\n" +# "MULTIPLE LINES\n", <--note the comma here +# "MORE\n", +# +# just replacing ';' with '\n,' won't work, since sed is bad in printing +# multiple lines. Also, how to input newlines to sed is not portable across +# platforms. + +# Match excluding a trailing ';' as \1, keep any trailing ';' in \2 +s/^\(.*[^;]\)\(;\|\)$/"\1\\n"\2/ +# Replace trailing ';' as ',' +s/;$/,/ |