rasti-db 3.0.0 → 4.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e15c6a4b9b21397abc6135f83b6b863a8e8147e9bfea492cf25fd1d54db00cb
4
- data.tar.gz: 0cebbbf49d1262804bfa559e6c696763761242b46a877c50c5f7dce528c9f16c
3
+ metadata.gz: 331138443b43eefbd6f4e9008755646f790a7a12329d5fb342936b97de8d9bfe
4
+ data.tar.gz: 9723170bbbf65352a112312a6b72953a69055ceca9d115763705d6363b00e669
5
5
  SHA512:
6
- metadata.gz: 468ca823921d6eaece74f8b0479e25fc767ed4c7e08a63fe24fd633a467ab3469cf199994b8a53e4207505e5090679011a1a5623d52f6322b910d59594a80ce0
7
- data.tar.gz: 938c0fea726c3e70c1fe791d3034eae9f11077c2614321eb252556bf7ad5a8a3f8a470bb9dd3b833ad1e84cec57557a703f4f6591ae2e6dc064b163702264bef
6
+ metadata.gz: '00777167854dfaa0f0e133b157ac97e56a8a94c840b79a024a2f0af507a995a77f72372b263ee4a00770970d0f6646f0c034ee5cee733dae892bb85cae78f756'
7
+ data.tar.gz: 66f2f804441640d2cfe7ed3694000092e9d8f7541e6478c4be98b475b7e3d70de7d8a7c78c63b19a8542c89cf1ff7311291f6c09f8d339ba8a2d257a2a671efe
@@ -0,0 +1,26 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ '**' ]
6
+ pull_request:
7
+ branches: [ '**' ]
8
+
9
+ jobs:
10
+ test:
11
+
12
+ name: Tests
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby-version: ['2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', 'jruby-9.2.9.0']
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Set up Ruby
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ bundler-cache: true
25
+ - name: Run tests
26
+ run: bundle exec rake
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rasti::DB
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rasti-db.svg)](https://rubygems.org/gems/rasti-db)
4
- [![Build Status](https://travis-ci.org/gabynaiman/rasti-db.svg?branch=master)](https://travis-ci.org/gabynaiman/rasti-db)
4
+ [![CI](https://github.com/gabynaiman/rasti-db/actions/workflows/ci.yml/badge.svg)](https://github.com/gabynaiman/rasti-db/actions/workflows/ci.yml)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/rasti-db/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/rasti-db?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/gabynaiman/rasti-db.svg)](https://codeclimate.com/github/gabynaiman/rasti-db)
7
7
 
@@ -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
@@ -58,17 +58,15 @@ module Rasti
58
58
  end
59
59
 
60
60
  def select_computed_attributes(*computed_attributes)
61
- ds = computed_attributes.inject(dataset) do |ds, name|
61
+ ds = computed_attributes.inject(dataset) do |inner_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(inner_ds, environment).select_append(computed_attribute.identifier.as(name))
64
64
  end
65
65
  build_query dataset: ds
66
66
  end
67
67
 
68
68
  def all
69
- with_graph(dataset.all).map do |row|
70
- collection_class.model.new row
71
- end
69
+ build_models dataset.all
72
70
  end
73
71
  alias_method :to_a, :all
74
72
 
@@ -114,13 +112,11 @@ module Rasti
114
112
  end
115
113
 
116
114
  def first
117
- row = dataset.first
118
- row ? build_model(row) : nil
115
+ build_model dataset.first
119
116
  end
120
117
 
121
118
  def last
122
- row = dataset.last
123
- row ? build_model(row) : nil
119
+ build_model dataset.last
124
120
  end
125
121
 
126
122
  def detect(*args, &block)
@@ -137,8 +133,8 @@ module Rasti
137
133
 
138
134
  raise NQL::InvalidExpressionError.new(filter_expression) if sentence.nil?
139
135
 
140
- ds = sentence.computed_attributes(collection_class).inject(dataset) do |ds, name|
141
- collection_class.computed_attributes[name].apply_join ds
136
+ ds = sentence.computed_attributes(collection_class).inject(dataset) do |inner_ds, name|
137
+ collection_class.computed_attributes[name].apply_join inner_ds, environment
142
138
  end
143
139
  query = build_query dataset: ds
144
140
 
@@ -164,7 +160,21 @@ module Rasti
164
160
  end
165
161
 
166
162
  def build_model(row)
167
- collection_class.model.new with_graph(row)
163
+ row ? build_models([row]).first : nil
164
+ end
165
+
166
+ def build_models(rows)
167
+ with_graph(rows).map do |row|
168
+ collection_class.model.new slice_defined_attributes(row)
169
+ end
170
+ end
171
+
172
+ def slice_defined_attributes(row)
173
+ row.select { |k,_| defined_attributes.include? k }
174
+ end
175
+
176
+ def defined_attributes
177
+ @defined_attributes ||= collection_class.model.attribute_names.to_set
168
178
  end
169
179
 
170
180
  def chainable(&block)
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module DB
3
- VERSION = '3.0.0'
3
+ VERSION = '4.1.1'
4
4
  end
5
5
  end
data/rasti-db.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_runtime_dependency 'sequel', '~> 5.0'
22
- spec.add_runtime_dependency 'rasti-model', '~> 1.0'
22
+ spec.add_runtime_dependency 'rasti-model', '~> 2.0'
23
23
  spec.add_runtime_dependency 'treetop', '~> 1.4.8'
24
24
  spec.add_runtime_dependency 'consty', '~> 1.0', '>= 1.0.3'
25
25
  spec.add_runtime_dependency 'timing', '~> 0.1', '>= 0.1.3'
@@ -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
@@ -19,7 +19,8 @@ Post = Rasti::DB::Model[:id, :title, :body, :user_id, :user, :comments, :cat
19
19
  Comment = Rasti::DB::Model[:id, :text, :user_id, :user, :post_id, :post, :tags]
20
20
  Category = Rasti::DB::Model[:id, :name, :posts]
21
21
  Person = Rasti::DB::Model[:document_number, :first_name, :last_name, :birth_date, :user_id, :user, :languages, :full_name]
22
- Language = Rasti::DB::Model[:id, :name, :people]
22
+ Language = Rasti::DB::Model[:id, :name, :people, :countries]
23
+ Country = Rasti::DB::Model[:id, :name, :language_id]
23
24
 
24
25
 
25
26
  class Users < Rasti::DB::Collection
@@ -47,8 +48,8 @@ class Posts < Rasti::DB::Collection
47
48
  one_to_many :comments
48
49
 
49
50
  query :created_by, ->(user_id) { where user_id: user_id }
50
-
51
- query :entitled do |title|
51
+
52
+ query :entitled do |title|
52
53
  where title: title
53
54
  end
54
55
 
@@ -106,6 +107,11 @@ class Languages < Rasti::DB::Collection
106
107
 
107
108
  many_to_many :people, collection: People, relation_data_source_name: :default
108
109
  one_to_many :posts
110
+ one_to_many :countries
111
+ end
112
+
113
+ class Countries < Rasti::DB::Collection
114
+ many_to_one :language
109
115
  end
110
116
 
111
117
 
@@ -114,7 +120,7 @@ class Minitest::Spec
114
120
  let(:users) { Users.new environment }
115
121
 
116
122
  let(:posts) { Posts.new environment }
117
-
123
+
118
124
  let(:comments) { Comments.new environment }
119
125
 
120
126
  let(:categories) { Categories.new environment }
@@ -123,9 +129,11 @@ class Minitest::Spec
123
129
 
124
130
  let(:languages) { Languages.new environment }
125
131
 
132
+ let(:countries) { Countries.new environment }
133
+
126
134
  let(:driver) { (RUBY_ENGINE == 'jruby') ? 'jdbc:sqlite::memory:' : {adapter: :sqlite} }
127
135
 
128
- let :environment do
136
+ let :environment do
129
137
  Rasti::DB::Environment.new default: Rasti::DB::DataSource.new(db),
130
138
  custom: Rasti::DB::DataSource.new(custom_db)
131
139
  end
@@ -179,6 +187,13 @@ class Minitest::Spec
179
187
  primary_key [:language_id, :document_number]
180
188
  end
181
189
 
190
+ db.create_table :countries do
191
+ primary_key :id
192
+ String :name, null: false, unique: true
193
+ Integer :population, null: false
194
+ Integer :language_id, null: false, index: true
195
+ end
196
+
182
197
  end
183
198
  end
184
199
 
data/spec/query_spec.rb CHANGED
@@ -32,6 +32,8 @@ describe 'Query' do
32
32
  db[:categories_posts].insert post_id: 2, category_id: 2
33
33
  db[:categories_posts].insert post_id: 2, category_id: 3
34
34
  db[:categories_posts].insert post_id: 3, category_id: 3
35
+
36
+ db[:countries].insert name: 'Argentina', population: 40000000, language_id: 1
35
37
  end
36
38
 
37
39
  let(:users_query) { Rasti::DB::Query.new collection_class: Users, dataset: db[:users], environment: environment }
@@ -44,6 +46,8 @@ describe 'Query' do
44
46
 
45
47
  let(:languages_query) { Rasti::DB::Query.new collection_class: Languages, dataset: custom_db[:languages], environment: environment }
46
48
 
49
+ let(:countries_query) { Rasti::DB::Query.new collection_class: Countries, dataset: db[:countries], environment: environment }
50
+
47
51
  it 'Count' do
48
52
  users_query.count.must_equal 10
49
53
  end
@@ -166,6 +170,25 @@ describe 'Query' do
166
170
  end
167
171
  end
168
172
 
173
+ describe 'Ignore undefined attributes' do
174
+
175
+ it 'First level' do
176
+ countries_query.raw.first.must_equal id: 1,
177
+ name: 'Argentina',
178
+ population: 40000000,
179
+ language_id: 1
180
+
181
+ [countries_query.detect(id: 1), countries_query.where(id: 1).order(:name).last, countries_query.all.first].each do |country|
182
+ country.must_equal Country.new(language_id: 1, id: 1, name: 'Argentina')
183
+ end
184
+ end
185
+
186
+ it 'Graph nested' do
187
+ languages_query.graph(:countries).first.countries.must_equal [Country.new(language_id: 1, id: 1, name: 'Argentina')]
188
+ end
189
+
190
+ end
191
+
169
192
  it 'Map' do
170
193
  users_query.map(&:name).must_equal db[:users].map(:name)
171
194
  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: 3.0.0
4
+ version: 4.1.1
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-03-11 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.0'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.0'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: treetop
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -294,10 +294,10 @@ extensions: []
294
294
  extra_rdoc_files: []
295
295
  files:
296
296
  - ".coveralls.yml"
297
+ - ".github/workflows/ci.yml"
297
298
  - ".gitignore"
298
299
  - ".ruby-gemset"
299
300
  - ".ruby-version"
300
- - ".travis.yml"
301
301
  - Gemfile
302
302
  - LICENSE.txt
303
303
  - README.md
@@ -395,7 +395,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
395
395
  - !ruby/object:Gem::Version
396
396
  version: '0'
397
397
  requirements: []
398
- rubygems_version: 3.0.1
398
+ rubygems_version: 3.0.9
399
399
  signing_key:
400
400
  specification_version: 4
401
401
  summary: Database collections and relations
data/.travis.yml DELETED
@@ -1,22 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - 2.1
5
- - 2.2
6
- - 2.3
7
- - 2.4
8
- - 2.5
9
- - 2.6
10
- - 2.7
11
- - jruby-9.2.9.0
12
- - ruby-head
13
- - jruby-head
14
-
15
- matrix:
16
- fast_finish: true
17
- allow_failures:
18
- - rvm: ruby-head
19
- - rvm: jruby-head
20
-
21
- jdk:
22
- - openjdk8