Index: include/asterisk/res_odbc.h =================================================================== --- include/asterisk/res_odbc.h (revision 89535) +++ include/asterisk/res_odbc.h (working copy) @@ -85,6 +85,12 @@ */ int ast_odbc_sanity_check(struct odbc_obj *obj); +/*! \brief Checks if the database natively supports backslash as an escape character. + * \param obj The ODBC object + * \return Returns 1 if an ESCAPE clause is needed to support '\', 0 otherwise + */ +int ast_odbc_backslash_is_escape(struct odbc_obj *obj); + /*! \brief Prepares, executes, and returns the resulting statement handle. * \param obj The ODBC object * \param prepare_cb A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound. Index: configs/res_odbc.conf.sample =================================================================== --- configs/res_odbc.conf.sample (revision 89535) +++ configs/res_odbc.conf.sample (working copy) @@ -35,6 +35,9 @@ username => oscar password => thegrouch pre-connect => yes +; Many databases have a default of '\' to escape special characters. MS SQL +; Server does not. +backslash_is_escape => no Index: res/res_config_odbc.c =================================================================== --- res/res_config_odbc.c (revision 89535) +++ res/res_config_odbc.c (working copy) @@ -136,11 +136,12 @@ return NULL; newval = va_arg(aq, const char *); op = !strchr(newparam, ' ') ? " =" : ""; - snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op); + snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?%s", table, newparam, op, + strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); while((newparam = va_arg(aq, const char *))) { op = !strchr(newparam, ' ') ? " =" : ""; snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?%s", newparam, op, - strcasestr(newparam, "LIKE") ? " ESCAPE '\\'" : ""); + strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); newval = va_arg(aq, const char *); } va_end(aq); @@ -267,11 +268,11 @@ newval = va_arg(aq, const char *); op = !strchr(newparam, ' ') ? " =" : ""; snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?%s", table, newparam, op, - strcasestr(newparam, "LIKE") ? " ESCAPE '\\'" : ""); + strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); while((newparam = va_arg(aq, const char *))) { op = !strchr(newparam, ' ') ? " =" : ""; snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?%s", newparam, op, - strcasestr(newparam, "LIKE") ? " ESCAPE '\\'" : ""); + strcasestr(newparam, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\'" : ""); newval = va_arg(aq, const char *); } if (initfield) Index: res/res_odbc.c =================================================================== --- res/res_odbc.c (revision 89535) +++ res/res_odbc.c (working copy) @@ -66,6 +66,7 @@ unsigned int limit:10; /* Gives a limit of 1023 maximum */ unsigned int count:10; /* Running count of pooled connections */ unsigned int delme:1; /* Purge the class */ + unsigned int backslash_is_escape:1; /* On this database, the backslash is a native escape sequence */ AST_LIST_HEAD(, odbc_obj) odbc_obj; }; @@ -212,7 +213,7 @@ struct ast_config *config; struct ast_variable *v; char *cat, *dsn, *username, *password; - int enabled, pooling, limit; + int enabled, pooling, limit, bse; int connect = 0, res = 0; struct odbc_class *new; @@ -235,6 +236,7 @@ connect = 0; pooling = 0; limit = 0; + bse = 1; for (v = ast_variable_browse(config, cat); v; v = v->next) { if (!strcasecmp(v->name, "pooling")) { if (ast_true(v->value)) @@ -259,6 +261,8 @@ username = v->value; } else if (!strcasecmp(v->name, "password")) { password = v->value; + } else if (!strcasecmp(v->name, "backslash_is_escape")) { + bse = ast_true(v->value); } } @@ -298,6 +302,8 @@ } } + new->backslash_is_escape = bse ? 1 : 0; + odbc_register_class(new, connect); ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn); } @@ -379,6 +385,11 @@ obj->used = 0; } +int ast_odbc_backslash_is_escape(struct odbc_obj *obj) +{ + return obj->parent->backslash_is_escape; +} + struct odbc_obj *ast_odbc_request_obj(const char *name, int check) { struct odbc_obj *obj = NULL; @@ -533,7 +544,7 @@ struct ast_config *config; struct ast_variable *v; char *cat, *dsn, *username, *password; - int enabled, pooling, limit; + int enabled, pooling, limit, bse; int connect = 0, res = 0; struct odbc_class *new, *class; @@ -560,6 +571,7 @@ connect = 0; pooling = 0; limit = 0; + bse = 1; for (v = ast_variable_browse(config, cat); v; v = v->next) { if (!strcasecmp(v->name, "pooling")) { pooling = 1; @@ -583,6 +595,8 @@ username = v->value; } else if (!strcasecmp(v->name, "password")) { password = v->value; + } else if (!strcasecmp(v->name, "backslash_is_escape")) { + bse = ast_true(v->value); } } @@ -637,6 +651,8 @@ } } + new->backslash_is_escape = bse; + if (class) { ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn); } else {