blob: 9d4e99eaa200cfca324ba524dcba61584e7020a3 [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <osmocom/core/utils.h>
21
22#include <sqlite3.h>
23
24#include "logging.h"
25#include "db.h"
26
27static const char *stmt_sql[] = {
Harald Weltee687be52016-05-03 18:49:27 +020028 [SEL_BY_IMSI] = "SELECT id,imsi,msisdn,vlr_number,sgsn_number,sgsn_address,periodic_lu_tmr,periodic_rau_tau_tmr,nam_cs,nam_ps,lmsi,ms_purged_cs,ms_purged_ps FROM subscriber WHERE imsi = ?",
29 [UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?",
30 [UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?",
Harald Weltee72cf552016-04-28 07:18:49 +020031 [AUC_BY_IMSI] = "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn FROM subscriber LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id WHERE imsi = ?",
32 [AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
Harald Welte79f71402016-06-10 17:33:38 +020033 [UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?",
34 [UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?",
Harald Weltee72cf552016-04-28 07:18:49 +020035};
36
37static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
38{
39 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
40}
41
42static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
43{
44 switch (type) {
45 case 0:
46 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
47 break;
48 case 1:
49 LOGP(DDB, LOGL_DEBUG, stmt);
50 break;
51 case 2:
52 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
53 break;
54 default:
55 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
56 break;
57 }
58}
59
60void db_close(struct db_context *dbc)
61{
62 unsigned int i;
63
64 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
65 /* it is ok to call finalize on NULL */
66 sqlite3_finalize(dbc->stmt[i]);
67 }
68 sqlite3_close(dbc->db);
69 talloc_free(dbc);
70}
71
72struct db_context *db_open(void *ctx, const char *fname)
73{
74 struct db_context *dbc = talloc_zero(ctx, struct db_context);
75 unsigned int i;
76 int rc;
77
78 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
79 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
80
81 dbc->fname = talloc_strdup(dbc, fname);
82
83 for (i = 0; i < 0xfffff; i++) {
84 const char *o = sqlite3_compileoption_get(i);
85 if (!o)
86 break;
87 LOGP(DDB, LOGL_DEBUG, "SQlite3 compiled with '%s'\n", o);
88 }
89
90 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
91 if (rc != SQLITE_OK)
92 LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 error log callback\n");
93
94 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
95 if (rc != SQLITE_OK)
96 LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 SQL statement log callback\n");
97
98 rc = sqlite3_open(dbc->fname, &dbc->db);
99 if (rc != SQLITE_OK) {
100 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
101 talloc_free(dbc);
102 return NULL;
103 }
104
105 /* enable extended result codes */
106 rc = sqlite3_extended_result_codes(dbc->db, 1);
107 if (rc != SQLITE_OK)
108 LOGP(DDB, LOGL_ERROR, "Unable to enable SQlite3 extended result codes\n");
109
Harald Welteabd1a542016-05-03 18:50:41 +0200110 char *err_msg;
111 rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
112 if (rc != SQLITE_OK)
113 LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
114 err_msg);
115
Harald Weltee72cf552016-04-28 07:18:49 +0200116 /* prepare all SQL statements */
117 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
118 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
119 &dbc->stmt[i], NULL);
120 if (rc != SQLITE_OK) {
121 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
122 goto out_free;
123 }
124 }
125
126 return dbc;
127out_free:
128 db_close(dbc);
129 return NULL;
130}