Index: res/res_config_mysql.c =================================================================== --- res/res_config_mysql.c (revision 599) +++ res/res_config_mysql.c (working copy) @@ -100,6 +100,7 @@ char *stringp; char *chunk; char *op; + char dbname[50] = ""; const char *newparam, *newval; struct ast_variable *var=NULL, *prev=NULL; @@ -134,7 +135,10 @@ if ((valsz = strlen (newval)) * 2 + 1 > sizeof(buf)) valsz = (sizeof(buf) - 1) / 2; mysql_real_escape_string(&dbread.handle, buf, newval, valsz); - snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, buf); + if (strcmp(database,dbread.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "SELECT * FROM %s%s WHERE %s%s '%s'", dbname, table, newparam, op, buf); while ((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); if (!strchr(newparam, ' ')) @@ -203,6 +207,7 @@ char *stringp; char *chunk; char *op; + char dbname[50] = ""; const char *newparam, *newval; struct ast_variable *var = NULL; struct ast_config *cfg = NULL; @@ -253,7 +258,10 @@ if ((valsz = strlen (newval)) * 2 + 1 > sizeof(buf)) valsz = (sizeof(buf) - 1) / 2; mysql_real_escape_string(&dbread.handle, buf, newval, valsz); - snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, buf); + if (strcmp(database,dbread.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "SELECT * FROM %s%s WHERE %s%s '%s'", dbname, table, newparam, op, buf); while ((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); if (!strchr(newparam, ' ')) op = " ="; else op = ""; @@ -322,6 +330,7 @@ my_ulonglong numrows; char sql[512]; char buf[511]; /* Keep this size uneven as it is 2n+1. */ + char dbname[50] = ""; int valsz; const char *newparam, *newval; @@ -351,7 +360,10 @@ if ((valsz = strlen (newval)) * 2 + 1 > sizeof(buf)) valsz = (sizeof(buf) - 1) / 2; mysql_real_escape_string(&dbwrite.handle, buf, newval, valsz); - snprintf(sql, sizeof(sql), "UPDATE %s SET %s = '%s'", table, newparam, buf); + if (strcmp(database,dbwrite.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "UPDATE %s%s SET %s = '%s'", dbname, table, newparam, buf); while((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); if ((valsz = strlen (newval)) * 2 + 1 > sizeof(buf)) @@ -397,6 +409,7 @@ char buf[511]; /* Keep this size uneven as it is 2n+1. */ char fields[512]; char values[512]; + char dbname[50] = ""; int valsz; const char *newparam, *newval; @@ -441,7 +454,10 @@ snprintf(values + strlen(values), sizeof(values), ", '%s'", buf); } va_end(ap); - snprintf(sql, sizeof(sql), "INSERT into %s (%s) values (%s)", table, fields, values); + if (strcmp(database,dbwrite.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "INSERT into %s%s (%s) values (%s)", dbname, table, fields, values); ast_debug(1,"MySQL RealTime: Insert SQL: %s\n", sql); /* Execution. */ @@ -472,6 +488,7 @@ char sql[512]; char buf[511]; /* Keep this size uneven as it is 2n+1. */ int valsz; + char dbname[50] = ""; const char *newparam, *newval; if (!table) { @@ -500,7 +517,10 @@ if ((valsz = strlen (lookup)) * 2 + 1 > sizeof(buf)) valsz = (sizeof(buf) - 1) / 2; mysql_real_escape_string(&dbwrite.handle, buf, lookup, valsz); - snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE %s = '%s'", table, keyfield, buf); + if (strcmp(database,dbwrite.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "DELETE FROM %s%s WHERE %s = '%s'", dbname, table, keyfield, buf); while ((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); if ((valsz = strlen (newval)) * 2 + 1 > sizeof(buf)) @@ -544,6 +564,7 @@ struct ast_category *cur_cat; char sql[250] = ""; char last[80] = ""; + char dbname[50] = ""; int last_cat_metric = 0; ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED); @@ -554,17 +575,19 @@ return NULL; } - snprintf(sql, sizeof(sql), "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id", table, file); - - ast_debug(1, "MySQL RealTime: Static SQL: %s\n", sql); - - /* We now have our complete statement; Lets connect to the server and execute it. */ ast_mutex_lock(&dbread.lock); if (!mysql_reconnect(&dbread)) { ast_mutex_unlock(&dbread.lock); return NULL; } + if (strcmp(database,dbread.name)) { + snprintf(dbname,sizeof(dbname),"%s.",database); + } + snprintf(sql, sizeof(sql), "SELECT category, var_name, var_val, cat_metric FROM %s%s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id", dbname, table, file); + + ast_debug(1, "MySQL RealTime: Static SQL: %s\n", sql); + if (mysql_real_query(&dbread.handle, sql, strlen(sql))) { ast_log(LOG_WARNING, "MySQL RealTime: Failed to query database. Check debug for more info.\n"); ast_debug(1, "MySQL RealTime: Query: %s\n", sql);