2 * Asterisk -- A telephony toolkit for Linux.
4 * Copyright (C) 1999, Mark Spencer
6 * Mark Spencer <markster@linux-support.net>
8 * res_config_odbc.c <odbc+odbc plugin for portable configuration engine >
9 * Copyright (C) 2004 Anthony Minessale II <anthmct@yahoo.com>
13 #include <asterisk/file.h>
14 #include <asterisk/logger.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/pbx.h>
17 #include <asterisk/config.h>
18 #include <asterisk/config_pvt.h>
19 #include <asterisk/module.h>
20 #include <asterisk/lock.h>
24 #include <asterisk/res_odbc.h>
28 static char *tdesc = "Odbc Configuration";
29 static struct ast_config_reg reg1;
37 static struct ast_config *config_odbc(char *file, struct ast_config *new_config_s,struct ast_category **new_cat_p,struct ast_variable **new_v_p,int recur
38 #ifdef PRESERVE_COMMENTS
39 ,struct ast_comment_struct *acs
47 struct ast_config *config,*new;
48 struct ast_variable *v,*cur_v,*new_v;
49 struct ast_category *cur_cat,*new_cat;
52 int configured=0, res=0;
54 SQLINTEGER err,commented,cat_metric,var_metric;
56 char sql[255],filename[128],category[128],var_name[128],var_val[128];
69 new = ast_new_config();
73 if(!file || !strcmp(file,"res_config_odbc.conf"))
74 return NULL; // cant configure myself with myself !
76 config = ast_load("res_config_odbc.conf");
79 for(v = ast_variable_browse(config,"settings");v;v=v->next) {
80 if(!strcmp(v->name,"table")) {
81 strncpy(table,v->value,sizeof(table));
84 else if(!strcmp(v->name,"connection")) {
85 strncpy(connection,v->value,sizeof(connection));
95 obj = fetch_odbc_obj(connection);
103 res=SQLAllocHandle(SQL_HANDLE_STMT,obj->con, &stmt);
105 SQLBindCol(stmt,1,SQL_C_ULONG,&id,sizeof(id),&err);
106 SQLBindCol(stmt,2,SQL_C_ULONG,&cat_metric,sizeof(cat_metric),&err);
107 SQLBindCol(stmt,3,SQL_C_ULONG,&var_metric,sizeof(var_metric),&err);
108 SQLBindCol(stmt,4,SQL_C_ULONG,&commented,sizeof(commented),&err);
109 SQLBindCol(stmt,5,SQL_C_CHAR,&filename,sizeof(filename),&err);
110 SQLBindCol(stmt,6,SQL_C_CHAR,&category,sizeof(category),&err);
111 SQLBindCol(stmt,7,SQL_C_CHAR,&var_name,sizeof(var_name),&err);
112 SQLBindCol(stmt,8,SQL_C_CHAR,&var_val,sizeof(var_val),&err);
114 sprintf(sql,"select * from %s where filename='%s' and commented=0 order by filename,cat_metric,var_metric,id",table,file);
115 res = SQLExecDirect(stmt,sql,SQL_NTS);
117 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
118 ast_log(LOG_WARNING,"SQL select error!\n[%s]\n\n",sql);
122 res=SQLNumResultCols(stmt,&rowcount);
124 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
125 ast_log(LOG_WARNING,"SQL select error!\n[%s]\n\n",sql);
137 cur_cat = *new_cat_p;
147 while(res != SQL_NO_DATA) {
148 //ast_log(LOG_NOTICE,"found %s %s %s\n",filename,var_name,var_val);
149 // build the config sql table probably needs to be improved!
151 if(!strcmp(var_name,"#include") && recur < MAX_INCLUDE_LEVEL) {
153 config_odbc(var_val,new,&cur_cat,&cur_v,recur + 1
154 #ifdef PRESERVE_COMMENTS
164 if(strcmp(last,category)) {
165 strcpy(last,category);
166 new_cat = (struct ast_category *) ast_new_category(category);
174 cur_cat->next = new_cat;
175 cur_cat = cur_cat->next;
181 new_v = ast_new_variable(var_name,var_val);
186 cur_cat->root = new_v;
187 cur_v = cur_cat->root;
199 SQLFreeHandle(SQL_HANDLE_STMT,stmt);
202 ast_log(LOG_NOTICE,"found nothing\n");
218 int unload_module(void)
220 ast_cust_config_deregister(®1);
221 ast_log(LOG_NOTICE,"res_config_odbc unloaded.\n");
222 STANDARD_HANGUP_LOCALUSERS;
226 int load_module(void)
229 memset(®1,0,sizeof(struct ast_config_reg));
230 strcpy(reg1.name,"odbc");
231 reg1.func = config_odbc;
232 ast_cust_config_register(®1);
233 ast_log(LOG_NOTICE,"res_config_odbc loaded.\n");
237 char *description(void)
244 /* never unload a config module */
250 return ASTERISK_GPL_KEY;