2007-03-09 20:44:13 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_core_db.c -- sqlite wrapper and extensions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
2007-05-14 17:10:46 +00:00
|
|
|
#include "private/switch_core_pvt.h"
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static void db_pick_path(char *dbname, char *buf, switch_size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
memset(buf, 0, size);
|
|
|
|
if (strchr(dbname, '/')) {
|
|
|
|
strncpy(buf, dbname, size);
|
|
|
|
} else {
|
|
|
|
snprintf(buf, size, "%s%s%s.db", SWITCH_GLOBAL_dirs.db_dir, SWITCH_PATH_SEPARATOR, dbname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_open(const char *filename, switch_core_db_t **ppDb)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_open(filename, ppDb);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_close(switch_core_db_t *db)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_close(db);
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(const unsigned char *) switch_core_db_column_text(switch_core_db_stmt_t *stmt, int iCol)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_column_text(stmt, iCol);
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_core_db_column_name(switch_core_db_stmt_t *stmt, int N)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_column_name(stmt, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_column_count(switch_core_db_stmt_t *pStmt)
|
|
|
|
{
|
|
|
|
return sqlite3_column_count(pStmt);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_core_db_errmsg(switch_core_db_t *db)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_errmsg(db);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_exec(db, sql, callback, data, errmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_finalize(switch_core_db_stmt_t *pStmt)
|
|
|
|
{
|
|
|
|
return sqlite3_finalize(pStmt);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_prepare(switch_core_db_t *db, const char *zSql, int nBytes, switch_core_db_stmt_t **ppStmt, const char **pzTail)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
|
|
|
return sqlite3_prepare(db, zSql, nBytes, ppStmt, pzTail);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_step(switch_core_db_stmt_t *stmt)
|
|
|
|
{
|
|
|
|
return sqlite3_step(stmt);
|
|
|
|
}
|
|
|
|
|
2007-03-10 21:57:02 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_reset(switch_core_db_stmt_t *pStmt)
|
|
|
|
{
|
|
|
|
return sqlite3_reset(pStmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_bind_int(switch_core_db_stmt_t *pStmt, int i, int iValue)
|
|
|
|
{
|
|
|
|
return sqlite3_bind_int(pStmt, i, iValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_bind_int64(switch_core_db_stmt_t *pStmt, int i, int64_t iValue)
|
|
|
|
{
|
|
|
|
return sqlite3_bind_int64(pStmt, i, iValue);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_bind_text(switch_core_db_stmt_t *pStmt, int i, const char *zData, int nData, switch_core_db_destructor_type_t xDel)
|
2007-03-10 21:57:02 +00:00
|
|
|
{
|
|
|
|
return sqlite3_bind_text(pStmt, i, zData, nData, xDel);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_core_db_bind_double(switch_core_db_stmt_t *pStmt, int i, double dValue)
|
|
|
|
{
|
|
|
|
return sqlite3_bind_double(pStmt, i, dValue);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int64_t) switch_core_db_last_insert_rowid(switch_core_db_t *db)
|
2007-03-10 21:57:02 +00:00
|
|
|
{
|
|
|
|
return sqlite3_last_insert_rowid(db);
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_get_table(switch_core_db_t *db, const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg)
|
2007-03-10 21:57:02 +00:00
|
|
|
{
|
|
|
|
return sqlite3_get_table(db, sql, resultp, nrow, ncolumn, errmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_core_db_free_table(char **result)
|
|
|
|
{
|
|
|
|
sqlite3_free_table(result);
|
|
|
|
}
|
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
SWITCH_DECLARE(void) switch_core_db_free(char *z)
|
|
|
|
{
|
|
|
|
sqlite3_free(z);
|
|
|
|
}
|
|
|
|
|
2007-04-12 18:22:09 +00:00
|
|
|
SWITCH_DECLARE(int) switch_core_db_changes(switch_core_db_t *db) {
|
|
|
|
return sqlite3_changes(db);
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(char *) switch_mprintf(const char *zFormat, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *z;
|
|
|
|
va_start(ap, zFormat);
|
|
|
|
z = sqlite3_vmprintf(zFormat, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(char *filename)
|
2007-03-09 20:44:13 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_core_db_t *db;
|
|
|
|
char path[1024];
|
|
|
|
|
|
|
|
db_pick_path(filename, path, sizeof(path));
|
|
|
|
if (switch_core_db_open(path, &db)) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", switch_core_db_errmsg(db));
|
|
|
|
switch_core_db_close(db);
|
|
|
|
db = NULL;
|
|
|
|
}
|
|
|
|
return db;
|
2007-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-10-18 16:17:42 +00:00
|
|
|
SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *drop_sql, char *reactive_sql)
|
2007-03-29 22:31:56 +00:00
|
|
|
{
|
|
|
|
char *errmsg;
|
|
|
|
|
|
|
|
if (db) {
|
|
|
|
if (test_sql) {
|
|
|
|
switch_core_db_exec(db, test_sql, NULL, NULL, &errmsg);
|
|
|
|
|
|
|
|
if (errmsg) {
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql);
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_core_db_free(errmsg);
|
|
|
|
errmsg = NULL;
|
2007-10-18 16:17:42 +00:00
|
|
|
if (drop_sql) {
|
|
|
|
switch_core_db_exec(db, drop_sql, NULL, NULL, &errmsg);
|
|
|
|
}
|
|
|
|
if (errmsg) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
|
|
|
|
switch_core_db_free(errmsg);
|
|
|
|
errmsg = NULL;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_core_db_exec(db, reactive_sql, NULL, NULL, &errmsg);
|
|
|
|
if (errmsg) {
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_core_db_free(errmsg);
|
|
|
|
errmsg = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|