simple-sql 0.4.12 → 0.4.13
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 +4 -4
- data/lib/simple/sql/helpers/row_converter.rb +41 -31
- data/lib/simple/sql/result/records.rb +5 -1
- data/lib/simple/sql/version.rb +1 -1
- data/spec/simple/sql_associations_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f577e360eaff0f4796c8bbb52026e6ad135bf0bf
|
4
|
+
data.tar.gz: 91bb7c60f202bdc03021f8baccf060f116f78cf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b7844945f0fe1f7befd22015813693dd908744a8c97d8a8a1e9351c91a85148a844df8c8bffd97cf5099cb81461d941fa214145dc8310fbfed186c14654d64a
|
7
|
+
data.tar.gz: 5d5fe7b5ec335edc2d0e95abe0a13e1e14e48c0bec0c64ae78fd7ad967289e470863191ae00854426a4300db92bdd036a39f281e37c27779ad8f6499672db754
|
@@ -1,75 +1,85 @@
|
|
1
|
-
module Simple::SQL::Helpers::RowConverter
|
1
|
+
module Simple::SQL::Helpers::RowConverter
|
2
2
|
SELF = self
|
3
3
|
|
4
4
|
# returns an array of converted records
|
5
|
-
def self.
|
5
|
+
def self.convert_row(records, into:, associations: nil)
|
6
6
|
hsh = records.first
|
7
7
|
return records unless hsh
|
8
8
|
|
9
9
|
converter = if into == :struct
|
10
|
-
StructConverter.for(attributes: hsh.keys)
|
10
|
+
StructConverter.for(attributes: hsh.keys, associations: associations)
|
11
11
|
else
|
12
|
-
TypeConverter.for(type: into)
|
12
|
+
TypeConverter.for(type: into, associations: associations)
|
13
13
|
end
|
14
14
|
|
15
|
-
records.map { |record| converter.
|
15
|
+
records.map { |record| converter.convert_row(record) }
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.convert(record, into:) # :nodoc:
|
19
|
-
ary =
|
19
|
+
ary = convert_row([record], into: into)
|
20
20
|
ary.first
|
21
21
|
end
|
22
22
|
|
23
23
|
class TypeConverter #:nodoc:
|
24
|
-
def self.for(type:)
|
25
|
-
new(type: type)
|
24
|
+
def self.for(type:, associations:)
|
25
|
+
new(type: type, associations: associations)
|
26
26
|
end
|
27
27
|
|
28
|
-
def initialize(type:)
|
29
|
-
@type
|
28
|
+
def initialize(type:, associations:)
|
29
|
+
@type = type
|
30
|
+
@associations = associations
|
30
31
|
end
|
31
32
|
|
32
|
-
def
|
33
|
+
def convert_row(hsh)
|
34
|
+
hsh = convert_associations(hsh) if @associations
|
35
|
+
@type.new hsh
|
36
|
+
end
|
37
|
+
|
38
|
+
def convert_associations(hsh)
|
33
39
|
updates = {}
|
34
|
-
|
40
|
+
|
41
|
+
@associations.each do |key|
|
42
|
+
value = hsh.fetch(key)
|
35
43
|
case value
|
36
44
|
when Hash then updates[key] = SELF.convert(value, into: @type)
|
37
|
-
when Array then updates[key] = SELF.
|
45
|
+
when Array then updates[key] = SELF.convert_row(value, into: @type)
|
38
46
|
end
|
39
47
|
end
|
40
48
|
|
41
|
-
hsh
|
42
|
-
|
43
|
-
@type.new hsh
|
49
|
+
hsh.merge(updates)
|
44
50
|
end
|
45
51
|
end
|
46
52
|
|
47
53
|
class StructConverter # :nodoc:
|
48
|
-
def self.for(attributes:)
|
54
|
+
def self.for(attributes:, associations:)
|
49
55
|
@cache ||= {}
|
50
|
-
@cache[attributes] ||= new(attributes)
|
56
|
+
@cache[[attributes, associations]] ||= new(attributes: attributes, associations: associations)
|
51
57
|
end
|
52
58
|
|
53
|
-
def initialize(attributes)
|
59
|
+
def initialize(attributes:, associations:)
|
60
|
+
@attributes = attributes
|
61
|
+
@associations = associations
|
62
|
+
@association_indices = associations.map { |association| attributes.index(association) } if associations
|
63
|
+
|
54
64
|
@klass = Struct.new(*attributes)
|
55
65
|
end
|
56
66
|
|
57
|
-
def
|
58
|
-
values = hsh.values_at(*@
|
59
|
-
updates = {}
|
67
|
+
def convert_row(hsh)
|
68
|
+
values = hsh.values_at(*@attributes)
|
60
69
|
|
61
|
-
values
|
70
|
+
convert_associations(values) if @associations
|
71
|
+
@klass.new(*values)
|
72
|
+
end
|
73
|
+
|
74
|
+
# convert values at the <tt>@association_indices</tt>.
|
75
|
+
def convert_associations(values)
|
76
|
+
@association_indices.each do |idx|
|
77
|
+
value = values[idx]
|
62
78
|
case value
|
63
|
-
when Hash then
|
64
|
-
when Array then
|
79
|
+
when Hash then values[idx] = SELF.convert(value, into: :struct)
|
80
|
+
when Array then values[idx] = SELF.convert_row(value, into: :struct)
|
65
81
|
end
|
66
82
|
end
|
67
|
-
|
68
|
-
updates.each do |idx, updated_value|
|
69
|
-
values[idx] = updated_value
|
70
|
-
end
|
71
|
-
|
72
|
-
@klass.new(*values)
|
73
83
|
end
|
74
84
|
end
|
75
85
|
end
|
@@ -11,6 +11,7 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
|
|
11
11
|
@hash_records = records
|
12
12
|
@target_type = target_type
|
13
13
|
@pg_source_oid = pg_source_oid
|
14
|
+
@associations = []
|
14
15
|
|
15
16
|
materialize
|
16
17
|
end
|
@@ -54,6 +55,9 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
|
|
54
55
|
AssociationLoader.preload @hash_records, association,
|
55
56
|
host_table: host_table, schema: schema, as: as,
|
56
57
|
order_by: order_by, limit: limit
|
58
|
+
|
59
|
+
@associations << association
|
60
|
+
|
57
61
|
materialize
|
58
62
|
end
|
59
63
|
|
@@ -64,7 +68,7 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
|
|
64
68
|
|
65
69
|
def materialize
|
66
70
|
records = @hash_records
|
67
|
-
records = RowConverter.
|
71
|
+
records = RowConverter.convert_row(records, associations: @associations, into: @target_type) if @target_type != Hash
|
68
72
|
replace(records)
|
69
73
|
end
|
70
74
|
end
|
data/lib/simple/sql/version.rb
CHANGED
@@ -134,6 +134,16 @@ describe "Simple::SQL::Result#preload" do
|
|
134
134
|
|
135
135
|
expect(organizations.first.users.first).to be_a(OpenStruct)
|
136
136
|
end
|
137
|
+
|
138
|
+
it "only touches associations, leaving non-associated hashes and arrays alone" do
|
139
|
+
organizations = SQL.all "SELECT id, ARRAY[10000] AS ary FROM organizations", into: OpenStruct
|
140
|
+
expect(organizations.first.ary).to eq([10000])
|
141
|
+
end
|
142
|
+
|
143
|
+
it "only touches associations, leaving non-associated hashes and arrays alone" do
|
144
|
+
organizations = SQL.all "SELECT id, ARRAY[10000] AS ary FROM organizations", into: :struct
|
145
|
+
expect(organizations.first.ary).to eq([10000])
|
146
|
+
end
|
137
147
|
end
|
138
148
|
|
139
149
|
describe ":order_by" do
|