astdb2sqlite3.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Berkeley DB to SQLite3 converter
00022  *
00023  * \author Terry Wilson <twilson@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 //ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369013 $")
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 #include <sqlite3.h>
00039 #include <libgen.h> /* OS X doesn't have the basename function in strings.h */
00040 
00041 #include "db1-ast/include/db.h"
00042 
00043 #define MAX_DB_FIELD 256
00044 #define MIN(a,b) \
00045 ({ typeof (a) _a = (a); \
00046    typeof (b) _b = (b); \
00047    a < _b ? _a : _b; })
00048 
00049 static sqlite3 *astdb;
00050 
00051 #define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \
00052    const char stmt##_sql[] = sql;
00053 
00054 DEFINE_SQL_STATEMENT(put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
00055 DEFINE_SQL_STATEMENT(create_astdb_stmt, "CREATE TABLE IF NOT EXISTS astdb(key VARCHAR(256), value VARCHAR(256), PRIMARY KEY(key))")
00056 
00057 static int db_execute_transaction_sql(const char *sql)
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 }
00071 
00072 static int ast_db_begin_transaction(void)
00073 {
00074    return db_execute_transaction_sql("BEGIN TRANSACTION");
00075 }
00076 
00077 static int ast_db_commit_transaction(void)
00078 {
00079    return db_execute_transaction_sql("COMMIT");
00080 }
00081 
00082 static int ast_db_rollback_transaction(void)
00083 {
00084    return db_execute_transaction_sql("ROLLBACK");
00085 }
00086 
00087 static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen)
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 }
00105 
00106 static int convert_bdb_to_sqlite3(const char *bdb_dbname)
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 }
00138 
00139 static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
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 }
00148 
00149 static int db_create_astdb(void)
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 }
00168 
00169 static int init_statements(void)
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 }
00175 
00176 static int db_open(const char *dbname)
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 }
00186 
00187 static int sql_db_init(const char *dbname)
00188 {
00189    if (db_open(dbname) || db_create_astdb() || init_statements()) {
00190       return -1;
00191    }
00192 
00193    return 0;
00194 }
00195 
00196 int main(int argc, char *argv[])
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 }

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