relation_to_struct 1.1.0 → 1.2.0
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: 20d99ebc500ab5e07fd4b78fcb158790fa26b475
|
4
|
+
data.tar.gz: 5af91e2d780a6e0ec03383c3c29599674707f0a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 732739b20458d9223cb161bdc4c3c9928b28a2b1c70536aca9371ddf642264a83b6f78ea1dd8eb78862d22069672a78b4cc12930d9cfbb97f448aa506ecdc759
|
7
|
+
data.tar.gz: 258ea4297e9b7f2538d54a727b9b4eeee01df5f8dfae0041c5ee8ebf2ecc0ba1753866aa0d127e9699aedfc2af95005a74d76b35e97995899e3450b9d444b4c4
|
@@ -43,6 +43,20 @@ module RelationToStruct::ActiveRecordBaseExtension
|
|
43
43
|
raise ArgumentError, 'Expected only a single result to be returned'
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
def tuple_from_sql(sql, binds=[])
|
48
|
+
result = connection.select_all(sanitize_sql(sql, nil), "Value SQL Load", binds)
|
49
|
+
values = result.cast_values()
|
50
|
+
|
51
|
+
case values.size
|
52
|
+
when 0
|
53
|
+
nil
|
54
|
+
when 1
|
55
|
+
result.columns.size == 1 ? values : values[0]
|
56
|
+
else
|
57
|
+
raise ArgumentError, 'Expected only a single result to be returned'
|
58
|
+
end
|
59
|
+
end
|
46
60
|
end
|
47
61
|
end
|
48
62
|
|
@@ -24,7 +24,7 @@ describe ActiveRecord::Base do
|
|
24
24
|
expect(ActiveRecord::Base.value_from_sql(sql)).to eq(23)
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'raises an error when multiple
|
27
|
+
it 'raises an error when multiple rows are selected' do
|
28
28
|
expect do
|
29
29
|
sql = "SELECT * FROM (VALUES (1), (2)) t"
|
30
30
|
ActiveRecord::Base.value_from_sql(sql)
|
@@ -56,6 +56,64 @@ describe ActiveRecord::Base do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
describe "#tuple_from_sql" do
|
60
|
+
it 'allows selecting one value with SQL directly' do
|
61
|
+
sql = "SELECT 1"
|
62
|
+
expect(ActiveRecord::Base.tuple_from_sql(sql)).to eq([1])
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'allows selecting multiple values with SQL directly' do
|
66
|
+
sql = "SELECT 1, 23"
|
67
|
+
expect(ActiveRecord::Base.tuple_from_sql(sql)).to eq([1, 23])
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raises an error when multiple rows are selected' do
|
71
|
+
expect do
|
72
|
+
sql = "SELECT * FROM (VALUES (1, 3), (2, 4)) t"
|
73
|
+
ActiveRecord::Base.tuple_from_sql(sql)
|
74
|
+
end.to raise_error(ArgumentError, 'Expected only a single result to be returned')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'supports binds' do
|
78
|
+
sql = ["SELECT ?, ?", 5, 6]
|
79
|
+
expect(ActiveRecord::Base.tuple_from_sql(sql)).to eq([5, 6])
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'supports a single array' do
|
83
|
+
if active_record_supports_arrays?
|
84
|
+
Economist.create!(name: 'F.A. Hayek')
|
85
|
+
Economist.create!(name: 'Ludwig von Mises')
|
86
|
+
|
87
|
+
result = ActiveRecord::Base.tuple_from_sql(<<-SQL)
|
88
|
+
SELECT ARRAY_AGG(name ORDER BY id) AS names
|
89
|
+
FROM economists
|
90
|
+
SQL
|
91
|
+
expected_names = ['F.A. Hayek', 'Ludwig von Mises']
|
92
|
+
expect(result).to eq([expected_names])
|
93
|
+
else
|
94
|
+
skip "DB selection doesn't support ARRAY[]"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'supports multiple arrays' do
|
99
|
+
if active_record_supports_arrays?
|
100
|
+
Economist.create!(name: 'F.A. Hayek')
|
101
|
+
Economist.create!(name: 'Ludwig von Mises')
|
102
|
+
|
103
|
+
result = ActiveRecord::Base.tuple_from_sql(<<-SQL)
|
104
|
+
SELECT
|
105
|
+
ARRAY_AGG(name ORDER BY id) AS names,
|
106
|
+
ARRAY_AGG(CHAR_LENGTH(name) ORDER BY id) AS lengths
|
107
|
+
FROM economists
|
108
|
+
SQL
|
109
|
+
expected_names = ['F.A. Hayek', 'Ludwig von Mises']
|
110
|
+
expect(result).to eq([expected_names, expected_names.map(&:size)])
|
111
|
+
else
|
112
|
+
skip "DB selection doesn't support ARRAY[]"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
59
117
|
describe "#structs_from_sql" do
|
60
118
|
it 'ActiveRecord::Base should respond to :structs_from_sql' do
|
61
119
|
expect(ActiveRecord::Base.respond_to?(:structs_from_sql)).to eq(true)
|
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Coleman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|