TODO

@param $query
@param array $args
@param bool $returnArray
@return array|bool
@throws \Exception
172714
public function select($query, $args = array(), $returnArray = true)
    {
        if($this->pdo) {

            if (!defined('GL_INSTALL') || !GL_INSTALL) {
                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            } else {
                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
            }
            $stmt = $this->pdo->prepare($query);
            $result = $stmt->execute($args);
            //$list = array();
            if ($result) {
                if ($returnArray) {
                    return $stmt->fetchAll(\PDO::FETCH_ASSOC);
                }
                return true;
            } else {
                //            if($this->getLastError())
                throw new \Exception($query . "\n" . $this->getLastError());
            }
        } else {
            throw new \Exception("Error executing query: $query\nIt seems that the connection was not opened!");

        }
    }
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;
    }
654874
public function query($sql, $arguments = [], $resultType = MYSQLI_ASSOC)
    {
        $ret = null;

        $rows = $this->queryAll($sql, $arguments, $resultType);
        if (count($rows) > 0) {
            $ret = $rows[0];
        }

        return $ret;
    }
1343319
private function _mysql_query($query) {

        // call "mysqli_query"
        $result = mysqli_query($this->link, $query);

        // stop if there was an error
        if (!$result) throw new Exception($this->_mysql_error());

        // return the result if query was successful
        return $result;

    }
1076278
public function executeAsTransaction($query, $arg_sets, $which_connection='')
    {
        $which_connection = $which_connection === '' ? $this->default_db : $which_connection;
        $ret = true;
        $statement = $this->prepare($query, $which_connection);
        $this->startTransaction($which_connection);
        $this->lockTimeout(5, $which_connection);
        foreach ($arg_sets as $args) {
            if (!$this->execute($statement, $args, $which_connection)) {
                $ret = false;
                break;
            }
        }
        if ($ret === true) {
            $this->commitTransaction($which_connection);
        } else {
            $this->rollbackTransaction($which_connection);
        }

        return $ret;
    }
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));
    }
386481
private function getResult($command, $args, $fixArgs = false, $returnRaw = false)
    {
        $cmd = $this->getCommand($command, $args);

        // Marketo expects parameter arrays in the format id=1&id=2, Guzzle formats them as id[0]=1&id[1]=2.
        // Use a quick regex to fix it where necessary.
        if ($fixArgs) {
            $cmd->prepare();

            $url = preg_replace('/id%5B([0-9]+)%5D/', 'id', $cmd->getRequest()->getUrl());
            $cmd->getRequest()->setUrl($url);
        }

        $cmd->prepare();

        if ($returnRaw) {
            return $cmd->getResponse()->getBody(true);
        }

        return $cmd->getResult();
    }
202029
public function append($query, $args = null)
    {
        if ($query instanceof PDBquery) {
            $args = $query->getArgs();
            $query = $query->getQuery();
        };
        $this->_query .= ' ' . $query;
        if (is_array($args)) {
            foreach ($args as $arg) {
                $this->_args[] = $arg;  // Faster than array_merge() which copies
            };
        } elseif ($args !== null) {
            $this->_args[] = $args;
        };
        return $this;
    }
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 );
	}
126230
public function getList($id, $args = array(), $returnRaw = false)
    {
        $args['id'] = $id;

        return $this->getResult('getList', $args, false, $returnRaw);
    }
822019
private static function executeQuery($db, $method, array $args)
    {
        self::$query_count++;
        self::debugStart(current($args)); // sql is always the first arg
        $ret = call_user_func_array(array($db, $method), $args);
        self::debugEnd();

        if (MDB2::isError($ret)) {
            throw new SwatDBException($ret);
        }

        return $ret;
    }
776586
public function refund(array $args) {
        $data = $this->getRefund()->parameters($this->merge($args))->text();
        if (empty($data)) {
            return false;
        }
        if ($data['return_code'] != 'SUCCESS') {
            throw new Exception($data['return_msg']);
        }
        if ($data['result_code'] != 'SUCCESS') {
            throw new Exception($data['err_code_des']);
        }
        return $data;
    }
1091834
function execute(WriterInterface $query, callable $callback) {
		$this->result = null;
		$this->query = $query;
		$this->notify();
		$this->result = $this->getConnection()->execParams($query, $query->getParams(), $query->getTypes());
		$this->notify();
		return $callback($this->result);
	}
433764
public static function formatExportQuery($scope = '', $args = null)
    {
        $query = '';

        if ($scope === static::SCOPE_ALL) {
            $query = 'all';
        }

        if ($scope === static::SCOPE_CURRENT_PAGE) {
            $query = "page:$args";
        }

        if ($scope === static::SCOPE_SELECTED_ROWS) {
            $query = "selected:$args";
        }

        return [static::$queryName => $query];
    }
722845
protected function mergeOrReplaceQuery($query)
    {
        if (is_array($query)) {
            return array_merge($this->query, $query);
        }

        if (!is_string($query)) {
            throw new InvalidArgumentException('Query has to be array or string not '.gettype($query));
        }

        if ($query === '') {
            return [];
        }

        $normalized = ltrim($query, '?');

        $items = [];
        parse_str($normalized, $items);

        // If the query starts with a ? overwrite the complete url
        if ($query[0] == '?') {
            return $items;
        }

        return array_merge($this->query, $items);
    }
1079573
public function runGen($returnArray = true)
	{
		$sql = $this->sqlGen();
		/** @var \mysqli_stmt $stmt */
		$stmt = $this->prepare($sql);
		if ($stmt===null) {
			return false;
		}
		$parType = implode('', $this->whereParamType);
		$values = array_values($this->whereParamValue);
		if ($parType) {
			$reval = $stmt->bind_param($parType, ...$values);
			if (!$reval) {
				$this->throwError("Error in bind","","type: ".json_encode($parType)." values:".json_encode($values));
				return false;
			}
		}
		$this->runQuery($stmt);
		$rows = $stmt->get_result();
		if ($this->genSqlFields) {
			$this->lastSqlFields=$this->obtainSqlFields($rows);
		}
		$stmt->close();
		$this->builderReset();
		if ($returnArray) {
			$r = $rows->fetch_all(MYSQLI_ASSOC);
			$rows->free_result();
			return $r;
		} else {
			return $rows;
		}
	}
867364
public function prepare($query, $args = null) {

		if(strlen($query) === 0) return;

		if(!is_array($args)) {
			$args = func_get_args();
			array_shift($args);
		}


		$query = str_replace(array("'?'", '"?"'), '?', $query);
		$query = preg_replace('/\?/', '%s', $query);
		$args = array_map(array($this, 'escape'), $args);

		return @vsprintf($query, $args);

	}