module Microframe::ORM::QueryUtils

Public Instance Methods

build_query(queryhash) click to toggle source
# 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) click to toggle source
# File lib/microframe/orm/query_utils.rb, line 10
def execute(query)
  Connection.execute(query)
end
parse_result_to_objects(result) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/microframe/orm/query_utils.rb, line 38
def process_limit(queryhash)
  process_generic("LIMIT", queryhash)
end
process_order(queryhash) click to toggle source
# File lib/microframe/orm/query_utils.rb, line 34
def process_order(queryhash)
  process_generic("ORDER BY", queryhash)
end
process_query(queryset) click to toggle source
# 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) click to toggle source
# File lib/microframe/orm/query_utils.rb, line 14
def process_select(queryhash)
  queryhash["SELECT"] ||= ["*"]
  "SELECT #{queryhash["SELECT"].join(", ")}"
end
process_where(queryhash) click to toggle source
# File lib/microframe/orm/query_utils.rb, line 24
def process_where(queryhash)
  return "" unless queryhash["WHERE"]
  "WHERE #{queryhash["WHERE"].join(" AND ")}"
end
query_processes() click to toggle source
# File lib/microframe/orm/query_utils.rb, line 42
def query_processes
  [:process_select, :process_from, :process_where, :process_order, :process_limit]
end