simple-sql 0.4.10 → 0.4.11

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: f24d06442bae77c5c1a51362e9ef1ac83e50034e6ce1b47b480ed8290f40e2b3
4
- data.tar.gz: b2b0a88578e3c977ecf88739512d94a39bcbc8ce9eafac7225e02a925300ac9a
3
+ metadata.gz: 4f506a9925d4e81450d698169f04c60b69ca2191e1a353bbaa13b76a0ca48055
4
+ data.tar.gz: c2e9f14973dfdab36c1e49da800f4ae2e6e571e567338ebcc2be627f114938c3
5
5
  SHA512:
6
- metadata.gz: 2ebd2eda811287cf9dbc8ec9deefb512aa7e5b02fbeb3d8c789d254958ff2d7148c16a65fb529ef672a7626f2e8871822493bb8444e602c112070b5be66f6aa5
7
- data.tar.gz: bdfff2c6a865217aad9507ba6550b8122bc9f449a7cb8a09cc393ce97d75a60eec79a21f0edef183addc73e4378656e099b2ce84871f5a7e2b9721bbff670926
6
+ metadata.gz: 73d9bf9e990411871d8ed7d4f7a6a7d803aab52466387000441bcd777a35073ec6a6a9f5ff878e96e35d6ce469e5ddfcae5ec3de625b0024b5f6522dce1c8c9b
7
+ data.tar.gz: 75e21099aa4181e6fec9b7aa5919dd964ad271b4d20e0908f32de8fd7b6b150a7647162b946f376473303aa7d11bfc58a1132d91db19fa6a18f4f23a014bfd36
data/.gitignore CHANGED
@@ -2,4 +2,8 @@ coverage
2
2
  rdoc
3
3
  pkg
4
4
  log/test.log
5
- .rspec.data
5
+ .rspec.data
6
+ Gemfile.lock
7
+ .rake_t_cache
8
+ .rspec.status
9
+ simple-sql-0.4.10.gem
data/.tm_properties ADDED
@@ -0,0 +1,9 @@
1
+ # Settings
2
+ fontName = "Menlo"
3
+ fontSize = 13
4
+ fileBrowserGlob = "{*,.tm_properties,.htaccess,}"
5
+ excludeDirectories = "{node_modules,coverage,log,}"
6
+
7
+ # Variables
8
+ PATH = "$PATH:$HOME/bin"
9
+ TM_GIT = "/opt/local/bin/git"
@@ -37,14 +37,8 @@ module Simple
37
37
  end
38
38
 
39
39
  def table_info(schema: "public")
40
- columns = if schema == "public"
41
- "table_name AS name, *"
42
- else
43
- "table_schema || '.' || table_name AS name, *"
44
- end
45
-
46
40
  recs = all <<~SQL, schema, into: Hash
47
- SELECT #{columns}
41
+ SELECT table_schema || '.' || table_name AS name, *
48
42
  FROM information_schema.tables
49
43
  WHERE table_schema=$1
50
44
  SQL
@@ -1,6 +1,7 @@
1
1
  # rubocop:disable Metrics/AbcSize
2
2
  # rubocop:disable Metrics/MethodLength
3
3
  # rubocop:disable Metrics/ParameterLists
4
+ # rubocop:disable Style/GuardClause
4
5
 
5
6
  #
6
7
  # This module implements a pretty generic AssociationLoader.
@@ -20,13 +21,13 @@ module ::Simple::SQL::Result::AssociationLoader # :nodoc:
20
21
  #
21
22
  # Raises an ArgumentError if no matching table can be found.
22
23
  def find_associated_table(association, schema:)
23
- association = association.to_s
24
+ fq_association = "#{schema}.#{association}"
24
25
 
25
26
  tables_in_schema = ::Simple::SQL::Reflection.table_info(schema: schema).keys
26
27
 
27
- return "#{schema}.#{association}" if tables_in_schema.include?(association)
28
- return "#{schema}.#{association.singularize}" if tables_in_schema.include?(association.singularize)
29
- return "#{schema}.#{association.pluralize}" if tables_in_schema.include?(association.pluralize)
28
+ return fq_association if tables_in_schema.include?(fq_association)
29
+ return fq_association.singularize if tables_in_schema.include?(fq_association.singularize)
30
+ return fq_association.pluralize if tables_in_schema.include?(fq_association.pluralize)
30
31
 
31
32
  raise ArgumentError, "Don't know how to find foreign table for association #{association.inspect}"
32
33
  end
@@ -46,6 +47,9 @@ module ::Simple::SQL::Result::AssociationLoader # :nodoc:
46
47
  # Raises an ArgumentError if no association can be found between these tables.
47
48
  #
48
49
  def find_matching_relation(host_table, associated_table)
50
+ expect! host_table => /^[^.]+.[^.]+$/
51
+ expect! associated_table => /^[^.]+.[^.]+$/
52
+
49
53
  sql = <<~SQL
50
54
  WITH foreign_keys AS(
51
55
  SELECT
@@ -67,10 +71,16 @@ module ::Simple::SQL::Result::AssociationLoader # :nodoc:
67
71
  SQL
68
72
 
69
73
  relations = SQL.all(sql, host_table, associated_table, into: :struct)
70
- raise ArgumentError, "Found two potential matches for the #{association.inspect} relation" if relations.length > 1
71
- raise ArgumentError, "Found no potential match for the #{association.inspect} relation" if relations.empty?
72
74
 
73
- relations.first
75
+ return relations.first if relations.length == 1
76
+
77
+ description = "relation between #{host_table.inspect} and #{associated_table.inspect}"
78
+
79
+ if relations.empty?
80
+ raise "Didn't find any potential match for #{description}"
81
+ else
82
+ raise "Found two or more potential matches for #{description}"
83
+ end
74
84
  end
75
85
 
76
86
  # preloads a belongs_to association.
@@ -19,7 +19,6 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result::Rows
19
19
 
20
20
  AssociationLoader = ::Simple::SQL::Result::AssociationLoader
21
21
 
22
-
23
22
  # Preloads an association.
24
23
  #
25
24
  # This can now be used as follows:
@@ -45,7 +44,7 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result::Rows
45
44
  expect! as => [nil, Symbol]
46
45
 
47
46
  # resolve oid into table and schema name.
48
- schema, host_table = SQL.ask <<~SQL, @pg_source_oid
47
+ schema, host_table = ::Simple::SQL.ask <<~SQL, @pg_source_oid
49
48
  SELECT nspname AS schema, relname AS host_table
50
49
  FROM pg_class
51
50
  JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.4.10"
3
+ VERSION = "0.4.11"
4
4
  end
5
5
  end
@@ -13,7 +13,7 @@ describe "Simple::SQL::Reflection" do
13
13
 
14
14
  describe ".tables" do
15
15
  it "returns the tables in the public schema" do
16
- expect(SQL::Reflection.tables).to include("users")
16
+ expect(SQL::Reflection.tables).to include("public.users")
17
17
  end
18
18
 
19
19
  it "returns tables in a non-'public' schema" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
@@ -173,8 +173,8 @@ extra_rdoc_files: []
173
173
  files:
174
174
  - ".gitignore"
175
175
  - ".rubocop.yml"
176
+ - ".tm_properties"
176
177
  - Gemfile
177
- - Gemfile.lock
178
178
  - README.md
179
179
  - Rakefile
180
180
  - bin/console
data/Gemfile.lock DELETED
@@ -1,94 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- simple-sql (0.4.10)
5
- expectation (~> 1)
6
- pg (~> 0.20)
7
- pg_array_parser (~> 0)
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- activemodel (4.2.10)
13
- activesupport (= 4.2.10)
14
- builder (~> 3.1)
15
- activerecord (4.2.10)
16
- activemodel (= 4.2.10)
17
- activesupport (= 4.2.10)
18
- arel (~> 6.0)
19
- activesupport (4.2.10)
20
- i18n (~> 0.7)
21
- minitest (~> 5.1)
22
- thread_safe (~> 0.3, >= 0.3.4)
23
- tzinfo (~> 1.1)
24
- arel (6.0.4)
25
- ast (2.3.0)
26
- awesome_print (0.4.0)
27
- builder (3.2.3)
28
- byebug (10.0.2)
29
- concurrent-ruby (1.0.5)
30
- database_cleaner (1.6.2)
31
- diff-lcs (1.3)
32
- docile (1.1.5)
33
- expectation (1.1.1)
34
- i18n (0.9.1)
35
- concurrent-ruby (~> 1.0)
36
- json (2.1.0)
37
- minitest (5.10.3)
38
- parallel (1.10.0)
39
- parser (2.4.0.2)
40
- ast (~> 2.3)
41
- pg (0.20.0)
42
- pg_array_parser (0.0.9)
43
- powerpack (0.1.1)
44
- rainbow (2.2.2)
45
- rake
46
- rake (11.3.0)
47
- rspec (3.7.0)
48
- rspec-core (~> 3.7.0)
49
- rspec-expectations (~> 3.7.0)
50
- rspec-mocks (~> 3.7.0)
51
- rspec-core (3.7.0)
52
- rspec-support (~> 3.7.0)
53
- rspec-expectations (3.7.0)
54
- diff-lcs (>= 1.2.0, < 2.0)
55
- rspec-support (~> 3.7.0)
56
- rspec-mocks (3.7.0)
57
- diff-lcs (>= 1.2.0, < 2.0)
58
- rspec-support (~> 3.7.0)
59
- rspec-support (3.7.0)
60
- rubocop (0.52.1)
61
- parallel (~> 1.10)
62
- parser (>= 2.4.0.2, < 3.0)
63
- powerpack (~> 0.1)
64
- rainbow (>= 2.2.2, < 4.0)
65
- ruby-progressbar (~> 1.7)
66
- unicode-display_width (~> 1.0, >= 1.0.1)
67
- ruby-progressbar (1.9.0)
68
- simplecov (0.15.1)
69
- docile (~> 1.1.0)
70
- json (>= 1.8, < 3)
71
- simplecov-html (~> 0.10.0)
72
- simplecov-html (0.10.2)
73
- thread_safe (0.3.6)
74
- tzinfo (1.2.4)
75
- thread_safe (~> 0.1)
76
- unicode-display_width (1.1.3)
77
-
78
- PLATFORMS
79
- ruby
80
-
81
- DEPENDENCIES
82
- activerecord (~> 4)
83
- awesome_print (~> 0)
84
- byebug
85
- database_cleaner (~> 1)
86
- pg (= 0.20)
87
- rake (~> 11)
88
- rspec (~> 3.7)
89
- rubocop (~> 0.52.1)
90
- simple-sql!
91
- simplecov (~> 0)
92
-
93
- BUNDLED WITH
94
- 1.16.2