Class: Aquel::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/aquel/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



7
8
9
# File 'lib/aquel/executor.rb', line 7

def initialize
  @contexts = {}
end

Instance Attribute Details

#contextObject (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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aquel/executor.rb', line 52

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[val] = item[val]
      when String
        result[val] = item[val]
      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
# File 'lib/aquel/executor.rb', line 17

def execute(sql)
  ast = parser.parse(sql).parsetree.first
  type = ast['SELECT']['fromClause'][0]['RANGEVAR']['relname']

  items = []
  attributes = Attributes.new

  walk(ast['SELECT']['whereClause'], attributes)
  context = @contexts[type]
  items = context.execute(attributes)
  items = filter(items, attributes)
  items = colum_filter(items, ast['SELECT']['targetList'])
end

#expr_value(expr) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/aquel/executor.rb', line 86

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



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 31

def filter(items, attributes)
  attributes.each do |k, v|
    case k
    when Fixnum
      items = items.find_all do |item|
        v.operate(item[k])
      end
    when String
      items = items.find_all do |item|
        if item[k]
          v.operate(item[k])
        else
          true
        end
      end
    end
  end

  items
end

#parserObject



94
95
96
# File 'lib/aquel/executor.rb', line 94

def parser
  @parser ||= PgQuery
end

#walk(node, attributes) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aquel/executor.rb', line 73

def walk(node, attributes)
  if aexpr = node['AEXPR']
    k = expr_value(aexpr['lexpr'])
    v = expr_value(aexpr['rexpr'])
    attributes[k] = Attribute.new(: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