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>
12 #include <asterisk/file.h>
13 #include <asterisk/logger.h>
14 #include <asterisk/channel.h>
15 #include <asterisk/pbx.h>
16 #include <asterisk/config.h>
17 #include <asterisk/config_pvt.h>
18 #include <asterisk/module.h>
19 #include <asterisk/lock.h>
23 #include <asterisk/res_odbc.h>
25 static char *tdesc = "ODBC Configuration";
26 static struct ast_config_reg reg1;
32 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
33 #ifdef PRESERVE_COMMENTS
34 , struct ast_comment_struct *acs
38 struct ast_config *config, *new;
39 struct ast_variable *v, *cur_v, *new_v;
40 struct ast_category *cur_cat, *new_cat;
43 int configured = 0, res = 0;
45 SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
47 char sql[255], filename[128], category[128], var_name[128], var_val[128];
48 SQLSMALLINT rowcount=0;
58 new = ast_new_config ();
63 if (!file || !strcmp (file, "res_config_odbc.conf"))
64 return NULL; // cant configure myself with myself !
66 config = ast_load ("res_config_odbc.conf");
69 for (v = ast_variable_browse (config, "settings"); v; v = v->next) {
70 if (!strcmp (v->name, "table")) {
71 strncpy (table, v->value, sizeof (table));
73 } else if (!strcmp (v->name, "connection")) {
74 strncpy (connection, v->value, sizeof (connection));
84 obj = fetch_odbc_obj (connection);
88 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
90 SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
91 SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
92 SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
93 SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
94 SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
95 SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
96 SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
97 SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
99 sprintf (sql, "select * from %s where filename='%s' and commented=0 order by filename,cat_metric desc,var_metric asc,id", table, file);
100 res = SQLExecDirect (stmt, sql, SQL_NTS);
102 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
103 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
107 res = SQLNumResultCols (stmt, &rowcount);
109 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
110 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
115 res = SQLFetch (stmt);
118 cur_cat = *new_cat_p;
126 while (res != SQL_NO_DATA) {
127 if (!strcmp (var_name, "#include") && recur < MAX_INCLUDE_LEVEL) {
129 config_odbc (var_val, new, &cur_cat, &cur_v, recur + 1
130 #ifdef PRESERVE_COMMENTS
135 if (strcmp (last, category) || last_cat_metric != cat_metric) {
136 strcpy (last, category);
137 last_cat_metric = cat_metric;
138 new_cat = (struct ast_category *) ast_new_category (category);
145 cur_cat->next = new_cat;
146 cur_cat = cur_cat->next;
152 new_v = ast_new_variable (var_name, var_val);
156 cur_cat->root = new_v;
157 cur_v = cur_cat->root;
165 res = SQLFetch (stmt);
168 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
170 ast_log (LOG_NOTICE, "found nothing\n");
176 int unload_module (void)
178 ast_cust_config_deregister (®1);
179 ast_log (LOG_NOTICE, "res_config_odbc unloaded.\n");
180 STANDARD_HANGUP_LOCALUSERS;
184 int load_module (void)
186 memset (®1, 0, sizeof (struct ast_config_reg));
187 strcpy (reg1.name, "odbc");
188 reg1.func = config_odbc;
189 ast_cust_config_register (®1);
190 ast_log (LOG_NOTICE, "res_config_odbc loaded.\n");
194 char *description (void)
201 /* never unload a config module */
207 return ASTERISK_GPL_KEY;