Class: Perpetuity::Postgres::SQLSelect

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/sql_select.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SQLSelect

Returns a new instance of SQLSelect.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#group_byObject (readonly)

Returns the value of attribute group_by.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def group_by
  @group_by
end

#limitObject (readonly)

Returns the value of attribute limit.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def offset
  @offset
end

#orderObject (readonly)

Returns the value of attribute order.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def order
  @order
end

#selectionObject (readonly)

Returns the value of attribute selection.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def selection
  @selection
end

#tableObject (readonly)

Returns the value of attribute table.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def table
  @table
end

#whereObject (readonly)

Returns the value of attribute where.



6
7
8
# File 'lib/perpetuity/postgres/sql_select.rb', line 6

def where
  @where
end

Instance Method Details

#group_by_clauseObject



37
38
39
40
41
# File 'lib/perpetuity/postgres/sql_select.rb', line 37

def group_by_clause
  if group_by
    " GROUP BY #{group_by}"
  end
end

#limit_clauseObject



58
59
60
61
62
# File 'lib/perpetuity/postgres/sql_select.rb', line 58

def limit_clause
  if limit
    " LIMIT #{limit}"
  end
end

#offset_clauseObject



64
65
66
67
68
# File 'lib/perpetuity/postgres/sql_select.rb', line 64

def offset_clause
  if offset
    " OFFSET #{offset}"
  end
end

#order_clauseObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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_sObject



23
24
25
26
27
28
29
# 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_clauseObject



31
32
33
34
35
# File 'lib/perpetuity/postgres/sql_select.rb', line 31

def where_clause
  if where
    " WHERE #{where}"
  end
end