class Perpetuity::Postgres::SQLSelect

Attributes

group_by[R]
limit[R]
offset[R]
order[R]
selection[R]
table[R]
where[R]

Public Class Methods

new(*args) click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 8
def initialize *args
  @selection = if args.one?
                 '*'
               else
                 args.shift
               end
  options = args.first
  @table = options.fetch(:from)
  @where = options[:where]
  @group_by = options[:group]
  @order = options[:order]
  @limit = options[:limit]
  @offset = options[:offset]
end

Public Instance Methods

group_by_clause() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 37
def group_by_clause
  if group_by
    " GROUP BY #{group_by}"
  end
end
limit_clause() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 58
def limit_clause
  if limit
    " LIMIT #{limit}"
  end
end
offset_clause() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 64
def offset_clause
  if offset
    " OFFSET #{offset}"
  end
end
order_clause() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 43
def order_clause
  order = Array(self.order)
  order.map! do |(attribute, direction)|
    if direction
      "#{attribute} #{direction.to_s.upcase}"
    else
      attribute
    end
  end

  unless order.empty?
    " ORDER BY #{order.join(',')}"
  end
end
to_s() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 23
def to_s
  "SELECT #{selection} FROM #{TableName.new(table)}" << where_clause.to_s <<
                                                        group_by_clause.to_s <<
                                                        order_clause.to_s <<
                                                        limit_clause.to_s <<
                                                        offset_clause.to_s
end
where_clause() click to toggle source
# File lib/perpetuity/postgres/sql_select.rb, line 31
def where_clause
  if where
    " WHERE #{where}"
  end
end