Called before each db query.
@param string $sql
@param array array of parameters
@param int $type type of query
@param mixed $extrainfo driver specific extra information
@return void
437926
protected function query_start($sql, array $params=null, $type, $extrainfo=null) {
        parent::query_start($sql, $params, $type, $extrainfo);
        // pgsql driver tents to send debug to output, we do not need that ;-)
        $this->last_error_reporting = error_reporting(0);
    }
630560
protected function init($request, $builder, $type = 'builder')
    {
        $this->request    = $request;
        $this->query_type = $type;
        $this->columns    = $builder->columns;
        $this->connection = $builder->getConnection();
        $this->prefix     = $this->connection->getTablePrefix();
        $this->database   = $this->connection->getDriverName();
        if ($this->isDebugging()) {
            $this->connection->enableQueryLog();
        }
    }
408334
public function query($type = 'object')
    {
        $sql = 'select '.$this->columns.' from '.$this->getTables($this->tables).' '.$this->where.' '.$this->order.' '.$this->group;

        static::$sql = static::$register[] = $sql;
        //
        if ($data = Database::read($sql)) {
            return self::fetch($data, $type);
        } elseif (Database::execerr()) {
            throw new QueryException();
        }
    }
1113459
public function override(string $name, Type $type) : void
    {
        if (! isset($this->instances[$name])) {
            throw DBALException::typeNotFound($name);
        }

        if (! in_array($this->findTypeName($type), [$name, null], true)) {
            throw DBALException::typeAlreadyRegistered($type);
        }

        $this->instances[$name] = $type;
    }
567356
public static function execute($conn, $sql) {
    $conn = is_array($conn) ? self::connect($conn) : $conn;
    $result = $conn->query($sql);
    if (!$result) {
      throw new SqlException("Cannot execute $sql: " . $conn->error);
    }

    if ($result && $result !== TRUE) {
      $result->free_result();
    }

  }
611855
public function orderBy(string $column, string $type = null)
    {
        if ($type) {
            $type = strtoupper($type);

            if (!in_array($type, ['ASC', 'DESC'])) {
                throw new SqlException('Wrong ORDER type for statement.');
            }
        }

        $this->addOrderBy($this->dialect()->quoteName($column), $type);

        return $this;
    }
548490
protected function beforeQuery($sql)
    {
        // Reset state
        $this->rowCount = 0;
        $this->lastStatementError = null;

        // Flush if necessary
        if ($this->isQueryDDL($sql)) {
            $this->flushStatements();
        }
    }
1250373
protected function query ( $sql )
	{
		$sql = $this->sql_printf(func_get_args());
		if (!($res = mysqli_query( $this->conn, $sql)))
		{
			$this->sql_errcheck($sql);
		}
		if (!is_bool($res))
		{
			((mysqli_free_result($res) || (is_object($res) && (get_class($res) == "mysqli_result"))) ? true : false);
		}
	}
1211662
public function execute($sql, array $params=null) {
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

        $result = true;
        $this->query_start($sql, $params, SQL_QUERY_UPDATE);

        try {
            $sth = $this->pdb->prepare($sql);
            $sth->execute($params);
        } catch (PDOException $ex) {
            $this->lastError = $ex->getMessage();
            $result = false;
        }

        $this->query_end($result);
        return $result;
    }
704318
public function complete_sql($sql = null, array $params = [], array $extra_params = [])
    {
        if (func_num_args()) {
            if (isset($extra_params['page']))
                $this->page = $extra_params['page'];
            if (isset($extra_params['perPage']))
                $this->limit = $extra_params['perPage'];
            if (isset($extra_params['offset']))
                $this->offset = $extra_params['offset'];
            if ($this->page && $this->limit)
                $this->will_paginate = true;
                
            $this->complete_sql = [$sql, $params];
            return $this;
        } else
            return $this->complete_sql;
    }
1335460
public function bindParam($column, &$param, $type = null)
    {
        if ($type === null) :
            $this->_statement->bindParam($column, $param);
        else :
            $this->_statement->bindParam($column, $param, $type);
        endif;
    }
12678
final protected function cache_query($sql, $result){
        if( isset($this->query_cache[$sql]) ){
            $this->query_cache[$sql] = $result;
        }elseif( count($this->query_cache) >= $this->cache_limit ){
            // Remove oldest cache
            array_shift($this->query_cache);
            // save query
            $this->query_cache[$sql] = $result;
        }
    }
532272
public function premaintainQuery()
    {
        foreach ($this->pre_sql as $sql) {
            $nsql = Assignment::reporticoLookupStringToPhp($sql);
            $recordSet = false;
            $errorCode = false;
            $errorMessage = false;
            try {
                $recordSet = $conn->Execute($nsql);
            } catch (PDOException $ex) {
                $errorCode = $ex->getCode();
                $errorMessage = $ex->getMessage();
            }
            if (!$recordSet) {
                if ($errorMessage) {
                    ReporticoApp::handleError("Query Failed<BR><BR>" . $nsql . "<br><br>" .
                        $errorMessage);
                } else {
                    ReporticoApp::handleError("Query Failed<BR><BR>" . $nsql . "<br><br>" .
                        "Status " . $conn->ErrorNo() . " - " .
                        $conn->ErrorMsg());
                }

            }
        }

        $this->fetchColumnAttributes();

        // Run query for each target. Currently having more than
        // one target means first target is array which becomes source
        // for second target
        //for ($i = 0; $i < count($this->targets); $i++ )
        for ($i = 0; $i < 1; $i++) {
            $target = &$this->targets[$i];

            $target->setQuery($this);
            $target->setColumns($this->columns);
            $target->start();
        }
    }
112418
@Override
    public void runReport(Map<String, Object> extra) {
        PreparedStatement prestatement = null;
        PreparedStatement poststatement = null;
        PreparedStatement statement = null;
        try {
            getStartConnection();
            if (StringUtils.isNotBlank(presql))
            {
                FreemarkerSQLResult prefreemarkerSQLResult = freemakerParams(extra, false, presql);
                prestatement = connection.prepareStatement(prefreemarkerSQLResult.getSql());
                for(int i=0;i<prefreemarkerSQLResult.getParams().size();i++) {
                    prestatement.setObject(i + 1, prefreemarkerSQLResult.getParams().get(i));
                }
                prestatement.setQueryTimeout(queryTimeout);
                prestatement.execute();
            }
            FreemarkerSQLResult freemarkerSQLResult = freemakerParams(extra, true, freemarkerSql);
            statement = connection.prepareStatement(freemarkerSQLResult.getSql());
            for(int i=0;i<freemarkerSQLResult.getParams().size();i++) {
                statement.setObject(i + 1, freemarkerSQLResult.getParams().get(i));
            }
            statement.setQueryTimeout(queryTimeout);
            rows = resultSetToMap(statement.executeQuery());
            if (StringUtils.isNotBlank(postsql))
            {
                FreemarkerSQLResult postfreemarkerSQLResult = freemakerParams(extra, false, postsql);
                poststatement = connection.prepareStatement(postfreemarkerSQLResult.getSql());
                for(int i=0;i<postfreemarkerSQLResult.getParams().size();i++) {
                    poststatement.setObject(i + 1, postfreemarkerSQLResult.getParams().get(i));
                }
                poststatement.setQueryTimeout(queryTimeout);
                poststatement.execute();
            }
        } catch (Exception ex) {
            errors.add(ex.getMessage());
        } finally {
            try {
                if (statement != null && !statement.isClosed())
                {
                    statement.close();
                    statement = null;
                }
                if (prestatement != null && !prestatement.isClosed())
                {
                    prestatement.close();
                    prestatement = null;
                }
                if (poststatement != null && !poststatement.isClosed())
                {
                    poststatement.close();
                    poststatement = null;
                }
                if (connection != null && !connection.isClosed())
                {
                    connection.close();
                    connection = null;
                }
            } catch (SQLException e) {
                errors.add(e.getMessage());
                e.printStackTrace();
            }
        }
    }
1287492
public function setSql($sql, array $params = [])
    {
        $this->sql = (string) $sql;

        $this->setSqlParameters($params);
    }
547413
public function exec_script($sql)
    {
        $sql  = $this->fix_table_names($sql);
        $buff = '';
        $exec = '';

        foreach (explode("\n", $sql) as $line) {
            $trimmed = trim($line);
            if ($trimmed == '' || preg_match('/^--/', $trimmed)) {
                continue;
            }

            if ($trimmed == 'GO') {
                $exec = $buff;
            }
            else if ($trimmed[strlen($trimmed)-1] == ';') {
                $exec = $buff . substr(rtrim($line), 0, -1);
            }

            if ($exec) {
                $this->query($exec);
                $buff = '';
                $exec = '';
                if ($this->db_error) {
                    break;
                }
            }
            else {
                $buff .= $line . "\n";
            }
        }

        return !$this->db_error;
    }
1346643
final public function fetch($sql, $type = \PDO::FETCH_ASSOC)
    {
        if (!$this->DB) {
            throw  new Exception('您还没有连接数据库', Exception::CODE_DATABASE_ERROR);
        }
        $info = $this->DB->query($sql)->fetch($type);
        $this->lastQueryAt = time();
        return $info;
    }