TODO

@param $query
@param array $args
@param null $cache
@param null $duration
@return mixed
@throws \Exception
541394
public function select($query, $args = array(), $cache = null, $duration = null)
    {
        if ($this->connection) {
            if ($this->pre_cache($result, $cache)) {
                return $result;
            }

            Profiler::start('query', 'database');
            $result = $this->connection->select($query, $args, true);
            Profiler::stop('query');
            return $this->cache($result, $cache, $duration);
        }
        throw new \Exception('Database connection is not open!');
    }
757851
public function filter($query, array $args = [])
    {
        $valuesGroupedByFilterKey = [];

        /**
         * @var string
         * @var mixed  $value
         */
        foreach ($args as $key => $value) {
            /**
             * @var string
             * @var string[] $argNames
             */
            foreach ($this->multiArgumentFiltersArgNames as $filterKey => $argNames) {
                // Group together the values if multiple arguments are given the same key
                if (in_array($key, $argNames)) {
                    $valuesGroupedByFilterKey[$filterKey][] = $value;
                }
            }

            // Filters that only take a single argument can be applied directly
            if ($filterInfo = Arr::get($this->singleArgumentFilters, $key)) {
                /** @var \Nuwave\Lighthouse\Support\Contracts\ArgFilterDirective $argFilterDirective */
                $argFilterDirective = $filterInfo['filter'];
                $columnName = $filterInfo['columnName'];

                $query = $argFilterDirective->applyFilter($query, $columnName, $value);
            }
        }

        /**
         * @var string
         * @var array  $values
         */
        foreach ($valuesGroupedByFilterKey as $filterKey => $values) {
            $columnName = Str::before($filterKey, '.');

            if ($values) {
                $argFilterDirective = $this->multiArgumentFilters[$filterKey];

                $query = $argFilterDirective->applyFilter($query, $columnName, $values);
            }
        }

        return $query;
    }
1238281
public function current(array $query = array())
    {
        if(!$this->request)
            throw new \Exedra\Exception\InvalidArgumentException('Http Request does not exist.');

        $uri = $this->request->getUri();

        if(count($query) > 0)
        {
            // append query to uri
            if($uri->getQuery())
                $query = http_build_query($query);
            else
                $query = '?' . http_build_query($query);
        }
        else
        {
            $query = '';
        }

        return $this->request->getUri().$query;
    }
1351446
public function select_count($query, $args = array(), $cache = null)
    {
        $result = $this->select_first($query, $args, $cache);
        if ($result) {
            return current($result);
        }
        return 0;
    }
13185
public static function fetch( $query, $params = array(), $handler = null, $arguments = array( 'obj' ) ) 
	{
		return Handler::create( $handler )->fetch( $query, $params, $arguments );
	}
1173039
public function query($query, array $params = [])
    {
        $this->lastStatement = $this->getPDO()->prepare($query);
        $this->lastStatement->execute($this->prepareParams($params));
        return $this->lastStatement;
    }
1297433
public static function emulateQuery(\PDO $PDO, array $args)
    {
        $argsCount = count($args);

        try {
            $stmt = $PDO->prepare($args[0]);

            if ($stmt === false) {
                throw new \Exception('prepare failed');
            }

            if ($argsCount == 4) {
                $stmt->setFetchMode($args[1], $args[2], $args[3]);
            }

            if ($argsCount == 3) {
                $stmt->setFetchMode($args[1], $args[2]);
            }

            if ($argsCount == 2) {
                $stmt->setFetchMode($args[1]);
            }

            $stmt->execute();
            return $stmt;
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
    }
1189720
public function query($query=null, $params = [])
    {
        $result = false;
        $result_key = [];
        
        if ($query && static::isSql($query)) {
            $sql = $query;
        } else {
            $result_key = $this->getQuery()->set($query)->get('result_key');
            
            $queryType = $this->getQuery()->type();
            
            // limit select count
            if ((!$queryType || $queryType === 'select') 
                && !isset($this->getQuery()['count']) 
                && !isset($this->getQuery()['limit'])
                && !isset($this->getQuery()['pagesize'])) 
            {
                $this->getQuery()->set('limit', 1000);
            }
            
            // update must set where
            if (($queryType === 'update' || $queryType === 'delete') && !$this->getQuery()->get('where')) {
                Ioc::logger('db')->error('query no set where: ' . json_encode($this->getQuery()->all()));
                return false;
            }
            
            $sql = $this->parse();
        }
        
        if ($params) {
            $this->bindParams($params);
        }
        
        $this->lastSql = $this->formatSql($sql);
        
        // sql log
        Ioc::logger('db')->debug($sql);
        
        try {
            $this->getAdapter()->prepare($sql);
            if (!empty($this->binds)) {
                $this->getAdapter()->bindParams($this->binds);
            }
            $bool = $this->getAdapter()->execute();
            
            if (!$bool) {
                throw new Exception($this->getAdapter()->getErrorMessage());
            }
        } catch (Exception $e) {
            
            $this->getAdapter()->rollback();
            throw new Exception('Excute sql error: ' . $e->getMessage() . ', sql:' . $sql);
        }
        
        $rawStatement = explode(" ", $sql);
        $statement = strtolower(trim($rawStatement[0]));
        
        if ($statement === 'show') {
            $result = $this->getAdapter()->fetchAll($fetchmode);
        } elseif ($statement === 'select') {
            $rkeys = $result_key ? StringHelper::toArray($result_key, ',\s') : null;
            
            while($rs = $this->getAdapter()->fetch()) {
                // 如果指定返回结果集的key，则按key重组数据
                if ($rkeys) {
                    if (count($rkeys) == 1) {
                        $rk1 = trim($rkeys[0]);
                        if (isset($rs[$rk1])) {
                            $result[$rs[$rk1]] = $rs;
                        } else {
                            $result[] = $rs;
                        }
                    } elseif (count($rkeys) == 2) {
                        $rk1 = trim($rkeys[0]);
                        $rk2 = trim($rkeys[1]);
                        if (!empty($rk2)) {
                            $result[$rs[$rk1]][$rs[$rk2]] = $rs;
                        } else {
                            $result[$rs[$rk1]][] = $rs;
                        }
                    }
                } else {
                    $result[] = $rs;
                }
            }
        } elseif ($statement === 'update' || $statement === 'delete') {
            $result = $this->getAdapter()->numberOfRows();
        } elseif ($statement === 'insert') {
            $result = $this->getAdapter()->lastId();
        } else {
            return null;
        }
        
        return $result;
    }
1184515
public function query(array $queryArgs, array $query = [], array $options = [])
    {
        $httpParams = [
            'query_parameters' => []
        ];
        $request = [
            'volatile' => $this->volatile
        ];

        if (array_key_exists('controller', $queryArgs)) {
            $request['controller'] = $queryArgs['controller'];
        } else {
            if (!array_key_exists('route', $queryArgs) || !array_key_exists('method', $queryArgs)) {
                throw new InvalidArgumentException('Unable to execute query: missing controller or route/method');
            }

            $request['controller'] = '';
        }

        if (array_key_exists('action', $queryArgs)) {
            $request['action'] = $queryArgs['action'];
        } else {
            if (!array_key_exists('route', $queryArgs) || !array_key_exists('method', $queryArgs)) {
                throw new InvalidArgumentException('Unable to execute query: missing action or route/method');
            }

            $request['action'] = '';
        }

        if (!empty($options)) {
            if (array_key_exists('volatile', $options)) {
                foreach ($options['volatile'] as $meta => $value) {
                    $request['volatile'][$meta] = $value;
                }
            }

            if (array_key_exists('requestId', $options)) {
                $request['requestId'] = $options['requestId'];
            }

            if (array_key_exists('httpParams', $options)) {
                foreach ($options['httpParams'] as $param => $value) {
                    $httpParams[$param] = $value;
                }
            }

            if (isset($options['query_parameters'])) {
                $httpParams['query_parameters'] = array_merge($httpParams['query_parameters'], $options['query_parameters']);
            }

            foreach (['refresh', 'from', 'size', 'scroll'] as $optionParam) {
                if (array_key_exists($optionParam, $options)) {
                    $httpParams['query_parameters'][$optionParam] = $options[$optionParam];
                }
            }
        }

        if (array_key_exists('volatile', $query)) {
            foreach ($query['volatile'] as $meta => $value) {
                $request['volatile'][$meta] = $value;
            }
        }

        foreach ($query as $attr => $value) {
            if ($attr === 'body' && empty($value)) {
                $request['body'] = (object)[];
            } else if ($attr !== 'volatile') {
                $request[$attr] = $value;
            }
        }

        $request = $this->addHeaders($request, $this->headers);

        /*
        * Do not add the token for the checkToken route, to avoid getting a token error when
        * a developer simply wish to verify his token
        */
        if ($this->jwtToken && !($request['controller'] === 'auth' && $request['action'] === 'checkToken')) {
            if (array_key_exists('headers', $request) && !is_array($request['headers'])) {
                $request['headers'] = [];
            }

            $request['headers']['authorization'] = 'Bearer ' . $this->jwtToken;
        }

        foreach (['collection', 'route', 'method', 'index'] as $queryArg) {
            if (array_key_exists($queryArg, $queryArgs)) {
                $request[$queryArg] = $queryArgs[$queryArg];
            }
        }

        if (!array_key_exists('requestId', $request)) {
            $request['requestId'] = Uuid::uuid4()->toString();
        }
         // @todo move this into RequestHandler
        return $this->emitRestRequest($this->convertRestRequest($request, $httpParams));
    }
1298960
protected function mappedQuery(Builder $query, $method, ArgumentBag $args)
    {
        $mapping = $this->getMappingForAttribute($args->get('column'));

        if ($this->relationMapping($mapping)) {
            return $this->mappedRelationQuery($query, $method, $args, $mapping);
        }

        $args->set('column', $mapping);

        return $query->callParent($method, $args->all());
    }
694091
protected function exec($query)
    {
        try {
            Connect::getConn()->exec($query);
        } catch (Exception $e) {
            static::$response["status"] = "error";
            static::$response["response"] = $e->getMessage();
            static::$response["code"] = $e->getCode();

            return static::terminate(static::$response);
        }
        return null;
    }
527513
protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column)
    {
        return $this->pluckMapped($query, $args, $table, $column);
    }
820226
public function query($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
    {
        $query = trim($query);
        if (empty($query)) {
            $query = $this->build();
            if (!$params) {
                $params = $this->getBindValues();
            }
        }

        $this->resetAll();
        $this->lastSql = $query;

        $this->execute($query, $params);

        $rawStatement = explode(" ", $query);

        $statement = strtolower(trim($rawStatement[0]));
        if ($statement === 'select' || $statement === 'show') {
            return $this->sQuery->fetchAll($fetchmode);
        } elseif ($statement === 'update' || $statement === 'delete') {
            return $this->sQuery->rowCount();
        } elseif ($statement === 'insert') {
            if ($this->sQuery->rowCount() > 0) {
                return $this->lastInsertId();
            }
        } else {
            return null;
        }

        return null;
    }
346283
public function fetch( $query, $params = array(), $arguments = array( 'obj' ) )
	{
		$sth = $this->statement( $query, $params );
		
		$args = null;
		
		foreach( $arguments as $argument )
		{
			$args |= constant( "\PDO::FETCH_".strtoupper( $argument ) );
		}
				
		return $sth->fetchAll( $args );
	}
1197018
function query($query,$bind_arr = null){
    $stmt = $this->mysqli->prepare($query);
    if(!$stmt){
     throw new Exception("can't prepare query:[$query] because [".$this->mysqli->error."]");
    }
    if(isset($bind_arr) && $bind_arr != null){
      call_user_func_array(array($stmt,'bind_param'),$this->makeValuesReferenced($bind_arr));
    }
    /* execute prepared statement */
    if(!$stmt->execute()){
       throw new Exception( $this->mysqli->error );
    }
    $answer = $this->fetch($stmt);
    $stmt->close();
    return $answer;
  }
741053
public function scopeFromDecadesAgo(Builder $query, $ago, $endDateTime = null)
    {
        return $this->scopeFromDateTimeRange(DateTime::getDecadesAgo($ago, $endDateTime));
    }
990718
public static function query($query)
    {
        $res = self::connection()->sql_query($query);

        if (!$res || self::connection()->sql_errno()) {
            // SQL statement failed
            $errorMsg = sprintf(
                'SQL Error: %s [errno: %s]',
                self::connection()->sql_error(),
                self::connection()->sql_errno()
            );

            if (defined('TYPO3_cliMode')) {
                throw new \Exception($errorMsg);
            } else {
                debug('SQL-QUERY: ' . $query, $errorMsg, __LINE__, __FILE__);
            }

            $res = null;
        }

        return $res;
    }