-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.rb
69 lines (62 loc) · 2.09 KB
/
query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require_relative 'table.rb'
class Query
attr_accessor :limit_var, :clauses, :ordered_by, :model
def initialize(model)
@model = model.is_a?(TableDefintion::Table) ? model.init.model : model
@clauses = {}
end
def is_valid(k, v)
if @model.model.keys.include?(k) and (not @model.singulars.include?(k.to_sym))
return @model.model[k].validator.validate(v)
end
if (@model.singulars.include?(k[0...-4].to_sym) and k.to_s.end_with?("__id")) or @model.singulars.include?(k.to_sym)
return (v.is_a?(Integer) or v.is_a?(Record))
end
return false
end
def where(dict)
for k, v in dict
if not self.is_valid(k, v)
raise "Invalid key-value pair (#{k}, #{v}) for #{model.name}"
end
@clauses[k] = v
end
return self
end
def limit(n)
@limit_var = n
return self
end
def order_by(by, desc)
keys = Set.new(@model.schema.to_dict[@model.name].table.obj.keys)
if (not @model.foreign_keys.include?(by.to_s[0...-4].to_sym)) and keys.include?(by.to_sym)
@ordered_by = by
@order_mode = desc ? "DESC" : "ASC"
return self
end
raise ArgumentError.new "Can only be ordered by non-foreign-key attributes, not '#{by}'"
end
def to_sql
initial = "SELECT * FROM #{@model.name}_ "
clauses_arr = []
for k, v in @clauses
if v.is_a? Record
clauses_arr.append("#{k}__id = #{v[:id]}")
elsif @model.singulars.include?(k)
clauses_arr.append("#{k}__id = #{v}")
else
clauses_arr.append("#{k} = #{v}")
end
end
if @clauses.length > 0
initial += "WHERE #{clauses_arr.join(" AND ")} "
end
if @limit_var != nil
initial += "LIMIT #{@limit_var.to_s} "
end
if @ordered_by != nil
initial += "ORDER BY #{@model.name}_.#{@ordered_by} #{@order_mode} "
end
return "#{initial};"
end
end