Index: gw/dlr_mysql.c
===================================================================
--- gw/dlr_mysql.c	(revision 4833)
+++ gw/dlr_mysql.c	(working copy)
@@ -136,27 +136,53 @@
     dlr_entry_destroy(entry);
 }
 
-static struct dlr_entry* dlr_mysql_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+static struct dlr_entry* dlr_mysql_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int use_dst)
 {
-    Octstr *sql;
+    Octstr *sql, *like = NULL;
     DBPoolConn *pconn;
     List *result = NULL, *row;
     struct dlr_entry *res = NULL;
     List *binds = gwlist_create();
+    int len;
 
     pconn = dbpool_conn_consume(pool);
     if (pconn == NULL) /* should not happens, but sure is sure */
         return NULL;
 
-    sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         fields->field_mask, fields->field_serv,
                         fields->field_url, fields->field_src,
                         fields->field_dst, fields->field_boxc,
                         fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+       else sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        fields->field_mask, fields->field_serv,
+                        fields->field_url, fields->field_src,
+                        fields->field_dst, fields->field_boxc,
+                        fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+    }
+    else sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        fields->field_mask, fields->field_serv,
+                        fields->field_url, fields->field_src,
+                        fields->field_dst, fields->field_boxc,
+                        fields->table, fields->field_smsc,
                         fields->field_ts);
 
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
@@ -164,10 +190,12 @@
 
     if (dbpool_conn_select(pconn, sql, binds, &result) != 0) {
         octstr_destroy(sql);
+        if (like) octstr_destroy(like);
         dbpool_conn_produce(pconn);
         return NULL;
     }
     octstr_destroy(sql);
+    if (like) octstr_destroy(like);
     gwlist_destroy(binds, NULL);
     dbpool_conn_produce(pconn);
 
@@ -193,12 +221,12 @@
     return res;
 }
 
-static void dlr_mysql_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+static void dlr_mysql_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int use_dst)
 {
-    Octstr *sql;
+    Octstr *sql, *like = NULL;
     DBPoolConn *pconn;
     List *binds = gwlist_create();
-    int res;
+    int res, len;
 
     debug("dlr.mysql", 0, "removing DLR from database");
 
@@ -207,12 +235,31 @@
     if (pconn == NULL)
         return;
 
-    sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+       else sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+    }
+    else sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        fields->table, fields->field_smsc,
                         fields->field_ts);
 
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
@@ -226,14 +273,15 @@
     dbpool_conn_produce(pconn);
     gwlist_destroy(binds, NULL);
     octstr_destroy(sql);
+    if (like) octstr_destroy(like);
 }
 
-static void dlr_mysql_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
+static void dlr_mysql_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status, int use_dst)
 {
-    Octstr *sql, *os_status;
+    Octstr *sql, *os_status, *like = NULL;
     DBPoolConn *pconn;
     List *binds = gwlist_create();
-    int res;
+    int res, len;
 
     debug("dlr.mysql", 0, "updating DLR status in database");
 
@@ -242,14 +290,35 @@
     if (pconn == NULL)
         return;
 
-    sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         fields->table, fields->field_status,
+                        fields->field_smsc, fields->field_ts,
+                        fields->field_dst);
+       else sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        fields->table, fields->field_status,
+                        fields->field_smsc, fields->field_ts,
+                        fields->field_dst);
+    }
+    else sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        fields->table, fields->field_status,
                         fields->field_smsc, fields->field_ts);
 
     os_status = octstr_format("%d", status);
     gwlist_append(binds, (Octstr *)os_status);
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
