Module: Microframe::ORM::QueryUtils

Included in:
InstanceQueries, Queryset
Defined in:
lib/microframe/orm/query_utils.rb

Instance Method Summary collapse

Instance Method Details

#build_query(queryhash) ⇒ Object



46
47
48
49
50
# File 'lib/microframe/orm/query_utils.rb', line 46

def build_query(queryhash)
  query = []
  query_processes.each { |process| query << send(process, queryhash)}
  query.join(" ").strip
end

#execute(query) ⇒ Object



10
11
12
# File 'lib/microframe/orm/query_utils.rb', line 10

def execute(query)
  Connection.execute(query)
end

#parse_result_to_objects(result) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/microframe/orm/query_utils.rb', line 52

def parse_result_to_objects(result)
  hash_objects = []
  result.each do |hash|
    obj = @model.new
    hash.each do |key, val|
      if key.is_a? String
        obj.instance_variable_set("@#{key}", val)
      end
    end
    hash_objects << obj
  end
  hash_objects
end

#process_from(queryhash) ⇒ Object



19
20
21
22
# File 'lib/microframe/orm/query_utils.rb', line 19

def process_from(queryhash)
  queryhash["FROM"] ||= table_name
  "FROM #{queryhash["FROM"]}"
end

#process_generic(name, queryhash) ⇒ Object



29
30
31
32
# File 'lib/microframe/orm/query_utils.rb', line 29

def process_generic(name, queryhash)
  return "" unless queryhash[name]
  "#{name} #{queryhash[name]}"
end

#process_limit(queryhash) ⇒ Object



38
39
40
# File 'lib/microframe/orm/query_utils.rb', line 38

def process_limit(queryhash)
  process_generic("LIMIT", queryhash)
end

#process_order(queryhash) ⇒ Object



34
35
36
# File 'lib/microframe/orm/query_utils.rb', line 34

def process_order(queryhash)
  process_generic("ORDER BY", queryhash)
end

#process_query(queryset) ⇒ Object



4
5
6
7
8
# File 'lib/microframe/orm/query_utils.rb', line 4

def process_query(queryset)
  return unless queryset
  query = build_query(queryset)
  execute(query)
end

#process_select(queryhash) ⇒ Object



14
15
16
17
# File 'lib/microframe/orm/query_utils.rb', line 14

def process_select(queryhash)
  queryhash["SELECT"] ||= ["*"]
  "SELECT #{queryhash["SELECT"].join(", ")}"
end

#process_where(queryhash) ⇒ Object



24
25
26
27
# File 'lib/microframe/orm/query_utils.rb', line 24

def process_where(queryhash)
  return "" unless queryhash["WHERE"]
  "WHERE #{queryhash["WHERE"].join(" AND ")}"
end

#query_processesObject



42
43
44
# File 'lib/microframe/orm/query_utils.rb', line 42

def query_processes
  [:process_select, :process_from, :process_where, :process_order, :process_limit]
end