WordPress.org

Support

Support » Plugins and Hacks » HyperDB » HyperDB – is_write_query too strict

HyperDB – is_write_query too strict

  • Came across an issue where only the first query (autoload options) was being sent to the read slave(s). Turns out a couple plugins had the syntax ‘SHOW TABLES LIKE…’ which HyperDB thought was a read query, since it didn’t match ‘SELECT’ as the first word. Made a quick fix for what I think are some common read statements to db.php:

    function is_write_query( $q ) {
    		$pattern = '/^\s*(SELECT|SHOW|DESCRIBE|EXPLAIN)\s+/i';
    		return (preg_match($pattern, $q)) ? false : true;
    	}

    As you can see my regex-fu is weak, I spent a good 40 minutes trying to negatively match those words. Gave up, instead positively match them and then return true if there’s *not* a match.

    http://wordpress.org/extend/plugins/hyperdb/

Viewing 1 replies (of 1 total)
  • Also, please note that newlines, tabs and sql comments will return a false-positive.

    Some of our queries passed to the is_write_query() use sql-comments, such as the following:

    $q = "/* a_function_call_name -- cached, CE */ SELECT foo FROM bar ...";

    You’ll need to do a preg_replace (or sommat) to the $q query BEFORE the return to ensure that you’ve escaped spaces, tabs, etc.

    My regex is weak, too, but I’ve got testing to do, so here’s mine:

    function is_write_query($q){
                            $q = preg_replace('/\/\* *([a-zA-Z0-9\-\.\_ ]*)\*\//',' ',$q);  // trim comments
                            $q = ltrim($q,"\t\r\n (");      // trim whitespace
                            return !preg_match('/^(?:SELECT|SHOW|DESCRIBE|EXPLAIN)\s/i',$q);
                    }       // end function is_write_query()

    Hope that helps someone else out there,

    –Chad

Viewing 1 replies (of 1 total)
  • The topic ‘HyperDB – is_write_query too strict’ is closed to new replies.
Skip to toolbar