Class: ModelSet::SQLQuery
Instance Attribute Summary
Attributes included from Conditioned
#conditions
Attributes inherited from Query
#limit, #set_class, #sort_order
Instance Method Summary
collapse
#add_conditions!, #invert!, #to_conditions
#size
Methods inherited from Query
after_query, #after_query, before_query, #before_query, #clear_cache!, #clear_limited_cache!, #condition_ops, #initialize, #limit!, #model_class, #model_name, #offset, #on_exception, on_exception, #page, #page!, #pages, #table_name, #transform_condition, #unlimited!, #unsorted!
Instance Method Details
#add_joins!(*joins) ⇒ Object
55
56
57
58
59
|
# File 'lib/model_set/sql_query.rb', line 55
def add_joins!(*joins)
@joins ||= []
@joins.concat(joins)
clear_cache!
end
|
#aggregate(query, opts = {}) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/model_set/sql_query.rb', line 47
def aggregate(query, opts = {})
sql = "SELECT #{query} #{from_clause}"
sql << " LIMIT #{opts[:limit]}" if opts[:limit]
sql << " GROUP BY #{opts[:group_by]}" if opts[:group_by]
result = db.select_rows(sql).first
result.size == 1 ? result.first : result
end
|
#anchor!(query, opts = {}) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/model_set/sql_query.rb', line 5
def anchor!(query, opts = {})
if @limit_fetch = opts[:limit_fetch]
@reorder = query.ids
else
if query.respond_to?(:sql)
sql = "#{id_field_with_prefix} IN (#{query.sql})"
else
sql = ids_clause(query.ids)
@reorder = query.ids
end
add_conditions!(sql)
end
end
|
#count ⇒ Object
95
96
97
|
# File 'lib/model_set/sql_query.rb', line 95
def count
@count ||= limit ? aggregate("COUNT(DISTINCT #{id_field_with_prefix})").to_i : size
end
|
#ids ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/model_set/sql_query.rb', line 19
def ids
if @ids.nil?
if @limit_fetch
base_conditions = conditions
@ids = [].to_ordered_set
@reorder.each_slice(@limit_fetch) do |ids|
self.conditions = to_conditions(:and, ids_clause(ids), *base_conditions)
@ids.concat fetch_id_set(sql)
if limit and @ids.size >= limit
@ids.reorder!(@reorder).limit!(limit)
break
end
end
self.conditions = base_conditions
else
@ids = fetch_id_set(sql)
@ids.reorder!(@reorder) if @reorder
end
end
@ids
end
|
#in!(ids, field = id_field_with_prefix) ⇒ Object
61
62
63
|
# File 'lib/model_set/sql_query.rb', line 61
def in!(ids, field = id_field_with_prefix)
add_conditions!( ids_clause(ids, field) )
end
|
#limit_enabled? ⇒ Boolean
41
42
43
44
45
|
# File 'lib/model_set/sql_query.rb', line 41
def limit_enabled?
return true if @limit_fetch
return false if @reorder
super
end
|
#order_by!(*args) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/model_set/sql_query.rb', line 65
def order_by!(*args)
opts = args.last.kind_of?(Hash) ? args.pop : {}
@sort_join = opts[:join]
@sort_order = args
@reorder = nil
clear_cache!
end
|
#reverse! ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/model_set/sql_query.rb', line 74
def reverse!
if @reorder
@reorder.reverse!
elsif @sort_order
@sort_order.collect! do |sub_order|
if sub_order =~ / DESC$/i
sub_order.slice(0..-6)
else
"#{sub_order} DESC"
end
end
else
@sort_order = ["#{id_field_with_prefix} DESC"]
end
clear_cache!
end
|
#sql ⇒ Object
91
92
93
|
# File 'lib/model_set/sql_query.rb', line 91
def sql
"#{select_clause} #{from_clause} #{order_clause} #{limit_clause}"
end
|