public function query($query, $return_set = false, $buffered = true)
    {
        $this->_result = null;

        $query = trim($query);

        $this->_result = @pg_query($this->_connection, $query);

        // Invalid Query

        if (!$this->_result) {

            $error_message = @\pg_last_error($this->_connection); //NOSONAR

            throw new ConnectionException('An error occurred while
					attempting to query the PostGres server: ' . $error_message);

        } // Valid Query

        else {

            $num_rows = @\pg_num_rows($this->_result);
            $matches = [];

            /** @noinspection PhpUnusedLocalVariableInspection */
            $match_count = preg_match(
                '/^([\w]+)(?:[\W]+|$)/i', $query, $matches);
            assert('$match_count === 1');

            $query_type = strtoupper($matches[1]);

            switch ($query_type) {

                case 'SELECT':
                case 'SHOW':
                case 'EXPLAIN':
                case 'DESCRIBE':

                    if ($return_set && $num_rows >= 1) {

                        return new PostGresResultSet($this->_result, $buffered);
                    } elseif ($num_rows == 1) {

                        return new PostGresResultRow($this->_result);
                    } elseif ($num_rows > 1) {

                        return new PostGresResultSet($this->_result, $buffered);
                    } else {

                        return false;
                    }

                    break;

                case 'DELETE':
                case 'INSERT':
                case 'REPLACE':
                case 'UPDATE':

                    return $this->getAffectedRows();

                    break;

                case 'START':
                case 'COMMIT':
                case 'ROLLBACK':
                case 'SET':

                    return true;

                    break;

                default:

                    return false;
            }
        }
    }