blob: b4fadca1a85efea2f4a1bc5be55aaee241bf5023 [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[] = {
28 [SEL_BY_IMSI] = "SELECT * FROM subscriber WHERE imsi = ?",
29 [UPD_BY_IMSI] = "UPDATE subscriber SET vlr_number = ? WHERE imsi = ?",
30 [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 = ?",
31 [AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
32};
33
34static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
35{
36 LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
37}
38
39static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
40{
41 switch (type) {
42 case 0:
43 LOGP(DDB, LOGL_DEBUG, "Opened database\n");
44 break;
45 case 1:
46 LOGP(DDB, LOGL_DEBUG, stmt);
47 break;
48 case 2:
49 LOGP(DDB, LOGL_DEBUG, "Closed database\n");
50 break;
51 default:
52 LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
53 break;
54 }
55}
56
57void db_close(struct db_context *dbc)
58{
59 unsigned int i;
60
61 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
62 /* it is ok to call finalize on NULL */
63 sqlite3_finalize(dbc->stmt[i]);
64 }
65 sqlite3_close(dbc->db);
66 talloc_free(dbc);
67}
68
69struct db_context *db_open(void *ctx, const char *fname)
70{
71 struct db_context *dbc = talloc_zero(ctx, struct db_context);
72 unsigned int i;
73 int rc;
74
75 LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
76 LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
77
78 dbc->fname = talloc_strdup(dbc, fname);
79
80 for (i = 0; i < 0xfffff; i++) {
81 const char *o = sqlite3_compileoption_get(i);
82 if (!o)
83 break;
84 LOGP(DDB, LOGL_DEBUG, "SQlite3 compiled with '%s'\n", o);
85 }
86
87 rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
88 if (rc != SQLITE_OK)
89 LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 error log callback\n");
90
91 rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
92 if (rc != SQLITE_OK)
93 LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 SQL statement log callback\n");
94
95 rc = sqlite3_open(dbc->fname, &dbc->db);
96 if (rc != SQLITE_OK) {
97 LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
98 talloc_free(dbc);
99 return NULL;
100 }
101
102 /* enable extended result codes */
103 rc = sqlite3_extended_result_codes(dbc->db, 1);
104 if (rc != SQLITE_OK)
105 LOGP(DDB, LOGL_ERROR, "Unable to enable SQlite3 extended result codes\n");
106
107 /* prepare all SQL statements */
108 for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
109 rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
110 &dbc->stmt[i], NULL);
111 if (rc != SQLITE_OK) {
112 LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
113 goto out_free;
114 }
115 }
116
117 return dbc;
118out_free:
119 db_close(dbc);
120 return NULL;
121}