Asterisk - The Open Source Telephony Project  18.5.0
astdb2sqlite3.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Berkeley DB to SQLite3 converter
22  *
23  * \author Terry Wilson <[email protected]>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sqlite3.h>
37 #include <libgen.h> /* OS X doesn't have the basename function in strings.h */
38 
39 #include "db1-ast/include/db.h"
40 
41 #define MAX_DB_FIELD 256
42 #define MIN(a,b) \
43 ({ typeof (a) _a = (a); \
44  typeof (b) _b = (b); \
45  a < _b ? _a : _b; })
46 
47 static sqlite3 *astdb;
48 
49 #define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \
50  const char stmt##_sql[] = sql;
51 
52 DEFINE_SQL_STATEMENT(put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
53 DEFINE_SQL_STATEMENT(create_astdb_stmt, "CREATE TABLE IF NOT EXISTS astdb(key VARCHAR(256), value VARCHAR(256), PRIMARY KEY(key))")
54 
55 static int db_execute_transaction_sql(const char *sql)
56 {
57  char *errmsg = NULL;
58  int res =0;
59 
60  sqlite3_exec(astdb, sql, NULL, NULL, &errmsg);
61  if (errmsg) {
62  fprintf(stderr, "Error executing SQL: %s\n", errmsg);
63  sqlite3_free(errmsg);
64  res = -1;
65  }
66 
67  return res;
68 }
69 
70 static int ast_db_begin_transaction(void)
71 {
72  return db_execute_transaction_sql("BEGIN TRANSACTION");
73 }
74 
75 static int ast_db_commit_transaction(void)
76 {
77  return db_execute_transaction_sql("COMMIT");
78 }
79 
81 {
82  return db_execute_transaction_sql("ROLLBACK");
83 }
84 
85 static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen)
86 {
87  int res = 0;
88 
89  if (sqlite3_bind_text(put_stmt, 1, key, keylen, SQLITE_STATIC) != SQLITE_OK) {
90  fprintf(stderr, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
91  res = -1;
92  } else if (sqlite3_bind_text(put_stmt, 2, value, valuelen, SQLITE_STATIC) != SQLITE_OK) {
93  fprintf(stderr, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
94  res = -1;
95  } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
96  fprintf(stderr, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb));
97  res = -1;
98  }
99  sqlite3_reset(put_stmt);
100 
101  return res;
102 }
103 
104 static int convert_bdb_to_sqlite3(const char *bdb_dbname)
105 {
106  DB *bdb;
107  DBT key = { 0, }, value = { 0, }, last_key = { 0, };
108  int res, last = 0;
109  char last_key_s[MAX_DB_FIELD];
110 
111  if (!(bdb = dbopen(bdb_dbname, O_RDONLY, AST_FILE_MODE, DB_BTREE, NULL))) {
112  fprintf(stderr, "Unable to open Asterisk database '%s'\n", bdb_dbname);
113  return -1;
114  }
115 
116  if (bdb->seq(bdb, &last_key, &value, R_LAST)) {
117  /* Empty database */
118  return 0;
119  }
120 
121  memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s)));
122  last_key_s[last_key.size - 1] = '\0';
123  for (res = bdb->seq(bdb, &key, &value, R_FIRST);
124  !res; res = bdb->seq(bdb, &key, &value, R_NEXT)) {
125  last = !strcmp(key.data, last_key_s);
126  db_put_raw((const char *) key.data, key.size - 1, (const char *) value.data, value.size - 1);
127  if (last) {
128  break;
129  }
130  }
131 
132  bdb->close(bdb);
133 
134  return 0;
135 }
136 
137 static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
138 {
139  if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
140  fprintf(stderr, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
141  return -1;
142  }
143 
144  return 0;
145 }
146 
147 static int db_create_astdb(void)
148 {
149  if (init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql))) {
150  return -1;
151  }
152 
154  if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
155  fprintf(stderr, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
157  sqlite3_reset(create_astdb_stmt);
158  return -1;
159  }
160 
162  sqlite3_reset(create_astdb_stmt);
163 
164  return 0;
165 }
166 
167 static int init_statements(void)
168 {
169  /* Don't initialize create_astdb_statement here as the astdb table needs to exist
170  * brefore these statements can be initialized */
171  return init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
172 }
173 
174 static int db_open(const char *dbname)
175 {
176  if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
177  fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
178  sqlite3_close(astdb);
179  return -1;
180  }
181 
182  return 0;
183 }
184 
185 static int sql_db_init(const char *dbname)
186 {
187  if (db_open(dbname) || db_create_astdb() || init_statements()) {
188  return -1;
189  }
190 
191  return 0;
192 }
193 
194 int main(int argc, char *argv[])
195 {
196  char *dbname;
197  struct stat dont_care;
198 
199  if (argc != 2) {
200  fprintf(stderr, "%s takes the path of astdb as its only argument\n", basename(argv[0]));
201  exit(-1);
202  }
203 
204  if (stat(argv[1], &dont_care)) {
205  fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
206  exit(-1);
207  }
208 
209  if (!(dbname = alloca(strlen(argv[1]) + sizeof(".sqlite3")))) {
210  exit(-1);
211  }
212 
213  strcpy(dbname, argv[1]);
214  strcat(dbname, ".sqlite3");
215 
216  if (!stat(dbname, &dont_care)) {
217  fprintf(stderr, "%s already exists!\n", dbname);
218  exit(-1);
219  }
220 
221  if (sql_db_init(dbname)) {
222  exit(-1);
223  }
224 
225  if (convert_bdb_to_sqlite3(argv[1])) {
226  fprintf(stderr, "Database conversion failed!\n");
227  exit(-1);
228  sqlite3_close(astdb);
229  }
230 
231  sqlite3_close(astdb);
232  return 0;
233 }
static int init_statements(void)
static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen)
Definition: astdb2sqlite3.c:85
void * data
Definition: db.h:86
Asterisk main include file. File version handling, generic pbx functions.
static int db_create_astdb(void)
size_t size
Definition: db.h:87
Definition: test_db.c:41
static int sql_db_init(const char *dbname)
#define MIN(a, b)
Definition: astdb2sqlite3.c:42
Definition: db.h:85
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
#define DEFINE_SQL_STATEMENT(stmt, sql)
Definition: astdb2sqlite3.c:49
#define AST_FILE_MODE
Definition: asterisk.h:32
static sqlite3 * astdb
Definition: astdb2sqlite3.c:47
#define R_LAST
Definition: db.h:96
struct sla_ringing_trunk * last
Definition: app_meetme.c:1092
Definition: db.h:103
static int ast_db_rollback_transaction(void)
Definition: astdb2sqlite3.c:80
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
#define R_NEXT
Definition: db.h:97
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
static struct ast_str * dbname
Definition: cdr_mysql.c:77
static int ast_db_commit_transaction(void)
Definition: astdb2sqlite3.c:75
#define MAX_DB_FIELD
Definition: astdb2sqlite3.c:41
static int ast_db_begin_transaction(void)
Definition: astdb2sqlite3.c:70
static int convert_bdb_to_sqlite3(const char *bdb_dbname)
#define R_FIRST
Definition: db.h:93
static int db_open(const char *dbname)
int main(int argc, char *argv[])
#define EXISTS(a, b, c, d)
Definition: db.h:129
static DB * bdb
Definition: astdb2bdb.c:42
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const