Class: Microframe::ORM::Queryset

Inherits:
Object
  • Object
show all
Includes:
QueryUtils
Defined in:
lib/microframe/orm/queryset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryUtils

#build_query, #execute, #parse_result_to_objects, #process_from, #process_generic, #process_limit, #process_order, #process_query, #process_select, #process_where, #query_processes

Constructor Details

#initialize(model) ⇒ Queryset

Returns a new instance of Queryset.



10
11
12
13
14
# File 'lib/microframe/orm/queryset.rb', line 10

def initialize(model)
  @queryset = {}
  @table_name = model.table_name
  @model = model
end

Instance Attribute Details

#querysetObject (readonly)

Returns the value of attribute queryset.



8
9
10
# File 'lib/microframe/orm/queryset.rb', line 8

def queryset
  @queryset
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



8
9
10
# File 'lib/microframe/orm/queryset.rb', line 8

def table_name
  @table_name
end

Instance Method Details

#add_query(field, condition) ⇒ Object



49
50
51
52
53
# File 'lib/microframe/orm/queryset.rb', line 49

def add_query(field, condition)
  @queryset ||= {}
  @queryset[field] = queryset[field] ?  queryset[field] << condition : [condition]
  self
end

#all(val = "*") ⇒ Object



22
23
24
25
# File 'lib/microframe/orm/queryset.rb', line 22

def all(val = "*")
  add_query("SELECT", "#{val}") unless queryset["SELECT"]
  fetch
end

#fetchObject



55
56
57
58
59
# File 'lib/microframe/orm/queryset.rb', line 55

def fetch
  result = process_query(queryset)
  @queryset = {}
  parse_result_to_objects(result)
end

#find(id) ⇒ Object



27
28
29
# File 'lib/microframe/orm/queryset.rb', line 27

def find(id)
  where(id: id)
end

#find_by(option) ⇒ Object



31
32
33
# File 'lib/microframe/orm/queryset.rb', line 31

def find_by(option)
  where(option)
end

#limit(val) ⇒ Object



39
40
41
42
# File 'lib/microframe/orm/queryset.rb', line 39

def limit(val)
  @queryset["LIMIT"] = val
  self
end

#loadObject



61
62
63
# File 'lib/microframe/orm/queryset.rb', line 61

def load
  fetch.first
end

#order(val) ⇒ Object



44
45
46
47
# File 'lib/microframe/orm/queryset.rb', line 44

def order(val)
  @queryset["ORDER BY"] = val
  self
end

#select(val) ⇒ Object



35
36
37
# File 'lib/microframe/orm/queryset.rb', line 35

def select(val)
  add_query("SELECT", val)
end

#where(options) ⇒ Object



16
17
18
19
20
# File 'lib/microframe/orm/queryset.rb', line 16

def where(options)
  sql = []
  options.each {|key, val| sql << "#{key.to_s} = '#{val}'"}
  add_query("WHERE",  sql.join(" AND "))
end