rasti-db 4.0.0 → 4.1.0

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: 63f41e1bc62674437962478fc753698954559bf6bfcc491ab6bcdb76ea9082aa
4
- data.tar.gz: e57dad5c6377b82c14a3a0775bc9f2b4abb75570edece928d3679e4c172e571d
3
+ metadata.gz: c26c0f818529ec0d1a752cf310c1d23cf235d2c8c83e13a62d8d61bc90b3b400
4
+ data.tar.gz: 57022ffbb145a76ad9532d16a1ed890f31862f7fc6ad4cea6d40aae0490908ca
5
5
  SHA512:
6
- metadata.gz: acce93a9224b4531de96969886f2d8c384a047f1b58169f344a8eca518eb8328d6d46ff9359d0ebba15dc0ab017785ff5120616678c9651581fc5ea0f1ff646b
7
- data.tar.gz: 3d85bf6a76c25a55e775c36bdb0dff504e28e945bdb94afd341416e8337f3bd442de20f03f50a87e3290784a4743786d4a4152f6c4cd698b8302d9b9f5e28c4b
6
+ metadata.gz: 94746821604273fdf6483d5410f8c73f4793901116478a17d3ecba6e7039705ecf8cf5b8ad0ccb30adb294d052ff144c6d0fe5ca042ca1fd558e3caf5c45de62
7
+ data.tar.gz: '0684ae374582f8cd633d44aacd720e80b3575e3ef8186de2252d9f73e17f03fe438a54a407127bb320f9ec8996f3f5dc71c99294b503c28797e34faf4c886f22'
@@ -9,8 +9,8 @@ module Rasti
9
9
  @join = join
10
10
  end
11
11
 
12
- def apply_join(dataset)
13
- join ? join.call(dataset) : dataset
12
+ def apply_join(dataset, environment)
13
+ join ? join.call(dataset, environment) : dataset
14
14
  end
15
15
 
16
16
  private
@@ -60,7 +60,7 @@ module Rasti
60
60
  def select_computed_attributes(*computed_attributes)
61
61
  ds = computed_attributes.inject(dataset) do |ds, name|
62
62
  computed_attribute = collection_class.computed_attributes[name]
63
- computed_attribute.apply_join(ds).select_append(computed_attribute.identifier.as(name))
63
+ computed_attribute.apply_join(ds, environment).select_append(computed_attribute.identifier.as(name))
64
64
  end
65
65
  build_query dataset: ds
66
66
  end
@@ -138,7 +138,7 @@ module Rasti
138
138
  raise NQL::InvalidExpressionError.new(filter_expression) if sentence.nil?
139
139
 
140
140
  ds = sentence.computed_attributes(collection_class).inject(dataset) do |ds, name|
141
- collection_class.computed_attributes[name].apply_join ds
141
+ collection_class.computed_attributes[name].apply_join ds, environment
142
142
  end
143
143
  query = build_query dataset: ds
144
144
 
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module DB
3
- VERSION = '4.0.0'
3
+ VERSION = '4.1.0'
4
4
  end
5
5
  end
@@ -2,7 +2,7 @@ require 'minitest_helper'
2
2
 
3
3
  describe 'ComputedAttribute' do
4
4
 
5
- it 'Apply Join wiht join attribute must generate correct query' do
5
+ it 'Apply Join with join attribute must generate correct query' do
6
6
  dataset = db[:users]
7
7
  computed_attribute = Rasti::DB::ComputedAttribute.new(Sequel[:comments_count][:value]) do |dataset|
8
8
  subquery = dataset.db.from(:comments)
@@ -13,7 +13,7 @@ describe 'ComputedAttribute' do
13
13
  dataset.join_table(:inner, subquery, :user_id => :id)
14
14
  end
15
15
  expected_query = "SELECT *, `comments_count`.`value` AS 'value' FROM `users` INNER JOIN (SELECT `user_id`, count(`id`) AS 'value' FROM `comments` GROUP BY `user_id`) AS 'comments_count' ON (`comments_count`.`user_id` = `users`.`id`)"
16
- computed_attribute.apply_join(dataset)
16
+ computed_attribute.apply_join(dataset, environment)
17
17
  .select_append(computed_attribute.identifier)
18
18
  .sql
19
19
  .must_equal expected_query
@@ -23,10 +23,30 @@ describe 'ComputedAttribute' do
23
23
  dataset = db[:people]
24
24
  computed_attribute = Rasti::DB::ComputedAttribute.new Sequel.join([:first_name, ' ', :last_name])
25
25
  expected_query = "SELECT * FROM `people` WHERE ((`first_name` || ' ' || `last_name`) = 'FULL NAME')"
26
- computed_attribute.apply_join(dataset)
26
+ computed_attribute.apply_join(dataset, environment)
27
27
  .where(computed_attribute.identifier => 'FULL NAME')
28
28
  .sql
29
29
  .must_equal expected_query
30
30
  end
31
31
 
32
+ it 'Apply join with join and environment attributes must generate correct query' do
33
+ dataset = db[:users]
34
+
35
+ expected_query = "SELECT *, `comments_count`.`value` AS 'value' FROM `users` INNER JOIN (SELECT `user_id`, count(`id`) AS 'value' FROM `comments` GROUP BY `user_id`) AS 'comments_count' ON (`comments_count`.`user_id` = `users`.`id`)"
36
+
37
+ computed_attribute = Rasti::DB::ComputedAttribute.new(Sequel[:comments_count][:value]) do |dataset, environment|
38
+ subquery = dataset.db.from(environment.data_source(:custom).qualify(:comments))
39
+ .select(Sequel[:user_id], Sequel.function('count', :id).as(:value))
40
+ .group(:user_id)
41
+ .as(:comments_count)
42
+
43
+ dataset.join_table(:inner, subquery, :user_id => :id)
44
+ end
45
+
46
+ computed_attribute.apply_join(dataset, environment)
47
+ .select_append(computed_attribute.identifier)
48
+ .sql
49
+ .must_equal expected_query
50
+ end
51
+
32
52
  end
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: 4.0.0
4
+ version: 4.1.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: 2021-06-01 00:00:00.000000000 Z
11
+ date: 2021-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel