Class: InfluxORM::Query
- Inherits:
-
Object
- Object
- InfluxORM::Query
- Defined in:
- lib/influx_orm/query.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #count ⇒ Object
- #fill(val) ⇒ Object
-
#format_conds(conds, relation) ⇒ Object
conds: [‘val’, ‘col_name = 1 AND c2 = 2’] relation: :and :or.
- #group_by(*group) ⇒ Object
-
#initialize(model) ⇒ Query
constructor
A new instance of Query.
- #limit(n) ⇒ Object
- #offset(n) ⇒ Object
- #or(conds = {}) ⇒ Object
- #order_by(order) ⇒ Object
- #reload ⇒ Object
- #result ⇒ Object
- #select(s) ⇒ Object
- #slimit(n) ⇒ Object
- #soffset(n) ⇒ Object
- #to_sql ⇒ Object
- #where(conds = {}) ⇒ Object
Constructor Details
#initialize(model) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/influx_orm/query.rb', line 5 def initialize(model) @model = model @select = "*" @where_conds = [] @or_conds = [] @group = [] @fill = nil @order = nil @limit = nil @slimit = nil @offset = nil @soffset = nil @result = nil end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/influx_orm/query.rb', line 3 def model @model end |
Instance Method Details
#count ⇒ Object
22 23 24 25 26 27 |
# File 'lib/influx_orm/query.rb', line 22 def count r = select("count(*)").result return 0 if r.empty? row = r.first['values'].first row[row.except('time').keys.first] end |
#fill(val) ⇒ Object
49 50 51 52 |
# File 'lib/influx_orm/query.rb', line 49 def fill(val) @fill = val self end |
#format_conds(conds, relation) ⇒ Object
conds: [‘val’, ‘col_name = 1 AND c2 = 2’] relation: :and :or
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/influx_orm/query.rb', line 109 def format_conds(conds, relation) conds_strs = conds.map do |sub_cond| next sub_cond if sub_cond.is_a?(String) sub_cond.map do |k, v| if v.is_a?(Hash) compare_cond_to_sql(k, v) else case v when Numeric, true, false then "#{k} = #{v}" else "#{k} = '#{v}'" end end end.join(' AND ') end relation_str = case relation.to_sym when :and then ' AND ' when :or then ' OR ' else raise InfluxORM::Error.new("Invalid relation value '#{relation}'") end conds_strs.map {|str| "(#{str})" }.join(relation_str) end |
#group_by(*group) ⇒ Object
44 45 46 47 |
# File 'lib/influx_orm/query.rb', line 44 def group_by(*group) @group = group self end |
#limit(n) ⇒ Object
59 60 61 62 |
# File 'lib/influx_orm/query.rb', line 59 def limit(n) @limit = n self end |
#offset(n) ⇒ Object
69 70 71 72 |
# File 'lib/influx_orm/query.rb', line 69 def offset(n) @offset = n self end |
#or(conds = {}) ⇒ Object
39 40 41 42 |
# File 'lib/influx_orm/query.rb', line 39 def or(conds = {}) @or_conds << conds if conds.present? self end |
#order_by(order) ⇒ Object
54 55 56 57 |
# File 'lib/influx_orm/query.rb', line 54 def order_by(order) @order = order self end |
#reload ⇒ Object
101 102 103 104 |
# File 'lib/influx_orm/query.rb', line 101 def reload @result = nil result end |
#result ⇒ Object
97 98 99 |
# File 'lib/influx_orm/query.rb', line 97 def result @result ||= model.connection.query(to_sql) end |
#select(s) ⇒ Object
29 30 31 32 |
# File 'lib/influx_orm/query.rb', line 29 def select(s) @select = s self end |
#slimit(n) ⇒ Object
64 65 66 67 |
# File 'lib/influx_orm/query.rb', line 64 def slimit(n) @slimit = n self end |
#soffset(n) ⇒ Object
74 75 76 77 |
# File 'lib/influx_orm/query.rb', line 74 def soffset(n) @soffset = n self end |
#to_sql ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/influx_orm/query.rb', line 79 def to_sql sql = "SELECT #{select_to_s} FROM #{model.table_name}" if @where_conds.present? sql += " WHERE #{format_conds(@where_conds, :and)}" sql += " OR #{format_conds(@or_conds, :or)}" if @or_conds.present? elsif @or_conds.present? sql += " WHERE #{format_conds(@or_conds, :or)}" end sql += " GROUP BY #{@group.join(', ')}" if @group.present? sql += " fill(#{@fill})" if @fill sql += " ORDER BY #{order_to_s}" if @order sql += " LIMIT #{@limit}" if @limit sql += " SLIMIT #{@slimit}" if @slimit sql += " OFFSET #{@offset}" if @offset sql += " SOFFSET #{@soffset}" if @soffset sql end |
#where(conds = {}) ⇒ Object
34 35 36 37 |
# File 'lib/influx_orm/query.rb', line 34 def where(conds = {}) @where_conds << conds if conds.present? self end |