Class: SfCli::Sf::Model::QueryMethods::QueryCondition
- Inherits:
-
Object
- Object
- SfCli::Sf::Model::QueryMethods::QueryCondition
- Defined in:
- lib/sf_cli/sf/model/query_condition.rb
Instance Attribute Summary collapse
-
#all_field_names ⇒ Object
readonly
Returns the value of attribute all_field_names.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#limit_num ⇒ Object
readonly
Returns the value of attribute limit_num.
-
#object_name ⇒ Object
readonly
Returns the value of attribute object_name.
-
#row_order ⇒ Object
readonly
Returns the value of attribute row_order.
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(connection, object_name, field_names) ⇒ QueryCondition
constructor
A new instance of QueryCondition.
- #limit(num) ⇒ Object
- #order(*fields) ⇒ Object
- #pluck(field_name) ⇒ Object
- #select(*expr) ⇒ Object
- #select_fields ⇒ Object
- #take ⇒ Object
- #to_soql ⇒ Object
- #where(*expr) ⇒ Object
Constructor Details
#initialize(connection, object_name, field_names) ⇒ QueryCondition
Returns a new instance of QueryCondition.
10 11 12 13 14 15 16 17 18 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 10 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 end |
Instance Attribute Details
#all_field_names ⇒ Object (readonly)
Returns the value of attribute all_field_names.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def all_field_names @all_field_names end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def conditions @conditions end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def connection @connection end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def fields @fields end |
#limit_num ⇒ Object (readonly)
Returns the value of attribute limit_num.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def limit_num @limit_num end |
#object_name ⇒ Object (readonly)
Returns the value of attribute object_name.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def object_name @object_name end |
#row_order ⇒ Object (readonly)
Returns the value of attribute row_order.
8 9 10 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 8 def row_order @row_order end |
Instance Method Details
#all ⇒ Object
117 118 119 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 117 def all connection.query(to_soql, Object.const_get(object_name.to_sym)) end |
#limit(num) ⇒ Object
96 97 98 99 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 96 def limit(num) @limit_num = num return self end |
#order(*fields) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 101 def order(*fields) return self if fields&.empty? @row_order = fields return self end |
#pluck(field_name) ⇒ Object
121 122 123 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 121 def pluck(field_name) all.map{|record| record.__send__(field_name.to_sym)} end |
#select(*expr) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 85 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 |
#select_fields ⇒ Object
129 130 131 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 129 def select_fields (fields.empty? ? all_field_names : fields).join(', ') end |
#take ⇒ Object
125 126 127 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 125 def take limit(1).all.first end |
#to_soql ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 108 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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sf_cli/sf/model/query_condition.rb', line 20 def where(*expr) return self if expr&.empty? return self if expr.map{|o| (o == '' || o == {} || o == []) ? nil : o}.compact.size.zero? return self unless [Hash, Symbol, String].any?{|klass| expr.first.instance_of? klass} if expr.size > 1 return self if expr.size < 3 value = case expr[2].class.name.to_sym when :String %|'#{expr[2]}'| when :Time expr[2].to_datetime when :Array candidates = expr[2].map do |o| case o.class.name.to_sym when :String %|'#{o}'| when :Time o.to_datetime else o end end %|IN (#{candidates.join(', ')})| else expr[2] end conditions << %|#{expr[0]} #{expr[1]} #{value}| return self end if expr[0].instance_of? String conditions << expr[0] return self end new_conditions = expr[0].map do |k,v| case v.class.name.to_sym when :String %|#{k} = '#{v}'| when :Time %|#{k} = #{v.to_datetime}| when :Array candidates = v.map do |o| case o.class.name.to_sym when :String %|'#{o}'| when :Time %|#{o.to_datetime}| else o end end %|#{k} IN (#{candidates.join(', ')})| else "#{k} = #{v}" end end conditions.append new_conditions return self end |