2 * Asterisk -- A telephony toolkit for Linux.
4 * Connect to PostgreSQL
6 * Copyright (C) 2002, Christos Ricudis
8 * Christos Ricudis <ricudis@paiko.gr>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/linkedlists.h>
20 #include <asterisk/chanvars.h>
21 #include <asterisk/lock.h>
26 #include <sys/types.h>
35 static char *tdesc = "Simple PostgreSQL Interface";
37 static char *app = "PGSQL";
39 static char *synopsis = "Do several SQLy things";
41 static char *descrip =
42 " PGSQL(): Do several SQLy things\n";
46 Syntax of SQL commands :
48 Connect #var option-string
50 Connects to a database using the option-string and stores the
51 connection identifier in $var
54 Query var connection-identifier query-string
56 Submits query-string to database backend and stores the result
60 Fetch statusvar result-identifier var1 var2 var3 ... varn
62 Fetches a row from the query and stores end-of-table status in
63 ${statusvar} and columns in ${var1}..${varn}
66 Clear result-identifier
68 Clears data structures associated with result-identifier
71 Disconnect connection-identifier
73 Disconnects from named connection
79 $2 = Connection Identifier
80 $3 = Result Identifier
81 $4 = Fetch Status Identifier (0 = no more rows)
82 $5, $6 = Data variables
85 exten => s,2,PGSQL,"Connect connid host=localhost user=asterisk dbname=credit";
86 exten => s,3,PGSQL,"Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${callerid}";
87 exten => s,4,PGSQL,"Fetch fetchid ${resultid} datavar1 datavar2";
88 exten => s,5,GotoIf,"${fetchid}=1?s|6:s|8";
89 exten => s,6,blablabla ${datavar1} ${datavar2} (does blablabla, datavar1 = username, datavar2 = credit);
91 exten => s,8,PGSQL,"Clear ${resultid}";
92 exten => s,9,PGSQL,"Disconnect ${connid}";
100 extern void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
102 #define AST_PGSQL_ID_DUMMY 0
103 #define AST_PGSQL_ID_CONNID 1
104 #define AST_PGSQL_ID_RESID 2
105 #define AST_PGSQL_ID_FETCHID 3
107 struct ast_PGSQL_id {
108 int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
111 AST_LIST_ENTRY(ast_PGSQL_id) entries;
114 AST_LIST_HEAD(PGSQLidshead,ast_PGSQL_id) PGSQLidshead;
116 static void *find_identifier(int identifier,int identifier_type) {
117 struct PGSQLidshead *headp;
118 struct ast_PGSQL_id *i;
124 if (AST_LIST_LOCK(headp)) {
125 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
127 AST_LIST_TRAVERSE(headp,i,entries) {
128 if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
135 ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
137 AST_LIST_UNLOCK(headp);
143 static int add_identifier(int identifier_type,void *data) {
144 struct ast_PGSQL_id *i,*j;
145 struct PGSQLidshead *headp;
152 if (AST_LIST_LOCK(headp)) {
153 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
156 i=malloc(sizeof(struct ast_PGSQL_id));
157 AST_LIST_TRAVERSE(headp,j,entries) {
158 if (j->identifier>maxidentifier) {
159 maxidentifier=j->identifier;
163 i->identifier=maxidentifier+1;
164 i->identifier_type=identifier_type;
166 AST_LIST_INSERT_HEAD(headp,i,entries);
167 AST_LIST_UNLOCK(headp);
169 return(i->identifier);
172 static int del_identifier(int identifier,int identifier_type) {
173 struct ast_PGSQL_id *i;
174 struct PGSQLidshead *headp;
179 if (AST_LIST_LOCK(headp)) {
180 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
182 AST_LIST_TRAVERSE(headp,i,entries) {
183 if ((i->identifier==identifier) &&
184 (i->identifier_type==identifier_type)) {
185 AST_LIST_REMOVE(headp,i,ast_PGSQL_id,entries);
191 AST_LIST_UNLOCK(headp);
195 ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
202 static int aPGSQL_connect(struct ast_channel *chan, void *data) {
219 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
220 var=strtok_r(NULL," ",&ptrptr);
221 optionstring=strtok_r(NULL,"\n",&ptrptr);
223 karoto = PQconnectdb(optionstring);
224 if (PQstatus(karoto) == CONNECTION_BAD) {
225 ast_log(LOG_WARNING,"Connection to database using '%s' failed. postgress reports : %s\n", optionstring,
226 PQerrorMessage(karoto));
229 ast_log(LOG_WARNING,"adding identifier\n");
230 id=add_identifier(AST_PGSQL_ID_CONNID,karoto);
233 pbx_builtin_setvar_helper(chan,var,s);
240 static int aPGSQL_query(struct ast_channel *chan, void *data) {
243 char *s1,*s2,*s3,*s4,*s5;
259 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
260 s3=strtok_r(NULL," ",&ptrptr);
261 while (1) { // ugly trick to make branches with break;
263 s4=strtok_r(NULL," ",&ptrptr);
265 querystring=strtok_r(NULL,"\n",&ptrptr);
266 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
267 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_query\n",id);
271 PGSQLres=PQexec(karoto,querystring);
272 if (PGSQLres==NULL) {
273 ast_log(LOG_WARNING,"aPGSQL_query: Connection Error (connection identifier = %d, error message : %s)\n",id,PQerrorMessage(karoto));
277 if (PQresultStatus(PGSQLres) == PGRES_BAD_RESPONSE ||
278 PQresultStatus(PGSQLres) == PGRES_NONFATAL_ERROR ||
279 PQresultStatus(PGSQLres) == PGRES_FATAL_ERROR) {
280 ast_log(LOG_WARNING,"aPGSQL_query: Query Error (connection identifier : %d, error message : %s)\n",id,PQcmdStatus(PGSQLres));
284 nres=PQnfields(PGSQLres);
285 id1=add_identifier(AST_PGSQL_ID_RESID,PGSQLres);
287 sprintf(s5,"%d",id1);
288 pbx_builtin_setvar_helper(chan,var,s);
298 static int aPGSQL_fetch(struct ast_channel *chan, void *data) {
301 char *s1,*s2,*s3,*s4,*s5,*s6,*s7;
310 struct ast_var_t *variables;
311 struct varshead *headp;
313 headp=&chan->varshead;
321 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
322 s3=strtok_r(NULL," ",&ptrptr);
323 while (1) { // ugly trick to make branches with break;
327 AST_LIST_TRAVERSE(headp,variables,entries) {
328 if (strncasecmp(ast_var_name(variables),s3,strlen(s3))==0) {
329 s7=ast_var_value(variables);
337 pbx_builtin_setvar_helper(chan,s3,s7);
340 s4=strtok_r(NULL," ",&ptrptr);
341 id=atoi(s4); // resultid
342 if ((PGSQLres=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
343 ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_fetch\n",id);
347 id=atoi(s7); //fetchid
348 if ((lalares=find_identifier(id,AST_PGSQL_ID_FETCHID))==NULL) {
353 del_identifier(id,AST_PGSQL_ID_FETCHID);
355 nres=PQnfields(PGSQLres);
356 ast_log(LOG_WARNING,"ast_PGSQL_fetch : nres = %d i = %d ;\n",nres,i);
357 for (j=0;j<nres;j++) {
358 s5=strtok_r(NULL," ",&ptrptr);
360 ast_log(LOG_WARNING,"ast_PGSQL_fetch : More tuples (%d) than variables (%d)\n",nres,j);
364 s6=PQgetvalue(PGSQLres,i,j);
366 ast_log(LOG_WARNING,"PWgetvalue(res,%d,%d) returned NULL in ast_PGSQL_fetch\n",i,j);
369 ast_log(LOG_WARNING,"===setting variable '%s' to '%s'\n",s5,s6);
370 pbx_builtin_setvar_helper(chan,s5,s6);
373 if (i<PQntuples(PGSQLres)) {
374 lalares=malloc(sizeof(int));
376 id1=add_identifier(AST_PGSQL_ID_FETCHID,lalares);
381 sprintf(s5,"%d",id1);
382 ast_log(LOG_WARNING,"Setting var '%s' to value '%s'\n",s3,s);
383 pbx_builtin_setvar_helper(chan,s3,s);
392 static int aPGSQL_reset(struct ast_channel *chan, void *data) {
404 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
405 s3=strtok_r(NULL," ",&ptrptr);
407 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
408 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_reset\n",id);
417 static int aPGSQL_clear(struct ast_channel *chan, void *data) {
429 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
430 s3=strtok_r(NULL," ",&ptrptr);
432 if ((karoto=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
433 ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_clear\n",id);
436 del_identifier(id,AST_PGSQL_ID_RESID);
446 static int aPGSQL_disconnect(struct ast_channel *chan, void *data) {
458 strtok_r(s1," ",&ptrptr); // eat the first token, we already know it :P
459 s3=strtok_r(NULL," ",&ptrptr);
461 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
462 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_disconnect\n",id);
465 del_identifier(id,AST_PGSQL_ID_CONNID);
472 static int aPGSQL_debug(struct ast_channel *chan, void *data) {
473 ast_log(LOG_WARNING,"Debug : %s\n",(char *)data);
479 static int PGSQL_exec(struct ast_channel *chan, void *data)
485 ast_log(LOG_WARNING, "APP_PGSQL requires an argument (see manual)\n");
491 if (strncasecmp("connect",data,strlen("connect"))==0) {
492 result=(aPGSQL_connect(chan,data));
493 } else if (strncasecmp("query",data,strlen("query"))==0) {
494 result=(aPGSQL_query(chan,data));
495 } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
496 result=(aPGSQL_fetch(chan,data));
497 } else if (strncasecmp("reset",data,strlen("reset"))==0) {
498 result=(aPGSQL_reset(chan,data));
499 } else if (strncasecmp("clear",data,strlen("clear"))==0) {
500 result=(aPGSQL_clear(chan,data));
501 } else if (strncasecmp("debug",data,strlen("debug"))==0) {
502 result=(aPGSQL_debug(chan,data));
503 } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
504 result=(aPGSQL_disconnect(chan,data));
506 ast_log(LOG_WARNING, "Unknown APP_PGSQL argument : %s\n",(char *)data);
510 LOCAL_USER_REMOVE(u);
515 int unload_module(void)
517 STANDARD_HANGUP_LOCALUSERS;
518 return ast_unregister_application(app);
521 int load_module(void)
523 struct PGSQLidshead *headp;
527 AST_LIST_HEAD_INIT(headp);
528 return ast_register_application(app, PGSQL_exec, synopsis, descrip);
531 char *description(void)
539 STANDARD_USECOUNT(res);
545 return ASTERISK_GPL_KEY;