postgresql_cursor 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/postgresql_cursor/cursor.rb +35 -4
- data/lib/postgresql_cursor/version.rb +1 -1
- data/test/test_postgresql_cursor.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df0425fd30e37c9d61b270416c9892375b0abe69
|
4
|
+
data.tar.gz: f59a3b1291c6476a16b08b084118be22b76020f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5decdc3d118f14eac9d93d46c34bb3b11f51f784bf5ea5a6825aaffe9f85ae23d9094992ef938f363d003435014ef7d771d5d5db835cac22e811248cc881a38
|
7
|
+
data.tar.gz: 59f0e52d29bccd10d3b9e693d4c40e2920672eefcd3f103309b4f8a4c96d8aa055c9326c03dba11a21b2bb29a05896454d6c37730054c5f85f17c24c1bff23cf
|
data/README.md
CHANGED
@@ -144,13 +144,13 @@ ActiveRecord is designed and optimized for web performance. In a web transaction
|
|
144
144
|
around 20 rows is returned to the user. When you do this
|
145
145
|
|
146
146
|
```ruby
|
147
|
-
Product.
|
147
|
+
Product.where("id>0").each { |product| product.process }
|
148
148
|
```
|
149
149
|
|
150
150
|
The database returns all matching result set rows to ActiveRecord, which instantiates each row with
|
151
151
|
the data returned. This function returns an array of all these rows to the caller.
|
152
152
|
|
153
|
-
|
153
|
+
Asynchronous, Background, or Offline processing may require processing a large amount of data.
|
154
154
|
When there is a very large number of rows, this requires a lot more memory to hold the data. Ruby
|
155
155
|
does not return that memory after processing the array, and the causes your process to "bloat". If you
|
156
156
|
don't have enough memory, it will cause an exception.
|
@@ -21,7 +21,6 @@ module PostgreSQLCursor
|
|
21
21
|
class Cursor
|
22
22
|
include Enumerable
|
23
23
|
attr_reader :sql, :options, :connection, :count, :result
|
24
|
-
@@cursor_seq = 0
|
25
24
|
|
26
25
|
# Public: Start a new PostgreSQL cursor query
|
27
26
|
# sql - The SQL statement with interpolated values
|
@@ -50,6 +49,8 @@ module PostgreSQLCursor
|
|
50
49
|
def iterate_type(type=nil)
|
51
50
|
if type.nil? || type == Hash
|
52
51
|
@iterate = :each_row
|
52
|
+
elsif type == Array
|
53
|
+
@iterate = :each_array
|
53
54
|
else
|
54
55
|
@iterate = :each_instance
|
55
56
|
@type = type
|
@@ -66,6 +67,8 @@ module PostgreSQLCursor
|
|
66
67
|
def each(&block)
|
67
68
|
if @iterate == :each_row
|
68
69
|
self.each_row(&block)
|
70
|
+
elsif @iterate == :each_array
|
71
|
+
self.each_array(&block)
|
69
72
|
else
|
70
73
|
self.each_instance(@type, &block)
|
71
74
|
end
|
@@ -78,6 +81,19 @@ module PostgreSQLCursor
|
|
78
81
|
end
|
79
82
|
end
|
80
83
|
|
84
|
+
def each_array(&block)
|
85
|
+
old_iterate = @iterate
|
86
|
+
@iterate = :each_array
|
87
|
+
begin
|
88
|
+
rv = self.each_tuple do |row|
|
89
|
+
block.call(row)
|
90
|
+
end
|
91
|
+
ensure
|
92
|
+
@iterate = old_iterate
|
93
|
+
end
|
94
|
+
rv
|
95
|
+
end
|
96
|
+
|
81
97
|
def each_instance(klass=nil, &block)
|
82
98
|
klass ||= @type
|
83
99
|
self.each_tuple do |row|
|
@@ -117,7 +133,7 @@ module PostgreSQLCursor
|
|
117
133
|
has_do_while = @options.has_key?(:while)
|
118
134
|
@count = 0
|
119
135
|
@column_types = nil
|
120
|
-
|
136
|
+
with_optional_transaction do
|
121
137
|
begin
|
122
138
|
open
|
123
139
|
while (row = fetch) do
|
@@ -161,7 +177,8 @@ module PostgreSQLCursor
|
|
161
177
|
# Public: Opens (actually, "declares") the cursor. Call this before fetching
|
162
178
|
def open
|
163
179
|
set_cursor_tuple_fraction
|
164
|
-
|
180
|
+
#@cursor = Digest::MD5.hexdigest(@sql)
|
181
|
+
@cursor = SecureRandom.uuid.gsub("-","")
|
165
182
|
hold = @options[:with_hold] ? 'with hold ' : ''
|
166
183
|
@result = @connection.execute("declare cursor_#{@cursor} cursor #{hold}for #{@sql}")
|
167
184
|
@block = []
|
@@ -182,7 +199,12 @@ module PostgreSQLCursor
|
|
182
199
|
def fetch_block(block_size=nil)
|
183
200
|
block_size ||= @block_size ||= @options.fetch(:block_size) { 1000 }
|
184
201
|
@result = @connection.execute("fetch #{block_size} from cursor_#{@cursor}")
|
185
|
-
|
202
|
+
|
203
|
+
if @iterate == :each_array
|
204
|
+
@block = @result.each_row.collect {|row| row }
|
205
|
+
else
|
206
|
+
@block = @result.collect {|row| row }
|
207
|
+
end
|
186
208
|
end
|
187
209
|
|
188
210
|
# Public: Closes the cursor
|
@@ -190,6 +212,15 @@ module PostgreSQLCursor
|
|
190
212
|
@connection.execute("close cursor_#{@cursor}")
|
191
213
|
end
|
192
214
|
|
215
|
+
# Private: Open transaction unless with_hold option, specified
|
216
|
+
def with_optional_transaction
|
217
|
+
if @options[:with_hold]
|
218
|
+
yield
|
219
|
+
else
|
220
|
+
@connection.transaction { yield }
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
193
224
|
# Private: Sets the PostgreSQL cursor_tuple_fraction value = 1.0 to assume all rows will be fetched
|
194
225
|
# This is a value between 0.1 and 1.0 (PostgreSQL defaults to 0.1, this library defaults to 1.0)
|
195
226
|
# used to determine the expected fraction (percent) of result rows returned the the caller.
|
@@ -31,6 +31,14 @@ class TestPostgresqlCursor < Minitest::Test
|
|
31
31
|
assert_equal 1000, n
|
32
32
|
end
|
33
33
|
|
34
|
+
def test_each_array
|
35
|
+
c = PostgreSQLCursor::Cursor.new("select * from products where id = 1")
|
36
|
+
c.each_array do |ary|
|
37
|
+
assert_equal Array, ary.class
|
38
|
+
assert_equal 1, ary[0].to_i
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
34
42
|
def test_relation
|
35
43
|
nn = 0
|
36
44
|
Product.where("id>0").each_row {|r| nn += 1 }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allen Fair
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02
|
11
|
+
date: 2016-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|