function cps4wp_query($sql)
{
    global $wpdb;
    // echo $sql;
    $logto = 'queries';
    // The end of the query may be protected against changes
    $end = '';
    // Remove unusefull spaces
    $initial = $sql = trim($sql);
    if (0 === strpos($sql, 'SELECT')) {
        // clutserpoint doesnot support @ in queries
        if (false !== strpos($sql, "@")) {
            return false;
        }
        $logto = 'SELECT';
        $s = new SQLSelect($sql);
        return $GLOBALS['cps4wp_result'] = $s->toCps();
    } elseif (0 === strpos($sql, 'UPDATE')) {
        $logto = 'UPDATE';
        $s = new SQLUpdate($sql);
        return $GLOBALS['cps4wp_result'] = $s->toCps();
    } elseif (0 === strpos($sql, 'INSERT')) {
        $logto = 'INSERT';
        $s = new SQLInsert($sql);
        return $GLOBALS['cps4wp_result'] = $s->toCps();
    } elseif (0 === strpos($sql, 'DELETE')) {
        $logto = 'DELETE';
        $s = new SQLDelete($sql);
        return $GLOBALS['cps4wp_result'] = $s->toCps();
    } elseif (0 === strpos($sql, 'SHOW TABLES')) {
        $logto = 'SHOWTABLES';
        $o = new SQLAbstract();
        return $o->execute('SELECT DISTINCT __table FROM ' . DB_NAME);
    } elseif (0 === strpos($sql, 'OPTIMIZE TABLE')) {
        $logto = 'OPTIMIZE';
        $sql = str_replace('OPTIMIZE TABLE', 'VACUUM', $sql);
    } elseif (0 === strpos($sql, 'SET NAMES') && false !== strpos($sql, 'COLLATE')) {
        $logto = 'SETNAMES';
        $sql = "SET NAMES 'utf8'";
        $sql = false;
        //cps don't need this now
    }
    // Load up upgrade and install functions as required
    $begin = substr($sql, 0, 3);
    $search = array('SHO', 'ALT', 'DES', 'CRE', 'DRO');
    if (in_array($begin, $search)) {
        require_once CPS4WP_ROOT . '/driver_pgsql_install.php';
        $sql = cps4wp_installing($sql, $logto);
    }
    // WP 2.9.1 uses a comparison where text data is not quoted
    $pattern = '/AND meta_value = (-?\\d+)/';
    $sql = preg_replace($pattern, 'AND meta_value = \'$1\'', $sql);
    // Generic "INTERVAL xx YEAR|MONTH|DAY|HOUR|MINUTE|SECOND" handler
    $pattern = '/INTERVAL[ ]+(\\d+)[ ]+(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)/';
    $sql = preg_replace($pattern, "'\$1 \$2'::interval", $sql);
    $pattern = '/DATE_SUB[ ]*\\(([^,]+),([^\\)]+)\\)/';
    $sql = preg_replace($pattern, '($1::timestamp - $2)', $sql);
    // Remove illegal characters
    $sql = str_replace('`', '', $sql);
    // Field names with CAPITALS need special handling
    if (false !== strpos($sql, 'ID')) {
        $pattern = '/ID([^ ])/';
        $sql = preg_replace($pattern, 'ID $1', $sql);
        $pattern = '/ID$/';
        $sql = preg_replace($pattern, 'ID ', $sql);
        $pattern = '/\\(ID/';
        $sql = preg_replace($pattern, '( ID', $sql);
        $pattern = '/,ID/';
        $sql = preg_replace($pattern, ', ID', $sql);
        $pattern = '/[0-9a-zA-Z_]+ID/';
        $sql = preg_replace($pattern, '"$0"', $sql);
        $pattern = '/\\.ID/';
        $sql = preg_replace($pattern, '."ID"', $sql);
        $pattern = '/[\\s]ID /';
        $sql = preg_replace($pattern, ' "ID" ', $sql);
        $pattern = '/"ID "/';
        $sql = preg_replace($pattern, ' "ID" ', $sql);
    }
    // CAPITALS
    // Empty "IN" statements are erroneous
    $sql = str_replace('IN (\'\')', 'IN (NULL)', $sql);
    $sql = str_replace('IN ( \'\' )', 'IN (NULL)', $sql);
    $sql = str_replace('IN ()', 'IN (NULL)', $sql);
    // Put back the end of the query if it was separated
    $sql .= $end;
    // For insert ID catching
    if ($logto == 'INSERT') {
        $pattern = '/INSERT INTO (\\w+)\\s+\\([ a-zA-Z_"]+/';
        preg_match($pattern, $sql, $matches);
        $GLOBALS['cps4wp_ins_table'] = $matches[1];
        $match_list = split(' ', $matches[0]);
        if ($GLOBALS['cps4wp_ins_table']) {
            $GLOBALS['cps4wp_ins_field'] = trim($match_list[3], ' ()	');
            if (!$GLOBALS['cps4wp_ins_field']) {
                $GLOBALS['cps4wp_ins_field'] = trim($match_list[4], ' ()	');
            }
        }
        $GLOBALS['cps4wp_last_insert'] = $sql;
    } elseif (isset($GLOBALS['cps4wp_queued_query'])) {
        pg_query($GLOBALS['cps4wp_queued_query']);
        unset($GLOBALS['cps4wp_queued_query']);
    }
    // Correct quoting for PostgreSQL 9.1+ compatibility
    $sql = str_replace("\\'", "''", $sql);
    $sql = str_replace('\\"', '"', $sql);
    if (CPS4WP_DEBUG) {
        if ($initial != $sql) {
            error_log('[' . microtime(true) . "] Converting :\n{$initial}\n---- to ----\n{$sql}\n---------------------\n", 3, CPS4WP_LOG . 'cps4wp_' . $logto . '.log');
        } else {
            error_log('[' . microtime(true) . "] {$sql}\n---------------------\n", 3, CPS4WP_LOG . 'cps4wp_unmodified.log');
        }
    }
    return $sql;
}