soql_builder 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -1
- data/lib/soql/query.rb +15 -29
- data/lib/soql/version.rb +1 -1
- data/lib/soql_builder.rb +26 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fec9b88d7c0f5058efaa40ba1e8ffcee51deb04d4c35b23b06f7c66eef1d592
|
4
|
+
data.tar.gz: 44ee7df2bbed7541d7b73cf36542556b115e362f23a5241848c1fe9b9b124240
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bd972ce802d352af0d2e5559739f309b56fde0820291020901ec07b53264d3c4a06037bc2a008ca2f742976df3487137df0b9a330013fe54305641316de9946
|
7
|
+
data.tar.gz: f7bdd7a18dfdabb6918d22ac29067ff15baf727fd6dcbf9c1be1a78c211e73267937653f80eca8c2a3adc5e7778eea39f904df5ea124073e157da581bd299ebd
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# SOQL-BUILDER
|
1
|
+
# SOQL-BUILDER
|
2
2
|
|
3
3
|
[](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
|
-
@
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
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
|
-
@
|
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
|
-
@
|
29
|
+
@fields = []
|
30
|
+
@subqueries = []
|
31
|
+
@object_table = ''
|
32
|
+
@where = ''
|
33
|
+
@limit = ''
|
17
34
|
end
|
18
35
|
|
19
36
|
def fields(fields = [])
|
20
|
-
@
|
37
|
+
@fields = fields
|
21
38
|
self
|
22
39
|
end
|
23
40
|
|
24
41
|
def add_subquery(table:, fields: [])
|
25
|
-
@
|
26
|
-
@
|
42
|
+
@subquery = { object_table: table, fields: fields }
|
43
|
+
@subqueries << @subquery
|
27
44
|
self
|
28
45
|
end
|
29
46
|
|
30
47
|
def from(table)
|
31
|
-
@
|
48
|
+
@object_table = table
|
32
49
|
self
|
33
50
|
end
|
34
51
|
|
35
52
|
def where(where_condition)
|
36
|
-
@
|
53
|
+
@where = where_condition
|
37
54
|
self
|
38
55
|
end
|
39
56
|
|
40
57
|
def limit(limit_number)
|
41
|
-
@
|
58
|
+
@limit = limit_number.to_s
|
42
59
|
end
|
43
60
|
end
|