Run a query statement as a prepared PDO statement. Optionally returns the result.

@param \PDOStatement $pStmt PDO statement
@param bool $pReturnResults TRUE to return the results, or FALSE not to.
@throws \Exception
@return mixed
497028
public function pdoQuery(PDOStatement $pStmt, $pReturnResults = true)
    {
        $returnValue = null;

        try {
            // Call the database routine
            $returnValue = $pStmt->execute();

            if ($returnValue && $pReturnResults) {
                // Fetch all rows into an array.
                $rows = $pStmt->fetchAll(PDO::FETCH_ASSOC);

                if ($this->isArray($rows)) {
                    $returnValue = $rows;
                }
            }

            $pStmt->closeCursor();
        } catch (Exception $pError) {
            throw new Exception('A PDO Error has occurred: ' . $pError->getMessage());
        }

        return $returnValue;
    }
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();
		});
	}
919274
public function query($query, array $binds = array())
    {
        //Argument 1 must be a string or null
        Argument::i()->test(1, 'string', self::QUERY);
        
        $request = new \stdClass();
        
        $request->query = $query;
        $request->binds = $binds;
        
        //event trigger
        $this->trigger('sql-query-before', $request);
        
        $connection = $this->getConnection();
        $query      = (string) $request->query;
        $stmt       = $connection->prepare($query);
        
        //bind some more values
        foreach ($request->binds as $key => $value) {
            $stmt->bindValue($key, $value);
        }
        
        //PDO Execute
        if (!$stmt->execute()) {
            $error = $stmt->errorInfo();
            
            //unpack binds for the report
            foreach ($binds as $key => $value) {
                $query = str_replace($key, "'$value'", $query);
            }
            
            //throw Exception
            Exception::i()
                ->setMessage(Exception::QUERY_ERROR)
                ->addVariable($query)
                ->addVariable($error[2])
                ->trigger();
        }
        
        $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        
        if($this->queryCache) {
            //log query
            $this->queries[] = array(
                'query'     => $query,
                'binds'     => $binds,
                'results'   => $results);
        }

        //clear binds
        $this->binds = array();
        
        //event trigger
        $this->trigger('sql-query-after', $query, $binds, $results);
        
        return $results;
    }
1167672
public function query($pQuery, array $pParams = array())
    {
        $this->_stm = Agl::app()->getDb()->getConnection()->prepare($pQuery);

        if (! $this->_stm->execute($pParams)) {
            $error = $this->_stm->errorInfo();
            throw new Exception("The query failed with message '" . $error[2] . "'");
        }

        if (Agl::app()->isDebugMode()) {
            Agl::app()->getDb()->incrementCounter();
        }

        return $this->_stm->fetchAll(PDO::FETCH_ASSOC);
    }
143592
protected final void useResultSet( ResultSet pResultSet ) throws SQLException
  {
    boolean lFirst = true;

    if( !pResultSet.next() )
    {
      handleEmptyResultSet();
    }
    else
    {
      while( lFirst || pResultSet.next() )
      {
        lFirst = false;

        useResultSetRow( pResultSet );
      }
    }
  }
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;
        });
    }
437874
public function statement($query, $bindings = array())
	{
		return $this->run($query, $bindings, function($me, $query, $bindings)
		{
			if ($me->pretending()) return true;

			$bindings = $me->prepareBindings($bindings);

			return $me->getPdo()->prepare($query)->execute($bindings);
		});
	}
1082652
private function binding(\PDOStatement $sql, array $bindings = []): \PDOStatement
    {
        foreach ($bindings as $binding=>$value) {
            if (!is_array($value)) {
                $sql->bindValue($binding, $value);
            } else {
                switch(strtolower($value[1])) {
                    case 'date':
                        $sql->bindValue($binding, $this->time($value[0], 'Y-m-d'), \PDO::PARAM_STR);
                        break;
                    case 'time':
                        $sql->bindValue($binding, $this->time($value[0], 'Y-m-d H:i:s.u'), \PDO::PARAM_STR);
                        break;
                    case 'bool':
                    case 'boolean':
                        $sql->bindValue($binding, boolval($value[0]), \PDO::PARAM_BOOL);
                        break;
                    case 'null':
                        $sql->bindValue($binding, NULL, \PDO::PARAM_NULL);
                        break;
                    case 'int':
                    case 'integer':
                    case 'number':
                    case 'limit':
                    case 'offset':
                        $sql->bindValue($binding, intval($value[0]), \PDO::PARAM_INT);
                        break;
                    case 'str':
                    case 'string':
                    case 'text':
                    case 'float':
                    case 'varchar':
                    case 'varchar2':
                        $sql->bindValue($binding, strval($value[0]), \PDO::PARAM_STR);
                        break;
                    case 'lob':
                    case 'large':
                    case 'object':
                    case 'blob':
                        $sql->bindParam($binding, $value[0], \PDO::PARAM_LOB, strlen($value[0]));
                        break;
                    case 'like':
                        $sql->bindValue($binding, '%'.$value[0].'%', \PDO::PARAM_STR);
                        break;
                    default:
                        if (is_int($value[1])) {
                            $sql->bindValue($binding, $value[0], $value[1]);
                        } else {
                            $sql->bindValue($binding, strval($value[0]), \PDO::PARAM_STR);
                        }
                }
            }
        }
        return $sql;
    }
1193145
public function select($query, $bindings = array(),$useReadPdo = false)
    {
        return $this->run($query, $bindings, function(self $me, $query, array $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
            // node from the database, and will either be an array or objects.
            $statement = $me->getCypherQuery($query, $bindings);

            return $statement->getResultSet();
        });
    }
200975
private function prepare_statement($stmt, $args=[]) {
		if (!$this->connection) {
			self::$logger->error(sprintf(
				"SQL: connection failed: '%s'.",
				json_encode($this->verified_params)));
			throw new SQLError(SQLError::CONNECTION_ERROR,
				$this->dbtype . " connection error.");
		}

		$conn = $this->connection;
		try {
			$pstmt = $conn->prepare($stmt);
		} catch (\PDOException $e) {
			self::$logger->error(sprintf(
				"SQL: execution failed: %s <- '%s': %s.",
				$stmt, json_encode($args),
				$e->getMessage()));
			throw new SQLError(
				SQLError::EXECUTION_ERROR,
				sprintf("Execution error: %s.", $e->getMessage()),
				$stmt, $args
			);
		}

		try {
			$pstmt->execute(array_values($args));
		} catch (\PDOException $e) {
			self::$logger->error(sprintf(
				"SQL: execution failed: %s <- '%s': %s.",
				$stmt, json_encode($args),
				$e->getMessage()));
			throw new SQLError(
				SQLError::EXECUTION_ERROR,
				sprintf("Execution error: %s.", $e->getMessage()),
				$stmt, $args
			);
		}

		return $pstmt;
	}
80191
public static boolean execute(PreparedStatement ps, Object... params) throws SQLException {
		StatementUtil.fillParams(ps, params);
		return ps.execute();
	}
1088994
public function statement($query, $bindings = array(), $rawResults = false)
    {
        return $this->run($query, $bindings, function(self $me, $query, array $bindings) use($rawResults)
        {
            if ($me->pretending()) return true;

            $statement = $me->getCypherQuery($query, $bindings);

            $result = $statement->getResultSet();

            return ($rawResults === true) ? $result : $result instanceof ResultSet;
        });
    }
661388
public static function executeStmt($stmt, $types, $inserts) {
        self::init();
        try {
            if(!$stmt) {
                throw new DbException('Statement not established');
            } elseif(!$stmt->bind_param($types, ...$inserts)) {
                throw new DbException('Binding error');
            }
            $stmt->execute();
        } catch(DbException $e) {
            self::$_ops->formatError($inserts, $e->getMessage());
        } finally {
            return self::evaluateQuery($stmt);
        }

    }
495804
public function query($sqlStatement, $bindParams=null, $bindTypes=null) {
		try {
			$results = $this->callParentQuery($sqlStatement, $bindParams, $bindTypes);
            $results->setFetchMode(Db::FETCH_ASSOC);
            return $results;
		} catch(\PDOException $e) {
			if($e->getCode() === self::MYSQL_WENT_AWAY_CODE || $e->getMessage() === 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away') {
				$this->connect($this->_descriptor);
				return $this->callParentQuery($sqlStatement, $bindParams, $bindTypes);
			} else {
				throw $e;
			}
		}
	}
1145885
public function hydrateAll($stmt, $resultSetMapping, array $hints = [])
    {
        $this->stmt  = $stmt;
        $this->rsm   = $resultSetMapping;
        $this->hints = $hints;

        $this->em->getEventManager()->addEventListener([Events::onClear], $this);

        $this->prepare();

        $result = $this->hydrateAllData();

        $this->cleanup();

        return $result;
    }
5576
public function execute($inputParams = [])
    {
        $bingID = $this->pdo->getBingId();
        $pool = $this->pdo->pool;
        if(!empty($inputParams)){
            $this->_boundColumns = $inputParams;
        }
        if(!empty($this->_boundColumns)){
            $this->prepareParamName();
            $this->_resultData = $pool->prepareAndExecute($this->statement,$this->_boundColumns,$bingID);
            $this->pdo->setLastInsertId($this->_resultData->insert_id);
        }else{
            $this->_resultData = $pool->doQuery($this->statement,$bingID);
        }
        if($this->_resultData->result === false){
            throw new PDOException($this->_resultData->error);
        }
        return true;
    }
800634
public function prepare($query, $options=null)
    {
        $stmt = null;
        try {
            $stmt = new Statement($this, $query);
        } catch (\Exception $e) {
            throw new \PDOException($e->getMessage());
        }
        return $stmt;
    }