pg_csv 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -3
- data/Gemfile.lock +1 -2
- data/README.md +1 -1
- data/lib/pg_csv.rb +33 -16
- data/lib/pg_csv_version.rb +1 -1
- data/{benchmark → spec/benchmark}/bench.rb +10 -3
- metadata +76 -85
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -40,7 +40,7 @@ PgCsv.new(:sql => User.good.to_sql).export('a1.csv')
|
|
40
40
|
PgCsv.new(:sql => sql).export('a2.gz', :type => :gzip)
|
41
41
|
PgCsv.new(:sql => sql).export('a3.csv', :temp_file => true)
|
42
42
|
PgCsv.new(:sql => sql, :type => :plain).export
|
43
|
-
File.open("a4.csv", 'a'){|f|
|
43
|
+
File.open("a4.csv", 'a'){|f| PgCsv.new(:sql => "select * from users").\
|
44
44
|
export(f, :type => :stream) }
|
45
45
|
PgCsv.new(:sql => sql).export('a5.csv', :delimiter => "\t")
|
46
46
|
PgCsv.new(:sql => sql).export('a6.csv', :header => true)
|
data/lib/pg_csv.rb
CHANGED
@@ -7,30 +7,38 @@ class PgCsv
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# do export :to - filename or stream
|
10
|
-
def export(to = nil, opts = {}, &
|
11
|
-
@
|
10
|
+
def export(to = nil, opts = {}, &row_proc)
|
11
|
+
@row_proc = row_proc
|
12
12
|
@local_options = opts.symbolize_keys
|
13
13
|
|
14
14
|
raise ":connection should be" unless connection
|
15
15
|
raise ":sql should be" unless sql
|
16
16
|
|
17
|
-
with_temp_file(to, temp_file, temp_dir) do |
|
18
|
-
export_to(
|
17
|
+
with_temp_file?(to, temp_file, temp_dir) do |dest|
|
18
|
+
export_to(dest)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
protected
|
23
23
|
|
24
|
-
def with_temp_file(
|
24
|
+
def self.with_temp_file(dest, tmp_dir = '/tmp', &block)
|
25
|
+
require 'fileutils'
|
26
|
+
require 'tempfile'
|
27
|
+
|
28
|
+
tempfile = Tempfile.new("pg_csv", tmp_dir)
|
29
|
+
yield(tempfile.path)
|
30
|
+
|
31
|
+
FileUtils.mv(tempfile.path, dest)
|
32
|
+
end
|
33
|
+
|
34
|
+
def with_temp_file?(to, use_temp_file, tmp_dir)
|
25
35
|
if use_temp_file && [:file, :gzip].include?(type)
|
26
36
|
check_str(to)
|
37
|
+
|
38
|
+
self.class.with_temp_file(to, tmp_dir) do |filename|
|
39
|
+
yield(filename)
|
40
|
+
end
|
27
41
|
|
28
|
-
require 'fileutils'
|
29
|
-
require 'tempfile'
|
30
|
-
|
31
|
-
tempfile = Tempfile.new("pg_csv", tmp_dir)
|
32
|
-
yield(tempfile.path)
|
33
|
-
FileUtils.mv(tempfile.path, to)
|
34
42
|
info "<=== moving export to #{to}"
|
35
43
|
else
|
36
44
|
yield(to)
|
@@ -66,7 +74,7 @@ protected
|
|
66
74
|
|
67
75
|
when :yield
|
68
76
|
# not real saving anywhere, just yield each record
|
69
|
-
raise "
|
77
|
+
raise "row_proc should be" unless @row_proc
|
70
78
|
result = load_data{|_|}
|
71
79
|
end
|
72
80
|
|
@@ -105,12 +113,21 @@ protected
|
|
105
113
|
info "<= query"
|
106
114
|
|
107
115
|
info "=> write data"
|
108
|
-
|
116
|
+
if columns_str
|
117
|
+
yield(@row_proc ? @row_proc[columns_str] : columns_str)
|
118
|
+
end
|
109
119
|
|
110
120
|
count = 0
|
111
|
-
|
112
|
-
|
113
|
-
|
121
|
+
if @row_proc
|
122
|
+
while row = raw.get_copy_data()
|
123
|
+
yield(@row_proc[row])
|
124
|
+
count += 1
|
125
|
+
end
|
126
|
+
else
|
127
|
+
while row = raw.get_copy_data()
|
128
|
+
yield(row)
|
129
|
+
count += 1
|
130
|
+
end
|
114
131
|
end
|
115
132
|
info "<= write data"
|
116
133
|
|
data/lib/pg_csv_version.rb
CHANGED
@@ -20,9 +20,16 @@ class PgCsv
|
|
20
20
|
def load_data
|
21
21
|
n = o(:times).to_i
|
22
22
|
c = 0
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
if @row_proc
|
24
|
+
n.times do
|
25
|
+
c += 1
|
26
|
+
yield(@row_proc["#{c},#{c*2},#{c * 249},#{rand(100)},#{rand(n)},blablabla,hahah,ahah,ahaha,ahahah,ah,1.55234143\n"])
|
27
|
+
end
|
28
|
+
else
|
29
|
+
n.times do
|
30
|
+
c += 1
|
31
|
+
yield("#{c},#{c*2},#{c * 249},#{rand(100)},#{rand(n)},blablabla,hahah,ahah,ahaha,ahahah,ah,1.55234143\n")
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
metadata
CHANGED
@@ -1,134 +1,125 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_csv
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Makarchev Konstantin
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: pg
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :runtime
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: activerecord
|
36
23
|
prerelease: false
|
37
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
38
33
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
46
38
|
type: :runtime
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: rspec
|
50
39
|
prerelease: false
|
51
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
52
49
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
60
54
|
type: :development
|
61
|
-
version_requirements: *id003
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: rake
|
64
55
|
prerelease: false
|
65
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
66
65
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
segments:
|
72
|
-
- 0
|
73
|
-
version: "0"
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
74
70
|
type: :development
|
75
|
-
|
76
|
-
|
77
|
-
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
|
79
|
+
on millions rows.
|
80
|
+
email:
|
78
81
|
- kostya27@gmail.com
|
79
82
|
executables: []
|
80
|
-
|
81
83
|
extensions: []
|
82
|
-
|
83
84
|
extra_rdoc_files: []
|
84
|
-
|
85
|
-
files:
|
85
|
+
files:
|
86
86
|
- .gitignore
|
87
87
|
- Gemfile
|
88
88
|
- Gemfile.lock
|
89
89
|
- MIT-LICENSE
|
90
90
|
- README.md
|
91
91
|
- Rakefile
|
92
|
-
- benchmark/bench.rb
|
93
92
|
- lib/pg_csv.rb
|
94
93
|
- lib/pg_csv_version.rb
|
95
94
|
- pg_csv.gemspec
|
95
|
+
- spec/benchmark/bench.rb
|
96
96
|
- spec/pg_csv_spec.rb
|
97
97
|
- spec/spec_helper.rb
|
98
98
|
- spec/spec_support.rb
|
99
99
|
- spec/tmp/.gitkeep
|
100
100
|
homepage: http://github.com/kostya/pg_csv
|
101
101
|
licenses: []
|
102
|
-
|
103
102
|
post_install_message:
|
104
103
|
rdoc_options: []
|
105
|
-
|
106
|
-
require_paths:
|
104
|
+
require_paths:
|
107
105
|
- lib
|
108
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
107
|
none: false
|
110
|
-
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
|
114
|
-
|
115
|
-
- 0
|
116
|
-
version: "0"
|
117
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
113
|
none: false
|
119
|
-
requirements:
|
120
|
-
- -
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
|
123
|
-
segments:
|
124
|
-
- 0
|
125
|
-
version: "0"
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
126
118
|
requirements: []
|
127
|
-
|
128
119
|
rubyforge_project:
|
129
120
|
rubygems_version: 1.8.24
|
130
121
|
signing_key:
|
131
122
|
specification_version: 3
|
132
|
-
summary: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
|
123
|
+
summary: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
|
124
|
+
on millions rows.
|
133
125
|
test_files: []
|
134
|
-
|