Fri Aug 24 02:22:24 2007

Asterisk developer's documentation


app_addon_sql_mysql.c File Reference

#include <asterisk.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <mysql/mysql.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/linkedlists.h>
#include <asterisk/chanvars.h>
#include <asterisk/lock.h>

Include dependency graph for app_addon_sql_mysql.c:

Go to the source code of this file.

Data Structures

struct  ast_MYSQL_id

Defines

#define AST_MODULE   "app_addon_sql_mysql"
#define AST_MYSQL_ID_CONNID   1
#define AST_MYSQL_ID_DUMMY   0
#define AST_MYSQL_ID_FETCHID   3
#define AST_MYSQL_ID_RESID   2
#define EXTRA_LOG   0

Functions

static int add_identifier (int identifier_type, void *data)
static int add_identifier_and_set_asterisk_int (struct ast_channel *chan, char *varname, int identifier_type, void *data)
static int aMYSQL_clear (struct ast_channel *chan, char *data)
static int aMYSQL_connect (struct ast_channel *chan, char *data)
static int aMYSQL_disconnect (struct ast_channel *chan, char *data)
static int aMYSQL_fetch (struct ast_channel *chan, char *data)
static int aMYSQL_query (struct ast_channel *chan, char *data)
 AST_LIST_HEAD (MYSQLidshead, ast_MYSQL_id)
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Simple Mysql Interface")
 AST_MUTEX_DEFINE_STATIC (_mysql_mutex)
static int del_identifier (int identifier, int identifier_type)
static int load_module (void)
static int MYSQL_exec (struct ast_channel *chan, void *data)
static int safe_scan_int (char **data, char *delim, int def)
static int set_asterisk_int (struct ast_channel *chan, char *varname, int id)
static int unload_module (void)

Variables

static char * app = "MYSQL"
static char * descrip
static char * synopsis = "Do several mySQLy things"


Define Documentation

#define AST_MODULE   "app_addon_sql_mysql"

Definition at line 39 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_CONNID   1

Definition at line 84 of file app_addon_sql_mysql.c.

Referenced by aMYSQL_connect(), aMYSQL_disconnect(), and aMYSQL_query().

#define AST_MYSQL_ID_DUMMY   0

Definition at line 83 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_FETCHID   3

Definition at line 86 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_RESID   2

Definition at line 85 of file app_addon_sql_mysql.c.

Referenced by aMYSQL_clear(), aMYSQL_fetch(), and aMYSQL_query().

#define EXTRA_LOG   0

Definition at line 41 of file app_addon_sql_mysql.c.


Function Documentation

static int add_identifier ( int  identifier_type,
void *  data 
) [static]

Definition at line 125 of file app_addon_sql_mysql.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_MYSQL_id::data, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, LOG_WARNING, and malloc.

Referenced by add_identifier_and_set_asterisk_int().

00125                                                           {
00126    struct ast_MYSQL_id *i,*j;
00127    struct MYSQLidshead *headp;
00128    int maxidentifier=0;
00129    
00130    headp=&_mysql_ids_head;
00131    i=NULL;
00132    j=NULL;
00133    
00134    if (AST_LIST_LOCK(headp)) {
00135       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00136       return(-1);
00137    } else {
00138       i=malloc(sizeof(struct ast_MYSQL_id));
00139       AST_LIST_TRAVERSE(headp,j,entries) {
00140          if (j->identifier>maxidentifier) {
00141             maxidentifier=j->identifier;
00142          }
00143       }
00144       i->identifier=maxidentifier+1;
00145       i->identifier_type=identifier_type;
00146       i->data=data;
00147       AST_LIST_INSERT_HEAD(headp,i,entries);
00148       AST_LIST_UNLOCK(headp);
00149    }
00150    return i->identifier;
00151 }

static int add_identifier_and_set_asterisk_int ( struct ast_channel chan,
char *  varname,
int  identifier_type,
void *  data 
) [static]

Definition at line 195 of file app_addon_sql_mysql.c.

References add_identifier(), and set_asterisk_int().

Referenced by aMYSQL_connect(), and aMYSQL_query().

00195                                                                                                                          {
00196    return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
00197 }

static int aMYSQL_clear ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 348 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_RESID, del_identifier(), id, LOG_WARNING, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00348                                                               {
00349 
00350    MYSQL_RES *mysqlres;
00351 
00352    int id;
00353    strsep(&data," "); // eat the first token, we already know it :P 
00354    id = safe_scan_int(&data," \n",-1);
00355    if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
00356       ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
00357    } else {
00358       mysql_free_result(mysqlres);
00359       del_identifier(id,AST_MYSQL_ID_RESID);
00360    }
00361 
00362    return 0;
00363 }

static int aMYSQL_connect ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 211 of file app_addon_sql_mysql.c.

References add_identifier_and_set_asterisk_int(), ast_log(), AST_MYSQL_ID_CONNID, dbhost, dbname, dbpass, dbuser, LOG_WARNING, mysql, and strsep().

Referenced by MYSQL_exec().

00211                                                                 {
00212    
00213    MYSQL *mysql;
00214 
00215    char *connid_var;
00216    char *dbhost;
00217    char *dbuser;
00218    char *dbpass;
00219    char *dbname;
00220     
00221    strsep(&data," "); // eat the first token, we already know it :P 
00222 
00223    connid_var=strsep(&data," ");
00224    dbhost=strsep(&data," ");
00225    dbuser=strsep(&data," ");
00226    dbpass=strsep(&data," ");
00227    dbname=strsep(&data,"\n");
00228    
00229    if( connid_var && dbhost && dbuser && dbpass && dbname ) {
00230       mysql = mysql_init(NULL);
00231       if (mysql) {
00232          if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
00233             add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
00234             return 0;
00235          }
00236          else {
00237             ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
00238          }
00239       }
00240       else {
00241          ast_log(LOG_WARNING,"myslq_init returned NULL\n");
00242       }
00243    }
00244    else {
00245       ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
00246    }
00247 
00248    return -1;
00249 }

static int aMYSQL_disconnect ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 365 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_CONNID, del_identifier(), id, LOG_WARNING, mysql, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00365                                                                    {
00366    
00367    MYSQL *mysql;
00368    int id;
00369    strsep(&data," "); // eat the first token, we already know it :P 
00370 
00371    id = safe_scan_int(&data," \n",-1);
00372    if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
00373       ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
00374    } else {
00375       mysql_close(mysql);
00376       del_identifier(id,AST_MYSQL_ID_CONNID);
00377    } 
00378 
00379    return 0;
00380 }

static int aMYSQL_fetch ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 298 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_RESID, LOG_WARNING, pbx_builtin_setvar_helper(), safe_scan_int(), set_asterisk_int(), and strsep().

Referenced by MYSQL_exec().

00298                                                               {
00299    
00300    MYSQL_RES *mysqlres;
00301    MYSQL_ROW mysqlrow;
00302 
00303    char *fetchid_var,*s5,*s6;
00304    int resultid,numFields,j;
00305    
00306    strsep(&data," "); // eat the first token, we already know it :P 
00307 
00308    fetchid_var = strsep(&data," ");
00309    resultid    = safe_scan_int(&data," ",-1);
00310 
00311    if (fetchid_var && (resultid>=0) ) {
00312       if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
00313          /* Grab the next row */
00314          if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
00315             numFields=mysql_num_fields(mysqlres);
00316             for (j=0;j<numFields;j++) {
00317                s5=strsep(&data," ");
00318                if (s5==NULL) {
00319                   ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
00320                   break;
00321                }
00322                s6=mysqlrow[j];
00323                pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
00324             }
00325 #if EXTRA_LOG
00326             ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
00327 #endif
00328             set_asterisk_int(chan,fetchid_var,1); // try more rows
00329          } else {
00330 #if EXTRA_LOG
00331             ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
00332 #endif
00333             set_asterisk_int(chan,fetchid_var,0); // no more rows
00334          }
00335          return 0;
00336       }
00337       else {
00338          ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
00339       }
00340    }
00341    else {
00342       ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
00343    }
00344 
00345    return -1;
00346 }

static int aMYSQL_query ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 251 of file app_addon_sql_mysql.c.

References add_identifier_and_set_asterisk_int(), ast_log(), AST_MYSQL_ID_CONNID, AST_MYSQL_ID_RESID, LOG_WARNING, mysql, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00251                                                               {
00252    
00253    MYSQL       *mysql;
00254    MYSQL_RES   *mysqlres;
00255 
00256    char *resultid_var;
00257    int connid;
00258    char *querystring;
00259    int mysql_query_res;
00260 
00261    strsep(&data," "); // eat the first token, we already know it :P 
00262 
00263    resultid_var = strsep(&data," ");
00264    connid       = safe_scan_int(&data," ",-1);
00265    querystring  = strsep(&data,"\n");
00266 
00267    if (resultid_var && (connid>=0) && querystring) {
00268       if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
00269          mysql_query_res = mysql_query(mysql,querystring);
00270          if (mysql_query_res != 0) {
00271             ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
00272          }
00273          else {
00274             if ((mysqlres=mysql_use_result(mysql))!=NULL) {
00275                add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
00276                return 0;
00277             }
00278             else if (mysql_field_count(mysql)==0) {
00279                return 0;  // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
00280             }
00281             else {
00282                ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
00283             }
00284          }
00285       }
00286       else {
00287          ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
00288       }
00289    }
00290    else {
00291       ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
00292    }
00293    
00294    return -1;
00295 }

AST_LIST_HEAD ( MYSQLidshead  ,
ast_MYSQL_id   
)

Definition at line 95 of file app_addon_sql_mysql.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_MYSQL_id::data, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, and LOG_WARNING.

00098                                                                  {
00099    struct MYSQLidshead *headp;
00100    struct ast_MYSQL_id *i;
00101    void *res=NULL;
00102    int found=0;
00103    
00104    headp=&_mysql_ids_head;
00105    
00106    if (AST_LIST_LOCK(headp)) {
00107       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00108    } else {
00109       AST_LIST_TRAVERSE(headp,i,entries) {
00110          if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
00111             found=1;
00112             res=i->data;
00113             break;
00114          }
00115       }
00116       if (!found) {
00117          ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
00118       }
00119       AST_LIST_UNLOCK(headp);
00120    }
00121    
00122    return res;
00123 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Simple Mysql Interface"   
)

AST_MUTEX_DEFINE_STATIC ( _mysql_mutex   ) 

static int del_identifier ( int  identifier,
int  identifier_type 
) [static]

Definition at line 153 of file app_addon_sql_mysql.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), free, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, and LOG_WARNING.

Referenced by aMYSQL_clear(), and aMYSQL_disconnect().

00153                                                               {
00154    struct ast_MYSQL_id *i;
00155    struct MYSQLidshead *headp;
00156    int found=0;
00157    
00158         headp=&_mysql_ids_head;
00159         
00160         if (AST_LIST_LOCK(headp)) {
00161       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00162    } else {
00163       AST_LIST_TRAVERSE(headp,i,entries) {
00164          if ((i->identifier==identifier) && 
00165              (i->identifier_type==identifier_type)) {
00166             AST_LIST_REMOVE(headp,i,entries);
00167             free(i);
00168             found=1;
00169             break;
00170          }
00171       }
00172       AST_LIST_UNLOCK(headp);
00173    }
00174                    
00175    if (found==0) {
00176       ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
00177       return(-1);
00178    } else {
00179       return(0);
00180    }
00181 }

static int load_module ( void   )  [static]

Definition at line 431 of file app_addon_sql_mysql.c.

References app, AST_LIST_HEAD_INIT, ast_register_application(), descrip, MYSQL_exec(), and synopsis.

00432 {
00433    struct MYSQLidshead *headp = &_mysql_ids_head;
00434    AST_LIST_HEAD_INIT(headp);
00435    return ast_register_application(app, MYSQL_exec, synopsis, descrip);
00436 }

static int MYSQL_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 382 of file app_addon_sql_mysql.c.

References aMYSQL_clear(), aMYSQL_connect(), aMYSQL_disconnect(), aMYSQL_fetch(), aMYSQL_query(), ast_log(), ast_module_user_add, ast_module_user_remove, ast_mutex_lock(), ast_mutex_unlock(), ast_strdupa, ast_module_user::chan, LOG_WARNING, pbx_builtin_setvar_helper(), and result.

Referenced by load_module().

00383 {
00384    struct ast_module_user *u;
00385    int result;
00386    char sresult[10];
00387 
00388 #if EXTRA_LOG
00389    fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
00390 #endif
00391 
00392    if (!data) {
00393       ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
00394       return -1;
00395    }
00396 
00397    u = ast_module_user_add(chan);
00398    result=0;
00399 
00400    ast_mutex_lock(&_mysql_mutex);
00401 
00402    if (strncasecmp("connect",data,strlen("connect"))==0) {
00403       result=aMYSQL_connect(chan,ast_strdupa(data));
00404    } else   if (strncasecmp("query",data,strlen("query"))==0) {
00405       result=aMYSQL_query(chan,ast_strdupa(data));
00406    } else   if (strncasecmp("fetch",data,strlen("fetch"))==0) {
00407       result=aMYSQL_fetch(chan,ast_strdupa(data));
00408    } else   if (strncasecmp("clear",data,strlen("clear"))==0) {
00409       result=aMYSQL_clear(chan,ast_strdupa(data));
00410    } else   if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
00411       result=aMYSQL_disconnect(chan,ast_strdupa(data));
00412    } else {
00413       ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
00414       result=-1;  
00415    }
00416       
00417    ast_mutex_unlock(&_mysql_mutex);
00418 
00419    ast_module_user_remove(u);
00420    snprintf(sresult, sizeof(sresult), "%d", result);
00421    pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
00422    return 0;
00423 }

static int safe_scan_int ( char **  data,
char *  delim,
int  def 
) [static]

Definition at line 199 of file app_addon_sql_mysql.c.

References s, and strsep().

Referenced by aMYSQL_clear(), aMYSQL_disconnect(), aMYSQL_fetch(), and aMYSQL_query().

00199                                                               {
00200    char* end;
00201    int res = def;
00202    char* s = strsep(data,delim);
00203    if( s ) {
00204       res = strtol(s,&end,10);
00205       if (*end) res = def;  /* not an integer */
00206    }
00207    return res;
00208 }

static int set_asterisk_int ( struct ast_channel chan,
char *  varname,
int  id 
) [static]

Definition at line 183 of file app_addon_sql_mysql.c.

References ast_log(), LOG_WARNING, pbx_builtin_setvar_helper(), and s.

Referenced by add_identifier_and_set_asterisk_int(), and aMYSQL_fetch().

00183                                                                              {
00184    if( id>=0 ) {
00185       char s[100] = "";
00186       snprintf(s, sizeof(s)-1, "%d", id);
00187 #if EXTRA_LOG
00188       ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
00189 #endif
00190       pbx_builtin_setvar_helper(chan,varname,s);
00191    }
00192    return id;
00193 }

static int unload_module ( void   )  [static]

Definition at line 425 of file app_addon_sql_mysql.c.

References app, ast_module_user_hangup_all, and ast_unregister_application().

00426 {
00427    ast_module_user_hangup_all();
00428    return ast_unregister_application(app);
00429 }


Variable Documentation

char* app = "MYSQL" [static]

Definition at line 43 of file app_addon_sql_mysql.c.

char* descrip [static]

Definition at line 47 of file app_addon_sql_mysql.c.

char* synopsis = "Do several mySQLy things" [static]

Definition at line 45 of file app_addon_sql_mysql.c.


Generated on Fri Aug 24 02:22:24 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1