Class: SQL::Composer::Compiler
- Inherits:
-
Object
- Object
- SQL::Composer::Compiler
- Defined in:
- lib/sql/composer/compiler.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #call(ast) ⇒ Object
-
#initialize(backend, options) ⇒ Compiler
constructor
A new instance of Compiler.
- #to_s ⇒ Object
- #visit(node) ⇒ Object
- #visit_from(node) ⇒ Object
- #visit_literal(node) ⇒ Object
- #visit_order(node) ⇒ Object
- #visit_select(nodes) ⇒ Object
- #visit_where(node) ⇒ Object
- #with(new_options) ⇒ Object
Constructor Details
#initialize(backend, options) ⇒ Compiler
20 21 22 23 24 25 |
# File 'lib/sql/composer/compiler.rb', line 20 def initialize(backend, ) @backend = backend @options = @nodes = [:nodes] || [] @tokens = [:tokens] end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
12 13 14 |
# File 'lib/sql/composer/compiler.rb', line 12 def backend @backend end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
14 15 16 |
# File 'lib/sql/composer/compiler.rb', line 14 def nodes @nodes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/sql/composer/compiler.rb', line 16 def @options end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
18 19 20 |
# File 'lib/sql/composer/compiler.rb', line 18 def tokens @tokens end |
Instance Method Details
#call(ast) ⇒ Object
27 28 29 30 |
# File 'lib/sql/composer/compiler.rb', line 27 def call(ast) with_tokens(tokens) { ast.map { |node| visit(node) } } Statement.new(compiler: freeze) end |
#to_s ⇒ Object
36 37 38 |
# File 'lib/sql/composer/compiler.rb', line 36 def to_s with_tokens(tokens) { nodes.map(&:to_s).join("\n") }.last end |
#visit(node) ⇒ Object
40 41 42 43 44 |
# File 'lib/sql/composer/compiler.rb', line 40 def visit(node) visitor, *args = node __send__(:"visit_#{visitor}", args) self end |
#visit_from(node) ⇒ Object
50 51 52 53 |
# File 'lib/sql/composer/compiler.rb', line 50 def visit_from(node) source, _ = node add_node(Nodes::From, source: source) end |
#visit_literal(node) ⇒ Object
46 47 48 |
# File 'lib/sql/composer/compiler.rb', line 46 def visit_literal(node) add_node(Nodes::Literal, value: node[0]) end |
#visit_order(node) ⇒ Object
63 64 65 |
# File 'lib/sql/composer/compiler.rb', line 63 def visit_order(node) add_node(Nodes::Order, operations: node) end |
#visit_select(nodes) ⇒ Object
55 56 57 |
# File 'lib/sql/composer/compiler.rb', line 55 def visit_select(nodes) add_node(Nodes::Select, identifiers: nodes) end |
#visit_where(node) ⇒ Object
59 60 61 |
# File 'lib/sql/composer/compiler.rb', line 59 def visit_where(node) add_node(Nodes::Where, operations: node) end |
#with(new_options) ⇒ Object
32 33 34 |
# File 'lib/sql/composer/compiler.rb', line 32 def with() self.class.new(backend, .merge().merge(nodes: nodes)) end |