[Asterisk-cvs] asterisk-addons res_config_mysql.c,1.8,1.9
kpfleming
kpfleming
Fri Aug 26 16:33:03 CDT 2005
Update of /usr/cvsroot/asterisk-addons
In directory mongoose.digium.com:/tmp/cvs-serv9004
Modified Files:
res_config_mysql.c
Log Message:
make driver pay attention to database name in extconfig.conf (issue #4973)
Index: res_config_mysql.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/res_config_mysql.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- res_config_mysql.c 22 Apr 2005 03:29:41 -0000 1.8
+++ res_config_mysql.c 26 Aug 2005 20:34:41 -0000 1.9
@@ -6,7 +6,10 @@
* Mark Spencer <markster at digium.com> - Asterisk Author
* Matthew Boehm <mboehm at cytelcom.com> - MySQL RealTime Driver Author
*
- * res_config_mysql.c <mysql plugin for RealTime configuration engine >
+ * res_config_mysql.c <mysql plugin for RealTime configuration engine>
+ *
+ * v1.9 - (08-19-05) - Added support to correctly honor the family database specified
+ * in extconfig.conf (bug #4973)
*
* v1.8 - (04-21-05) - Modified return values of update_mysql to better indicate
* what really happened.
@@ -62,7 +65,7 @@
static time_t connect_time;
static int parse_config(void);
-static int mysql_reconnect(void);
+static int mysql_reconnect(const char *database);
static int realtime_mysql_status(int fd, int argc, char **argv);
STANDARD_LOCAL_USER;
@@ -70,7 +73,7 @@
LOCAL_USER_DECL;
static char cli_realtime_mysql_status_usage[] =
-"Usage: show realtime mysql status\n"
+"Usage: realtime mysql status\n"
" Shows connection information for the MySQL RealTime driver\n";
static struct ast_cli_entry cli_realtime_mysql_status = {
@@ -120,7 +123,7 @@
ast_log(LOG_DEBUG, "MySQL RealTime: Retrieve SQL: %s\n", sql);
/* We now have our complete statement; Lets connect to the server and execute it. */
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(database)) {
return NULL;
}
@@ -231,7 +234,7 @@
ast_log(LOG_DEBUG, "MySQL RealTime: Retrieve SQL: %s\n", sql);
/* We now have our complete statement; Lets connect to the server and execute it. */
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(database)) {
return NULL;
}
@@ -314,7 +317,7 @@
ast_log(LOG_DEBUG,"MySQL RealTime: Update SQL: %s\n", sql);
/* We now have our complete statement; Lets connect to the server and execute it. */
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(database)) {
return -1;
}
@@ -370,11 +373,10 @@
ast_log(LOG_DEBUG, "MySQL RealTime: Static SQL: %s\n", sql);
/* We now have our complete statement; Lets connect to the server and execute it. */
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(database)) {
return NULL;
}
- ast_mutex_lock(&mysql_lock);
if(mysql_real_query(&mysql, sql, strlen(sql))) {
ast_log(LOG_WARNING, "MySQL RealTime: Failed to query database. Check debug for more info.\n");
ast_log(LOG_DEBUG, "MySQL RealTime: Query: %s\n", sql);
@@ -435,7 +437,7 @@
{
parse_config();
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(NULL)) {
ast_log(LOG_WARNING, "MySQL RealTime: Couldn't establish connection. Check debug.\n");
ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: %s\n", mysql_error(&mysql));
}
@@ -481,7 +483,7 @@
/* Need to unlock so that mysql_reconnect can regain the lock. */
ast_mutex_unlock(&mysql_lock);
- if(!mysql_reconnect()) {
+ if(!mysql_reconnect(NULL)) {
ast_log(LOG_WARNING, "MySQL RealTime: Couldn't establish connection. Check debug.\n");
ast_log(LOG_DEBUG, "MySQL RealTime: Cannot Connect: %s\n", mysql_error(&mysql));
}
@@ -576,17 +578,24 @@
return ASTERISK_GPL_KEY;
}
-static int mysql_reconnect()
+static int mysql_reconnect(const char *database)
{
+ char my_database[50];
+
+ if(!database || ast_strlen_zero(database))
+ ast_copy_string(my_database, dbname, sizeof(my_database));
+ else
+ ast_copy_string(my_database, database, sizeof(my_database));
+
ast_mutex_lock(&mysql_lock);
- if((!connected) && (dbhost || dbsock) && dbuser && dbpass && dbname) {
+ if((!connected) && (dbhost || dbsock) && dbuser && dbpass && my_database) {
if(!mysql_init(&mysql)) {
ast_log(LOG_WARNING, "MySQL RealTime: Insufficient memory to allocate MySQL resource.\n");
connected = 0;
ast_mutex_unlock(&mysql_lock);
return 0;
}
- if(mysql_real_connect(&mysql, dbhost, dbuser, dbpass, dbname, dbport, dbsock, 0)) {
+ if(mysql_real_connect(&mysql, dbhost, dbuser, dbpass, my_database, dbport, dbsock, 0)) {
ast_log(LOG_DEBUG, "MySQL RealTime: Successfully connected to database.\n");
connected = 1;
connect_time = time(NULL);
@@ -600,18 +609,24 @@
return 0;
}
} else {
- int error;
- error = mysql_ping(&mysql);
-
- if(error != 0) {
+ if(mysql_ping(&mysql) != 0) {
connected = 0;
- ast_log(LOG_ERROR, "MySQL RealTime: Attempted to reconnect. Failed. Check debug for more info.\n");
+ ast_log(LOG_ERROR, "MySQL RealTime: Failed to reconnect. Check debug for more info.\n");
ast_log(LOG_DEBUG, "MySQL RealTime: Server Error: %s\n", mysql_error(&mysql));
ast_mutex_unlock(&mysql_lock);
return 0;
}
- ast_log(LOG_DEBUG, "MySQL RealTime: Everything is fine.\n");
+
connected = 1;
+
+ if(mysql_select_db(&mysql, my_database) != 0) {
+ ast_log(LOG_WARNING, "MySQL RealTime: Unable to select database: %s. Still Connected.\n", my_database);
+ ast_log(LOG_DEBUG, "MySQL RealTime: Database Select Failed: %s\n", mysql_error(&mysql));
+ ast_mutex_unlock(&mysql_lock);
+ return 0;
+ }
+
+ ast_log(LOG_DEBUG, "MySQL RealTime: Everything is fine.\n");
ast_mutex_unlock(&mysql_lock);
return 1;
}
@@ -622,7 +637,7 @@
char status[256], status2[100] = "";
int ctime = time(NULL) - connect_time;
- if(mysql_reconnect()) {
+ if(mysql_reconnect(NULL)) {
if(dbhost) {
snprintf(status, 255, "Connected to %s@%s, port %d", dbname, dbhost, dbport);
} else if(dbsock) {
More information about the svn-commits
mailing list