Class: Perpetuity::Postgres::SQLSelect
- Inherits:
-
Object
- Object
- Perpetuity::Postgres::SQLSelect
- Defined in:
- lib/perpetuity/postgres/sql_select.rb
Instance Attribute Summary collapse
-
#group_by ⇒ Object
readonly
Returns the value of attribute group_by.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#where ⇒ Object
readonly
Returns the value of attribute where.
Instance Method Summary collapse
- #group_by_clause ⇒ Object
-
#initialize(*args) ⇒ SQLSelect
constructor
A new instance of SQLSelect.
- #limit_clause ⇒ Object
- #offset_clause ⇒ Object
- #order_clause ⇒ Object
- #to_s ⇒ Object
- #where_clause ⇒ Object
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 = args.first @table = .fetch(:from) @where = [:where] @group_by = [:group] @order = [:order] @limit = [:limit] @offset = [:offset] end |
Instance Attribute Details
#group_by ⇒ Object (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 |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
6 7 8 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 6 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
6 7 8 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 6 def offset @offset end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
6 7 8 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 6 def order @order end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
6 7 8 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 6 def selection @selection end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
6 7 8 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 6 def table @table end |
#where ⇒ Object (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_clause ⇒ Object
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_clause ⇒ Object
58 59 60 61 62 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 58 def limit_clause if limit " LIMIT #{limit}" end end |
#offset_clause ⇒ Object
64 65 66 67 68 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 64 def offset_clause if offset " OFFSET #{offset}" end end |
#order_clause ⇒ Object
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_s ⇒ Object
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_clause ⇒ Object
31 32 33 34 35 |
# File 'lib/perpetuity/postgres/sql_select.rb', line 31 def where_clause if where " WHERE #{where}" end end |