Class: FluentQuery::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent-query/compiler.rb

Overview

Query compiler.

Constant Summary collapse

FORMATTING_DIRECTIVES =

Contains all possible formatting directives.

[:i, :s, :l, :b, :f, :d, :t, :sql, :and, :or]
DIRECTIVE_PREFIX =

Indicates directive prefix.

"%%"

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ Compiler

Returns a new instance of Compiler.



54
55
56
# File 'lib/fluent-query/compiler.rb', line 54

def initialize(processor)
    @_processor = processor
end

Instance Method Details

#callsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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) ⇒ Object



63
64
65
# File 'lib/fluent-query/compiler.rb', line 63

def compile_formatting(directive)
    self.calls[directive.to_sym]
end