postgresql_cursor 0.6.8 → 0.6.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 +4 -4
- data/README.md +12 -1
- data/lib/postgresql_cursor/cursor.rb +14 -9
- data/lib/postgresql_cursor/version.rb +1 -1
- data/test/helper.rb +2 -1
- data/test/test_postgresql_cursor.rb +9 -0
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c274e5d594957c08a12d1ba4808e0265a081b8f4904736548e5d61524c4cf12f
|
|
4
|
+
data.tar.gz: cef01d957175ccb3ac9a3866f456d4a502486cb734322b1acb9fa9a127494df6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 33b45e046fe9ae45a6013b62d69894e8f4b129835588d9aeb297714fab9d1046de5c225bf0aa938577fb0ec7a9a67dbc5c07da53e3f8be59ddf1e7fdabfd4579
|
|
7
|
+
data.tar.gz: 2623728af17821817d33dc616fa921ef8b0f7c9ab5d4edf4d62de11bfaf1579dd1bde1388d9b2b990bc36bfe556de0398314af2ad839b80c657d3120e2c08e80
|
data/README.md
CHANGED
|
@@ -88,6 +88,17 @@ Product.each_row.map {|r| r["id"].to_i } #=> [1, 2, 3, ...]
|
|
|
88
88
|
Product.each_instance.map {|r| r.id }.each {|id| p id } #=> [1, 2, 3, ...]
|
|
89
89
|
Product.each_instance.lazy.inject(0) {|sum,r| sum + r.quantity } #=> 499500
|
|
90
90
|
```
|
|
91
|
+
|
|
92
|
+
### PostgreSQLCursor and collection rendering
|
|
93
|
+
|
|
94
|
+
You can render cursor collection, using enumeration as collection attribute.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
render partial: "some_partial", collection: Product.each_instance
|
|
98
|
+
render partial: "some_partial", collection: Product.each_row
|
|
99
|
+
render partial: "some_partial", collection: Product.each_hash
|
|
100
|
+
```
|
|
101
|
+
|
|
91
102
|
### Hashes vs. Instances
|
|
92
103
|
|
|
93
104
|
The each_row method returns the Hash of strings for speed (as this allows you to process a lot of rows).
|
|
@@ -106,7 +117,7 @@ To limit the columns returned to just those you need, use `.select(:id, :name)`
|
|
|
106
117
|
query method.
|
|
107
118
|
|
|
108
119
|
```ruby
|
|
109
|
-
Product.select(:id, :name).
|
|
120
|
+
Product.select(:id, :name).each_instance { |product| product.process }
|
|
110
121
|
```
|
|
111
122
|
|
|
112
123
|
Pluck is a great alternative instead of using a cursor. It does not instantiate
|
|
@@ -71,6 +71,12 @@ module PostgreSQLCursor
|
|
|
71
71
|
self
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
+
# ActiveRecord call #size when rendering a collection
|
|
75
|
+
# Define it and return some dummy value
|
|
76
|
+
def size
|
|
77
|
+
-1
|
|
78
|
+
end
|
|
79
|
+
|
|
74
80
|
# Public: Yields each row of the result set to the passed block
|
|
75
81
|
#
|
|
76
82
|
# Yields the row to the block. The row is a hash with symbolized keys.
|
|
@@ -113,7 +119,7 @@ module PostgreSQLCursor
|
|
|
113
119
|
if ::ActiveRecord::VERSION::MAJOR < 4
|
|
114
120
|
model = klass.send(:instantiate, row)
|
|
115
121
|
else
|
|
116
|
-
@column_types ||= column_types
|
|
122
|
+
@column_types ||= column_types(klass)
|
|
117
123
|
model = klass.send(:instantiate, row, @column_types)
|
|
118
124
|
end
|
|
119
125
|
block.call(model)
|
|
@@ -147,7 +153,7 @@ module PostgreSQLCursor
|
|
|
147
153
|
if ::ActiveRecord::VERSION::MAJOR < 4
|
|
148
154
|
klass.send(:instantiate, row)
|
|
149
155
|
else
|
|
150
|
-
@column_types ||= column_types
|
|
156
|
+
@column_types ||= column_types(klass)
|
|
151
157
|
klass.send(:instantiate, row, @column_types)
|
|
152
158
|
end
|
|
153
159
|
end
|
|
@@ -222,7 +228,7 @@ module PostgreSQLCursor
|
|
|
222
228
|
row
|
|
223
229
|
end
|
|
224
230
|
|
|
225
|
-
def column_types
|
|
231
|
+
def column_types(klass)
|
|
226
232
|
return nil if ::ActiveRecord::VERSION::MAJOR < 4
|
|
227
233
|
return @column_types if @column_types
|
|
228
234
|
|
|
@@ -239,6 +245,8 @@ module PostgreSQLCursor
|
|
|
239
245
|
# types[fname] = @connection.get_type_map.fetch(ftype)
|
|
240
246
|
end
|
|
241
247
|
|
|
248
|
+
klass.attribute_types.each_key { |k| types.delete(k) }
|
|
249
|
+
|
|
242
250
|
@column_types = types
|
|
243
251
|
end
|
|
244
252
|
|
|
@@ -292,12 +300,9 @@ module PostgreSQLCursor
|
|
|
292
300
|
# This is a value between 0.1 and 1.0 (PostgreSQL defaults to 0.1, this library defaults to 1.0)
|
|
293
301
|
# used to determine the expected fraction (percent) of result rows returned the the caller.
|
|
294
302
|
# This value determines the access path by the query planner.
|
|
295
|
-
def set_cursor_tuple_fraction
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
@cursor_tuple_fraction = frac
|
|
299
|
-
@result = @connection.execute("set cursor_tuple_fraction to #{frac}")
|
|
300
|
-
frac
|
|
303
|
+
def set_cursor_tuple_fraction
|
|
304
|
+
frac = @options.fetch(:fraction, 1.0)
|
|
305
|
+
@result = @connection.execute("set cursor_tuple_fraction to #{frac}")
|
|
301
306
|
end
|
|
302
307
|
end
|
|
303
308
|
end
|
data/test/helper.rb
CHANGED
|
@@ -12,7 +12,8 @@ ActiveRecord::Base.establish_connection(adapter: 'postgresql',
|
|
|
12
12
|
class Product < ActiveRecord::Base
|
|
13
13
|
has_many :prices
|
|
14
14
|
|
|
15
|
-
# create table
|
|
15
|
+
# create table products (id serial primary key, data varchar);
|
|
16
|
+
# create table prices (id serial primary key, product_id integer references products(id));
|
|
16
17
|
def self.generate(max=1_000)
|
|
17
18
|
max.times do |i|
|
|
18
19
|
connection.execute("insert into products values (#{i+1})")
|
|
@@ -225,4 +225,13 @@ class TestPostgresqlCursor < Minitest::Test
|
|
|
225
225
|
cursor = Product.first.prices.each_instance
|
|
226
226
|
refute cursor.instance_variable_get(:@type).loaded?
|
|
227
227
|
end
|
|
228
|
+
|
|
229
|
+
def test_size
|
|
230
|
+
r = Product.each_instance
|
|
231
|
+
assert_equal -1, r.size
|
|
232
|
+
r = Product.each_hash
|
|
233
|
+
assert_equal -1, r.size
|
|
234
|
+
r = Product.each_row
|
|
235
|
+
assert_equal -1, r.size
|
|
236
|
+
end
|
|
228
237
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: postgresql_cursor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.11
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Allen Fair
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -134,7 +133,6 @@ homepage: http://github.com/afair/postgresql_cursor
|
|
|
134
133
|
licenses:
|
|
135
134
|
- MIT
|
|
136
135
|
metadata: {}
|
|
137
|
-
post_install_message:
|
|
138
136
|
rdoc_options: []
|
|
139
137
|
require_paths:
|
|
140
138
|
- lib
|
|
@@ -149,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
149
147
|
- !ruby/object:Gem::Version
|
|
150
148
|
version: '0'
|
|
151
149
|
requirements: []
|
|
152
|
-
rubygems_version:
|
|
153
|
-
signing_key:
|
|
150
|
+
rubygems_version: 4.0.10
|
|
154
151
|
specification_version: 4
|
|
155
152
|
summary: ActiveRecord PostgreSQL Adapter extension for using a cursor to return a
|
|
156
153
|
large result set
|