relation_to_struct 0.0.3 → 0.0.4
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4917a1af7ed5ef3d71c3f31314f67cf7150f2903
|
4
|
+
data.tar.gz: d1e99802577449b4b789a7b52476a4a7333b6b36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bf5ac98b2a2945701453e84c48576ba6257a2be03109d5efa32dd1e4d1bb1d23abc57bb21f570798e8d919d19707ca8e71c2374aa53d3ea139e09a1a3804cbf
|
7
|
+
data.tar.gz: d4cdf8c1d1d0de0b9cc9e3b40112888a5b20f6053a7c5e7faca2895cd4db7d14a8d3b769246fe112ab7516ebe88e3a1d9d7df121acfeef4b62a98540cc1c4c35
|
@@ -4,13 +4,24 @@ module RelationToStruct::ActiveRecordBaseExtension
|
|
4
4
|
module ClassMethods
|
5
5
|
def structs_from_sql(struct_class, sql, binds=[])
|
6
6
|
result = connection.select_all(sanitize_sql(sql, nil), "Structs SQL Load", binds)
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
if result.columns.size != struct_class.members.size
|
9
|
+
raise ArgumentError, 'Expected struct fields and columns lengths to be equal'
|
10
|
+
end
|
11
|
+
|
12
|
+
if result.columns.size == 1
|
13
|
+
result.cast_values().map do |tuple|
|
14
|
+
struct_class.new(tuple)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
result.cast_values().map do |tuple|
|
18
|
+
struct_class.new(*tuple)
|
19
|
+
end
|
9
20
|
end
|
10
21
|
end
|
11
22
|
|
12
23
|
def pluck_from_sql(sql, binds=[])
|
13
|
-
result = connection.select_all(sanitize_sql(sql, nil), "
|
24
|
+
result = connection.select_all(sanitize_sql(sql, nil), "Pluck SQL Load", binds)
|
14
25
|
result.cast_values()
|
15
26
|
end
|
16
27
|
end
|
@@ -6,10 +6,21 @@ module RelationToStruct::ActiveRecordRelationExtension
|
|
6
6
|
|
7
7
|
relation = spawn
|
8
8
|
result = klass.connection.select_all(relation.arel, nil, relation.arel.bind_values + bind_values)
|
9
|
+
|
10
|
+
if result.columns.size != struct_class.members.size
|
11
|
+
raise ArgumentError, 'Expected struct fields and columns lengths to be equal'
|
12
|
+
end
|
13
|
+
|
9
14
|
result.cast_values(klass.column_types)
|
10
15
|
|
11
|
-
result.
|
12
|
-
|
16
|
+
if result.columns.size == 1
|
17
|
+
result.cast_values(klass.column_types).map do |tuple|
|
18
|
+
struct_class.new(tuple)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
result.cast_values(klass.column_types).map do |tuple|
|
22
|
+
struct_class.new(*tuple)
|
23
|
+
end
|
13
24
|
end
|
14
25
|
end
|
15
26
|
end
|
@@ -59,6 +59,28 @@ describe RelationToStruct do
|
|
59
59
|
struct_column_klass = structs_results.first.date.class
|
60
60
|
expect(pluck_column_klass).to eq(struct_column_klass)
|
61
61
|
end
|
62
|
+
|
63
|
+
it '#to_structs should properly cast a single array column' do
|
64
|
+
Economist.create!(name: 'F.A. Hayek')
|
65
|
+
|
66
|
+
pluck_results = Economist.select('name').pluck('array[name]') rescue nil
|
67
|
+
if pluck_results
|
68
|
+
expect(pluck_results).to eq([['F.A. Hayek']]) # Verify ActiveRecord interface.
|
69
|
+
|
70
|
+
test_struct = Struct.new(:names)
|
71
|
+
structs_results = Economist.select('array[name]').to_structs(test_struct)
|
72
|
+
expect(structs_results.first.names).to eq(['F.A. Hayek'])
|
73
|
+
else
|
74
|
+
skip "DB selection doesn't support ARRAY[]"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it '#to_structs should raise an error when column count does not match struct size' do
|
79
|
+
expect do
|
80
|
+
test_struct = Struct.new(:id, :name, :extra_field)
|
81
|
+
Economist.select('id, name').to_structs(test_struct)
|
82
|
+
end.to raise_error(ArgumentError, 'Expected struct fields and columns lengths to be equal')
|
83
|
+
end
|
62
84
|
end
|
63
85
|
|
64
86
|
describe 'non-model specific querying' do
|
@@ -81,5 +103,28 @@ describe RelationToStruct do
|
|
81
103
|
sql = "SELECT 1 * 23, 25"
|
82
104
|
expect(ActiveRecord::Base.pluck_from_sql(sql)).to eq([[23, 25]])
|
83
105
|
end
|
106
|
+
|
107
|
+
it 'structs_from_sql should properly cast a single array column' do
|
108
|
+
Economist.create!(name: 'F.A. Hayek')
|
109
|
+
Economist.create!(name: 'Ludwig von Mises')
|
110
|
+
|
111
|
+
pluck_results = Economist.select('name').order('id').limit(1).pluck('array[name]') rescue nil
|
112
|
+
if pluck_results
|
113
|
+
expect(pluck_results).to eq([['F.A. Hayek']]) # Verify ActiveRecord interface.
|
114
|
+
|
115
|
+
test_struct = Struct.new(:names)
|
116
|
+
structs_results = ActiveRecord::Base.structs_from_sql(test_struct, 'SELECT ARRAY_AGG(name ORDER BY id) FROM economists')
|
117
|
+
expect(structs_results.first.names).to eq(['F.A. Hayek', 'Ludwig von Mises'])
|
118
|
+
else
|
119
|
+
skip "DB selection doesn't support ARRAY[]"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'structs_from_sql should raise an error when column count does not match struct size' do
|
124
|
+
expect do
|
125
|
+
test_struct = Struct.new(:id, :name, :extra_field)
|
126
|
+
ActiveRecord::Base.structs_from_sql(test_struct, 'SELECT id, name FROM economists')
|
127
|
+
end.to raise_error(ArgumentError, 'Expected struct fields and columns lengths to be equal')
|
128
|
+
end
|
84
129
|
end
|
85
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relation_to_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Coleman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|