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

忽略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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog