Class: Yamori::QueryMethods::QueryCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/yamori/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.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yamori/query_condition.rb', line 17

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.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def all_field_names
  @all_field_names
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def conditions
  @conditions
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def connection
  @connection
end

#count_selectObject (readonly)

Returns the value of attribute count_select.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def count_select
  @count_select
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def fields
  @fields
end

#limit_numObject (readonly)

Returns the value of attribute limit_num.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def limit_num
  @limit_num
end

#max_select_fieldObject (readonly)

Returns the value of attribute max_select_field.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def max_select_field
  @max_select_field
end

#min_select_fieldObject (readonly)

Returns the value of attribute min_select_field.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def min_select_field
  @min_select_field
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def object_name
  @object_name
end

#row_orderObject (readonly)

Returns the value of attribute row_order.



6
7
8
# File 'lib/yamori/query_condition.rb', line 6

def row_order
  @row_order
end

Instance Method Details

#allObject



76
77
78
# File 'lib/yamori/query_condition.rb', line 76

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

#countObject



88
89
90
91
# File 'lib/yamori/query_condition.rb', line 88

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

#limit(num) ⇒ Object



55
56
57
58
# File 'lib/yamori/query_condition.rb', line 55

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

#max(field_name) ⇒ Object



93
94
95
96
# File 'lib/yamori/query_condition.rb', line 93

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

#min(field_name) ⇒ Object



98
99
100
101
# File 'lib/yamori/query_condition.rb', line 98

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

#not(*expr) ⇒ Object



37
38
39
40
41
42
# File 'lib/yamori/query_condition.rb', line 37

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

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

#order(*fields) ⇒ Object



60
61
62
63
64
65
# File 'lib/yamori/query_condition.rb', line 60

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

  @row_order = fields
  return self
end

#pluck(field_name) ⇒ Object



80
81
82
# File 'lib/yamori/query_condition.rb', line 80

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

#select(*expr) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/yamori/query_condition.rb', line 44

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



84
85
86
# File 'lib/yamori/query_condition.rb', line 84

def take
  limit(1).all.first
end

#to_soqlObject



67
68
69
70
71
72
73
74
# File 'lib/yamori/query_condition.rb', line 67

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



30
31
32
33
34
35
# File 'lib/yamori/query_condition.rb', line 30

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

  conditions.append to_string_expr(expr)
  self
end