rasti-db 1.4.0 → 2.2.0

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -3
  3. data/README.md +88 -24
  4. data/lib/rasti/db.rb +2 -1
  5. data/lib/rasti/db/collection.rb +79 -46
  6. data/lib/rasti/db/computed_attribute.rb +22 -0
  7. data/lib/rasti/db/data_source.rb +18 -0
  8. data/lib/rasti/db/environment.rb +32 -0
  9. data/lib/rasti/db/nql/nodes/attribute.rb +37 -0
  10. data/lib/rasti/db/nql/nodes/binary_node.rb +4 -0
  11. data/lib/rasti/db/nql/nodes/comparisons/base.rb +5 -1
  12. data/lib/rasti/db/nql/nodes/comparisons/equal.rb +2 -2
  13. data/lib/rasti/db/nql/nodes/comparisons/greater_than.rb +2 -2
  14. data/lib/rasti/db/nql/nodes/comparisons/greater_than_or_equal.rb +2 -2
  15. data/lib/rasti/db/nql/nodes/comparisons/include.rb +2 -2
  16. data/lib/rasti/db/nql/nodes/comparisons/less_than.rb +2 -2
  17. data/lib/rasti/db/nql/nodes/comparisons/less_than_or_equal.rb +2 -2
  18. data/lib/rasti/db/nql/nodes/comparisons/like.rb +2 -2
  19. data/lib/rasti/db/nql/nodes/comparisons/not_equal.rb +2 -2
  20. data/lib/rasti/db/nql/nodes/comparisons/not_include.rb +2 -2
  21. data/lib/rasti/db/nql/nodes/conjunction.rb +2 -2
  22. data/lib/rasti/db/nql/nodes/disjunction.rb +2 -2
  23. data/lib/rasti/db/nql/nodes/parenthesis_sentence.rb +6 -2
  24. data/lib/rasti/db/nql/nodes/sentence.rb +6 -2
  25. data/lib/rasti/db/nql/syntax.rb +33 -33
  26. data/lib/rasti/db/nql/syntax.treetop +12 -12
  27. data/lib/rasti/db/query.rb +107 -43
  28. data/lib/rasti/db/relations/base.rb +22 -8
  29. data/lib/rasti/db/relations/graph.rb +129 -0
  30. data/lib/rasti/db/relations/many_to_many.rb +58 -24
  31. data/lib/rasti/db/relations/many_to_one.rb +17 -12
  32. data/lib/rasti/db/relations/one_to_many.rb +27 -16
  33. data/lib/rasti/db/version.rb +1 -1
  34. data/rasti-db.gemspec +3 -7
  35. data/spec/collection_spec.rb +223 -52
  36. data/spec/computed_attribute_spec.rb +32 -0
  37. data/spec/minitest_helper.rb +76 -15
  38. data/spec/model_spec.rb +4 -2
  39. data/spec/nql/computed_attributes_spec.rb +29 -0
  40. data/spec/nql/filter_condition_spec.rb +4 -2
  41. data/spec/nql/syntax_parser_spec.rb +12 -5
  42. data/spec/query_spec.rb +319 -85
  43. data/spec/relations_spec.rb +27 -7
  44. metadata +41 -7
  45. data/lib/rasti/db/helpers.rb +0 -16
  46. data/lib/rasti/db/nql/nodes/field.rb +0 -23
  47. data/lib/rasti/db/relations/graph_builder.rb +0 -60
@@ -30,14 +30,19 @@ describe 'Relations' do
30
30
  relation.many_to_one?.must_equal false
31
31
  relation.many_to_many?.must_equal false
32
32
  relation.one_to_one?.must_equal false
33
+
34
+ relation.from_one?.must_equal true
35
+ relation.from_many?.must_equal false
36
+ relation.to_one?.must_equal false
37
+ relation.to_many?.must_equal true
33
38
  end
34
39
 
35
40
  it 'Graph' do
36
41
  user_id = db[:users].insert name: 'User 1'
37
- 1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...' }
42
+ 1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...', language_id: 1 }
38
43
  rows = db[:users].all
39
44
 
40
- Users.relations[:posts].graph_to rows, db
45
+ Users.relations[:posts].fetch_graph environment, rows
41
46
 
42
47
  rows[0][:posts].must_equal posts.where(user_id: user_id).all
43
48
  end
@@ -72,14 +77,19 @@ describe 'Relations' do
72
77
  relation.many_to_one?.must_equal true
73
78
  relation.many_to_many?.must_equal false
74
79
  relation.one_to_one?.must_equal false
80
+
81
+ relation.from_one?.must_equal false
82
+ relation.from_many?.must_equal true
83
+ relation.to_one?.must_equal true
84
+ relation.to_many?.must_equal false
75
85
  end
76
86
 
77
87
  it 'Graph' do
78
88
  user_id = db[:users].insert name: 'User 1'
79
- db[:posts].insert user_id: user_id, title: 'Post 1', body: '...'
89
+ db[:posts].insert user_id: user_id, title: 'Post 1', body: '...', language_id: 1
80
90
  rows = db[:posts].all
81
91
 
82
- Posts.relations[:user].graph_to rows, db
92
+ Posts.relations[:user].fetch_graph environment, rows
83
93
 
84
94
  rows[0][:user].must_equal users.first
85
95
  end
@@ -120,12 +130,17 @@ describe 'Relations' do
120
130
  relation.many_to_one?.must_equal false
121
131
  relation.many_to_many?.must_equal true
122
132
  relation.one_to_one?.must_equal false
133
+
134
+ relation.from_one?.must_equal false
135
+ relation.from_many?.must_equal true
136
+ relation.to_one?.must_equal false
137
+ relation.to_many?.must_equal true
123
138
  end
124
139
 
125
140
  it 'Graph' do
126
141
  user_id = db[:users].insert name: 'User 1'
127
142
 
128
- 1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...' }
143
+ 1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...', language_id: 1 }
129
144
 
130
145
  1.upto(4) { |i| db[:categories].insert name: "Category #{i}" }
131
146
 
@@ -136,7 +151,7 @@ describe 'Relations' do
136
151
 
137
152
  rows = db[:posts].all
138
153
 
139
- Posts.relations[:categories].graph_to rows, db
154
+ Posts.relations[:categories].fetch_graph environment, rows
140
155
 
141
156
  rows[0][:categories].must_equal categories.where(id: [1,2]).all
142
157
  rows[1][:categories].must_equal categories.where(id: [3,4]).all
@@ -172,6 +187,11 @@ describe 'Relations' do
172
187
  relation.many_to_one?.must_equal false
173
188
  relation.many_to_many?.must_equal false
174
189
  relation.one_to_one?.must_equal true
190
+
191
+ relation.from_one?.must_equal true
192
+ relation.from_many?.must_equal false
193
+ relation.to_one?.must_equal true
194
+ relation.to_many?.must_equal false
175
195
  end
176
196
 
177
197
  it 'Graph' do
@@ -186,7 +206,7 @@ describe 'Relations' do
186
206
 
187
207
  rows = db[:users].all
188
208
 
189
- Users.relations[:person].graph_to rows, db
209
+ Users.relations[:person].fetch_graph environment, rows
190
210
 
191
211
  2.times do |i|
192
212
  rows[i][:person].must_equal people.find("document_#{i}")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-08 00:00:00.000000000 Z
11
+ date: 2020-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -112,20 +112,48 @@ dependencies:
112
112
  - - "~>"
113
113
  - !ruby/object:Gem::Version
114
114
  version: '1.0'
115
+ - !ruby/object:Gem::Dependency
116
+ name: hierarchical_graph
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '1.0'
122
+ type: :runtime
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '1.0'
129
+ - !ruby/object:Gem::Dependency
130
+ name: hash_ext
131
+ requirement: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '0.5'
136
+ type: :runtime
137
+ prerelease: false
138
+ version_requirements: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - "~>"
141
+ - !ruby/object:Gem::Version
142
+ version: '0.5'
115
143
  - !ruby/object:Gem::Dependency
116
144
  name: rake
117
145
  requirement: !ruby/object:Gem::Requirement
118
146
  requirements:
119
147
  - - "~>"
120
148
  - !ruby/object:Gem::Version
121
- version: '11.0'
149
+ version: '12.3'
122
150
  type: :development
123
151
  prerelease: false
124
152
  version_requirements: !ruby/object:Gem::Requirement
125
153
  requirements:
126
154
  - - "~>"
127
155
  - !ruby/object:Gem::Version
128
- version: '11.0'
156
+ version: '12.3'
129
157
  - !ruby/object:Gem::Dependency
130
158
  name: minitest
131
159
  requirement: !ruby/object:Gem::Requirement
@@ -249,9 +277,12 @@ files:
249
277
  - lib/rasti-db.rb
250
278
  - lib/rasti/db.rb
251
279
  - lib/rasti/db/collection.rb
252
- - lib/rasti/db/helpers.rb
280
+ - lib/rasti/db/computed_attribute.rb
281
+ - lib/rasti/db/data_source.rb
282
+ - lib/rasti/db/environment.rb
253
283
  - lib/rasti/db/model.rb
254
284
  - lib/rasti/db/nql/invalid_expression_error.rb
285
+ - lib/rasti/db/nql/nodes/attribute.rb
255
286
  - lib/rasti/db/nql/nodes/binary_node.rb
256
287
  - lib/rasti/db/nql/nodes/comparisons/base.rb
257
288
  - lib/rasti/db/nql/nodes/comparisons/equal.rb
@@ -272,14 +303,13 @@ files:
272
303
  - lib/rasti/db/nql/nodes/constants/time.rb
273
304
  - lib/rasti/db/nql/nodes/constants/true.rb
274
305
  - lib/rasti/db/nql/nodes/disjunction.rb
275
- - lib/rasti/db/nql/nodes/field.rb
276
306
  - lib/rasti/db/nql/nodes/parenthesis_sentence.rb
277
307
  - lib/rasti/db/nql/nodes/sentence.rb
278
308
  - lib/rasti/db/nql/syntax.rb
279
309
  - lib/rasti/db/nql/syntax.treetop
280
310
  - lib/rasti/db/query.rb
281
311
  - lib/rasti/db/relations/base.rb
282
- - lib/rasti/db/relations/graph_builder.rb
312
+ - lib/rasti/db/relations/graph.rb
283
313
  - lib/rasti/db/relations/many_to_many.rb
284
314
  - lib/rasti/db/relations/many_to_one.rb
285
315
  - lib/rasti/db/relations/one_to_many.rb
@@ -293,9 +323,11 @@ files:
293
323
  - lib/rasti/db/version.rb
294
324
  - rasti-db.gemspec
295
325
  - spec/collection_spec.rb
326
+ - spec/computed_attribute_spec.rb
296
327
  - spec/coverage_helper.rb
297
328
  - spec/minitest_helper.rb
298
329
  - spec/model_spec.rb
330
+ - spec/nql/computed_attributes_spec.rb
299
331
  - spec/nql/dependency_tables_spec.rb
300
332
  - spec/nql/filter_condition_spec.rb
301
333
  - spec/nql/syntax_parser_spec.rb
@@ -328,9 +360,11 @@ specification_version: 4
328
360
  summary: Database collections and relations
329
361
  test_files:
330
362
  - spec/collection_spec.rb
363
+ - spec/computed_attribute_spec.rb
331
364
  - spec/coverage_helper.rb
332
365
  - spec/minitest_helper.rb
333
366
  - spec/model_spec.rb
367
+ - spec/nql/computed_attributes_spec.rb
334
368
  - spec/nql/dependency_tables_spec.rb
335
369
  - spec/nql/filter_condition_spec.rb
336
370
  - spec/nql/syntax_parser_spec.rb
@@ -1,16 +0,0 @@
1
- module Rasti
2
- module DB
3
- module Helpers
4
- module WithSchema
5
-
6
- private
7
-
8
- def with_schema(table, field=nil)
9
- qualified_table = schema ? Sequel[schema][table] : Sequel[table]
10
- field ? Sequel[qualified_table][field] : qualified_table
11
- end
12
-
13
- end
14
- end
15
- end
16
- end
@@ -1,23 +0,0 @@
1
- module Rasti
2
- module DB
3
- module NQL
4
- module Nodes
5
- class Field < Treetop::Runtime::SyntaxNode
6
-
7
- def identifier
8
- tables.empty? ? Sequel[column.to_sym] : Sequel[tables.join('__').to_sym][column.to_sym]
9
- end
10
-
11
- def tables
12
- _tables.elements.map{ |e| e.table.text_value }
13
- end
14
-
15
- def column
16
- _column.text_value
17
- end
18
-
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,60 +0,0 @@
1
- module Rasti
2
- module DB
3
- module Relations
4
- class GraphBuilder
5
- class << self
6
-
7
- def graph_to(rows, relations, collection_class, db, schema=nil)
8
- return if rows.empty?
9
-
10
- parse(relations).each do |relation_name, nested_relations|
11
- relation = get_relation collection_class, relation_name
12
- relation.graph_to rows, db, schema, nested_relations
13
- end
14
- end
15
-
16
- def joins_to(dataset, relations, collection_class, schema=nil)
17
- ds = recursive_joins dataset, recursive_parse(relations), collection_class, schema
18
- qualified_collection_name = schema ? Sequel[schema][collection_class.collection_name] : Sequel[collection_class.collection_name]
19
- ds.distinct.select_all(qualified_collection_name)
20
- end
21
-
22
- private
23
-
24
- def get_relation(collection_class, relation_name)
25
- raise "Undefined relation #{relation_name} for #{collection_class}" unless collection_class.relations.key? relation_name
26
- collection_class.relations[relation_name]
27
- end
28
-
29
- def parse(relations)
30
- relations.each_with_object({}) do |relation, hash|
31
- tail = relation.to_s.split '.'
32
- head = tail.shift.to_sym
33
- hash[head] ||= []
34
- hash[head] << tail.join('.') unless tail.empty?
35
- end
36
- end
37
-
38
- def recursive_parse(relations)
39
- parse(relations).each_with_object({}) do |(key, value), hash|
40
- hash[key] = recursive_parse value
41
- end
42
- end
43
-
44
- def recursive_joins(dataset, joins, collection_class, schema, prefix=nil)
45
- joins.each do |relation_name, nested_joins|
46
- relation = get_relation collection_class, relation_name
47
-
48
- dataset = relation.join_to dataset, schema, prefix
49
-
50
- dataset = recursive_joins dataset, nested_joins, relation.target_collection_class, schema, relation.join_relation_name(prefix) unless nested_joins.empty?
51
- end
52
-
53
- dataset
54
- end
55
-
56
- end
57
- end
58
- end
59
- end
60
- end