rom-sql 0.9.0 → 0.9.1

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
  SHA1:
3
- metadata.gz: 4fdb75ab8064473be4552789976b049b939ed356
4
- data.tar.gz: 3c2cc1dd4f7ee9428f6a2f45a2ecc1368c6e5882
3
+ metadata.gz: 7afadeffec4c6a560640ef08d8dbe333d067bf6c
4
+ data.tar.gz: 096577d996f4be2697da8ca271e57c60cdbdf18e
5
5
  SHA512:
6
- metadata.gz: a96f739270f64f4828b7d777d949b6b20bfd7161c6a19813347d1f0fdbdbe4f125c387195ab01dc184fbac06ab3faf83cfda76e608f212937336529663fd4d87
7
- data.tar.gz: af5dbaba037a0ef816b32af01ba4b2c504eaf6310463d244b38e79304b8b4face04137857b9c12f9b30c87c73cb044e369153a0e6b8322f9d5949b0ef689cc91
6
+ metadata.gz: 65fc494f45e9e7a76ba43aeb541d5c38719b020ae7ef5092e3e1698f53e3ddf15c23baa4fe97c3a8bd0cb646b4ebec3c0efad77bf03aaeee9317cc9bf5aea2fb
7
+ data.tar.gz: 493b7c1cae2cb730c092e5b30df0e866797c2fdac2a5ce0c2264cbd2cf05a86ed66521c219b8d5c4c12687425bbf3ed82e193c6f5583969a49106264a17add47
@@ -1,3 +1,12 @@
1
+ ## v0.9.1 2016-12-23
2
+
3
+ ### Added
4
+
5
+ * Support for PG enums in schema inferrer (flash-gordon)
6
+ * `ROM::SQL::Relation#read` method which accepts an SQL string and returns a new relation (solnic)
7
+
8
+ [Compare v0.9.0...v0.9.1](https://github.com/rom-rb/rom-sql/compare/v0.9.0...v0.9.1)
9
+
1
10
  ## v0.9.0 2016-11-08
2
11
 
3
12
  ### Added
data/Gemfile CHANGED
@@ -2,8 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rom', github: 'rom-rb/rom', branch: 'master'
6
-
7
5
  group :test do
8
6
  gem 'byebug', platforms: :mri
9
7
  gem 'dry-struct'
@@ -1,3 +1,4 @@
1
+ require 'set'
1
2
  require 'rom/sql/schema/inferrer'
2
3
  require 'rom/sql/extensions/postgres/types'
3
4
 
@@ -7,17 +8,11 @@ module ROM
7
8
  class PostgresInferrer < Inferrer[:postgres]
8
9
  defines :db_numeric_types, :db_type_mapping, :db_array_type_matcher
9
10
 
10
- db_numeric_types(
11
- 'smallint' => true,
12
- 'integer' => true,
13
- 'bigint' => true,
14
- 'decimal' => true,
15
- 'numeric' => true,
16
- 'real' => true,
17
- 'double precision' => true,
18
- 'serial' => true,
19
- 'bigserial' => true,
20
- ).freeze
11
+ db_numeric_types %w(
12
+ smallint integer bigint
13
+ decimal numeric real
14
+ double\ precision serial bigserial
15
+ ).to_set.freeze
21
16
 
22
17
  db_type_mapping(
23
18
  'uuid' => Types::PG::UUID,
@@ -41,9 +36,11 @@ module ROM
41
36
  type.meta(primary_key: true)
42
37
  end
43
38
 
44
- def map_type(ruby_type, db_type)
39
+ def map_type(ruby_type, db_type, enum_values: nil, **_)
45
40
  if db_type.end_with?(self.class.db_array_type_matcher)
46
41
  Types::PG::Array(db_type)
42
+ elsif enum_values
43
+ Types::String.enum(*enum_values)
47
44
  else
48
45
  map_db_type(db_type) || super
49
46
  end
@@ -54,9 +51,7 @@ module ROM
54
51
  end
55
52
 
56
53
  def numeric?(ruby_type, db_type)
57
- self.class.db_numeric_types.fetch(db_type) do
58
- ruby_type == :integer
59
- end
54
+ self.class.db_numeric_types.include?(db_type) || ruby_type == :integer
60
55
  end
61
56
  end
62
57
  end
@@ -507,6 +507,20 @@ module ROM
507
507
  def unique?(criteria)
508
508
  where(criteria).count.zero?
509
509
  end
510
+
511
+ # Return a new relation from a raw SQL string
512
+ #
513
+ # @example
514
+ # users.read('SELECT name FROM users')
515
+ #
516
+ # @param [String] sql The SQL string
517
+ #
518
+ # @return [SQL::Relation]
519
+ #
520
+ # @api public
521
+ def read(sql)
522
+ __new__(dataset.db[sql])
523
+ end
510
524
  end
511
525
  end
512
526
  end
@@ -53,7 +53,7 @@ module ROM
53
53
  if primary_key
54
54
  map_pk_type(type, db_type)
55
55
  else
56
- mapped_type = map_type(type, db_type)
56
+ mapped_type = map_type(type, db_type, rest)
57
57
  mapped_type = mapped_type.optional if allow_null
58
58
  mapped_type = mapped_type.meta(foreign_key: true, relation: foreign_key) if foreign_key
59
59
  mapped_type
@@ -64,7 +64,7 @@ module ROM
64
64
  self.class.numeric_pk_type.meta(primary_key: true)
65
65
  end
66
66
 
67
- def map_type(ruby_type, db_type)
67
+ def map_type(ruby_type, db_type, **_kw)
68
68
  self.class.ruby_type_mapping.fetch(ruby_type) {
69
69
  raise UnknownDBTypeError, "Cannot find corresponding type for #{ruby_type || db_type}"
70
70
  }
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module SQL
3
- VERSION = '0.9.0'.freeze
3
+ VERSION = '0.9.1'.freeze
4
4
  end
5
5
  end
@@ -1,8 +1,15 @@
1
1
  RSpec.describe 'ROM::SQL::Schema::PostgresInferrer', :postgres do
2
2
  include_context 'database setup'
3
3
 
4
+ colors = %w(red orange yellow green blue purple)
5
+
4
6
  before do
7
+ conn.extension :pg_enum
8
+
5
9
  conn.drop_table?(:test_inferrence)
10
+ conn.drop_enum(:rainbow, if_exists: true)
11
+
12
+ conn.create_enum(:rainbow, colors)
6
13
 
7
14
  conn.create_table :test_inferrence do
8
15
  primary_key :id, :uuid
@@ -11,6 +18,7 @@ RSpec.describe 'ROM::SQL::Schema::PostgresInferrer', :postgres do
11
18
  Decimal :money, null: false
12
19
  column :tags, "text[]"
13
20
  column :tag_ids, "bigint[]"
21
+ rainbow :color
14
22
  end
15
23
  end
16
24
 
@@ -33,7 +41,8 @@ RSpec.describe 'ROM::SQL::Schema::PostgresInferrer', :postgres do
33
41
  jsonb_data: ROM::SQL::Types::PG::JSONB.optional.meta(name: :jsonb_data),
34
42
  money: ROM::SQL::Types::Decimal.meta(name: :money),
35
43
  tags: ROM::SQL::Types::PG::Array('text').optional.meta(name: :tags),
36
- tag_ids: ROM::SQL::Types::PG::Array('biging').optional.meta(name: :tag_ids)
44
+ tag_ids: ROM::SQL::Types::PG::Array('biging').optional.meta(name: :tag_ids),
45
+ color: ROM::SQL::Types::String.enum(*colors).optional.meta(name: :color)
37
46
  )
38
47
  end
39
48
  end
@@ -0,0 +1,21 @@
1
+ RSpec.describe ROM::Relation, '#read' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ let(:users) do
8
+ relation.read('SELECT name FROM users')
9
+ end
10
+
11
+ it 'returns results from raw SQL' do
12
+ expect(users).to match_array([{ name: 'Jane' }, { name: 'Joe' }])
13
+ end
14
+
15
+ it 'returns a new SQL relation' do
16
+ materialized = users.()
17
+ expect(materialized).to match_array([{ name: 'Jane' }, { name: 'Joe' }])
18
+ expect(materialized.source).to be(users)
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -258,6 +258,7 @@ files:
258
258
  - spec/unit/relation/primary_key_spec.rb
259
259
  - spec/unit/relation/project_spec.rb
260
260
  - spec/unit/relation/qualified_columns_spec.rb
261
+ - spec/unit/relation/read_spec.rb
261
262
  - spec/unit/relation/rename_spec.rb
262
263
  - spec/unit/relation/sum_spec.rb
263
264
  - spec/unit/relation/union_spec.rb
@@ -356,6 +357,7 @@ test_files:
356
357
  - spec/unit/relation/primary_key_spec.rb
357
358
  - spec/unit/relation/project_spec.rb
358
359
  - spec/unit/relation/qualified_columns_spec.rb
360
+ - spec/unit/relation/read_spec.rb
359
361
  - spec/unit/relation/rename_spec.rb
360
362
  - spec/unit/relation/sum_spec.rb
361
363
  - spec/unit/relation/union_spec.rb