Run a select statement against the database and returns a generator.

@param  string $query
@param  array $bindings
@param  bool $useReadPdo
@param  null | string $resultClass
@return \Generator
774553
public function cursor( $query, $bindings = [ ], $useReadPdo = true, $resultClass = null )
	{
		$fetchCustomClass = ! is_null($resultClass) && class_exists($resultClass);

		$statement = $this->run( $query, $bindings, function ( $query, $bindings ) use ( $useReadPdo, $fetchCustomClass ) {
			if( $this->pretending() )
			{
				return [ ];
			}

			// First we will create a statement for the query. Then, we will set the fetch
			// mode and prepare the bindings for the query. Once that's done we will be
			// ready to execute the query against the database and return the cursor.
			$statement = $this->prepared( $this->getPdoForSelect( $useReadPdo )->prepare( $query ), ! $fetchCustomClass );
			$this->bindValues(
				$statement, $this->prepareBindings( $bindings )
			);

			// Next, we'll execute the query against the database and return the statement
			// so we can return the cursor. The cursor will use a PHP generator to give
			// back one row at a time without using a bunch of memory to render them.

			$statement->execute();
			return $statement;
		});

		if( $fetchCustomClass )
		{
			while( $record = $statement->fetchObject( $resultClass ) )
			{
				yield $record;
			}
		}
		else
		{
			while( $record = $statement->fetch() )
			{
				yield $record;
			}
		}
	}
453207
public function affectingStatement($query, $bindings = [])
    {
        $start = $this->getLogTime();

        try {

            $result = $this->wpdb->query($this->bindParams($query, $bindings, true));

            if ($result === false || $this->wpdb->last_error) {
                throw new QueryException($query, $bindings, new \Exception($this->wpdb->last_error));
            }

            return intval($result);

        } catch (QueryException $e) {
            throw $e;
        } finally {
            $this->logQuery($query, $bindings, $this->getElapsedTime($start));
        }
    }
929214
protected function queryHandler($query, $bindings, $time, $connectionName)
    {
        $data = ['connectionName' => $connectionName];

        if ($this->recordSqlBindings) {
            $data['bindings'] = $bindings;
        }

        Integration::addBreadcrumb(new Breadcrumb(
            Breadcrumb::LEVEL_INFO,
            Breadcrumb::TYPE_USER,
            'sql.query',
            $query,
            $data
        ));
    }
26054
public function select($query, $bindings = [], $useReadPdo = true) {
    $types = [];

    $rows = $this->run($query, $bindings, function($query, $bindings) use($useReadPdo, &$types) {
      if($this->pretending()) {
        return [];
      }

      $statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
      $statement->execute($this->prepareBindings($bindings));

      for($i = 0; $i < $statement->columnCount(); $i++) {
        $meta = $statement->getColumnMeta($i);

        if($meta !== false) {
          $type = $meta['native_type'];

          if(isset($meta['sqlite:decl_type'])) {
            if($meta['sqlite:decl_type'] === 'datetime') {
              $type = $meta['sqlite:decl_type'];
            }
          }

          $types[$meta['name']] = strtolower($type);
        }
      }

      return $statement->fetchAll(PDO::FETCH_ASSOC);
    });

    foreach($rows as &$row) {
      foreach($row as $col => &$value) {
        if($value !== null) {
          switch($types[$col]) {
            case 'long':
            case 'integer':
              $value = (int)$value;
            break;

            case 'double':
              $value = (float)$value;
            break;

            case 'timestamp':
            case 'datetime':
              if(strpos($value, '.') === false) {
                $value = strtotime($value);
              } else {
                $value = (float)DateTime::createFromFormat('Y-m-d H:i:s.u', $value)->format('U.u');
              }
            break;
          }
        }
      }
    }

    return $rows;
  }
215356
public function statement($query, $bindings = [])
    {
        return $this->run($query, $bindings, function ($query, $bindings) {
            if ($this->pretending()) {
                return true;
            }

            $statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $this->prepareBindings($bindings));

            return $statement->execute();
        });
    }
915575
public function exec($sql = "", $bindingValues = array(), $insert = true)
    {
        //Check if the query is a SELECT|INSERT|UPDATE and :variation is available
        if (preg_match("/.*(SELECT|INSERT|UPDATE|SHOW|DELETE).*/", $sql)) {
            
            $this->sql = $sql;

            //Always prepare the SQL statement first
            $query = $this->connection->prepare($sql);

            if (preg_match('/\s*:(.+?)\s/', $sql)) {
                if (is_array($bindingValues) && !empty($bindingValues)) {
                    foreach ($bindingValues as $key => $val) {
                        //Clean the bindingValues. Check if they start with :
                        if (!preg_match('/:.*/', $key)) {
                            $bindingValues[":" . $key] = $val;
                            unset($bindingValues[$key]);
                        }
                    }
                }


                //Add the binding variables to the query
                if ($query->execute($bindingValues)) {
                    return $this->getLastInsertID();
                } else {
                    //@ToDo:When the save or update fails make sure we handle the error gracefully!
                    throw new \Exception(implode("\n", $query->errorInfo()));
                }

            } else {
                $result = $query->execute();
            }

            if (preg_match('/.*(SELECT|SHOW).*/', $sql)) {
                $query->setFetchMode(\PDO::FETCH_ASSOC);
                return $query->fetchAll();
            }

            return $result;
        }


        return false;
    }
927646
public function genUnionBindValues (&$query, $args)
	{
		
		foreach ($args as $key => $arg)
		{

			if (isset ($arg['where']) && is_array ($arg['where']))
			{
				$this->genBindValues ($query, $arg['where']);
			}
			
			if (isset ($arg['having']) && is_array ($arg['having']))
			{
				$this->genBindValues ($query, $arg['having']);
			}

		}
	}
534593
public function queryTableGenerator($query, $options = [])
    {
        $statement = $this->_runQuery($query, $options);
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
            yield $row;
        }
    }
4812
public function select($query, $bindings = array(), $useReadPdo = true)
	{
		return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
		{
			if ($me->pretending()) return array();

			// For select statements, we'll simply execute the query and return an array
			// of the database result set. Each element in the array will be a single
			// row from the database table, and will either be an array or objects.
			$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);

			$statement->execute($me->prepareBindings($bindings));

			return $statement->fetchAll($me->getFetchMode());
		});
	}
954997
public function execute($query, $bindings = [])
    {
        if (strtolower(substr($query, 0, 6)) === "select" && $this->forcePrefix) {
            $query = $this->forcePrefix . $query;
        }

        return $this->run($query, $bindings, function ($query, $bindings) {
            if ($this->pretending()) {
                return true;
            }

            $statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $bindings);

            $statement->execute();
            return $statement;
        });
    }
1105172
private function interpolate($query, $bindings)
	{
		$offset = 0;
		$index  = 0;

		foreach (Str::findLiteral('?', $query) as $placeholder)
		{
			$sub     = (is_null($bindings[$index])) ? 'NULL' : $this->quote($bindings[$index]);
			$query   = substr_replace($query, $sub, $placeholder + $offset, 1);
			$offset += (strlen($sub) - 1);
			$index++;
		}

		return $query;
	}
1350718
public function affectingStatement($query, $bindings = array())
	{
		return $this->run($query, $bindings, function($me, $query, $bindings)
		{
			if ($me->pretending()) return 0;

			// For update or delete statements, we want to get the number of rows affected
			// by the statement and return that back to the developer. We'll first need
			// to execute the statement and then we'll use PDO to fetch the affected.
			$statement = $me->getPdo()->prepare($query);

			$statement->execute($me->prepareBindings($bindings));

			return $statement->rowCount();
		});
	}
1122602
public function query(string $sql, array $bindings = []): IQueryBuilderHandler
    {
        $queryObject = new QueryObject($sql, $bindings, $this->getConnection());
        $this->connection->setLastQuery($queryObject);

        $this->fireEvents(EventHandler::EVENT_BEFORE_QUERY, $queryObject);

        [$response, $executionTime] = $this->statement($queryObject->getSql(), $queryObject->getBindings());

        $this->fireEvents(EventHandler::EVENT_AFTER_QUERY, $queryObject, [
            'execution_time' => $executionTime,
        ]);

        $this->pdoStatement = $response;

        return $this;
    }
809306
public function raw($query, array $bindings = array())
    {
        if (!empty($bindings)) {
            foreach ($bindings as $column => $value) {
                $this->bind($column, $value);
            }
        }

        $this->queryBuilder->raw($query);
        return $this;
    }
777516
public function insert($query, $bindings = [])
    {
        try {
            $this->statement($query, $bindings);

            return true;
        } catch (\Exception $e) {
            throw new InternalServerErrorException('Insert failed. ' . $e->getMessage());
        }
    }
766146
protected function statement($query, $bindings = array())
    {
        $statement  = $this->getActivePdo()->prepare($query);

        return $statement->execute($bindings);
    }
1130453
public function select($query, $bindings = array())
	{
		return $this->run($query, $bindings, function($me, $query, $bindings)
		{
			if ($me->pretending()) return array();

			// For select statements, we'll simply execute the query and return an array
			// of the database result set. Each element in the array will be a single
			// row from the database table, and will either be an array or objects.
			$statement = $me->getReadPdo()->prepare($query);

			$statement->execute($me->prepareBindings($bindings));

			return $statement->fetchAll($me->getFetchMode());
		});
	}