Class: SfCli::Sf::Model::QueryMethods::QueryCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/sf_cli/sf/model/query_condition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, object_name, field_names) ⇒ QueryCondition

Returns a new instance of QueryCondition.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sf_cli/sf/model/query_condition.rb', line 20

def initialize(connection, object_name, field_names)
  @object_name = object_name
  @all_field_names = field_names 
  @connection = connection
  @fields = []
  @conditions = [] 
  @limit_num = nil
  @row_order = nil
  @count_select = false
  @max_select_field = nil
  @min_select_field = nil
end

Instance Attribute Details

#all_field_namesObject (readonly)

Returns the value of attribute all_field_names.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def all_field_names
  @all_field_names
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def conditions
  @conditions
end

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def connection
  @connection
end

#count_selectObject (readonly)

Returns the value of attribute count_select.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def count_select
  @count_select
end

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def fields
  @fields
end

#limit_numObject (readonly)

Returns the value of attribute limit_num.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def limit_num
  @limit_num
end

#max_select_fieldObject (readonly)

Returns the value of attribute max_select_field.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def max_select_field
  @max_select_field
end

#min_select_fieldObject (readonly)

Returns the value of attribute min_select_field.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def min_select_field
  @min_select_field
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def object_name
  @object_name
end

#row_orderObject (readonly)

Returns the value of attribute row_order.



9
10
11
# File 'lib/sf_cli/sf/model/query_condition.rb', line 9

def row_order
  @row_order
end

Instance Method Details

#allObject



83
84
85
# File 'lib/sf_cli/sf/model/query_condition.rb', line 83

def all
  connection.query(to_soql, Object.const_get(object_name.to_sym))
end

#countObject



95
96
97
98
# File 'lib/sf_cli/sf/model/query_condition.rb', line 95

def count
  @count_select = true
  connection.query(to_soql, nil).first['expr0']
end

#limit(num) ⇒ Object



58
59
60
61
# File 'lib/sf_cli/sf/model/query_condition.rb', line 58

def limit(num)
  @limit_num = num
  return self
end

#max(field_name) ⇒ Object



100
101
102
103
# File 'lib/sf_cli/sf/model/query_condition.rb', line 100

def max(field_name)
  @max_select_field = field_name
  connection.query(to_soql, nil).first['expr0']
end

#min(field_name) ⇒ Object



105
106
107
108
# File 'lib/sf_cli/sf/model/query_condition.rb', line 105

def min(field_name)
  @min_select_field = field_name
  connection.query(to_soql, nil).first['expr0']
end

#not(*expr) ⇒ Object



40
41
42
43
44
45
# File 'lib/sf_cli/sf/model/query_condition.rb', line 40

def not(*expr)
  return self unless valid_expr?(expr)

  conditions.append %|(NOT(#{to_string_expr(expr)}))|
  self
end

#order(*fields) ⇒ Object



63
64
65
66
67
68
# File 'lib/sf_cli/sf/model/query_condition.rb', line 63

def order(*fields)
  return self if fields&.empty?

  @row_order = fields
  return self
end

#pluck(field_name) ⇒ Object



87
88
89
# File 'lib/sf_cli/sf/model/query_condition.rb', line 87

def pluck(field_name)
  connection.query(to_soql, nil).map{|record| record[field_name.to_s]}
end

#select(*expr) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/sf_cli/sf/model/query_condition.rb', line 47

def select(*expr)
  return self if expr&.empty?

  if expr.size > 1
    @fields = self.fields + expr
  else
    self.fields << expr.first
  end
  return self
end

#takeObject



91
92
93
# File 'lib/sf_cli/sf/model/query_condition.rb', line 91

def take
  limit(1).all.first
end

#to_csvObject



79
80
81
# File 'lib/sf_cli/sf/model/query_condition.rb', line 79

def to_csv
  connection.query(to_soql, Object.const_get(object_name.to_sym), :csv)
end

#to_soqlObject



70
71
72
73
74
75
76
77
# File 'lib/sf_cli/sf/model/query_condition.rb', line 70

def to_soql
  base   = 'SELECT %{select} FROM %{object}' % {select: select_fields, object: object_name}
  where  = conditions.size.zero?  ? nil : 'WHERE %{where}'    % {where: conditions.flatten.join(' AND ')}
  _order = row_order.nil?         ? nil : 'ORDER BY %{order}' % {order: row_order.join(', ')}
  limit  = limit_num.nil?         ? nil : 'LIMIT %{limit}'    % {limit: limit_num} 

  [base, where, _order, limit].compact.join(' ')
end

#where(*expr) ⇒ Object



33
34
35
36
37
38
# File 'lib/sf_cli/sf/model/query_condition.rb', line 33

def where(*expr)
  return self unless valid_expr?(expr)

  conditions.append to_string_expr(expr)
  self
end