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