class FluentQuery::Compiler

Query compiler.

Constants

DIRECTIVE_PREFIX

Indicates directive prefix.

FORMATTING_DIRECTIVES

Contains all possible formatting directives.

Public Class Methods

new(processor) click to toggle source

Constructor.

# File lib/fluent-query/compiler.rb, line 54
def initialize(processor)
    @_processor = processor
end

Public Instance Methods

calls() click to toggle source

Returns calls array.

# File lib/fluent-query/compiler.rb, line 72
def calls
    if @__calls_cache.nil?
        @__calls_cache = {
            :i => Proc::new { |v| @_processor.quote_value(v.to_i) },
            :s => Proc::new { |v| @_processor.quote_value(v.to_s) },
            :b => Proc::new { |v| @_processor.quote_value(v ? true : false) },
            :f => Proc::new { |v| @_processor.quote_value(v.to_f) },
            
            :l => Proc::new do |v|
                if v.array?
                    output = v
                elsif v.hash?
                    output = v.values
                elsif v.enumerable?
                    output = v
                else
                    raise ::FluentQuery::Exception::new('Invalid object provided to the list placeholder. Enumerable expected.')
                end

                "(" << @_processor.process_array(output) << ")"
            end,
    
            :d => Proc::new do |v|
                if v.string?
                    output = Date.parse(v)
                elsif argument.kind_of? DateTime
                    output = Date.parse(v.to_s)
                elsif argument.kind_of? Date
                    output = v
                end

                @_processor.quote_value(output)
            end,
                
            :t => Proc::new do |v|
                if v.string?
                    output = DateTime.parse(v)
                elsif argument.kind_of? Date
                    output = DateTime.parse(v.to_s)
                elsif argument.kind_of? DateTime
                    output = v
                end

                @_processor.quote_value(output)
            end,

            :sql => Proc::new do |v|
                if v.kind_of? MP::Fluent::Query
                    output = v.build!
                end
                
                @_processor.quote_subquery(output)
            end,
            
            :and => Proc::new do |v|
                operator = @_processor.driver.quote_operator(:and)
                @_processor.process_hash(v, operator)
            end,

            :or => Proc::new do |v|
                operator = @_processor.driver.quote_operator(:or)
                @_processor.process_hash(v, operator)
            end,
        }
    end
    
    @__calls_cache
end
compile(string) click to toggle source

Compiles string.

# File lib/fluent-query/compiler.rb, line 146
def compile(string) 
    output = FluentQuery::Compilers::Result::new
    prefix = self.class::DIRECTIVE_PREFIX
    buffer = ""

    # Builds compile informations
    if not @__compile_cache
        directives = self.class::FORMATTING_DIRECTIVES.map { |s| s.to_s }
        regexp = Regexp::new("^(" << directives.join("|") << ")(?:[^\w]|$)")
        
        @__compile_cache = regexp
    else
        regexp = @__compile_cache
    end
    
       
    # Splits to by directive separated parts
    string.split(prefix).each do |part|
        match = part.match(regexp)
        
        if match
            if not buffer.empty?
                output << buffer
            end
            
            output << self.compile_formatting(match[1])
            buffer = part[match[1].length..-1]
        else
            buffer << prefix << part
        end
    end
    
    output << buffer
    
    # Corrects and returns result
    output.first.replace(output.first[prefix.length..-1])   # strips out initial "%%"
    return output
    
end
compile_formatting(directive) click to toggle source

Compiles formatting.

# File lib/fluent-query/compiler.rb, line 63
def compile_formatting(directive)
    self.calls[directive.to_sym]
end