sf_cli 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,16 @@ module SfCli
6
6
  module QueryMethods
7
7
  # @private :nodoc: just for developers
8
8
  class QueryCondition
9
- attr_reader :connection, :object_name, :all_field_names, :fields, :conditions, :limit_num, :row_order
9
+ attr_reader :connection,
10
+ :object_name,
11
+ :all_field_names,
12
+ :fields,
13
+ :conditions,
14
+ :limit_num,
15
+ :row_order,
16
+ :count_select,
17
+ :max_select_field,
18
+ :min_select_field
10
19
 
11
20
  def initialize(connection, object_name, field_names)
12
21
  @object_name = object_name
@@ -16,71 +25,23 @@ module SfCli
16
25
  @conditions = []
17
26
  @limit_num = nil
18
27
  @row_order = nil
28
+ @count_select = false
29
+ @max_select_field = nil
30
+ @min_select_field = nil
19
31
  end
20
32
 
21
33
  def where(*expr)
22
- return self if expr&.empty?
23
- return self if expr.map{|o| (o == '' || o == {} || o == []) ? nil : o}.compact.size.zero?
24
- return self unless [Hash, Symbol, String].any?{|klass| expr.first.instance_of? klass}
34
+ return self unless valid_expr?(expr)
25
35
 
26
- if expr.size > 1
27
- return self if expr.size < 3
28
-
29
- value = case expr[2].class.name.to_sym
30
- when :String
31
- %|'#{expr[2]}'|
32
- when :Time
33
- expr[2].to_datetime
34
- when :Array
35
- candidates = expr[2].map do |o|
36
- case o.class.name.to_sym
37
- when :String
38
- %|'#{o}'|
39
- when :Time
40
- o.to_datetime
41
- else
42
- o
43
- end
44
- end
45
- %|IN (#{candidates.join(', ')})|
46
- else
47
- expr[2]
48
- end
49
- conditions << %|#{expr[0]} #{expr[1]} #{value}|
50
-
51
- return self
52
- end
36
+ conditions.append to_string_expr(expr)
37
+ self
38
+ end
53
39
 
54
- if expr[0].instance_of? String
55
- conditions << expr[0]
56
- return self
57
- end
40
+ def not(*expr)
41
+ return self unless valid_expr?(expr)
58
42
 
59
- new_conditions =
60
- expr[0].map do |k,v|
61
- case v.class.name.to_sym
62
- when :String
63
- %|#{k} = '#{v}'|
64
- when :Time
65
- %|#{k} = #{v.to_datetime}|
66
- when :Array
67
- candidates = v.map do |o|
68
- case o.class.name.to_sym
69
- when :String
70
- %|'#{o}'|
71
- when :Time
72
- %|#{o.to_datetime}|
73
- else
74
- o
75
- end
76
- end
77
- %|#{k} IN (#{candidates.join(', ')})|
78
- else
79
- "#{k} = #{v}"
80
- end
81
- end
82
- conditions.append new_conditions
83
- return self
43
+ conditions.append %|NOT(#{to_string_expr(expr)})|
44
+ self
84
45
  end
85
46
 
86
47
  def select(*expr)
@@ -115,21 +76,113 @@ module SfCli
115
76
  [base, where, _order, limit].compact.join(' ')
116
77
  end
117
78
 
79
+ def to_csv
80
+ connection.query(to_soql, Object.const_get(object_name.to_sym), :csv)
81
+ end
82
+
118
83
  def all
119
84
  connection.query(to_soql, Object.const_get(object_name.to_sym))
120
85
  end
121
86
 
122
87
  def pluck(field_name)
123
- all.map{|record| record.__send__(field_name.to_sym)}
88
+ connection.query(to_soql, nil).map{|record| record[field_name.to_s]}
124
89
  end
125
90
 
126
91
  def take
127
92
  limit(1).all.first
128
93
  end
129
94
 
95
+ def count
96
+ @count_select = true
97
+ connection.query(to_soql, nil).first['expr0']
98
+ end
99
+
100
+ def max(field_name)
101
+ @max_select_field = field_name
102
+ connection.query(to_soql, nil).first['expr0']
103
+ end
104
+
105
+ def min(field_name)
106
+ @min_select_field = field_name
107
+ connection.query(to_soql, nil).first['expr0']
108
+ end
109
+
110
+ private
111
+
130
112
  def select_fields
113
+ return 'COUNT(Id)' if count_select
114
+ return "MAX(#{max_select_field})" if max_select_field
115
+ return "MIN(#{min_select_field})" if min_select_field
116
+
131
117
  (fields.empty? ? all_field_names : fields).join(', ')
132
118
  end
119
+
120
+ def to_string_expr(expr)
121
+ return str_by_ternary_expr(expr) if expr.size > 1
122
+ return expr[0] if expr[0].instance_of? String
123
+
124
+ strs_by_hash_expr(expr)
125
+ end
126
+
127
+ def str_by_ternary_expr(expr)
128
+ return self if expr.size < 3
129
+
130
+ value = case expr[2].class.name.to_sym
131
+ when :String
132
+ %|'#{expr[2]}'|
133
+ when :Time
134
+ expr[2].to_datetime
135
+ when :Array
136
+ candidates = expr[2].map do |o|
137
+ case o.class.name.to_sym
138
+ when :String
139
+ %|'#{o}'|
140
+ when :Time
141
+ o.to_datetime
142
+ else
143
+ o
144
+ end
145
+ end
146
+ %|IN (#{candidates.join(', ')})|
147
+ else
148
+ expr[2]
149
+ end
150
+ %|#{expr[0]} #{expr[1]} #{value}|
151
+ end
152
+
153
+ def valid_expr?(expr)
154
+ return false if expr&.empty?
155
+ return false if expr.map{|o| (o == '' || o == {} || o == []) ? nil : o}.compact.size.zero?
156
+ return false unless [Hash, Symbol, String].any?{|klass| expr.first.instance_of? klass}
157
+
158
+ true
159
+ end
160
+
161
+ def strs_by_hash_expr(expr)
162
+ expr[0].map do |k,v|
163
+ case v.class.name.to_sym
164
+ when :String
165
+ %|#{k} = '#{v}'|
166
+ when :Time
167
+ %|#{k} = #{v.to_datetime}|
168
+ when :Array
169
+ candidates = v.map do |o|
170
+ case o.class.name.to_sym
171
+ when :String
172
+ %|'#{o}'|
173
+ when :Time
174
+ %|#{o.to_datetime}|
175
+ else
176
+ o
177
+ end
178
+ end
179
+ %|#{k} IN (#{candidates.join(', ')})|
180
+ else
181
+ "#{k} = #{v}"
182
+ end
183
+ end
184
+ .join(' AND ')
185
+ end
133
186
  end
134
187
  end
135
188
  end
@@ -22,10 +22,6 @@ module SfCli
22
22
  return qc
23
23
  end
24
24
 
25
- def find(id)
26
- connection.find(name.to_sym, id, Object.const_get(name.to_sym))
27
- end
28
-
29
25
  def limit(num)
30
26
  qc = QueryCondition.new(connection, self.name, self.field_names)
31
27
  qc.limit(num)
@@ -38,11 +34,25 @@ module SfCli
38
34
  qc
39
35
  end
40
36
 
37
+ def find(id)
38
+ connection.find(name.to_sym, id, Object.const_get(name.to_sym))
39
+ end
40
+
41
+ def find_by(*find_condition)
42
+ qc = QueryCondition.new(connection, self.name, self.field_names)
43
+ qc.where(*find_condition).take
44
+ end
45
+
41
46
  def all
42
47
  qc = QueryCondition.new(connection, self.name, self.field_names)
43
48
  qc.all
44
49
  end
45
50
 
51
+ def to_csv
52
+ qc = QueryCondition.new(connection, self.name, self.field_names)
53
+ qc.to_csv
54
+ end
55
+
46
56
  def pluck(field_name)
47
57
  qc = QueryCondition.new(connection, self.name, self.field_names)
48
58
  qc.pluck(field_name)
@@ -52,6 +62,21 @@ module SfCli
52
62
  qc = QueryCondition.new(connection, self.name, self.field_names)
53
63
  qc.take
54
64
  end
65
+
66
+ def count
67
+ qc = QueryCondition.new(connection, self.name, self.field_names)
68
+ qc.count
69
+ end
70
+
71
+ def max(field_name)
72
+ qc = QueryCondition.new(connection, self.name, self.field_names)
73
+ qc.max(field_name)
74
+ end
75
+
76
+ def min(field_name)
77
+ qc = QueryCondition.new(connection, self.name, self.field_names)
78
+ qc.min(field_name)
79
+ end
55
80
  end
56
81
  end
57
82
  end
@@ -37,8 +37,8 @@ module SfCli
37
37
  #
38
38
  # For query details, see {SfCli::Sf::Data::Query sf data query}
39
39
  #
40
- def exec_query(soql, format: nil, bulk: false, timeout: nil, model_class: nil)
41
- sf_data.query(soql, target_org: target_org, format: format, bulk: bulk, timeout: timeout, model_class: model_class)
40
+ def exec_query(soql, format: nil, bulk: false, wait: nil, model_class: nil)
41
+ sf_data.query(soql, target_org: target_org, format: format, bulk: bulk, wait: wait, model_class: model_class)
42
42
  end
43
43
 
44
44
  # @private :nodoc: just for developers
@@ -65,8 +65,8 @@ module SfCli
65
65
  end
66
66
 
67
67
  # @private :nodoc: just for developers
68
- def query(soql, klass)
69
- sf_data.query soql, target_org: target_org, model_class: klass
68
+ def query(soql, klass, format = nil)
69
+ sf_data.query soql, target_org: target_org, format: format, model_class: klass
70
70
  end
71
71
 
72
72
  # @private :nodoc: just for developers
@@ -5,6 +5,7 @@ module SfCli::Sf::Org
5
5
  #
6
6
  # Returns the org's connection information
7
7
  # @param target_org [Symbol,String] an alias of paticular org, or username can be used
8
+ # @param api_version [Numeric] override the api version used for api requests made by this command
8
9
  #
9
10
  # @note this function returns the org information including security sensitive things such as access token, username and so on.
10
11
  # @return [ConnectionInfo] the org's connection information
@@ -24,8 +25,8 @@ module SfCli::Sf::Org
24
25
  #
25
26
  # @see https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_org_commands_unified.htm#cli_reference_org_display_unified command reference
26
27
  #
27
- def display(target_org: nil)
28
- flags = {:"target-org" => target_org}
28
+ def display(target_org: nil, api_version: nil)
29
+ flags = {:"target-org" => target_org, :"api-version" => api_version}
29
30
  json = exec(__method__, flags: flags, redirection: :null_stderr)
30
31
 
31
32
  ConnectionInfo.new(
@@ -5,6 +5,7 @@ module SfCli::Sf::Sobject
5
5
  # Returns a schema object containing the Salesforce object schema
6
6
  # @param object_type [Symbol,String] object type(ex: Account)
7
7
  # @param target_org [Symbol,String] an alias of paticular org, or username can be used
8
+ # @param api_version [Numeric] override the api version used for api requests made by this command
8
9
  #
9
10
  # @return [Schema] a schema object that represents the object schema
10
11
  #
@@ -12,15 +13,16 @@ module SfCli::Sf::Sobject
12
13
  # schema = sf.sobject.describe :Account
13
14
  # schema.name # Account
14
15
  # schema.label # Account
15
- # schema.field_names # [:Id, :Name, ....]
16
- # schema.fields[:Name] # => {"aggregatable"=>true, "aiPredictionField"=>false, "autoNumber"=>false,...}
16
+ # schema.field_names # [:Id, :Name, ....]
17
+ # schema.fields.name_and_labels # [['Id', 'Account Id'], ['Name', 'Account Name'], ...]
17
18
  #
18
19
  # @see https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_sobject_commands_unified.htm#cli_reference_sobject_describe_unified command reference
19
20
  #
20
- def describe(object_type, target_org: nil)
21
+ def describe(object_type, target_org: nil, api_version: nil)
21
22
  flags = {
22
- :"sobject" => object_type,
23
- :"target-org" => target_org,
23
+ :"sobject" => object_type,
24
+ :"target-org" => target_org,
25
+ :"api-version" => api_version,
24
26
  }
25
27
  json = exec(__method__, flags: flags, redirection: :null_stderr)
26
28
  Schema.new(json['result'])
@@ -3,13 +3,15 @@ module SfCli::Sf::Sobject
3
3
  # Returns a list of Salesforce object name
4
4
  # @param object_type [Symbol,String] 'all' or 'custom'
5
5
  # @param target_org [Symbol,String] an alias of paticular org, or username can be used
6
+ # @param api_version [Numeric] override the api version used for api requests made by this command
6
7
  #
7
8
  # @see https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_sobject_commands_unified.htm#cli_reference_sobject_list_unified command reference
8
9
  #
9
- def list(object_type, target_org: nil)
10
+ def list(object_type, target_org: nil, api_version: nil)
10
11
  flags = {
11
- :"sobject" => (object_type.to_sym == :custom ? :custom : :all),
12
- :"target-org" => target_org,
12
+ :"sobject" => (object_type.to_sym == :custom ? :custom : :all),
13
+ :"target-org" => target_org,
14
+ :"api-version" => api_version,
13
15
  }
14
16
  json = exec(__method__, flags: flags, redirection: :null_stderr)
15
17
  json['result']