32 #include <sys/types.h> 41 #define MAX_DB_FIELD 256 43 ({ typeof (a) _a = (a); \ 44 typeof (b) _b = (b); \ 49 #define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \ 50 const char stmt##_sql[] = sql; 55 static
int db_execute_transaction_sql(const
char *sql)
62 fprintf(stderr,
"Error executing SQL: %s\n", errmsg);
72 return db_execute_transaction_sql(
"BEGIN TRANSACTION");
77 return db_execute_transaction_sql(
"COMMIT");
82 return db_execute_transaction_sql(
"ROLLBACK");
85 static int db_put_raw(
const char *key,
size_t keylen,
const char *
value,
size_t valuelen)
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));
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));
95 }
else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
96 fprintf(stderr,
"Couldn't execute statement: %s\n", sqlite3_errmsg(
astdb));
99 sqlite3_reset(put_stmt);
107 DBT key = { 0, },
value = { 0, }, last_key = { 0, };
112 fprintf(stderr,
"Unable to open Asterisk database '%s'\n", bdb_dbname);
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';
125 last = !strcmp(key.
data, last_key_s);
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));
149 if (
init_stmt(&create_astdb_stmt, create_astdb_stmt_sql,
sizeof(create_astdb_stmt_sql))) {
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);
162 sqlite3_reset(create_astdb_stmt);
171 return init_stmt(&put_stmt, put_stmt_sql,
sizeof(put_stmt_sql));
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);
194 int main(
int argc,
char *argv[])
197 struct stat dont_care;
200 fprintf(stderr,
"%s takes the path of astdb as its only argument\n", basename(argv[0]));
204 if (stat(argv[1], &dont_care)) {
205 fprintf(stderr,
"Unable to open %s: %s\n", argv[1], strerror(
errno));
209 if (!(dbname = alloca(strlen(argv[1]) +
sizeof(
".sqlite3")))) {
213 strcpy(dbname, argv[1]);
214 strcat(dbname,
".sqlite3");
216 if (!stat(dbname, &dont_care)) {
217 fprintf(stderr,
"%s already exists!\n", dbname);
226 fprintf(stderr,
"Database conversion failed!\n");
228 sqlite3_close(
astdb);
231 sqlite3_close(
astdb);
static int init_statements(void)
static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen)
Asterisk main include file. File version handling, generic pbx functions.
static int db_create_astdb(void)
static int sql_db_init(const char *dbname)
#define DEFINE_SQL_STATEMENT(stmt, sql)
struct sla_ringing_trunk * last
static int ast_db_rollback_transaction(void)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
static struct ast_str * dbname
static int ast_db_commit_transaction(void)
static int ast_db_begin_transaction(void)
static int convert_bdb_to_sqlite3(const char *bdb_dbname)
static int db_open(const char *dbname)
int main(int argc, char *argv[])
#define EXISTS(a, b, c, d)
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const