Class: SqlTools::Query
- Inherits:
-
Object
- Object
- SqlTools::Query
- Defined in:
- lib/sql_tools/query.rb
Instance Attribute Summary collapse
-
#common_table_expressions ⇒ Object
readonly
Returns the value of attribute common_table_expressions.
-
#from ⇒ Object
Returns the value of attribute from.
-
#join_nodes ⇒ Object
Returns the value of attribute join_nodes.
-
#select ⇒ Object
Returns the value of attribute select.
Instance Method Summary collapse
-
#initialize ⇒ Query
constructor
A new instance of Query.
- #joins ⇒ Object
- #object_alias_map ⇒ Object
- #objects ⇒ Object
- #predicate ⇒ Object
- #relations ⇒ Object
- #selections ⇒ Object
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
6 7 8 9 |
# File 'lib/sql_tools/query.rb', line 6 def initialize @common_table_expressions = {} @join_nodes = [] end |
Instance Attribute Details
#common_table_expressions ⇒ Object (readonly)
Returns the value of attribute common_table_expressions.
4 5 6 |
# File 'lib/sql_tools/query.rb', line 4 def common_table_expressions @common_table_expressions end |
#from ⇒ Object
Returns the value of attribute from.
3 4 5 |
# File 'lib/sql_tools/query.rb', line 3 def from @from end |
#join_nodes ⇒ Object
Returns the value of attribute join_nodes.
3 4 5 |
# File 'lib/sql_tools/query.rb', line 3 def join_nodes @join_nodes end |
#select ⇒ Object
Returns the value of attribute select.
3 4 5 |
# File 'lib/sql_tools/query.rb', line 3 def select @select end |
Instance Method Details
#joins ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sql_tools/query.rb', line 50 def joins join_nodes.map do |join_node| object_name = join_node.find_node(" (join\n (relation\n (object_reference name: (identifier) @object_name)))\n QUERY\n\n filter = PredicateFilter.new(self)\n object = object_alias_map[object_name]\n predicate = filter.filter(object)\n\n if join_node.children.any? { |child| child.type == :keyword_left }\n LeftJoin.new(object, predicate)\n else\n InnerJoin.new(object, predicate)\n end\n end\nend\n").text |
#object_alias_map ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sql_tools/query.rb', line 112 def object_alias_map @table_alias_map ||= @from.query("(relation) @relation").each_with_object({}) do |captures, map| relation = captures["relation"] relation_name = relation.children.first.name.text relation_alias = relation.respond_to?(:alias) ? relation.alias.text : relation_name map[relation_alias] = common_table_expressions[relation_name] || Table.new(relation_name, relation_alias) map[relation_name] = map[relation_alias] end end |
#objects ⇒ Object
110 |
# File 'lib/sql_tools/query.rb', line 110 def objects = object_alias_map.values.to_set |
#predicate ⇒ Object
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 |
# File 'lib/sql_tools/query.rb', line 80 def predicate @predicate ||= begin nodes = from.query(" (from\n (join\n predicate: (_) @predicate))\n (where\n predicate: (_) @predicate)\n QUERY\n\n builder = Predicate::Builder.new(self)\n predicates = nodes.flat_map do |predicate|\n visitor = PredicateVisitor.new(predicate).visit\n binding.b unless visitor.stack.size == 1\n builder.build(visitor.stack.last)\n end\n\n right = predicates.pop\n\n # This needs to pluck the left & right from binary expressions & rebuild the tree.\n # TODO: maybe this is the rotate algorithm, TBD\n while left = predicates.pop\n predicate = Predicate::Binary.new(left, \"AND\", right)\n right = predicate\n end\n\n right\n end\nend\n").map { |captures| captures["predicate"] } |
#relations ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/sql_tools/query.rb', line 70 def relations objects.each_with_object({}) do |object, map| map[object] ||= [] map[object] << predicates.select do |p| (p.left.is_a?(Column) && p.left.table == object) || (p.right.is_a?(Column) && p.right.table == object) end end end |
#selections ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sql_tools/query.rb', line 11 def selections terms = select.query(" (select_expression\n (term\n value: [\n (field\n (object_reference name: (identifier) @table_alias)?\n name: (identifier) @column_name)\n (invocation) @invocation\n (all_fields\n (object_reference name: (identifier) @table_alias)?) @all_fields\n ]\n alias: (identifier)? @selection_name))\n QUERY\n terms.map! do |captures|\n selection_name = captures[\"selection_name\"]&.text || captures[\"column_name\"]&.text\n\n if captures[\"all_fields\"]\n table = if table_alias = captures[\"table_alias\"]\n object_alias_map[table_alias.text]\n elsif objects.size == 1\n objects.first\n end\n Selection::AllFields.new(table)\n elsif table_alias = captures[\"table_alias\"]\n table = object_alias_map[table_alias.text]\n column_name = captures[\"column_name\"].text\n Selection::Column.new(selection_name, Column.new(table, column_name))\n elsif (column_name = captures[\"column_name\"]&.text) && objects.size == 1\n table = objects.first\n Selection::Column.new(selection_name, Column.new(table, column_name))\n elsif invocation = captures[\"invocation\"]\n Selection::Invocation.new(selection_name, invocation)\n else\n raise \"Unknown selection type\"\n end\n end\nend\n") |