--- src/util/dict_mysql.c.orig	Fri Jun  6 08:29:36 2003
+++ src/util/dict_mysql.c	Thu Jun 19 07:30:53 2003
@@ -79,6 +79,7 @@
 #include "dict.h"
 #include "msg.h"
 #include "mymalloc.h"
+#include "mac_parse.h"
 #include "dict_mysql.h"
 #include "argv.h"
 #include "vstring.h"
@@ -108,6 +109,7 @@
     char   *select_field;
     char   *where_field;
     char   *additional_conditions;
+    char   *direct_query;
     char  **hostnames;
     int     len_hosts;
 } MYSQL_NAME;
@@ -137,7 +139,23 @@
 static MYSQL_NAME *mysqlname_parse(const char *);
 static HOST host_init(char *);
 
+typedef struct {
+    const char *key;
+    VSTRING *query;
+} DICT_MYSQL_PARSE_ARG;
 
+static int parse_callback(int type, VSTRING *buf, char *context)
+{
+    DICT_MYSQL_PARSE_ARG *arg = (DICT_MYSQL_PARSE_ARG *)context;
+
+    if (type == MAC_PARSE_LITERAL)
+	vstring_strcat(arg->query, vstring_str(buf));
+    else if (type == MAC_PARSE_VARNAME) {
+	if (strcmp(vstring_str(buf), "key") == 0)
+	    vstring_strcat(arg->query, arg->key);
+    }
+    return 0;
+}
 
 /**********************************************************************
  * public interface dict_mysql_lookup
@@ -167,9 +185,16 @@
     }
     /* prepare the query */
     mysql_escape_string(name_escaped, name, (unsigned int) strlen(name));
-    vstring_sprintf(query, "select %s from %s where %s = '%s' %s", dict_mysql->name->select_field,
-       dict_mysql->name->table, dict_mysql->name->where_field, name_escaped,
+    if (dict_mysql->name->direct_query[0]) {
+	DICT_MYSQL_PARSE_ARG arg;
+	arg.key = name_escaped;
+	arg.query = query;
+	mac_parse(dict_mysql->name->direct_query, parse_callback, &arg);
+    } else {
+	vstring_sprintf(query, "select %s from %s where %s = '%s' %s", dict_mysql->name->select_field,
+	  dict_mysql->name->table, dict_mysql->name->where_field, name_escaped,
 		    dict_mysql->name->additional_conditions);
+    }
     if (msg_verbose)
 	msg_info("dict_mysql_lookup using sql query: %s", vstring_str(query));
     /* free mem associated with preparing the query */
@@ -420,26 +445,43 @@
     if (msg_verbose)
 	msg_info("mysqlname_parse(): set database name to '%s'", name->dbname);
 
-    /* table lookup */
-    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "table")) == NULL)
-	msg_fatal("%s: mysql options file does not include table name", mysqlcf_path);
+    /* direct query */
+    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "direct_query")) == NULL)
+	name->direct_query = mystrdup("");
     else
+	name->direct_query = mystrdup(nameval);
+    if (msg_verbose)
+	msg_info("mysqlname_parse(): set query to '%s'", name->direct_query);
+
+    /* table lookup */
+    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "table")) == NULL) {
+	if (!name->direct_query[0])
+	    msg_fatal("%s: mysql options file does not include table name", mysqlcf_path);
+	else
+	    name->table = mystrdup("");
+    } else
 	name->table = mystrdup(nameval);
     if (msg_verbose)
 	msg_info("mysqlname_parse(): set table name to '%s'", name->table);
 
     /* select field lookup */
-    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "select_field")) == NULL)
-	msg_fatal("%s: mysql options file does not include select field", mysqlcf_path);
-    else
+    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "select_field")) == NULL) {
+	if (!name->direct_query[0])
+	    msg_fatal("%s: mysql options file does not include select field", mysqlcf_path);
+	else
+	    name->select_field = mystrdup("");
+    } else
 	name->select_field = mystrdup(nameval);
     if (msg_verbose)
 	msg_info("mysqlname_parse(): set select_field to '%s'", name->select_field);
 
     /* where field lookup */
-    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "where_field")) == NULL)
-	msg_fatal("%s: mysql options file does not include where field", mysqlcf_path);
-    else
+    if ((nameval = (char *) dict_lookup(vstring_str(opt_dict_name), "where_field")) == NULL) {
+	if (!name->direct_query[0])
+	    msg_fatal("%s: mysql options file does not include where field", mysqlcf_path);
+	else
+	    name->where_field = mystrdup("");
+    } else
 	name->where_field = mystrdup(nameval);
     if (msg_verbose)
 	msg_info("mysqlname_parse(): set where_field to '%s'", name->where_field);
@@ -536,6 +578,7 @@
     myfree(dict_mysql->name->select_field);
     myfree(dict_mysql->name->where_field);
     myfree(dict_mysql->name->additional_conditions);
+    myfree(dict_mysql->name->direct_query);
     for (i = 0; i < dict_mysql->name->len_hosts; i++) {
 	myfree(dict_mysql->name->hostnames[i]);
     }
