soql_builder 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8d14dbf2824a0f8c671f65f401f7d906799b1002ebf6f2f0f9f052af1a5a6fd
4
- data.tar.gz: b2e297cc77be7e55221fbc232f4ee7f4c9747c152e7270038d216cf6dfc75068
3
+ metadata.gz: a874d53ad1ef9faef114a669fabf4a4d56a024f3a56975941abbd548144f045f
4
+ data.tar.gz: 7e8d704b14e8915e97b89b296e92c43f878c09a309cf96f7449cf417ebfbc61e
5
5
  SHA512:
6
- metadata.gz: 160a72383e1346bb941e4ccccef507f609adb6cd989ee36240c6ba493a90c75c0d4dfe383f97c25fd0a36ed55b76afaf0793ff2e3aee330423f17469dba72dbe
7
- data.tar.gz: 356133212d1ada277a09af0415be24e8c280da12f3d6e214067a93502afcfcfd34b56a0cb45e97a822fc1d431e6d9a414a73e893a71b2b12c10c567b18bedf2e
6
+ metadata.gz: 2eac4f39dfab102d9e62a48d4ec4876a7bd94c4acb5a7d04179b5e1ff752c5144f9e04f39f8577996f29ba5c718e396620f4bf30852d6fc62d504e2ed8fc1ea3
7
+ data.tar.gz: 4d35f2d585305841f6649b97e5e2431806e6016bc4b13deba776e081fd3278d96d3df93cf0aa0d490c62576eb553692dfa73141b33047e6dc3ee6582f419993a
data/lib/soql_builder.rb CHANGED
@@ -4,8 +4,8 @@ require 'soql_builder/interface'
4
4
 
5
5
  module SoqlBuilder
6
6
  class << self
7
- def new(type:, query: nil)
8
- @interface = Interface.new(type: type, query: query)
7
+ def new(type:)
8
+ @interface = Interface.new(type: type)
9
9
  end
10
10
 
11
11
  attr_reader :interface
@@ -1,62 +1,55 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'soql_builder/query'
3
+ require 'soql_builder/select_query'
4
4
 
5
5
  module SoqlBuilder
6
6
  class Interface
7
- def initialize(type:, query: nil)
8
- @query = query || SoqlBuilder::Query.new
9
- @type = type
10
- @fields = []
11
- @subqueries = []
12
- @subquery = { object_table: '', fields: [] }
13
- @object_table = ''
14
- @where = ''
15
- @limit = ''
7
+ def initialize(type:)
8
+ @query_klass = Module.const_get("SoqlBuilder::#{type.capitalize}Query")
9
+ @query_params = {
10
+ fields: [],
11
+ subqueries: [],
12
+ object_table: '',
13
+ where: '',
14
+ limit: ''
15
+ }
16
16
  end
17
17
 
18
18
  def query
19
- @query.structure_query(
20
- type: @type,
21
- fields: @fields,
22
- subqueries: @subqueries,
23
- object_table: @object_table,
24
- where: @where,
25
- limit: @limit
26
- )
19
+ @query_klass.structure_query(@query_params)
27
20
  end
28
21
 
29
22
  def clean
30
- @fields = []
31
- @subqueries = []
32
- @object_table = ''
33
- @where = ''
34
- @limit = ''
23
+ @query_params[:fields] = []
24
+ @query_params[:subqueries] = []
25
+ @query_params[:object_table] = ''
26
+ @query_params[:where] = ''
27
+ @query_params[:limit] = ''
35
28
  end
36
29
 
37
30
  def fields(fields = [])
38
- @fields = fields
31
+ @query_params[:fields] = fields
39
32
  self
40
33
  end
41
34
 
42
35
  def add_subquery(table:, fields: [])
43
- @subquery = { object_table: table, fields: fields }
44
- @subqueries << @subquery
36
+ subquery = { object_table: table, fields: fields }
37
+ @query_params[:subqueries] << subquery
45
38
  self
46
39
  end
47
40
 
48
41
  def from(table)
49
- @object_table = table
42
+ @query_params[:object_table] = table
50
43
  self
51
44
  end
52
45
 
53
46
  def where(where_condition)
54
- @where = where_condition
47
+ @query_params[:where] = where_condition
55
48
  self
56
49
  end
57
50
 
58
51
  def limit(limit_number)
59
- @limit = limit_number.to_s
52
+ @query_params[:limit] = limit_number.to_s
60
53
  end
61
54
  end
62
55
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SoqlBuilder
4
+ class SelectQuery
5
+ class << self
6
+ def structure_query(args = {})
7
+ query = 'select'.dup
8
+ query += " #{join_fields(args[:fields])}" unless args[:fields].empty?
9
+ query += join_subqueries(args[:subqueries]) unless args[:subqueries].empty?
10
+ query += " from #{args[:object_table]}" unless args[:object_table] == ''
11
+ query += " where #{args[:where]}" unless args[:where] == ''
12
+ query += " limit #{args[:limit]}" unless args[:limit] == ''
13
+ query
14
+ end
15
+
16
+ private
17
+
18
+ def join_fields(fields)
19
+ fields.join(', ')
20
+ end
21
+
22
+ def join_subqueries(subqueries)
23
+ subqueries.map do |subquery|
24
+ ", (select #{join_fields(subquery[:fields])} from #{subquery[:object_table]})"
25
+ end.join
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SoqlBuilder
4
- VERSION = '1.0.8'.freeze
4
+ VERSION = '1.0.9'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soql_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis
@@ -33,7 +33,7 @@ files:
33
33
  - README.md
34
34
  - lib/soql_builder.rb
35
35
  - lib/soql_builder/interface.rb
36
- - lib/soql_builder/query.rb
36
+ - lib/soql_builder/select_query.rb
37
37
  - lib/soql_builder/version.rb
38
38
  homepage: https://github.com/AlexAvlonitis/soql-builder
39
39
  licenses:
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SoqlBuilder
4
- class Query
5
- TYPES = {
6
- select: 'select'
7
- }.freeze
8
-
9
- def initialize
10
- @query = ''
11
- end
12
-
13
- def structure_query(args = {})
14
- @query = TYPES[args[:type]]
15
- @query += " #{join_fields(args[:fields])}" unless args[:fields].empty?
16
- @query += join_subqueries(args[:subqueries]) unless args[:subqueries].empty?
17
- @query += " from #{args[:object_table]}" unless args[:object_table] == ''
18
- @query += " where #{args[:where]}" unless args[:where] == ''
19
- @query += " limit #{args[:limit]}" unless args[:limit] == ''
20
- @query
21
- end
22
-
23
- private
24
-
25
- def join_fields(fields)
26
- fields.join(', ')
27
- end
28
-
29
- def join_subqueries(subqueries)
30
- subqueries.map do |subquery|
31
- ", (select #{join_fields(subquery[:fields])} from #{subquery[:object_table]})"
32
- end.join
33
- end
34
- end
35
- end