mirror of
https://github.com/asterisk/asterisk.git
synced 2025-11-18 15:49:56 +00:00
Massive code formatting cleanup in res_odbc.c thanks to bkw_
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@3404 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
152
res/res_odbc.c
152
res/res_odbc.c
@@ -9,8 +9,6 @@
|
||||
* Copyright (C) 2004 Anthony Minessale II <anthmct@yahoo.com>
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <asterisk/file.h>
|
||||
#include <asterisk/logger.h>
|
||||
#include <asterisk/channel.h>
|
||||
@@ -26,7 +24,8 @@
|
||||
#include <asterisk/res_odbc.h>
|
||||
#define MAX_ODBC_HANDLES 25
|
||||
|
||||
struct odbc_list {
|
||||
struct odbc_list
|
||||
{
|
||||
char name[80];
|
||||
odbc_obj *obj;
|
||||
int used;
|
||||
@@ -34,7 +33,8 @@ struct odbc_list {
|
||||
|
||||
static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
|
||||
|
||||
static void odbc_destroy(void) {
|
||||
static void odbc_destroy(void)
|
||||
{
|
||||
int x = 0;
|
||||
|
||||
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
|
||||
@@ -43,10 +43,10 @@ static void odbc_destroy(void) {
|
||||
ODBC_REGISTRY[x].obj = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static odbc_obj *odbc_read(struct odbc_list *registry,char *name) {
|
||||
static odbc_obj *odbc_read(struct odbc_list *registry, char *name)
|
||||
{
|
||||
int x = 0;
|
||||
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
|
||||
if (registry[x].used && !strcmp(registry[x].name, name)) {
|
||||
@@ -56,7 +56,8 @@ static odbc_obj *odbc_read(struct odbc_list *registry,char *name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int odbc_write(struct odbc_list *registry,char *name,odbc_obj *obj) {
|
||||
static int odbc_write(struct odbc_list *registry, char *name, odbc_obj * obj)
|
||||
{
|
||||
int x = 0;
|
||||
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
|
||||
if (!registry[x].used) {
|
||||
@@ -69,19 +70,19 @@ static int odbc_write(struct odbc_list *registry,char *name,odbc_obj *obj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void odbc_init(void) {
|
||||
static void odbc_init(void)
|
||||
{
|
||||
int x = 0;
|
||||
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
|
||||
memset(&ODBC_REGISTRY[x], 0, sizeof(struct odbc_list));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char *tdesc = "ODBC Resource";
|
||||
/* internal stuff */
|
||||
|
||||
|
||||
static int load_odbc_config(void) {
|
||||
static int load_odbc_config(void)
|
||||
{
|
||||
static char *cfg = "res_odbc.conf";
|
||||
struct ast_config *config;
|
||||
struct ast_variable *v;
|
||||
@@ -95,7 +96,6 @@ static int load_odbc_config(void) {
|
||||
config = ast_load(cfg);
|
||||
if (config) {
|
||||
for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
|
||||
|
||||
if (!strcmp(cat, "ENV")) {
|
||||
for (v = ast_variable_browse(config, cat); v; v = v->next) {
|
||||
env_var = malloc(strlen(v->name) + strlen(v->value) + 2);
|
||||
@@ -108,7 +108,6 @@ static int load_odbc_config(void) {
|
||||
cat = ast_category_browse(config, cat);
|
||||
}
|
||||
|
||||
|
||||
dsn = username = password = NULL;
|
||||
enabled = 1;
|
||||
connect = 0;
|
||||
@@ -133,36 +132,31 @@ static int load_odbc_config(void) {
|
||||
if (connect) {
|
||||
odbc_obj_connect(obj);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
} else {
|
||||
ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
ast_destroy(config);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int odbc_dump_fd(int fd,odbc_obj *obj) {
|
||||
|
||||
int odbc_dump_fd(int fd, odbc_obj * obj)
|
||||
{
|
||||
ast_cli(fd, "\n\nName: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int odbc_usage(int fd) {
|
||||
static int odbc_usage(int fd)
|
||||
{
|
||||
ast_cli(fd, "\n\nusage odbc <command> <arg1> .. <argn>\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int odbc_command(int fd, int argc, char **argv) {
|
||||
static int odbc_command(int fd, int argc, char **argv)
|
||||
{
|
||||
odbc_obj *obj;
|
||||
int x = 0;
|
||||
if (!argv[1])
|
||||
@@ -171,7 +165,6 @@ static int odbc_command(int fd, int argc, char **argv) {
|
||||
ast_cli(fd, "\n\n");
|
||||
|
||||
if (!strcmp(argv[1], "connect") || !strcmp(argv[1], "disconnect")) {
|
||||
|
||||
if (!argv[2])
|
||||
return odbc_usage(fd);
|
||||
|
||||
@@ -182,13 +175,9 @@ static int odbc_command(int fd, int argc, char **argv) {
|
||||
|
||||
if (!strcmp(argv[1], "disconnect"))
|
||||
odbc_obj_disconnect(obj);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
else if(!strcmp(argv[1],"show")) {
|
||||
} else if (!strcmp(argv[1], "show")) {
|
||||
if (!argv[2] || (argv[2] && !strcmp(argv[2], "all"))) {
|
||||
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
|
||||
if (!ODBC_REGISTRY[x].used)
|
||||
@@ -196,42 +185,40 @@ static int odbc_command(int fd, int argc, char **argv) {
|
||||
if (ODBC_REGISTRY[x].obj)
|
||||
odbc_dump_fd(fd, ODBC_REGISTRY[x].obj);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
obj = odbc_read(ODBC_REGISTRY, argv[2]);
|
||||
if (obj)
|
||||
odbc_dump_fd(fd, obj);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
} else {
|
||||
return odbc_usage(fd);
|
||||
|
||||
ast_cli(fd,"\n\n");
|
||||
}
|
||||
ast_cli(fd, "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static struct ast_cli_entry odbc_command_struct = {
|
||||
{"odbc", NULL}, odbc_command,
|
||||
"Execute ODBC Command","obdc <command> <arg1> .. <argn>", NULL };
|
||||
"Execute ODBC Command", "obdc <command> <arg1> .. <argn>", NULL
|
||||
};
|
||||
|
||||
/* api calls */
|
||||
|
||||
int register_odbc_obj(char *name,odbc_obj *obj) {
|
||||
int register_odbc_obj(char *name, odbc_obj * obj)
|
||||
{
|
||||
if (obj != NULL)
|
||||
return odbc_write(ODBC_REGISTRY, name, obj);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
odbc_obj *fetch_odbc_obj(char *name) {
|
||||
odbc_obj *fetch_odbc_obj(char *name)
|
||||
{
|
||||
return (odbc_obj *) odbc_read(ODBC_REGISTRY, name);
|
||||
}
|
||||
|
||||
|
||||
odbc_obj *new_odbc_obj(char *name,char *dsn,char *username, char *password) {
|
||||
odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
|
||||
{
|
||||
static odbc_obj *new;
|
||||
|
||||
new = malloc(sizeof(odbc_obj));
|
||||
@@ -263,8 +250,8 @@ odbc_obj *new_odbc_obj(char *name,char *dsn,char *username, char *password) {
|
||||
return new;
|
||||
}
|
||||
|
||||
void destroy_obdc_obj(odbc_obj **obj) {
|
||||
|
||||
void destroy_obdc_obj(odbc_obj ** obj)
|
||||
{
|
||||
odbc_obj_disconnect(*obj);
|
||||
|
||||
ast_mutex_lock(&(*obj)->lock);
|
||||
@@ -280,28 +267,30 @@ void destroy_obdc_obj(odbc_obj **obj) {
|
||||
free(*obj);
|
||||
}
|
||||
|
||||
odbc_status odbc_obj_disconnect(odbc_obj *obj) {
|
||||
odbc_status odbc_obj_disconnect(odbc_obj * obj)
|
||||
{
|
||||
int res;
|
||||
ast_mutex_lock(&obj->lock);
|
||||
|
||||
if(obj->up)
|
||||
if (obj->up) {
|
||||
res = SQLDisconnect(obj->con);
|
||||
else
|
||||
} else {
|
||||
res = -1;
|
||||
}
|
||||
|
||||
if (res == ODBC_SUCCESS) {
|
||||
ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
|
||||
obj->up = 0;
|
||||
} else {
|
||||
ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
|
||||
obj->name, obj->dsn);
|
||||
}
|
||||
else
|
||||
ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",obj->name,obj->dsn);
|
||||
|
||||
ast_mutex_unlock(&obj->lock);
|
||||
return ODBC_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
odbc_status odbc_obj_connect(odbc_obj *obj) {
|
||||
odbc_status odbc_obj_connect(odbc_obj * obj)
|
||||
{
|
||||
int res;
|
||||
long int err;
|
||||
short int mlen;
|
||||
@@ -310,7 +299,6 @@ odbc_status odbc_obj_connect(odbc_obj *obj) {
|
||||
ast_mutex_lock(&obj->lock);
|
||||
|
||||
if (obj->env == SQL_NULL_HANDLE) {
|
||||
|
||||
res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
|
||||
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
@@ -344,17 +332,10 @@ odbc_status odbc_obj_connect(odbc_obj *obj) {
|
||||
SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
res = SQLConnect(
|
||||
|
||||
obj->con,
|
||||
res = SQLConnect(obj->con,
|
||||
(SQLCHAR *) obj->dsn, SQL_NTS,
|
||||
(SQLCHAR *) obj->username, SQL_NTS,
|
||||
(SQLCHAR*) obj->password,SQL_NTS
|
||||
|
||||
);
|
||||
|
||||
(SQLCHAR *) obj->password, SQL_NTS);
|
||||
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
|
||||
@@ -362,8 +343,7 @@ odbc_status odbc_obj_connect(odbc_obj *obj) {
|
||||
if (option_verbose > 3)
|
||||
ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%ld %s\n", res, err, msg);
|
||||
return ODBC_FAIL;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
|
||||
if (option_verbose > 3)
|
||||
ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
|
||||
@@ -372,30 +352,23 @@ odbc_status odbc_obj_connect(odbc_obj *obj) {
|
||||
|
||||
ast_mutex_unlock(&obj->lock);
|
||||
return ODBC_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
STANDARD_LOCAL_USER;
|
||||
|
||||
LOCAL_USER_DECL;
|
||||
|
||||
|
||||
int unload_module(void) {
|
||||
int unload_module(void)
|
||||
{
|
||||
STANDARD_HANGUP_LOCALUSERS;
|
||||
|
||||
odbc_destroy();
|
||||
ast_cli_unregister(&odbc_command_struct);
|
||||
ast_log(LOG_NOTICE, "res_odbc unloaded.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_module(void) {
|
||||
int load_module(void)
|
||||
{
|
||||
odbc_init();
|
||||
load_odbc_config();
|
||||
ast_cli_register(&odbc_command_struct);
|
||||
@@ -403,28 +376,19 @@ int load_module(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *description(void) {
|
||||
char *description(void)
|
||||
{
|
||||
return tdesc;
|
||||
}
|
||||
|
||||
int usecount(void) {
|
||||
int usecount(void)
|
||||
{
|
||||
int res;
|
||||
STANDARD_USECOUNT(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
char *key() {
|
||||
char *key()
|
||||
{
|
||||
return ASTERISK_GPL_KEY;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user