/
db_sqlite.c
175 lines (151 loc) · 4.11 KB
/
db_sqlite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* $Id$
*
* SQlite module interface
*
* Copyright (C) 2010 Timo Teräs
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/time.h>
#include <sqlite3.h>
#include "../../core/sr_module.h"
#include "../../lib/srdb1/db_query.h"
#include "../../lib/srdb1/db.h"
#include "dbase.h"
#include "db_sqlite.h"
MODULE_VERSION
static int sqlite_bind_api(db_func_t *dbb)
{
if(dbb==NULL)
return -1;
memset(dbb, 0, sizeof(db_func_t));
dbb->use_table = db_sqlite_use_table;
dbb->init = db_sqlite_init;
dbb->close = db_sqlite_close;
dbb->free_result = db_sqlite_free_result;
dbb->query = db_sqlite_query;
dbb->insert = db_sqlite_insert;
dbb->delete = db_sqlite_delete;
dbb->update = db_sqlite_update;
dbb->raw_query = db_sqlite_raw_query;
return 0;
}
static db_param_list_t *db_param_list = NULL;
static void db_param_list_add(db_param_list_t *e) {
if (!db_param_list) {
db_param_list = e;
LM_DBG("adding readonly database [%s]\n", e->database.s);
clist_init(db_param_list, next, prev);
} else {
LM_DBG("adding append constraint [%s]\n", e->database.s);
clist_append(db_param_list, e, next, prev);
}
}
static void db_param_list_destroy(db_param_list_t *e) {
if (!e)
return;
if (e->database.s)
pkg_free(e->database.s);
pkg_free(e);
e = NULL;
}
static db_param_list_t *db_param_list_new(const char *db_filename) {
db_param_list_t *e = pkg_malloc(sizeof(db_param_list_t));
if (!e)
return NULL;
memset(e, 0, sizeof(db_param_list_t));
e->database.len = strlen(db_filename);
e->database.s = pkg_malloc(e->database.len+1);
if (!e->database.s) goto error;
strcpy(e->database.s, db_filename);
db_param_list_add(e);
return e;
error:
db_param_list_destroy(e);
return NULL;
}
db_param_list_t *db_param_list_search(char *db_filename) {
db_param_list_t *e;
if (!db_param_list) {
return NULL;
}
if (strcmp(db_filename, db_param_list->database.s) == 0) {
return db_param_list;
}
clist_foreach(db_param_list, e, next){
if (strcmp(db_filename, e->database.s) == 0) {
return e;
}
}
return NULL;
}
int db_set_readonly(modparam_t type, void *val) {
if(val==NULL)
return -1;
db_param_list_t *e = db_param_list_search((char*)val);
if (!e)
e = db_param_list_new((char *)val);
if (!e) {
LM_ERR("can't create a new db_param for [%s]\n", (char*) val);
return -1;
}
e->readonly = 1;
db_param_list_search((char *)val);
return 1;
}
static param_export_t params[] = {
{"db_set_readonly", PARAM_STRING|USE_FUNC_PARAM, (void*)db_set_readonly},
{0,0,0}
};
static cmd_export_t cmds[] = {
{"db_bind_api", (cmd_function)sqlite_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
if(db_api_init()<0)
return -1;
return 0;
}
static int sqlite_mod_init(void)
{
sqlite3_initialize();
LM_INFO("SQlite library version %s (compiled using %s)\n",
sqlite3_libversion(),
SQLITE_VERSION);
return 0;
}
static void sqlite_mod_destroy(void)
{
LM_INFO("SQlite terminate\n");
sqlite3_shutdown();
}
struct module_exports exports = {
"db_sqlite",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* module commands */
params, /* module parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
sqlite_mod_init, /* module initialization function */
0, /* response function*/
sqlite_mod_destroy, /* destroy function */
0 /* per-child init function */
};