json2sql 1.0.4 → 1.0.6
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/lib/json2sql/select_model.rb +8 -4
- data/lib/json2sql/version.rb +1 -1
- data/lib/json2sql/where_model.rb +19 -20
- data/lib/json2sql/where_relation.rb +10 -8
- data/lib/json2sql.rb +4 -4
- 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: d0e99fbd9cb204b8c8ab64c96c810433474f649cdd80af3b5c6e8f38c8f4f57f
|
|
4
|
+
data.tar.gz: 3a9ab7dfbee2f9b7509b320edd460b2d8ca177552bfccfac7b10164366628f1a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: de83391fec39ab3b474f4af3a360e0c842e2945c12fcee347507963b397c97b3b9157387758f1a07e9f5bb33e6e7ea675e5fb9f40c9365383c19776fc1c280fd
|
|
7
|
+
data.tar.gz: e6a6a70bbde21691e4ae1c1991ef4dabeaa4c0596f20a97a71886cf232aeb0b1484aaa2fe3016257d520f8d0aa6e4366a3e90deec85d9671f79d9c9ddcb4c6c9
|
|
@@ -262,8 +262,6 @@ module Json2sql
|
|
|
262
262
|
|
|
263
263
|
return unless children.is_a?(Hash)
|
|
264
264
|
|
|
265
|
-
relation = WhereRelation.parent(@table)
|
|
266
|
-
|
|
267
265
|
children.each do |key, value|
|
|
268
266
|
|
|
269
267
|
next unless value.is_a?(Hash)
|
|
@@ -274,6 +272,10 @@ module Json2sql
|
|
|
274
272
|
|
|
275
273
|
tbl = key.to_s
|
|
276
274
|
|
|
275
|
+
custom_key = value["key"].is_a?(String) && !value["key"].empty? ? value["key"] : nil
|
|
276
|
+
|
|
277
|
+
relation = WhereRelation.parent(@table, custom_key: custom_key)
|
|
278
|
+
|
|
277
279
|
@sql << Sanitizer.keyword_wrap(tbl, "'")
|
|
278
280
|
|
|
279
281
|
@sql << ", ("
|
|
@@ -293,8 +295,6 @@ module Json2sql
|
|
|
293
295
|
|
|
294
296
|
return unless parents.is_a?(Hash)
|
|
295
297
|
|
|
296
|
-
relation = WhereRelation.child(@table)
|
|
297
|
-
|
|
298
298
|
parents.each do |key, value|
|
|
299
299
|
|
|
300
300
|
next unless value.is_a?(Hash)
|
|
@@ -305,6 +305,10 @@ module Json2sql
|
|
|
305
305
|
|
|
306
306
|
tbl = key.to_s
|
|
307
307
|
|
|
308
|
+
custom_key = value["key"].is_a?(String) && !value["key"].empty? ? value["key"] : nil
|
|
309
|
+
|
|
310
|
+
relation = WhereRelation.child(@table, custom_key: custom_key)
|
|
311
|
+
|
|
308
312
|
@sql << Sanitizer.keyword_wrap(tbl, "'")
|
|
309
313
|
|
|
310
314
|
@sql << ", ("
|
data/lib/json2sql/version.rb
CHANGED
data/lib/json2sql/where_model.rb
CHANGED
|
@@ -41,9 +41,9 @@ module Json2sql
|
|
|
41
41
|
|
|
42
42
|
return unless has_relation || has_where_and || has_where_or
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
rules = []
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
rules << with_buffer { @relation.build_table_relation(@sql, @table) } if has_relation
|
|
47
47
|
|
|
48
48
|
scope = has_where_and ? " AND " : " OR "
|
|
49
49
|
|
|
@@ -51,14 +51,14 @@ module Json2sql
|
|
|
51
51
|
|
|
52
52
|
if group
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
rule = build_column_group(group, scope)
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
rules << rule unless rule.empty?
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
-
return if
|
|
59
|
+
return if rules.empty?
|
|
60
60
|
|
|
61
|
-
@sql << " WHERE " <<
|
|
61
|
+
@sql << " WHERE " << rules.join(" AND ")
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
private
|
|
@@ -69,16 +69,16 @@ module Json2sql
|
|
|
69
69
|
|
|
70
70
|
def build_column_group(params, scope)
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
rules = params.filter_map do |key, value|
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
rule = with_buffer { build_column_types(value, scope, key.to_s) }
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
rule.empty? ? nil : rule
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
-
return "" if
|
|
79
|
+
return "" if rules.empty?
|
|
80
80
|
|
|
81
|
-
"(
|
|
81
|
+
"(#{rules.join(scope)})"
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
# Dispatch by Ruby type of the value.
|
|
@@ -103,18 +103,18 @@ module Json2sql
|
|
|
103
103
|
|
|
104
104
|
if column == "and"
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
rule = build_column_group(params, " AND ")
|
|
107
107
|
|
|
108
|
-
@sql <<
|
|
108
|
+
@sql << rule unless rule.empty?
|
|
109
109
|
|
|
110
110
|
return
|
|
111
111
|
end
|
|
112
112
|
|
|
113
113
|
if column == "or"
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
rule = build_column_group(params, " OR ")
|
|
116
116
|
|
|
117
|
-
@sql <<
|
|
117
|
+
@sql << rule unless rule.empty?
|
|
118
118
|
|
|
119
119
|
return
|
|
120
120
|
end
|
|
@@ -129,14 +129,14 @@ module Json2sql
|
|
|
129
129
|
|
|
130
130
|
def build_action_group(params, scope, column)
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
rules = params.filter_map do |key, value|
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
rule = with_buffer { build_action_types(value, column, key.to_s) }
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
rule.empty? ? nil : rule
|
|
137
137
|
end
|
|
138
138
|
|
|
139
|
-
@sql <<
|
|
139
|
+
@sql << rules.join(scope) unless rules.empty?
|
|
140
140
|
end
|
|
141
141
|
|
|
142
142
|
def build_action_types(params, column, action)
|
|
@@ -370,7 +370,6 @@ module Json2sql
|
|
|
370
370
|
def get_action(action)
|
|
371
371
|
|
|
372
372
|
case action
|
|
373
|
-
when "=", "<", ">", "<=", ">=", "!=", "<>" then action
|
|
374
373
|
when "in" then "IN"
|
|
375
374
|
when "!in" then "NOT IN"
|
|
376
375
|
when "like" then "LIKE"
|
|
@@ -8,11 +8,13 @@ module Json2sql
|
|
|
8
8
|
|
|
9
9
|
attr_reader :table, :kind
|
|
10
10
|
|
|
11
|
-
def initialize(table, kind)
|
|
11
|
+
def initialize(table, kind, custom_key: nil)
|
|
12
12
|
|
|
13
13
|
@table = table.to_s
|
|
14
14
|
|
|
15
|
-
@kind
|
|
15
|
+
@kind = kind
|
|
16
|
+
|
|
17
|
+
@custom_key = custom_key.is_a?(String) && !custom_key.empty? ? custom_key : nil
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
# Factory: no relationship (top-level query).
|
|
@@ -25,17 +27,17 @@ module Json2sql
|
|
|
25
27
|
# Factory: foreign key is on the child table pointing to the parent.
|
|
26
28
|
# Produces: `parent`.`child_id` = `current`.`id`
|
|
27
29
|
|
|
28
|
-
def self.child(table)
|
|
30
|
+
def self.child(table, custom_key: nil)
|
|
29
31
|
|
|
30
|
-
new(table, CHILD)
|
|
32
|
+
new(table, CHILD, custom_key: custom_key)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
# Factory: foreign key is on the current/parent table pointing to the child.
|
|
34
36
|
# Produces: `current`.`parent_id` = `parent`.`id`
|
|
35
37
|
|
|
36
|
-
def self.parent(table)
|
|
38
|
+
def self.parent(table, custom_key: nil)
|
|
37
39
|
|
|
38
|
-
new(table, PARENT)
|
|
40
|
+
new(table, PARENT, custom_key: custom_key)
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
# Appends the JOIN condition for this relationship into sql.
|
|
@@ -51,7 +53,7 @@ module Json2sql
|
|
|
51
53
|
|
|
52
54
|
sql << "."
|
|
53
55
|
|
|
54
|
-
sql << build_table_id(current)
|
|
56
|
+
sql << (@custom_key ? Sanitizer.keyword_wrap(@custom_key) : build_table_id(current))
|
|
55
57
|
|
|
56
58
|
sql << " = "
|
|
57
59
|
|
|
@@ -68,7 +70,7 @@ module Json2sql
|
|
|
68
70
|
|
|
69
71
|
sql << "."
|
|
70
72
|
|
|
71
|
-
sql << build_table_id(table)
|
|
73
|
+
sql << (@custom_key ? Sanitizer.keyword_wrap(@custom_key) : build_table_id(table))
|
|
72
74
|
|
|
73
75
|
sql << " = "
|
|
74
76
|
|
data/lib/json2sql.rb
CHANGED
|
@@ -28,13 +28,13 @@ module Json2sql
|
|
|
28
28
|
# Deep-converts all Hash keys to Strings and recurses into nested Hashes
|
|
29
29
|
# and Arrays. Leaves all other values (Integers, Strings, etc.) unchanged.
|
|
30
30
|
|
|
31
|
-
def self.normalize(
|
|
31
|
+
def self.normalize(object)
|
|
32
32
|
|
|
33
|
-
return
|
|
33
|
+
return object.each_with_object({}) { |(key, value), hash| hash[key.to_s] = normalize(value) } if object.is_a?(Hash)
|
|
34
34
|
|
|
35
|
-
return
|
|
35
|
+
return object.map { |value| normalize(value) } if object.is_a?(Array)
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
object
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
end
|