Skip to content

Regex replacement SQL syntax error #68

Description

@borislav-angelov

I found an issue related to regex replacement function

        private function rewrite_regexp()
        {
            $pattern = '/\s([^\s]*)\s*regexp\s*(\'.*?\')/im';
            $this->_query = preg_replace($pattern, ' regexpp(\1, \2)', $this->_query);
        }

To reproduce the problem, here is an example

Query:

SELECT name FROM sqlite_master WHERE type='table' AND (name REGEXP '^wpda_' OR name REGEXP '^wbk_services' OR name REGEXP '^wbk_days_on_off' OR name REGEXP '^wbk_locked_time_slots' OR name REGEXP '^wbk_appointments' OR name REGEXP '^wbk_cancelled_appointments' OR name REGEXP '^wbk_email_templates' OR name REGEXP '^wbk_service_categories' OR name REGEXP '^wbk_gg_calendars' OR name REGEXP '^wbk_coupons')

After replacement (wrong SQL syntax) - double braces around first regexpp call
Query:

SELECT name FROM sqlite_master WHERE type='table' AND regexpp((name, '^wpda_') OR regexpp(name, '^wbk_services') OR regexpp(name, '^wbk_days_on_off') OR regexpp(name, '^wbk_locked_time_slots') OR regexpp(name, '^wbk_appointments') OR regexpp(name, '^wbk_cancelled_appointments') OR regexpp(name, '^wbk_email_templates') OR regexpp(name, '^wbk_service_categories') OR regexpp(name, '^wbk_gg_calendars') OR regexpp(name, '^wbk_coupons'))

Solution is to change the regex to:

        private function rewrite_regexp()
        {
            $pattern = '/([a-z0-9_$]+)\s+regexp\s+(\'.*?\')/im';
            $this->_query = preg_replace($pattern, ' regexpp(\1, \2)', $this->_query);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions