douyakao5308 2014-01-06 15:18
浏览 76
已采纳

忽略DB2 SELECT语句中的某些字符

When building a text search into a website I'm passing it through a PHP loop:

$queryList=explode(' ', $queryString);
foreach ($queryList as $queryParm) {
    foreach ($fields as $key => $query) {
        $whereStr.=($key == 0) ? " AND (ucase($query) LIKE ?" : " OR ucase($query) LIKE ?";
        $db->parms[]="%$queryParm%";
        if ($key+1 == count($fields)) $whereStr.=")";
    }
}

This will separate the query's words and make each one a separate parameter. I'm building a regex to only allow alphanumeric plus spaces, but now the problem is I need the SQL to match this regex, for example if there's an item named "FakeCo's Unicorn Repellant #123" then searching "fakecos unicorn repellant 123" should yield the result, ignoring the fact that the "'" and the "#" exist. In fact due to the structure of the list "123 fakeco" should show the result too.

Is there a way to apply a regex to a DB2 statement?

This is being ran against DB2 for i (version 7.1)

Edit: attempted solution (insert this before PHP mentioned above), some characters caused issues & had to be removed, poor performance when it did run

$sqlNeedsEscape=array("?", "'");
$ignoreChars="'\"?~!@#$%^&*()-=+[]{}|<>,./\\";
$ignoreChars=str_split($ignoreChars);
$whereBefore='';
$whereAfter='';
foreach ($ignoreChars as &$ic) {
    if (in_array($ic, $sqlNeedsEscape)) $ic="'$ic";
    error_log($ic);
    $whereBefore.="replace(";
    $whereAfter.=",'$ic','')";
}

Then change the $whereStr.= line to:

$whereStr.=($key == 0) ? " AND (ucase(trim($whereBefore $query $whereAfter)) LIKE ?" : " OR $whereBefore ucase(trim($query)) $whereAfter LIKE ?";

Effectively it was replace(replace(replace(query, '!', ''), '@', ''), '#', '') for each field, only wrap several more replaces around that. I quit chasing this idea when I saw the impact it had on performance. I will speak with our RPG devs about the UDF solution suggested by Buck Calabro. It sounds like the only "real" solution possible.

  • 写回答

4条回答 默认 最新

  • down00112 2014-01-09 21:26
    关注

    Since you're dealing with IBM i version 7.1, the best solution IMO would be to get the DB2 Omnifind Text Search Server (5733-OMF) product loaded...

    In short Omnifind provides "high-speed linguistic text searches"; so a search for mouse would find not only anything containing mouse but anything containing mice as well.

    http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzash%2Frzashkickoff.htm http://www-304.ibm.com/partnerworld/wps/servlet/ContentHandler/whitepaper/i/omnifind/search

    It is a no-charge licensed product. Note that it is also available for 6.1.

    In addition to providing the CONTAINS() function there's a SCORE() function that allows you to rank search results.

    Prior to 6.1, IBM offered a for charge product known as (DB2 Extender) Text Search Engine (5770-DE1) that had much of the same functionality but that was not as nice to use.

    HTH,

    Charles

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题