Exemplo n.º 1
0
 public function preparedQuery($sql, $data = NULL, $options = array())
 {
     $this->startProfile = microtime(true);
     $paramCodes = array('b' => PDO::PARAM_BOOL, 'n' => PDO::PARAM_NULL, 'i' => PDO::PARAM_INT, 's' => PDO::PARAM_STR, 'l' => PDO::PARAM_LOB);
     if (is_scalar($data) || $data == NULL) {
         $args = func_get_args();
         if (is_scalar($options)) {
             $options = array();
             $data = array_slice($args, 1);
         } elseif (count($args) > 3) {
             $options = array();
             $data = array_slice($args, 3);
         }
     }
     $this->data = $data;
     $this->sql = $sql;
     $this->params = array();
     $sql = preg_replace_callback('/(?:(\\w?)\\:([^\\:]+)\\:)|(\\?)/', array($this, 'bindvar'), trim($sql));
     if ($this->cache) {
         $key = md5($this->sql . serialize($this->params));
         if ($cached = $this->getCached($key)) {
             $stmt = PDOV_Statement::getCached($this, unserialize($cached));
             return $stmt;
         }
     }
     $stmt = $this->prepare($sql, $options);
     if ($stmt === FALSE) {
         $error = $this->errorInfo();
         throw new PDOV_Exception('DB Error - ' . $error[2]);
     }
     $stmt->sql = $this->sql;
     $stmt->params = $this->params;
     foreach ($this->params as $key => $value) {
         $stmt->bindValue($key + 1, $value, $paramCodes[$this->paramTypes[$key]]);
     }
     $stmt->execute();
     return $stmt;
 }
Exemplo n.º 2
0
    public function prepared_query($sql, $data = NULL, $options = array())
    {
        $this->startProfile = microtime(true);
        $this->paramNames = array();
        $this->paramTypes = array();
        $paramCodes = array('b' => \PDO::PARAM_BOOL, 'n' => \PDO::PARAM_NULL, 'i' => \PDO::PARAM_INT, 's' => \PDO::PARAM_STR, 'l' => \PDO::PARAM_LOB);
        $paramNames = array('b' => '(Boolean)', 'n' => '(Null)   ', 'i' => '(Integer)', 's' => '(String) ', 'l' => '(LOB)    ');
        if (is_scalar($data) || $data == NULL) {
            $args = func_get_args();
            if (is_scalar($options)) {
                $options = array();
                $data = array_slice($args, 1);
            } elseif (count($args) > 3) {
                $options = array();
                $data = array_slice($args, 3);
            }
        }
        $this->data = $data;
        $this->sql = $sql;
        $this->params = array();
        $sql = preg_replace_callback('/(?:(\\w?)\\:([^\\:]+)\\:)|(\\?)/', array($this, 'bindvar'), trim($sql));
        $sql = preg_replace('#=\\s*\\{\\{\\{\\/=\\/IS\\/\\}\\}\\}#', 'IS NULL', $sql);
        if ($this->cache) {
            $key = md5($this->sql . serialize($this->params));
            if ($cached = $this->getCached($key)) {
                $stmt = PDOV_Statement::getCached($this, unserialize($cached));
                return $stmt;
            }
        }
        $stmt = $this->prepare($sql, $options);
        if ($stmt === FALSE) {
            $error = $this->errorInfo();
            throw new PDOV_Exception('DB Error - ' . $error[2]);
        }
        $stmt->sql = $this->sql;
        $stmt->params = $this->params;
        foreach ($this->params as $key => $value) {
            $stmt->bindValue($key + 1, $value, $paramCodes[$this->paramTypes[$key]]);
        }
        $stmt->execute();
        if ($this->log_queries) {
            $debug_params = '';
            foreach ($this->params as $key => $value) {
                $value = htmlentities($value, ENT_QUOTES);
                $debug_params .= "<tr><th>{$key}:</th><td>{$paramNames[$this->paramTypes[$key]]}</td><td>{$this->paramNames[$key]}</td><td>=</td><td>{$value}</td></tr>";
            }
            $debug_return = $stmt->rowCount() . ' rows returned, took ' . number_format((microtime(true) - $this->startProfile) * 100, 2) . 'ms.';
            $backtrace = debug_backtrace(TRUE);
            $context = next($backtrace);
            if ($context['function'] == '{closure}') {
                next($backtrace);
                $context = next($backtrace);
                if ($context['class'] = 'LazyLoad') {
                    $path = $context['object']->_props->path;
                    $context = next($backtrace);
                    $queryfunc = $context['function'];
                    $context = next($backtrace);
                    $contextText = "LazyLoad query {$path}/{$queryfunc}() called from {$context['function']}() in {$context['file']}:{$context['line']}";
                } else {
                    prev($backtrace);
                }
            }
            if (!isset($contextText)) {
                $contextText = "Inline query from {$context['function']}() in {$context['file']}:{$context['line']}";
            }
            $logentry = <<<EOI
<br/><br/>
<fieldset class="debug pdov querylog entry">
\t<legend>{$contextText}</legend>
\t<fieldset class="debug pdov querylog sql">
\t\t<legend>SQL</legend>
\t\t<pre>{$sql}</pre>
\t</fieldset>
\t<fieldset class="debug pdov querylog params">
\t\t<legend>Params</legend>
\t\t<table>{$debug_params}</table>
\t</fieldset>
\t<pre>{$debug_return}</pre>
</fieldset>
EOI;
            $this->query_log[] = $logentry;
        }
        return $stmt;
    }