[svn-commits] ivaxer: branch ivaxer/ast_storage r273889 - /team/ivaxer/ast_storage/res/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Jul 5 08:55:23 CDT 2010


Author: ivaxer
Date: Mon Jul  5 08:55:20 2010
New Revision: 273889

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=273889
Log:
added odbc storage backend
- implemented the create/release functions

Added:
    team/ivaxer/ast_storage/res/res_storage_odbc.c   (with props)

Added: team/ivaxer/ast_storage/res/res_storage_odbc.c
URL: http://svnview.digium.com/svn/asterisk/team/ivaxer/ast_storage/res/res_storage_odbc.c?view=auto&rev=273889
==============================================================================
--- team/ivaxer/ast_storage/res/res_storage_odbc.c (added)
+++ team/ivaxer/ast_storage/res/res_storage_odbc.c Mon Jul  5 08:55:20 2010
@@ -1,0 +1,242 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Tilghman Lesher
+ *
+ * Tilghman Lesher <res_storage_odbc__v001 at the-tilghman.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief ODBC based storage engine
+ * \author Tilghman Lesher <res_storage_odbc__v001 at the-tilghman.com>
+ * \ingroup res
+ */
+ 
+/*** MODULEINFO
+	<depend>unixodbc</depend>
+	<depend>res_odbc</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/storage.h"
+#include "asterisk/module.h"
+#include "asterisk/res_odbc.h"
+#include "asterisk/app.h"
+#include "asterisk/config.h"
+#include "asterisk/cli.h"
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+
+struct columns {
+	SQLSMALLINT type;
+	SQLINTEGER size;
+	SQLSMALLINT decimals;
+	SQLSMALLINT radix;
+	SQLSMALLINT nullable;
+	SQLINTEGER octetlen;
+	AST_LIST_ENTRY(columns) list;
+	char name[1];
+};
+
+struct odbc_storage_pvt {
+	char odbc_class[128];
+	char tablename[128];
+	struct odbc_obj *conn;
+	AST_RWLIST_HEAD(odbc_columns, columns) columns;
+};
+
+static const struct ast_storage_be odbc_se;
+
+static int se_release(struct ast_storage *st)
+{
+	struct odbc_storage_pvt *pvt;
+
+	if (!st)
+		return 0;
+	if (st->storage_pvt) {
+		pvt = (struct odbc_storage_pvt *) st->storage_pvt;
+		if (pvt->conn) {
+			ast_odbc_release_obj(pvt->conn);
+		}
+		free(pvt);
+	}
+	free(st);
+	return 0;
+}
+
+static struct ast_storage *se_create(const char *uri)
+{
+	char tmp[256], columnname[80];
+	SQLRETURN ret;
+	struct ast_storage *st;
+	struct odbc_storage_pvt *pvt;
+	struct columns *entry;
+	int pathname_len, columnname_len;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(class);
+		AST_APP_ARG(tablename);
+		AST_APP_ARG(pathname);
+	);
+	SQLHSTMT stmt = NULL;
+	SQLLEN sqlptr;
+
+	AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
+
+	pathname_len = sizeof(char) * strlen(args.pathname);
+
+	st = ast_calloc(1, sizeof(struct ast_storage) + pathname_len);
+	if (!st) {
+		return NULL;
+	}
+
+	if (ast_strlen_zero(uri)) {
+		se_release(st);
+		return NULL;
+	}
+
+	pvt = ast_calloc(1, sizeof(struct odbc_storage_pvt));
+	if (!pvt) {
+		se_release(st);
+		return NULL;
+	}
+
+	st->storage_pvt = (void *)pvt;
+	st->be = &odbc_se;
+	ast_copy_string(tmp, uri, sizeof(tmp));
+
+	AST_RWLIST_HEAD_INIT(&pvt->columns);
+
+	ast_copy_string(pvt->odbc_class, args.class, sizeof(pvt->odbc_class));
+	ast_copy_string(pvt->tablename, args.tablename, sizeof(pvt->tablename));
+	ast_copy_string(st->path, args.pathname, pathname_len);
+
+	pvt->conn = ast_odbc_request_obj(pvt->odbc_class, 0);
+	if (!pvt->conn) {
+		se_release(st);
+		return NULL;
+	}
+
+	ret = SQLAllocHandle(SQL_HANDLE_STMT, pvt->conn->con, &stmt);
+	if (!SQL_SUCCEEDED(ret)) {
+		ast_log(LOG_ERROR, "Allocation of database handle failed.\n");
+		se_release(st);
+		return NULL;
+	}
+
+	/* Get the table columns */
+	ret = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)pvt->tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
+	if (!SUCCEEDED(ret)) {
+		ast_log(LOG_ERROR, "Unable to query database columns on table '%s@%s'.  Storage method fails.\n", pvt->tablename, pvt->odbc_class);
+		se_release(st);
+		return NULL;
+	}
+
+	while ((ret = SQLFetch(stmt)) != SQL_NO_DATA && ret != SQL_ERROR) {
+		SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
+		columnname_len = sizeof(char) * strlen(columnname);
+
+		entry = ast_calloc(1, sizeof(struct columns) + columnname_len);
+		if (!entry) {
+			ast_log(LOG_ERROR, "Unable to allocate column entry\n");
+			se_release(st);
+			return NULL;
+		}
+
+		ast_copy_string(entry->name, columnname, columnname_len);
+
+		SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
+		SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
+		SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
+		SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
+		SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
+		SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
+
+		AST_LIST_INSERT_TAIL(&pvt->columns, entry, list);
+	}
+
+	return st;
+}
+
+static struct ast_storage_fileobject *se_get(struct ast_storage *st, const char *objectname, const char *exts)
+{
+	return NULL;
+}
+
+static int se_put(struct ast_storage *st, struct ast_storage_fileobject *fo)
+{
+	return -1;
+}
+
+static int se_del(struct ast_storage *st, struct ast_storage_fileobject *fo)
+{
+	return -1;
+}
+
+static struct ast_storage_dirobject *se_listdir(struct ast_storage *st, const char *pathname)
+{
+	return NULL;
+}
+
+static ssize_t se_read(struct ast_storage *st, struct ast_storage_fileinst *fi, void *buf, size_t count)
+{
+	return -1;
+}
+
+static ssize_t se_write(struct ast_storage *st, struct ast_storage_fileinst *fi, void *buf, size_t count)
+{
+	return -1;
+}
+
+static unsigned long se_tell(struct ast_storage *st, struct ast_storage_fileinst *fi) {
+	return 0;
+}
+
+static int se_seek(struct ast_storage *st, struct ast_storage_fileinst *fi) {
+	return -1;
+}
+
+static const struct ast_storage_be odbc_se = {
+	.name = "odbc",
+	.create = se_create,
+	.release = se_release,
+	.get = se_get,
+	.put = se_put,
+	.read = se_read,
+	.write = se_write,
+	.del = se_del,
+	.tell = se_tell,
+	.seek = se_seek,
+	.listdir = se_listdir,
+};
+
+static int load_module(void)
+{
+	return ast_register_storage(&odbc_se);
+}
+
+static int unload_module(void)
+{
+	return ast_unregister_storage(odbc_se.name);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ODBC based storage engine");

Propchange: team/ivaxer/ast_storage/res/res_storage_odbc.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/ivaxer/ast_storage/res/res_storage_odbc.c
------------------------------------------------------------------------------
    svn:keywords = 'Author Date Id Revision'

Propchange: team/ivaxer/ast_storage/res/res_storage_odbc.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the svn-commits mailing list