Class: Metasql::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/metasql/query.rb

Overview

Query …

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed:, mapping: {}) ⇒ Query

Returns a new instance of Query.



6
7
8
9
# File 'lib/metasql/query.rb', line 6

def initialize(parsed:, mapping: {})
  @parsed = parsed
  @mapping = mapping.transform_keys(&:to_sym)
end

Instance Attribute Details

#mappingObject

Returns the value of attribute mapping.



4
5
6
# File 'lib/metasql/query.rb', line 4

def mapping
  @mapping
end

#parsedObject

Returns the value of attribute parsed.



4
5
6
# File 'lib/metasql/query.rb', line 4

def parsed
  @parsed
end

Instance Method Details

#deparseObject

Rebuild query from parsed tokens. For supplying parameter value, call ‘with()` before this method.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/metasql/query.rb', line 17

def deparse
  parsed.map do |token|
    case token
    when Metasql::Optional
      substitute_optional(query_array: token.query)
    when Metasql::Param
      substitute_param(param: token)
    else
      token
    end
  end.join
end

#with(mapping) ⇒ Object



11
12
13
# File 'lib/metasql/query.rb', line 11

def with(mapping)
  dup.tap { |q| q.mapping = mapping }
end