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.

Defines

#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 <twilson@digium.com>

Definition in file astdb2sqlite3.c.


Define Documentation

#define DEFINE_SQL_STATEMENT ( stmt,
sql   ) 

Value:

static sqlite3_stmt *stmt; \
   const char stmt##_sql[] = sql;

Definition at line 51 of file astdb2sqlite3.c.

#define MAX_DB_FIELD   256

Definition at line 43 of file astdb2sqlite3.c.

#define MIN ( a,
b   ) 

Value:

({ typeof (a) _a = (a); \
   typeof (b) _b = (b); \
   a < _b ? _a : _b; })

Definition at line 44 of file astdb2sqlite3.c.


Function Documentation

static int ast_db_begin_transaction ( void   )  [static]

Definition at line 72 of file astdb2sqlite3.c.

00073 {
00074    return db_execute_transaction_sql("BEGIN TRANSACTION");
00075 }

static int ast_db_commit_transaction ( void   )  [static]

Definition at line 77 of file astdb2sqlite3.c.

00078 {
00079    return db_execute_transaction_sql("COMMIT");
00080 }

static int ast_db_rollback_transaction ( void   )  [static]

Definition at line 82 of file astdb2sqlite3.c.

00083 {
00084    return db_execute_transaction_sql("ROLLBACK");
00085 }

static int convert_bdb_to_sqlite3 ( const char *  bdb_dbname  )  [static]

Definition at line 106 of file astdb2sqlite3.c.

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.

00107 {
00108    DB *bdb;
00109    DBT key = { 0, }, value = { 0, }, last_key = { 0, };
00110    int res, last = 0;
00111    char last_key_s[MAX_DB_FIELD];
00112 
00113    if (!(bdb = dbopen(bdb_dbname, O_RDONLY, AST_FILE_MODE, DB_BTREE, NULL))) {
00114       fprintf(stderr, "Unable to open Asterisk database '%s'\n", bdb_dbname);
00115       return -1;
00116    }
00117 
00118    if (bdb->seq(bdb, &last_key, &value, R_LAST)) {
00119       /* Empty database */
00120       return 0;
00121    }
00122 
00123    memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s)));
00124    last_key_s[last_key.size - 1] = '\0';
00125    for (res = bdb->seq(bdb, &key, &value, R_FIRST);
00126          !res; res = bdb->seq(bdb, &key, &value, R_NEXT)) {
00127       last = !strcmp(key.data, last_key_s);
00128       db_put_raw((const char *) key.data, key.size - 1, (const char *) value.data, value.size - 1);
00129       if (last) {
00130          break;
00131       }
00132    }
00133 
00134    bdb->close(bdb);
00135 
00136    return 0;
00137 }

static int db_create_astdb ( void   )  [static]

Definition at line 149 of file astdb2sqlite3.c.

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

00150 {
00151    if (init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql))) {
00152       return -1;
00153    }
00154 
00155    ast_db_begin_transaction();
00156    if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
00157       fprintf(stderr, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
00158       ast_db_rollback_transaction();
00159       sqlite3_reset(create_astdb_stmt);
00160       return -1;
00161    }
00162 
00163    ast_db_commit_transaction();
00164    sqlite3_reset(create_astdb_stmt);
00165 
00166    return 0;
00167 }

static int db_open ( const char *  dbname  )  [static]

Definition at line 176 of file astdb2sqlite3.c.

References astdb.

00177 {
00178    if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
00179       fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
00180       sqlite3_close(astdb);
00181       return -1;
00182    }
00183 
00184    return 0;
00185 }

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

Definition at line 87 of file astdb2sqlite3.c.

References astdb.

Referenced by convert_bdb_to_sqlite3().

00088 {
00089    int res = 0;
00090 
00091    if (sqlite3_bind_text(put_stmt, 1, key, keylen, SQLITE_STATIC) != SQLITE_OK) {
00092       fprintf(stderr, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
00093       res = -1;
00094    } else if (sqlite3_bind_text(put_stmt, 2, value, valuelen, SQLITE_STATIC) != SQLITE_OK) {
00095       fprintf(stderr, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
00096       res = -1;
00097    } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
00098       fprintf(stderr, "Couldn't execute statment: %s\n", sqlite3_errmsg(astdb));
00099       res = -1;
00100    }
00101    sqlite3_reset(put_stmt);
00102 
00103    return res;
00104 }

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

Definition at line 54 of file astdb2sqlite3.c.

References astdb, and NULL.

00058 {
00059    char *errmsg = NULL;
00060    int res =0;
00061 
00062    sqlite3_exec(astdb, sql, NULL, NULL, &errmsg);
00063    if (errmsg) {
00064       fprintf(stderr, "Error executing SQL: %s\n", errmsg);
00065       sqlite3_free(errmsg);
00066       res = -1;
00067    }
00068 
00069    return res;
00070 }

static int init_statements ( void   )  [static]

Definition at line 169 of file astdb2sqlite3.c.

References init_stmt().

00170 {
00171    /* Don't initialize create_astdb_statment here as the astdb table needs to exist
00172     * brefore these statments can be initialized */
00173    return init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
00174 }

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

Definition at line 139 of file astdb2sqlite3.c.

References astdb, and NULL.

Referenced by db_create_astdb(), and init_statements().

00140 {
00141    if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
00142       fprintf(stderr, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
00143       return -1;
00144    }
00145 
00146    return 0;
00147 }

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

Definition at line 196 of file astdb2sqlite3.c.

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

00197 {
00198    char *dbname;
00199    struct stat dont_care;
00200 
00201    if (argc != 2) {
00202       fprintf(stderr, "%s takes the path of astdb as its only argument\n", basename(argv[0]));
00203       exit(-1);
00204    }
00205 
00206    if (stat(argv[1], &dont_care)) {
00207       fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
00208       exit(-1);
00209    }
00210 
00211    if (!(dbname = alloca(strlen(argv[1]) + sizeof(".sqlite3")))) {
00212       exit(-1);
00213    }
00214 
00215    strcpy(dbname, argv[1]);
00216    strcat(dbname, ".sqlite3");
00217 
00218    if (!stat(dbname, &dont_care)) {
00219       fprintf(stderr, "%s already exists!\n", dbname);
00220       exit(-1);
00221    }
00222 
00223    if (sql_db_init(dbname)) {
00224       exit(-1);
00225    }
00226 
00227    if (convert_bdb_to_sqlite3(argv[1])) {
00228       fprintf(stderr, "Database conversion failed!\n");
00229       exit(-1);
00230       sqlite3_close(astdb);
00231    }
00232 
00233    sqlite3_close(astdb);
00234    return 0;
00235 }

static int sql_db_init ( const char *  dbname  )  [static]

Definition at line 187 of file astdb2sqlite3.c.

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

Referenced by main().

00188 {
00189    if (db_open(dbname) || db_create_astdb() || init_statements()) {
00190       return -1;
00191    }
00192 
00193    return 0;
00194 }


Variable Documentation

sqlite3* astdb [static]

Definition at line 49 of file astdb2sqlite3.c.


Generated on Thu Apr 16 06:28:57 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6