Class: FluentQuery::Queries::Processor

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

Overview

Query processor.

Its primary aim is to dive processing methods from query object as is. In fact, defines something as processing session.

Constant Summary collapse

COLUMN_REPLACEMENT =

Indicates column replacement to perform. (Flag.)

2
STRING_REPLACEMENT =

Indicates string replacement to perform. (Flag.)

1
FORMATTING_REPLACEMENT =

Indicates formatting directive replacements to perform. (Flag.)

4
FORMATTING_DIRECTIVE =

Describes formatting directive.

/%%(?:[islbfdt]|sql|and|or)(?:[^\w]|$)/
COLUMN_DIRECTIVE =

Describes column directive.

/(?:^|\.|[^\'"\w\\])?\[(?:[\w\.]*[^\\\W])\](?:[^\'"\w]|\.|$)/
STRING_DIRECTIVE =

Describes string directive.

/(?:^|[^\\])"/
FORMATTING_DIRECTIVE_SIMPLE =

Holds simple column definition.

/%%\w+/
COLUMN_DIRECTIVE_SIMPLE =

Holds simple column definition.

/\[.+\]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ Processor

Returns a new instance of Processor.



111
112
113
# File 'lib/fluent-query/queries/processor.rb', line 111

def initialize(driver)
    @driver = driver
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



97
98
99
# File 'lib/fluent-query/queries/processor.rb', line 97

def driver
  @driver
end

Instance Method Details

#compile(string) ⇒ Object

Compiles the string.



386
387
388
# File 'lib/fluent-query/queries/processor.rb', line 386

def compile(string)
    self.compiler.compile(string)
end

#compilerObject

Returns compiler.



374
375
376
377
378
379
380
# File 'lib/fluent-query/queries/processor.rb', line 374

def compiler
    if @_compiler.nil?
        @_compiler = FluentQuery::Compiler::new(self)
    end
    
    @_compiler
end

#expanderObject



344
345
346
347
348
349
350
# File 'lib/fluent-query/queries/processor.rb', line 344

def expander
    if @__fluent_expander.nil?
        @__fluent_expander = self.class::FORMATTING_DIRECTIVE            
    end

    @__fluent_expander
end

#nullObject



335
336
337
# File 'lib/fluent-query/queries/processor.rb', line 335

def null
    @driver.null
end

#process_array(array, glue = ",") ⇒ Object



123
124
125
126
127
128
# File 'lib/fluent-query/queries/processor.rb', line 123

def process_array(array, glue = ",")
    result = array.map do |i| 
        self.quote_value(i)
    end
    return result.join(glue + " ")
end

#process_directive(directive, argument = nil, replacements = 7) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/fluent-query/queries/processor.rb', line 299

def process_directive(directive, argument = nil, replacements = 7)
    if (replacements & self.class::COLUMN_REPLACEMENT > 0) and (directive[0].ord == 91)           # "[", Column directive
        result = directive.gsub(self.class::COLUMN_DIRECTIVE_SIMPLE) { |value| self.quote_identifier(value[1..-2]) }
    elsif (replacements & self.class::STRING_REPLACEMENT > 0) and (directive[-1].ord == 34)           # "\"", String directive
        result = directive.gsub('"', @driver.quote_string("").last)
    elsif (replacements & self.class::FORMATTING_REPLACEMENT > 0) and (directive[0..1] == "%%")   # Formatting directive
        result = directive.gsub(self.class::FORMATTING_DIRECTIVE_SIMPLE) { |value| self.process_formatting(value[2..-1], argument) }
    else
        result = directive
    end

    return result
end

#process_formatted(sequence, mode = :build) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/fluent-query/queries/processor.rb', line 242

def process_formatted(sequence, mode = :build)
    count = sequence.length
    i = 0
    output = ""
                                
    replacer_settings = self.class::COLUMN_REPLACEMENT | self.class::STRING_REPLACEMENT
    expander_settings = self.class::FORMATTING_REPLACEMENT
    
    while i < count 
        item = sequence[i]
         
        ##
        
        if item.kind_of? String
            item = item.dup
        
            # Calls to each occurence of directive matching to directive
            #  finding expression directive processing. In each call increases
            #  sequence position counter so moves forward.

            if mode.in? [:compile, :build, :prepare]
                item.gsub!(self.replacer) do |directive| 
                    directive.strip!
                    self.process_directive(directive, nil, replacer_settings)
                end
            end
            
            if mode.in? [:finish, :build]
                item.gsub!(self.expander) do |directive| 
                    self.process_directive(directive, sequence[i += 1], expander_settings) 
                end
            end
            
            output << item

        elsif item.kind_of? Symbol
            output << self.quote_identifier(item)
            
        else
            output << item.to_s
            
        end

        output << " "
        i += 1
    end

    return output
    
end

#process_formatting(directive, argument) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/fluent-query/queries/processor.rb', line 318

def process_formatting(directive, argument)
    proc = self.compiler.compile_formatting(directive)
    
    if proc
        output = proc.call(argument)
    else
        output = ""
    end
    
    return output
end

#process_hash(hash, glue = ",", equality = :comparing) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fluent-query/queries/processor.rb', line 143

def process_hash(hash, glue = ",", equality = :comparing)
    result = [ ]
    mode = equality
    
    hash.each_pair do |key, value|
        operator = @driver.quote_equality(value, mode)                        
        key = self.quote_identifier(key)
        value = self.quote_value(value)

        result << (key + " " + operator + " " + value)
    end

    return result.join(" " << glue << " ")
end

#process_identifiers(identifiers) ⇒ Object



226
227
228
# File 'lib/fluent-query/queries/processor.rb', line 226

def process_identifiers(identifiers)
    self.quote_identifiers(identifiers).join(", ")
end

#quote_identifier(identifier) ⇒ Object



207
208
209
# File 'lib/fluent-query/queries/processor.rb', line 207

def quote_identifier(identifier)
    @driver.quote_identifier(identifier.to_s)
end

#quote_identifiers(identifiers) ⇒ Object



216
217
218
# File 'lib/fluent-query/queries/processor.rb', line 216

def quote_identifiers(identifiers)
    identifiers.map { |i| self.quote_identifier(i) }
end

#quote_subquery(subquery) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/fluent-query/queries/processor.rb', line 194

def quote_subquery(subquery)
    if subquery.kind_of? FluentQuery::Query
        subquery = subquery.build!
    end
    
    return @driver.quote_subquery(subquery)
end

#quote_value(value) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fluent-query/queries/processor.rb', line 165

def quote_value(value)

    if (value.string?) or (value.symbol?)
        result = @driver.quote_string(value.to_s)
    elsif value.kind_of? Integer
        result = @driver.quote_integer(value)
    elsif value.array?
        result = "(" << self.process_array(value) << ")"    # TODO: question is, if we should do it here, if it's enough general just for processor
    elsif value.kind_of? Float
        result = @driver.quote_float(value)
    elsif (value.kind_of? TrueClass) or (value.kind_of? FalseClass)
        result = @driver.quote_boolean(value)
    elsif (value.kind_of? Date) or (value.kind_of? DateTime)
        result = @driver.quote_date_time(value)
    elsif value.nil?
        result = @driver.null
    else
        result = value.to_s
    end
    
    return result
    
end

#replacerObject



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/fluent-query/queries/processor.rb', line 357

def replacer
    if @__fluent_replacer.nil?
        parts = Array[self.class::COLUMN_DIRECTIVE, self.class::STRING_DIRECTIVE]

        result = ""
        result << "(?:(?:" << parts.join(")|(?:") << "))"
        
        @__fluent_replacer = Regexp::new(result)
    end

    @__fluent_replacer
end