vrana/notorm

Unwanted JOINS created base on quoted strings which contain a dot.

Closed this issue · 3 comments

For example

$url = "www.site.com/path";
table->where("'$url' LIKE CONCAT(`host`,'%')");
...

Will create a JOINs on tables www and site. Clearly that's not what we want. The solution is to ignore quoted strings in the Result::createJoins method.

before line 90 in Result.php add:

$val = preg_replace('~\'[^\']+\'~',"'dummy'",$val);
$val = preg_replace('~\"[^\"]+\"~','"dummy"',$val);

These could be combined into one regex but I don't think it would be faster. You could probably also change the preg_match_all on line 90 to achieve the same affect.

Ok so now there is a follow-on issue related to the Result::removeExtraDots method.

Temporary fix:

    protected function removeExtraDots($expression) {

        $symbols = array(':','.');
        $words = array('{{colon}}','{{dot}}');

        $fn = function($match) use ($symbols, $words){
                $match = str_replace($symbols,$words,$match[0]);
                return $match;
        };
        $expression = preg_replace_callback('~\'[^\']+\'~',$fn,$expression);
        $expression = preg_replace_callback('~\"[^\"]+\"~',$fn,$expression);

        $expression =  preg_replace('~(?:\\b[a-z_][a-z0-9_.:]*[.:])?([a-z_][a-z0-9_]*)[.:]([a-z_*])~i', '\\1.\\2', $expression); // rewrite tab1.tab2.col

        $expression = str_replace($words,$symbols,$expression);

        return $expression;
    }

This could no doubt be solved by "simply" changing the original regex from this method instead...

Please use "? LIKE ..." and pass the variable in data. It's safer and cleaner anyway.

Ah I didn't realize I could do that. Thanks!