Class: Aquel::Executor
- Inherits:
-
Object
- Object
- Aquel::Executor
- Defined in:
- lib/aquel/executor.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #colum_filter(items, target_list) ⇒ Object
- #define(name, &block) ⇒ Object
- #execute(sql) ⇒ Object
- #expr_value(expr) ⇒ Object
- #filter(items, attributes) ⇒ Object
-
#initialize ⇒ Executor
constructor
A new instance of Executor.
- #parser ⇒ Object
- #walk(node, attributes) ⇒ Object
Constructor Details
#initialize ⇒ Executor
Returns a new instance of Executor.
7 8 9 |
# File 'lib/aquel/executor.rb', line 7 def initialize @contexts = {} end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
5 6 7 |
# File 'lib/aquel/executor.rb', line 5 def context @context end |
Instance Method Details
#colum_filter(items, target_list) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/aquel/executor.rb', line 68 def colum_filter(items, target_list) items.map do |item| result = [] target_list.each do |target| val = expr_value(target['RESTARGET']['val']) case val when {"A_STAR"=>{}} result = item when Fixnum result << item[val-1] end end result end end |
#define(name, &block) ⇒ Object
11 12 13 14 15 |
# File 'lib/aquel/executor.rb', line 11 def define(name, &block) @contexts[name] = Context.new(block) self end |
#execute(sql) ⇒ Object
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 49 50 |
# File 'lib/aquel/executor.rb', line 17 def execute(sql) ast = parser.parse(sql).parsetree.first type = ast['SELECT']['fromClause'][0]['RANGEVAR']['relname'] context = @contexts[type] context.execute! items = [] attributes = Attributes.new walk(ast['SELECT']['whereClause'], attributes) document = context.document_block.call(attributes.equal_values) if context.items_block context.items_block.call(document).each do |item| value = context.split_block.call(item) items << (value.kind_of?(Array) ? value : [value]) end elsif context.find_by_block.size > 0 context.find_by_block.each do |k, v| v.call(attributes.equal_values[k], document).each do |item| value = context.split_block.call(item) items << (value.kind_of?(Array) ? value : [value]) end end else while item = context.item_block.call(document) items << context.split_block.call(item) end end items = filter(items, attributes) items = colum_filter(items, ast['SELECT']['targetList']) end |
#expr_value(expr) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/aquel/executor.rb', line 100 def expr_value(expr) if expr['COLUMNREF'] expr['COLUMNREF']['fields'][0] elsif expr['A_CONST'] expr['A_CONST']['val'] end end |
#filter(items, attributes) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aquel/executor.rb', line 52 def filter(items, attributes) attributes.each do |k, v| if k.kind_of?(Fixnum) items = items.find_all do |item| if v[:name] == '=' item[k-1] == v[:value] elsif v[:name] == '<>' item[k-1] != v[:value] end end end end items end |
#parser ⇒ Object
108 109 110 |
# File 'lib/aquel/executor.rb', line 108 def parser @parser ||= PgQuery end |
#walk(node, attributes) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/aquel/executor.rb', line 87 def walk(node, attributes) if aexpr = node['AEXPR'] k = expr_value(aexpr['lexpr']) v = expr_value(aexpr['rexpr']) attributes[k] = { :value => v, :name => aexpr['name'][0] } elsif aexpr = node['AEXPR AND'] walk(aexpr['lexpr'], attributes) walk(aexpr['rexpr'], attributes) elsif aexpr['AEXPR OR'] raise 'OR clauses are not supported yet' end end |