Class: Sqlbuilder::Statements::Select

Inherits:
Object
  • Object
show all
Includes:
Builders::SelectBuilder
Defined in:
lib/sqlbuilder/statements/select.rb

Instance Method Summary collapse

Methods included from Builders::SelectBuilder

#build_columns, #build_from, #build_joins, #build_limit, #build_offset, #build_order, #build_where

Constructor Details

#initialize(utils) ⇒ Select

Returns a new instance of Select.



8
9
10
11
12
# File 'lib/sqlbuilder/statements/select.rb', line 8

def initialize(utils)
  @utils = utils
  @columns = []
  @joins = []
end

Instance Method Details

#aggregation(name, column) ⇒ Object



61
62
63
64
65
# File 'lib/sqlbuilder/statements/select.rb', line 61

def aggregation(name, column)
  @columns << {col: column, aggregation: name}

  self
end

#buildObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sqlbuilder/statements/select.rb', line 67

def build
  sql = "SELECT"
  sql << " #{build_columns}"
  sql << " #{build_from}"
  sql << " #{build_joins}" unless @joins.empty?
  sql << " #{build_where}" if @where && !@where.empty?
  sql << " #{build_order}" if @order && !@order.empty?
  sql << " #{build_limit}" if @limit
  sql << " #{build_offset}" if @offset

  sql
end

#column(column, from: nil, as: nil) ⇒ Object



14
15
16
17
18
# File 'lib/sqlbuilder/statements/select.rb', line 14

def column(column, from: nil, as: nil)
  @columns << {col: column, from: from, as: as}

  self
end

#from(table, aliaz: nil) ⇒ Object



20
21
22
23
24
# File 'lib/sqlbuilder/statements/select.rb', line 20

def from(table, aliaz: nil)
  @table = table
  @aliaz = aliaz
  self
end

#join(from, type: "INNER", aliaz: nil, on: {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/sqlbuilder/statements/select.rb', line 50

def join(from, type: "INNER", aliaz: nil, on: {})
  @joins << {
    from:  from,
    type:  type,
    aliaz: aliaz,
    on:    on
  }

  self
end

#limit(limit) ⇒ Object



38
39
40
41
42
# File 'lib/sqlbuilder/statements/select.rb', line 38

def limit(limit)
  @limit = limit

  self
end

#offset(offset) ⇒ Object



44
45
46
47
48
# File 'lib/sqlbuilder/statements/select.rb', line 44

def offset(offset)
  @offset = offset

  self
end

#order(order_hash) ⇒ Object



32
33
34
35
36
# File 'lib/sqlbuilder/statements/select.rb', line 32

def order(order_hash)
  @order = order_hash

  self
end

#where(query_hash) ⇒ Object



26
27
28
29
30
# File 'lib/sqlbuilder/statements/select.rb', line 26

def where(query_hash)
  @where = query_hash

  self
end