2 * Asterisk -- A telephony toolkit for Linux.
4 * Connect to PostgreSQL
6 * Copyright (C) 2002, Christos Ricudis
8 * Christos Ricudis <ricudis@itc.auth.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) {
220 strsep(&stringp," "); // eat the first token, we already know it :P
221 var=strsep(&stringp," ");
222 optionstring=strsep(&stringp,"\n");
224 karoto = PQconnectdb(optionstring);
225 if (PQstatus(karoto) == CONNECTION_BAD) {
226 ast_log(LOG_WARNING,"Connection to database using '%s' failed. postgress reports : %s\n", optionstring,
227 PQerrorMessage(karoto));
230 ast_log(LOG_WARNING,"adding identifier\n");
231 id=add_identifier(AST_PGSQL_ID_CONNID,karoto);
234 pbx_builtin_setvar_helper(chan,var,s);
241 static int aPGSQL_query(struct ast_channel *chan, void *data) {
243 char *s1,*s2,*s3,*s4,*s5;
261 strsep(&stringp," "); // eat the first token, we already know it :P
262 s3=strsep(&stringp," ");
263 while (1) { // ugly trick to make branches with break;
265 s4=strsep(&stringp," ");
267 querystring=strsep(&stringp,"\n");
268 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
269 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_query\n",id);
273 PGSQLres=PQexec(karoto,querystring);
274 if (PGSQLres==NULL) {
275 ast_log(LOG_WARNING,"aPGSQL_query: Connection Error (connection identifier = %d, error message : %s)\n",id,PQerrorMessage(karoto));
279 if (PQresultStatus(PGSQLres) == PGRES_BAD_RESPONSE ||
280 PQresultStatus(PGSQLres) == PGRES_NONFATAL_ERROR ||
281 PQresultStatus(PGSQLres) == PGRES_FATAL_ERROR) {
282 ast_log(LOG_WARNING,"aPGSQL_query: Query Error (connection identifier : %d, error message : %s)\n",id,PQcmdStatus(PGSQLres));
286 nres=PQnfields(PGSQLres);
287 id1=add_identifier(AST_PGSQL_ID_RESID,PGSQLres);
289 sprintf(s5,"%d",id1);
290 pbx_builtin_setvar_helper(chan,var,s);
300 static int aPGSQL_fetch(struct ast_channel *chan, void *data) {
302 char *s1,*s2,*s3,*s4,*s5,*s6,*s7;
311 struct ast_var_t *variables;
312 struct varshead *headp;
315 headp=&chan->varshead;
324 strsep(&stringp," "); // eat the first token, we already know it :P
325 s3=strsep(&stringp," ");
326 while (1) { // ugly trick to make branches with break;
330 AST_LIST_TRAVERSE(headp,variables,entries) {
331 if (strncasecmp(ast_var_name(variables),s3,strlen(s3))==0) {
332 s7=ast_var_value(variables);
340 pbx_builtin_setvar_helper(chan,s3,s7);
343 s4=strsep(&stringp," ");
344 id=atoi(s4); // resultid
345 if ((PGSQLres=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
346 ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_fetch\n",id);
350 id=atoi(s7); //fetchid
351 if ((lalares=find_identifier(id,AST_PGSQL_ID_FETCHID))==NULL) {
356 del_identifier(id,AST_PGSQL_ID_FETCHID);
358 nres=PQnfields(PGSQLres);
359 ast_log(LOG_WARNING,"ast_PGSQL_fetch : nres = %d i = %d ;\n",nres,i);
360 for (j=0;j<nres;j++) {
361 s5=strsep(&stringp," ");
363 ast_log(LOG_WARNING,"ast_PGSQL_fetch : More tuples (%d) than variables (%d)\n",nres,j);
367 s6=PQgetvalue(PGSQLres,i,j);
369 ast_log(LOG_WARNING,"PWgetvalue(res,%d,%d) returned NULL in ast_PGSQL_fetch\n",i,j);
372 ast_log(LOG_WARNING,"===setting variable '%s' to '%s'\n",s5,s6);
373 pbx_builtin_setvar_helper(chan,s5,s6);
376 if (i<PQntuples(PGSQLres)) {
377 lalares=malloc(sizeof(int));
379 id1=add_identifier(AST_PGSQL_ID_FETCHID,lalares);
384 sprintf(s5,"%d",id1);
385 ast_log(LOG_WARNING,"Setting var '%s' to value '%s'\n",s3,s);
386 pbx_builtin_setvar_helper(chan,s3,s);
395 static int aPGSQL_reset(struct ast_channel *chan, void *data) {
408 strsep(&stringp," "); // eat the first token, we already know it :P
409 s3=strsep(&stringp," ");
411 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
412 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_reset\n",id);
421 static int aPGSQL_clear(struct ast_channel *chan, void *data) {
434 strsep(&stringp," "); // eat the first token, we already know it :P
435 s3=strsep(&stringp," ");
437 if ((karoto=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
438 ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_clear\n",id);
441 del_identifier(id,AST_PGSQL_ID_RESID);
451 static int aPGSQL_disconnect(struct ast_channel *chan, void *data) {
464 strsep(&stringp," "); // eat the first token, we already know it :P
465 s3=strsep(&stringp," ");
467 if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
468 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_disconnect\n",id);
471 del_identifier(id,AST_PGSQL_ID_CONNID);
478 static int aPGSQL_debug(struct ast_channel *chan, void *data) {
479 ast_log(LOG_WARNING,"Debug : %s\n",(char *)data);
485 static int PGSQL_exec(struct ast_channel *chan, void *data)
491 ast_log(LOG_WARNING, "APP_PGSQL requires an argument (see manual)\n");
497 if (strncasecmp("connect",data,strlen("connect"))==0) {
498 result=(aPGSQL_connect(chan,data));
499 } else if (strncasecmp("query",data,strlen("query"))==0) {
500 result=(aPGSQL_query(chan,data));
501 } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
502 result=(aPGSQL_fetch(chan,data));
503 } else if (strncasecmp("reset",data,strlen("reset"))==0) {
504 result=(aPGSQL_reset(chan,data));
505 } else if (strncasecmp("clear",data,strlen("clear"))==0) {
506 result=(aPGSQL_clear(chan,data));
507 } else if (strncasecmp("debug",data,strlen("debug"))==0) {
508 result=(aPGSQL_debug(chan,data));
509 } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
510 result=(aPGSQL_disconnect(chan,data));
512 ast_log(LOG_WARNING, "Unknown APP_PGSQL argument : %s\n",(char *)data);
516 LOCAL_USER_REMOVE(u);
521 int unload_module(void)
523 STANDARD_HANGUP_LOCALUSERS;
524 return ast_unregister_application(app);
527 int load_module(void)
529 struct PGSQLidshead *headp;
533 AST_LIST_HEAD_INIT(headp);
534 return ast_register_application(app, PGSQL_exec, synopsis, descrip);
537 char *description(void)
545 STANDARD_USECOUNT(res);
551 return ASTERISK_GPL_KEY;