Asterisk - The Open Source Telephony Project GIT-master-f3e88d3
Macros | Functions | Variables
astdb2sqlite3.c File Reference

Berkeley DB to SQLite3 converter. More...

#include "asterisk.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sqlite3.h>
#include <libgen.h>
#include "db1-ast/include/db.h"
Include dependency graph for astdb2sqlite3.c:

Go to the source code of this file.

Macros

#define DEFINE_SQL_STATEMENT(stmt, sql)
 
#define MAX_DB_FIELD   256
 
#define MIN(a, b)
 

Functions

static int ast_db_begin_transaction (void)
 
static int ast_db_commit_transaction (void)
 
static int ast_db_rollback_transaction (void)
 
static int convert_bdb_to_sqlite3 (const char *bdb_dbname)
 
static int db_create_astdb (void)
 
static int db_open (const char *dbname)
 
static int db_put_raw (const char *key, size_t keylen, const char *value, size_t valuelen)
 
 DEFINE_SQL_STATEMENT (put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
 
static int init_statements (void)
 
static int init_stmt (sqlite3_stmt **stmt, const char *sql, size_t len)
 
int main (int argc, char *argv[])
 
static int sql_db_init (const char *dbname)
 

Variables

static sqlite3 * astdb
 

Detailed Description

Berkeley DB to SQLite3 converter.

Author
Terry Wilson twils.nosp@m.on@d.nosp@m.igium.nosp@m..com

Definition in file astdb2sqlite3.c.

Macro Definition Documentation

◆ DEFINE_SQL_STATEMENT

#define DEFINE_SQL_STATEMENT (   stmt,
  sql 
)
Value:
static sqlite3_stmt *stmt; \
const char stmt##_sql[] = sql;

Definition at line 49 of file astdb2sqlite3.c.

◆ MAX_DB_FIELD

#define MAX_DB_FIELD   256

Definition at line 41 of file astdb2sqlite3.c.

◆ MIN

#define MIN (   a,
  b 
)
Value:
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
a < _b ? _a : _b; })
static struct test_val b
static struct test_val a
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t

Definition at line 42 of file astdb2sqlite3.c.

Function Documentation

◆ ast_db_begin_transaction()

static int ast_db_begin_transaction ( void  )
static

Definition at line 70 of file astdb2sqlite3.c.

71{
72 return db_execute_transaction_sql("BEGIN TRANSACTION");
73}

Referenced by db_create_astdb().

◆ ast_db_commit_transaction()

static int ast_db_commit_transaction ( void  )
static

Definition at line 75 of file astdb2sqlite3.c.

76{
77 return db_execute_transaction_sql("COMMIT");
78}

Referenced by db_create_astdb().

◆ ast_db_rollback_transaction()

static int ast_db_rollback_transaction ( void  )
static

Definition at line 80 of file astdb2sqlite3.c.

81{
82 return db_execute_transaction_sql("ROLLBACK");
83}

Referenced by db_create_astdb().

◆ convert_bdb_to_sqlite3()

static int convert_bdb_to_sqlite3 ( const char *  bdb_dbname)
static

Definition at line 104 of file astdb2sqlite3.c.

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}
struct sla_ringing_trunk * last
Definition: app_sla.c:332
static DB * bdb
Definition: astdb2bdb.c:42
#define MIN(a, b)
Definition: astdb2sqlite3.c:42
static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen)
Definition: astdb2sqlite3.c:85
#define MAX_DB_FIELD
Definition: astdb2sqlite3.c:41
#define AST_FILE_MODE
Definition: asterisk.h:32
#define R_NEXT
Definition: db.h:97
#define R_FIRST
Definition: db.h:93
@ DB_BTREE
Definition: db.h:103
#define R_LAST
Definition: db.h:96
#define NULL
Definition: resample.c:96
Definition: db.h:85
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
Definition: db.h:129
int value
Definition: syslog.c:37
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const

References AST_FILE_MODE, bdb, DBT::data, DB_BTREE, db_put_raw(), dbopen(), last, MAX_DB_FIELD, MIN, NULL, R_FIRST, R_LAST, R_NEXT, DBT::size, and value.

Referenced by main().

◆ db_create_astdb()

static int db_create_astdb ( void  )
static

Definition at line 147 of file astdb2sqlite3.c.

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}
static sqlite3 * astdb
Definition: astdb2sqlite3.c:47
static int ast_db_begin_transaction(void)
Definition: astdb2sqlite3.c:70
static int ast_db_commit_transaction(void)
Definition: astdb2sqlite3.c:75
static int ast_db_rollback_transaction(void)
Definition: astdb2sqlite3.c:80
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)

References ast_db_begin_transaction(), ast_db_commit_transaction(), ast_db_rollback_transaction(), astdb, and init_stmt().

Referenced by sql_db_init().

◆ db_open()

static int db_open ( const char *  dbname)
static

Definition at line 174 of file astdb2sqlite3.c.

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}
static char dbname[MAX_DB_OPTION_SIZE]

References astdb, and dbname.

Referenced by sql_db_init().

◆ db_put_raw()

static int db_put_raw ( const char *  key,
size_t  keylen,
const char *  value,
size_t  valuelen 
)
static

Definition at line 85 of file astdb2sqlite3.c.

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}

References astdb, and value.

Referenced by convert_bdb_to_sqlite3().

◆ DEFINE_SQL_STATEMENT()

DEFINE_SQL_STATEMENT ( put_stmt  ,
"INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)"   
)

Definition at line 52 of file astdb2sqlite3.c.

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}

References astdb, and NULL.

◆ init_statements()

static int init_statements ( void  )
static

Definition at line 167 of file astdb2sqlite3.c.

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}

References init_stmt().

Referenced by sql_db_init().

◆ init_stmt()

static int init_stmt ( sqlite3_stmt **  stmt,
const char *  sql,
size_t  len 
)
static

Definition at line 137 of file astdb2sqlite3.c.

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}
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References astdb, len(), and NULL.

Referenced by db_create_astdb(), and init_statements().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 194 of file astdb2sqlite3.c.

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 convert_bdb_to_sqlite3(const char *bdb_dbname)
static int sql_db_init(const char *dbname)
int errno

References astdb, convert_bdb_to_sqlite3(), dbname, errno, and sql_db_init().

◆ sql_db_init()

static int sql_db_init ( const char *  dbname)
static

Definition at line 185 of file astdb2sqlite3.c.

186{
188 return -1;
189 }
190
191 return 0;
192}
static int db_create_astdb(void)
static int db_open(const char *dbname)
static int init_statements(void)

References db_create_astdb(), db_open(), dbname, and init_statements().

Referenced by main().

Variable Documentation

◆ astdb

sqlite3* astdb
static