Class: ActiveForce::Query

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

Direct Known Subclasses

ActiveQuery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
10
# File 'lib/active_force/query.rb', line 5

def initialize table
  @table = table
  @conditions = []
  @table_id = 'Id'
  @query_fields = [@table_id]
end

Instance Attribute Details

#query_fieldsObject (readonly)

Returns the value of attribute query_fields.



3
4
5
# File 'lib/active_force/query.rb', line 3

def query_fields
  @query_fields
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/active_force/query.rb', line 3

def table
  @table
end

Instance Method Details

#allObject



16
17
18
# File 'lib/active_force/query.rb', line 16

def all
  self
end

#countObject



102
103
104
# File 'lib/active_force/query.rb', line 102

def count
  clone_and_set_instance_variables(query_fields: ["count(Id)"])
end

#fields(fields_collection = []) ⇒ Object



12
13
14
# File 'lib/active_force/query.rb', line 12

def fields fields_collection = []
  @query_fields += fields_collection.to_a
end

#find(id) ⇒ Object



76
77
78
# File 'lib/active_force/query.rb', line 76

def find id
  where("#{ @table_id } = '#{ id }'").limit 1
end

#firstObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_force/query.rb', line 80

def first
  if @records
    clone_and_set_instance_variables(
      size: 1,
      records: [@records.first],
      decorated_records: [@decorated_records&.first]
    )
  else
    clone_and_set_instance_variables(size: 1)
  end
end

#idsObject



110
111
112
# File 'lib/active_force/query.rb', line 110

def ids
  clone_and_set_instance_variables(query_fields: ["Id"])
end

#join(object_query) ⇒ Object



96
97
98
99
100
# File 'lib/active_force/query.rb', line 96

def join object_query
  chained_query = self.clone
  chained_query.fields ["(#{ object_query.to_s })"]
  chained_query
end

#last(limit = 1) ⇒ Object



92
93
94
# File 'lib/active_force/query.rb', line 92

def last(limit = 1)
  order("Id DESC").limit(limit)
end

#limit(size) ⇒ Object



60
61
62
# File 'lib/active_force/query.rb', line 60

def limit size
  size ? clone_and_set_instance_variables(size: size) : self
end

#limit_valueObject



64
65
66
# File 'lib/active_force/query.rb', line 64

def limit_value
  @size
end

#not(condition) ⇒ Object



46
47
48
# File 'lib/active_force/query.rb', line 46

def not condition
  condition ? where("NOT ((#{condition.join(') AND (')}))") : self
end

#offset(offset) ⇒ Object



68
69
70
# File 'lib/active_force/query.rb', line 68

def offset offset
  clone_and_set_instance_variables(offset: offset)
end

#offset_valueObject



72
73
74
# File 'lib/active_force/query.rb', line 72

def offset_value
  @offset
end

#or(query) ⇒ Object



50
51
52
53
54
# File 'lib/active_force/query.rb', line 50

def or query
  return self unless query

  clone_and_set_instance_variables(conditions: ["(#{and_conditions}) OR (#{query.and_conditions})"])
end

#order(order) ⇒ Object



56
57
58
# File 'lib/active_force/query.rb', line 56

def order order
  order ? clone_and_set_instance_variables(order: order) : self
end

#select(*columns) ⇒ Object



33
34
35
# File 'lib/active_force/query.rb', line 33

def select *columns
  clone_and_set_instance_variables(query_fields: columns)
end

#sum(field) ⇒ Object



106
107
108
# File 'lib/active_force/query.rb', line 106

def sum field
  clone_and_set_instance_variables(query_fields: ["sum(#{field})"])
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_force/query.rb', line 20

def to_s
  <<-SOQL.gsub(/\s+/, " ").strip
    SELECT
      #{ build_select }
    FROM
      #{ @table }
    #{ build_where }
    #{ build_order }
    #{ build_limit }
    #{ build_offset }
  SOQL
end

#where(condition = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/active_force/query.rb', line 37

def where condition = nil
  new_conditions = @conditions | [condition]
  if new_conditions != @conditions
    clone_and_set_instance_variables({conditions: new_conditions})
  else
    self
  end
end