soql_builder 1.0.6 → 1.0.7

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: e01133aafa8d9360cb78ada93dd1a4d3fcaa9fa671831343202ac0414634f4b6
4
- data.tar.gz: ead65eddcefbb4853110254ce559f78c6c620e31c5c7b7c8002166a6b441427d
3
+ metadata.gz: 4fec9b88d7c0f5058efaa40ba1e8ffcee51deb04d4c35b23b06f7c66eef1d592
4
+ data.tar.gz: 44ee7df2bbed7541d7b73cf36542556b115e362f23a5241848c1fe9b9b124240
5
5
  SHA512:
6
- metadata.gz: 97f41e9547619b75cb7534dd0ce4e4486c5bd36e5034e7f689948b295553a0022ba7a00dd47d533a5ee2b60979438d35e31c79242fd9593981de6db9e46b18e1
7
- data.tar.gz: 4085a471ed4ed7eddce845f4a34f47174ed934d08800306cac99f0ee71ba5a86abdeb2f07c5f72d369ff1ac1c3aeee3cf86a98cd2451ad4a1d3ce94c7fa26c27
6
+ metadata.gz: 7bd972ce802d352af0d2e5559739f309b56fde0820291020901ec07b53264d3c4a06037bc2a008ca2f742976df3487137df0b9a330013fe54305641316de9946
7
+ data.tar.gz: f7bdd7a18dfdabb6918d22ac29067ff15baf727fd6dcbf9c1be1a78c211e73267937653f80eca8c2a3adc5e7778eea39f904df5ea124073e157da581bd299ebd
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SOQL-BUILDER (BETA)
1
+ # SOQL-BUILDER
2
2
 
3
3
  [![Build Status](https://travis-ci.org/AlexAvlonitis/soql-builder.svg?branch=master)](https://travis-ci.org/AlexAvlonitis/soql-builder)
4
4
 
@@ -59,6 +59,26 @@ builder.query
59
59
 
60
60
  ```
61
61
 
62
+ **Select query with multiple subqueries**
63
+
64
+ ```ruby
65
+ builder.fields(['Name', 'Contract__r.Name'])
66
+ .add_subquery(
67
+ table: 'Account.Quotes',
68
+ fields: ['Quotes.Name', 'Quotes.id']
69
+ )
70
+ .add_subquery(
71
+ table: 'Account.Contacts',
72
+ fields: ['Contacts.Name']
73
+ )
74
+ .from('Account')
75
+ .where('id = 1')
76
+
77
+ builder.query
78
+ => "select Name, Contract__r.Name, (select Quotes.Name, Quotes.id from Account.Quotes), (select Contacts.Name from Account.Contacts) from Account where id = 1"
79
+
80
+ ```
81
+
62
82
  **Queries can be added one at a time, instead of chaining**
63
83
 
64
84
  ```ruby
data/lib/soql/query.rb CHANGED
@@ -2,42 +2,22 @@
2
2
 
3
3
  module Soql
4
4
  class Query
5
- attr_accessor :fields, :subquery, :object_table, :where, :limit
6
- attr_reader :type
7
-
8
5
  TYPES = {
9
6
  select: 'select'
10
7
  }.freeze
11
8
 
12
9
  def initialize
13
- @type = ''
14
- @fields = []
15
- @subquery = { object_table: '', fields: [] }
16
- @object_table = ''
17
- @where = ''
18
- @limit = ''
19
- end
20
-
21
- def type=(type)
22
- @type = TYPES[type]
10
+ @query = ''
23
11
  end
24
12
 
25
- def structure_query
26
- qs = type
27
- qs += " #{join_fields(fields)}" unless fields.empty?
28
- qs += ", (select #{join_fields(subquery[:fields])} from #{subquery[:object_table]})" unless subquery[:fields].empty?
29
- qs += " from #{object_table}" unless object_table == ''
30
- qs += " where #{where}" unless where == ''
31
- qs += " limit #{limit}" unless limit == ''
32
- qs
33
- end
34
-
35
- def clean
36
- @fields = []
37
- @subquery = { object_table: '', fields: [] }
38
- @object_table = ''
39
- @where = ''
40
- @limit = ''
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
41
21
  end
42
22
 
43
23
  private
@@ -45,5 +25,11 @@ module Soql
45
25
  def join_fields(fields)
46
26
  fields.join(', ')
47
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
48
34
  end
49
35
  end
data/lib/soql/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Soql
4
- VERSION = '1.0.6'.freeze
4
+ VERSION = '1.0.7'.freeze
5
5
  end
data/lib/soql_builder.rb CHANGED
@@ -5,39 +5,56 @@ require 'soql/query'
5
5
  class SoqlBuilder
6
6
  def initialize(type:, query: nil)
7
7
  @query = query || Soql::Query.new
8
- @query.type = type
8
+ @type = type
9
+ @fields = []
10
+ @subqueries = []
11
+ @subquery = { object_table: '', fields: [] }
12
+ @object_table = ''
13
+ @where = ''
14
+ @limit = ''
9
15
  end
10
16
 
11
17
  def query
12
- @query.structure_query
18
+ @query.structure_query(
19
+ type: @type,
20
+ fields: @fields,
21
+ subqueries: @subqueries,
22
+ object_table: @object_table,
23
+ where: @where,
24
+ limit: @limit
25
+ )
13
26
  end
14
27
 
15
28
  def clean
16
- @query.clean
29
+ @fields = []
30
+ @subqueries = []
31
+ @object_table = ''
32
+ @where = ''
33
+ @limit = ''
17
34
  end
18
35
 
19
36
  def fields(fields = [])
20
- @query.fields = fields
37
+ @fields = fields
21
38
  self
22
39
  end
23
40
 
24
41
  def add_subquery(table:, fields: [])
25
- @query.subquery[:object_table] = table
26
- @query.subquery[:fields] = fields
42
+ @subquery = { object_table: table, fields: fields }
43
+ @subqueries << @subquery
27
44
  self
28
45
  end
29
46
 
30
47
  def from(table)
31
- @query.object_table = table
48
+ @object_table = table
32
49
  self
33
50
  end
34
51
 
35
52
  def where(where_condition)
36
- @query.where = where_condition
53
+ @where = where_condition
37
54
  self
38
55
  end
39
56
 
40
57
  def limit(limit_number)
41
- @query.limit = limit_number.to_s
58
+ @limit = limit_number.to_s
42
59
  end
43
60
  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.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis