class DB::Query

A mutable query builder.

Public Class Methods

new(context, buffer = String.new) click to toggle source

Create a new query builder attached to the specified context. @parameter context [Context::Generic] the context which is used for escaping arguments.

# File lib/db/query.rb, line 31
def initialize(context, buffer = String.new)
        @context = context
        @connection = context.connection
        @buffer = +buffer
end

Public Instance Methods

call(&block) click to toggle source

Send the query to the remote server to be executed. See {Context::Session#call} for more details. @returns [Enumerable] The resulting records.

# File lib/db/query.rb, line 101
def call(&block)
        # Console.debug(self, "Executing query...", buffer: @buffer)
        @context.call(@buffer, &block)
end
clause(value) click to toggle source

Append a raw textual clause to the query buffer. @parameter value [String] A raw SQL string, e.g. ‘WHERE x > 10`. @returns [Query] The mutable query itself.

# File lib/db/query.rb, line 40
def clause(value)
        @buffer << " " unless @buffer.end_with?(" ") || @buffer.empty?
        
        @buffer << value
        
        return self
end
identifier(value) click to toggle source

Append an identifier value to the query buffer. Escapes the field according to the requirements of the underlying connection. @parameter value [String | Symbol | DB::Identifier] Passed to the underlying database connection for conversion to a string representation. @returns [Query] The mutable query itself.

# File lib/db/query.rb, line 64
def identifier(value)
        @buffer << " " unless @buffer.end_with?(" ")
        
        @connection.append_identifier(value, @buffer)
        
        return self
end
inspect() click to toggle source
# File lib/db/query.rb, line 110
def inspect
        "\#<#{self.class} #{@buffer.inspect}>"
end
interpolate(fragment, **parameters) click to toggle source

Interpolate a query fragment with the specified parameters. The parameters are escaped before being appended.

@parameter fragment [String] A fragment of SQL including placeholders, e.g. ‘WHERE x > %{column}`. @parameter parameters [Hash] The substitution parameters. @returns [Query] The mutable query itself.

# File lib/db/query.rb, line 78
def interpolate(fragment, **parameters)
        parameters.transform_values! do |value|
                case value
                when Symbol, Identifier
                        @connection.append_identifier(value)
                else
                        @connection.append_literal(value)
                end
        end
        
        @buffer << sprintf(fragment, parameters)
        
        return self
end
key_column(*arguments, **options) click to toggle source
# File lib/db/query.rb, line 93
def key_column(*arguments, **options)
        @buffer << @connection.key_column(*arguments, **options)
        
        return self
end
literal(value) click to toggle source

Append a literal value to the query buffer. Escapes the field according to the requirements of the underlying connection. @parameter value [Object] Any kind of object, passed to the underlying database connection for conversion to a string representation. @returns [Query] The mutable query itself.

# File lib/db/query.rb, line 52
def literal(value)
        @buffer << " " unless @buffer.end_with?(" ")
        
        @connection.append_literal(value, @buffer)
        
        return self
end
to_s() click to toggle source
# File lib/db/query.rb, line 106
def to_s
        @buffer
end