Class: Card::Query
- Inherits:
-
Object
show all
- Defined in:
- lib/card/query.rb,
lib/card/query/ref_clause.rb,
lib/card/query/card_clause.rb,
lib/card/query/value_clause.rb
Defined Under Namespace
Classes: CardClause, Clause, RefClause, SqlCond, SqlStatement, ValueClause
Constant Summary
collapse
- MODIFIERS =
{}
- OPERATORS =
%w{ != = =~ < > in ~ }.inject({}) {|h,v| h[v]=nil; h }.merge({
:eq => '=', :gt => '>', :lt => '<',
:match => '~', :ne => '!=', 'not in' => nil
}.stringify_keys)
Instance Method Summary
collapse
Constructor Details
#initialize(query) ⇒ Query
Returns a new instance of Query.
16
17
18
|
# File 'lib/card/query.rb', line 16
def initialize query
@card_clause = CardClause.build query
end
|
Instance Method Details
20
21
22
|
# File 'lib/card/query.rb', line 20
def query
@card_clause.query
end
|
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/card/query.rb', line 28
def run
retrn = query[:return].present? ? query[:return].to_s : 'card'
if retrn == 'card'
simple_run('name').map do |name|
Card.fetch name, :new=>{}
end
else
simple_run retrn
end
end
|
63
64
65
|
# File 'lib/card/query.rb', line 63
def run_sql
ActiveRecord::Base.connection.select_all( sql )
end
|
#simple_run(retrn) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/card/query.rb', line 40
def simple_run retrn
rows = run_sql
case retrn
when 'name' if query[:prepend] || query[:append]
rows.map do |row|
[ query[:prepend], row['name'], query[:append] ].compact * '+'
end
else
rows.map { |row| row['name'] }
end
when 'count'
rows.first['count'].to_i
when 'raw'
rows
when /id$/
rows.map { |row| row[retrn].to_i }
else
rows.map { |row| row[retrn] }
end
end
|
24
25
26
|
# File 'lib/card/query.rb', line 24
def sql
@sql ||= @card_clause.to_sql
end
|