Execute a given query and return the result as an Entity Iterator object

@param  PreparableSqlInterface $query Query to be executed
@return EntityIterator
769416
protected function executeAndGetResultsAsEntityIterator(PreparableSqlInterface $query)
    {
        $entities = $this->execute($query)
            ->toEntityArray();

        return new EntityIterator($entities);
    }
449416
public function queryWith(QueryInterface $query)
    {
        $this->query($query);
        $queryBuilder = $query->build();

        return $this->findWith($queryBuilder);
    }
39958
public function query(
        Query $query,
        array $parameters = []
    ): Result {
        $result = $this
            ->repository
            ->query($query, $parameters);

        return $this->applyTransformersToResult($result);
    }
345994
public function process(QueryInterface $query)
    {
        if (!$query instanceof ShowQuery || $query->getObject() !== ShowQuery::EXPRESSION_TABLES) {
            return null;
        }

        $tablet = $this->tablet->getTablet();
        $command = new GetSchema($tablet->getAlias());
        /* @var $vtCtldResult GetSchemaResult */
        $vtCtldResult = $this->client->executeCommand($command);

        return new Tables($vtCtldResult);
    }
274839
public function executeOn(Builder $query)
    {
        $this->stack->each(function (CriteriaInterface $criteria) use ($query) {
            $criteria->execute($query);
        });

        return $query;
    }
178205
public function execQuery(Query $query)
    {
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->connection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $query->isSingle());
        $resultDataType = $query->getResultDataType();

        foreach ($query as $sql) {
            $sqlResult = $sql->getResult();
            if ($resultDataType instanceof DataInterface) {
                $sqlResult->setDataType($resultDataType);
            }

            try {
                if ($this->error->hasError()) {
                    throw new \PDOException($this->error->getMessage());
                    break;
                }
                $sqlQuery = $sql->toString();
                $totalRows = 0;
                if ($sql->getCommand() == 'SELECT' && !$query->isSingle()) {
                    $template = '{SQL}';
                    $templateCounter = 'SELECT COUNT(*) totalRows FROM (%s) fakeTable';
                    $templateParams = array(
                        'SQL' => $sqlQuery,
                        'LIMIT' => $query->getLimit(),
                        'OFFSET' => $query->getOffset(),
                    );
                    if ($query->getLimit()) {
                        switch ($this->config->getType()) {
                            case 'firebird':
                                $template = 'SELECT FIRST {LIMIT} SKIP {OFFSET} * FROM ({SQL}) fakeTable';
                            break;
                            case 'oci':
                                $templateCounter = 'SELECT COUNT(*) "totalRows" FROM (%s) "fakeTable"';
                                $template = 'SELECT a.* FROM (SELECT b.*, rownum "[]" FROM '.
                                            '({SQL}) b WHERE rownum <= {LIMIT}) a WHERE "[]" > {OFFSET}';
                            break;
                            default:
                                $template = 'SELECT * FROM ({SQL}) fakeTable LIMIT {LIMIT} OFFSET {OFFSET}';
                            break;
                        }
                    }

                    $resultCounter = $this->connection->prepare(sprintf($templateCounter, $sqlQuery));
                    $resultCounter->execute();
                    $totalRows = $resultCounter->fetch();

                    $sqlResult->setTotalRows((int) @$totalRows['totalRows']);
                    foreach ($templateParams as $key => $val) {
                        $template = str_replace('{'.$key.'}', $val, $template);
                    }
                    $sqlQuery = $template;
                }

                $result = $this->connection->prepare($sqlQuery);
                $stmt = $result->execute();
                $sql->execute();

                if ($sql->isRead()) {
                    $columns = $this->_define_column_types($result);

                    $sqlResult->setColumnTypes($columns);
                    $sqlResult->setAffected(0);
                    $sqlResult->setStartRow($query->getOffset());
                    if (!$sqlResult->getTotalRows()) {
                        $sqlResult->setTotalRows($result->rowCount());
                    }

                    $rows = $result->fetchAll(\PDO::FETCH_ASSOC);

                    foreach ($rows as $row) {
                        $data = $this->_merge_values($sql->getResult()->getDataType(), $row, $columns);
                        $sqlResult->add($data);
                    }

                    $sqlResult->setTotalShows($sqlResult->length());
                    $sqlResult->setEnd(microtime(true));
                } elseif ($sql->isWrite() || $sql->isDDL()) {
                    $insertId = $this->connection->lastInsertId();
                    $affected = $result->rowCount();
                    $endTime = microtime(true);
                    $sqlResult->setEnd($endTime);

                    $columns = array(
                        'affected' => 'integer',
                        'seconds' => 'double',
                    );

                    $row = array(
                        'affected' => $affected,
                        'seconds' => $sqlResult->getSeconds(),
                    );

                    $sqlResult->setColumnTypes($columns);
                    $sqlResult->setAffected($affected);

                    $data = new Data();
                    $this->_merge_values($data, $row, $columns);
                    $sqlResult->add($data);
                    $sqlResult->setInsertId($insertId);
                }
            } catch (\PDOException $e) {
                $sql->execute(false);
                $this->error->setMessage($e->getMessage().". \n".$sql->toString());
                $sqlResult->setEnd(microtime(true));
            } catch (\Exception $e) {
                $sql->execute(false);
                $this->error->setMessage($e->getMessage().". \n".$sql->toString());
                $sqlResult->setEnd(microtime(true));
            }
        }
    }
110461
public function query( SqlInterface $query, $options = 0 )
    {

        $options += $options & ( self::RECONNECT_AS_REQUIRED | self::RECONNECT_NO ) ? 0 : self::RECONNECT_DEFAULT;

        $this->numQuerys++;

        $timings = array();
        $timings['start'] = microtime( true );

        $sql = $query->parse( $this );
        $timings['query_parsed'] = microtime( true );

        $this->debug->emit( Debug::INFO, self::QUERY_PARSED, $sql );

        // debugging search path pest
        // $search_path = pg_query( $this->resource->get(), "SHOW search_path"); $result = pg_fetch_row( $search_path ); d_pr( $result );

        // set exception
        $resource = $this->resource->get( $options & self::RECONNECT_AS_REQUIRED );

        try {

            // really ghetto debuggin for when shit really hits the fan
            // file_put_contents( '~/sqllog.out', "{$sql}\n\n", FILE_APPEND );

            // execute query
            if( false === $result = @pg_query( $resource, $sql ) ) {
                throw new \Exception();
            }

        } catch( \Exception $e ) {

            $exceptionFactory = new QueryExceptionFactory( $resource, $sql, $this->debug );
            throw $exceptionFactory->get();

        }

        $timings['query_executed'] = microtime( true );
        $timings['total'] = $timings['query_executed'] - $timings['start'];
        $timings['parsing'] = $timings['query_parsed'] - $timings['start'];
        $timings['total'] = $timings['query_executed'] - $timings['query_parsed'];

        $this->debug->emit( self::QUERY_OK, $sql, $timings );

        $resultResourceObject = new Result( $result, $this, $query, $timings );

        return $resultResourceObject;

    }
455165
public function exec($query, $args = array(), $removeCache = null)
    {
        if ($this->connection) {
            if ($this->getCache() && $removeCache) {
                $this->getCache()->remove($removeCache);
            }
            return $this->connection->select($query, $args, false);
        }
        throw new \Exception('Database connection is not open!');
    }
559639
public function prepare($query)
    {
        $this->connect();
        $options = [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL];
        $isObject = $query instanceof Query;
        if ($isObject && $query->isBufferedResultsEnabled() === false) {
            $options = [];
        }
        $statement = $this->_connection->prepare($isObject ? $query->sql() : $query, $options);

        return new SqlserverStatement($statement, $this);
    }
829797
public function scopeSort($query)
    {
        if (!$this->sorters) {

            $message = 'Please set the $sorters property.';
            throw new FilterableSortableException($message);
        }

        $this->sorters = app()->make($this->sorters);

        if (!$this->sorters instanceof QuerySorters) {

            $message = '$sorters property must be a class instance of QuerySorter.';
            throw new FilterableSortableException($message);
        }

        return $this->sorters->apply($query);
    }
1285334
public function run($query = false, $exception = true)
    {

        if (true === $query) {
            return $this->resolveQuery($this->query);
        }

        list($prepare, $resolved) = $this->resolvePreparedStatement($this->query, $this->params);

        if (false === $resolved) {
            if (true === $exception) {
                $this->resolveQueryException();
            }
        }

        return $prepare;
    }
1318486
protected static function preprocessQuery($query, $parameters = [])
    {
        if (is_array($parameters) && count($parameters)) {
            return vsprintf($query, $parameters);
        } else {
            if (isset($parameters)) {
                return sprintf($query, $parameters);
            } else {
                return $query;
            }
        }
    }
786535
private function getBuilder($query)
    {
        $queryType = $query->getType();

        if (!isset($this->builders[$queryType])) {
            throw new \InvalidArgumentException("Unknow query builder for {$queryType}.");
        }

        return $this->builders[$queryType];
    }
185914
public function execute($query = null)
    {
        try {
            $event = new ConnectionEvent($this, $query);
            $this->container->getDispatcher()->dispatch(ConnectionEvent::ON_QUERY, $event);
            $this->affectedRows = parent::exec($query);
        } catch (\Exception $exception) {
            throw new ConnectionException('Executing query has error: [:code] ":error"', [
                'code'  => $exception->getCode(),
                'error' => $exception->getMessage(),
            ]);
        }
        
        return $this;
    }
508113
protected function prepareQuery($query)
    {
        $query = $query->with($this->withs);

        if ($this->withCounts) {
            $query = $query->withCount($this->withCounts);
        }

        if ($this->orderBy) {
            $query = $query->orderBy($this->orderBy, $this->orderByDirection);
        }

        if ( ! empty($this->has)) {
            foreach ($this->has as $has) {
                $query = $query->has($has['relation'], $has['operator'], $has['value']);
            }
        }

        if ( ! empty($this->whereHas)) {
            foreach ($this->whereHas as $has) {
                $query = $query->whereHas($has[0], isset($has[1]) ? $has[1] : null);
            }
        }

        return $query;
    }
600769
public function execute(
        Query $query,
        MapInterface $variables
    ): SetInterface {
        return ($this->makeEntity)(
            $this->connection->execute($query),
            $variables
        );
    }
4452
public function process(QueryInterface $query)
    {
        if (!$query instanceof ShowQuery || $query->getObject() !== ShowQuery::EXPRESSION_CREATE_TABLE) {
            return null;
        }

        $table = $query->getCreateObjectExpression(ShowQuery::EXPRESSION_CREATE_TABLE);

        if (!$table) {
            throw new Exception('Table missing.');
        }

        $tablet = $this->tablet->getTablet();
        $schemaCmd = new GetSchema($tablet->getAlias());
        /* @var $result GetSchemaResult */
        $result = $this->client->executeCommand($schemaCmd);

        return new CreateTable($result, $table);
    }