Class: Restforce::Query::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/query/builder.rb

Overview

Private class, used by Restforce::Query

Constant Summary collapse

MAX_QUERY_SIZE =
20_000
QueryTooLargeError =
StandardError.new("Maximum query size of #{MAX_QUERY_SIZE} exceeded")

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
17
# File 'lib/restforce/query/builder.rb', line 9

def initialize
  @fields = []
  @tables = []
  @custom_conditions = []
  @conditions = {}
  @groupings = []
  @limit = nil
  @distinct = false
end

Instance Method Details

#distinctObject



30
31
32
33
# File 'lib/restforce/query/builder.rb', line 30

def distinct
  @distinct = true
  self
end

#from(*tables) ⇒ Object



35
36
37
38
# File 'lib/restforce/query/builder.rb', line 35

def from(*tables)
  @tables = tables.flatten
  self
end

#group_by(*groupings) ⇒ Object



46
47
48
49
# File 'lib/restforce/query/builder.rb', line 46

def group_by(*groupings)
  @groupings += (groupings.flatten - @groupings)
  self
end

#limit(lim) ⇒ Object



51
52
53
54
# File 'lib/restforce/query/builder.rb', line 51

def limit(lim)
  @limit = lim
  self
end

#renderObject Also known as: to_s

Raises:



56
57
58
59
60
61
62
# File 'lib/restforce/query/builder.rb', line 56

def render
  raise 'There must be at least one field in the SELECT clause' if @fields.empty?
  raise 'There must be at least one table in the FROM clause' if @tables.empty?
  result = "#{render_select}#{render_from}#{render_where}#{render_group_by}#{render_limit}"
  raise QueryTooLargeError if result.size > MAX_QUERY_SIZE
  result.strip
end

#select(*fields) ⇒ Object



19
20
21
22
# File 'lib/restforce/query/builder.rb', line 19

def select(*fields)
  @fields += (fields.flatten - @fields)
  self
end

#select_distinct(*fields) ⇒ Object



24
25
26
27
28
# File 'lib/restforce/query/builder.rb', line 24

def select_distinct(*fields)
  @fields += (fields.flatten - @fields)
  @groupings += (fields.flatten - @groupings)
  self
end

#where(custom_condition = nil, **conditions) ⇒ Object



40
41
42
43
44
# File 'lib/restforce/query/builder.rb', line 40

def where(custom_condition = nil, **conditions)
  @custom_conditions << "(#{custom_condition})" unless custom_condition.nil?
  @conditions = @conditions.merge(conditions)
  self
end