postgresql_cursor 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +9 -9
- data/README.md +12 -13
- data/lib/postgresql_cursor/active_record/relation/cursor_iterators.rb +17 -2
- data/lib/postgresql_cursor/version.rb +1 -1
- 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: 01a81f4aa5a5a18d0b26ad2bc2ae9d3b7d02dcb5
|
4
|
+
data.tar.gz: f8b3ca7158110a45ba7f28797a4d0c970f11bdb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 204072400a16ff6934d82d03b95b3bca16ee047bd2676e00fd799de199e6fb5e6e89dc0935cb3b58fc36279e0957ebe63394c0cfac6e5d7051a434aa0d78045d
|
7
|
+
data.tar.gz: 1454bea0121abc656677f69150581ee04884717eed3b53bae7097cc555ec23312f5b3469cf74628f21fe16b9df1ab2b07999c678ea7cfa615ee68a8aa35dd749
|
data/Gemfile.lock
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
postgresql_cursor (0.5.
|
4
|
+
postgresql_cursor (0.5.1)
|
5
5
|
activerecord (>= 3.2.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (4.1.
|
11
|
-
activesupport (= 4.1.
|
10
|
+
activemodel (4.1.5)
|
11
|
+
activesupport (= 4.1.5)
|
12
12
|
builder (~> 3.1)
|
13
|
-
activerecord (4.1.
|
14
|
-
activemodel (= 4.1.
|
15
|
-
activesupport (= 4.1.
|
13
|
+
activerecord (4.1.5)
|
14
|
+
activemodel (= 4.1.5)
|
15
|
+
activesupport (= 4.1.5)
|
16
16
|
arel (~> 5.0.0)
|
17
|
-
activesupport (4.1.
|
17
|
+
activesupport (4.1.5)
|
18
18
|
i18n (~> 0.6, >= 0.6.9)
|
19
19
|
json (~> 1.7, >= 1.7.7)
|
20
20
|
minitest (~> 5.1)
|
@@ -22,13 +22,13 @@ GEM
|
|
22
22
|
tzinfo (~> 1.1)
|
23
23
|
arel (5.0.1.20140414130214)
|
24
24
|
builder (3.2.2)
|
25
|
-
i18n (0.6.
|
25
|
+
i18n (0.6.11)
|
26
26
|
json (1.8.1)
|
27
27
|
minitest (5.3.3)
|
28
28
|
pg (0.17.1)
|
29
29
|
rake (10.3.1)
|
30
30
|
thread_safe (0.3.4)
|
31
|
-
tzinfo (1.2.
|
31
|
+
tzinfo (1.2.2)
|
32
32
|
thread_safe (~> 0.1)
|
33
33
|
|
34
34
|
PLATFORMS
|
data/README.md
CHANGED
@@ -43,19 +43,6 @@ Product.each_row_by_sql("select * from products") { |hash| Product.process(hash)
|
|
43
43
|
Product.each_instance_by_sql("select * from products") { |product| product.process }
|
44
44
|
```
|
45
45
|
|
46
|
-
###PostgreSQLCursor is an Enumerable
|
47
|
-
|
48
|
-
If you do not pass in a block, the cursor is returned, which mixes in the Enumerable
|
49
|
-
libary. With that, you can pass it around, or chain in the awesome enumerable things
|
50
|
-
like `map` and `reduce`. Furthermore, the cursors already act as `lazy`, but you can
|
51
|
-
also chain in `lazy` when you want to keep the memory footprint small for rest of the process.
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
Product.each_row.map {|r| r["id"].to_i } #=> [1, 2, 3, ...]
|
55
|
-
Product.each_instance.map {|r| r.id }.each {|id| p id } #=> [1, 2, 3, ...]
|
56
|
-
Product.each_instance.lazy.inject(0) {|sum,r| sum + r.quantity } #=> 499500
|
57
|
-
```
|
58
|
-
|
59
46
|
All these methods take an options hash to control things more:
|
60
47
|
|
61
48
|
block_size:n The number of rows to fetch from the database each time (default 1000)
|
@@ -74,6 +61,18 @@ Notes:
|
|
74
61
|
* Aliases each_hash and each_hash_by_sql are provided for each_row and each_row_by_sql
|
75
62
|
if you prefer to express what types are being returned.
|
76
63
|
|
64
|
+
###PostgreSQLCursor is an Enumerable
|
65
|
+
|
66
|
+
If you do not pass in a block, the cursor is returned, which mixes in the Enumerable
|
67
|
+
libary. With that, you can pass it around, or chain in the awesome enumerable things
|
68
|
+
like `map` and `reduce`. Furthermore, the cursors already act as `lazy`, but you can
|
69
|
+
also chain in `lazy` when you want to keep the memory footprint small for rest of the process.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Product.each_row.map {|r| r["id"].to_i } #=> [1, 2, 3, ...]
|
73
|
+
Product.each_instance.map {|r| r.id }.each {|id| p id } #=> [1, 2, 3, ...]
|
74
|
+
Product.each_instance.lazy.inject(0) {|sum,r| sum + r.quantity } #=> 499500
|
75
|
+
```
|
77
76
|
###Hashes vs. Instances
|
78
77
|
|
79
78
|
The each_row method returns the Hash of strings for speed (as this allows you to process a lot of rows).
|
@@ -20,7 +20,7 @@ module PostgreSQLCursor
|
|
20
20
|
# Returns the number of rows yielded to the block
|
21
21
|
def each_row(options={}, &block)
|
22
22
|
options = {:connection => self.connection}.merge(options)
|
23
|
-
cursor = PostgreSQLCursor::Cursor.new(
|
23
|
+
cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, options)
|
24
24
|
return cursor.each_row(&block) if block_given?
|
25
25
|
cursor
|
26
26
|
end
|
@@ -37,7 +37,7 @@ module PostgreSQLCursor
|
|
37
37
|
# Returns the number of rows yielded to the block
|
38
38
|
def each_instance(options={}, &block)
|
39
39
|
options = {:connection => self.connection}.merge(options)
|
40
|
-
cursor = PostgreSQLCursor::Cursor.new(
|
40
|
+
cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, options)
|
41
41
|
return cursor.each_instance(self, &block) if block_given?
|
42
42
|
cursor.iterate_type(self)
|
43
43
|
end
|
@@ -58,6 +58,21 @@ module PostgreSQLCursor
|
|
58
58
|
end
|
59
59
|
alias :pluck_instance :pluck_instances
|
60
60
|
|
61
|
+
private
|
62
|
+
|
63
|
+
# Returns sql string like #to_sql, but with bind parameters interpolated.
|
64
|
+
# ActiveRecord sets up query as prepared statements with bind variables.
|
65
|
+
# Cursors will prepare statements regardless.
|
66
|
+
def to_unprepared_sql
|
67
|
+
if self.connection.respond_to?(:unprepared_statement)
|
68
|
+
self.connection.unprepared_statement do
|
69
|
+
to_sql
|
70
|
+
end
|
71
|
+
else
|
72
|
+
to_sql
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
61
76
|
end
|
62
77
|
end
|
63
78
|
end
|
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.5.
|
4
|
+
version: 0.5.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: 2014-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|