class InfluxORM::Query

Attributes

model[R]

Public Class Methods

new(model) click to toggle source
# 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

Public Instance Methods

count() click to toggle source
# 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) click to toggle source
# File lib/influx_orm/query.rb, line 49
def fill(val)
  @fill = val
  self
end
format_conds(conds, relation) click to toggle source

conds: [{col_name: 'val'}, 'col_name = 1 AND c2 = 2'] relation: :and :or

# 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) click to toggle source
# File lib/influx_orm/query.rb, line 44
def group_by(*group)
  @group = group
  self
end
limit(n) click to toggle source
# File lib/influx_orm/query.rb, line 59
def limit(n)
  @limit = n
  self
end
offset(n) click to toggle source
# File lib/influx_orm/query.rb, line 69
def offset(n)
  @offset = n
  self
end
or(conds = {}) click to toggle source
# File lib/influx_orm/query.rb, line 39
def or(conds = {})
  @or_conds << conds if conds.present?
  self
end
order_by(order) click to toggle source
# File lib/influx_orm/query.rb, line 54
def order_by(order)
  @order = order
  self
end
reload() click to toggle source
# File lib/influx_orm/query.rb, line 101
def reload
  @result = nil
  result
end
result() click to toggle source
# File lib/influx_orm/query.rb, line 97
def result
  @result ||= model.connection.query(to_sql)
end
select(s) click to toggle source
# File lib/influx_orm/query.rb, line 29
def select(s)
  @select = s
  self
end
slimit(n) click to toggle source
# File lib/influx_orm/query.rb, line 64
def slimit(n)
  @slimit = n
  self
end
soffset(n) click to toggle source
# File lib/influx_orm/query.rb, line 74
def soffset(n)
  @soffset = n
  self
end
to_sql() click to toggle source
# 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 = {}) click to toggle source
# File lib/influx_orm/query.rb, line 34
def where(conds = {})
  @where_conds << conds if conds.present?
  self
end

Private Instance Methods

compare_cond_to_sql(name, hash) click to toggle source
# File lib/influx_orm/query.rb, line 144
def compare_cond_to_sql(name, hash)
  hash.map do |k, v|
    v = format_query_val(v) if name.to_sym == :time

    case k.to_sym
    when :gt then "#{name} > #{v}"
    when :gte then "#{name} >= #{v}"
    when :lt then "#{name} < #{v}"
    when :lte then "#{name} <= #{v}"
    else
      raise "Invalid compare '#{k}'"
    end
  end.join(' AND ')
end
format_query_val(val) click to toggle source
# File lib/influx_orm/query.rb, line 166
def format_query_val(val)
  case val
  when Time, DateTime
    "'#{val.iso8601}'"
  when Date
    "'#{val.to_time.iso8601}'"
  else
    val
  end
end
order_to_s() click to toggle source
# File lib/influx_orm/query.rb, line 159
def order_to_s
  return @order if @order.is_a?(String)
  @order.map do |k, v|
    "#{k} #{v}"
  end.join(', ')
end
select_to_s() click to toggle source
# File lib/influx_orm/query.rb, line 139
def select_to_s
  return @select if @select.is_a?(String)
  @select.map { |k, v| "#{k}(#{v})" }.join(', ')
end