pg_csv 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/pg_csv.rb +28 -13
- data/lib/pg_csv_version.rb +1 -1
- data/pg_csv.gemspec +1 -1
- data/spec/pg_csv_spec.rb +11 -0
- data/spec/spec_support.rb +1 -1
- metadata +75 -84
data/.gitignore
CHANGED
data/lib/pg_csv.rb
CHANGED
@@ -19,17 +19,17 @@ class PgCsv
|
|
19
19
|
# => :file - just save to file * default
|
20
20
|
|
21
21
|
def initialize(opts = {})
|
22
|
-
@options = opts
|
22
|
+
@options = opts.symbolize_keys
|
23
23
|
end
|
24
24
|
|
25
25
|
# do export :to - filename or stream
|
26
26
|
def export(to, opts = {})
|
27
|
-
@local_options = opts
|
27
|
+
@local_options = opts.symbolize_keys
|
28
28
|
|
29
29
|
raise ":connection should be" unless connection
|
30
|
-
raise ":sql should be" unless
|
30
|
+
raise ":sql should be" unless sql
|
31
31
|
|
32
|
-
with_temp_file(to,
|
32
|
+
with_temp_file(to, temp_file, temp_dir) do |_to|
|
33
33
|
export_to(_to)
|
34
34
|
end
|
35
35
|
end
|
@@ -38,12 +38,12 @@ protected
|
|
38
38
|
|
39
39
|
def with_temp_file(to, use_temp_file, tmp_dir)
|
40
40
|
if use_temp_file
|
41
|
-
|
41
|
+
check_str(to)
|
42
42
|
|
43
43
|
require 'fileutils'
|
44
44
|
require 'tempfile'
|
45
45
|
|
46
|
-
tempfile = Tempfile.new("pg_csv", tmp_dir
|
46
|
+
tempfile = Tempfile.new("pg_csv", tmp_dir)
|
47
47
|
yield(tempfile.path)
|
48
48
|
FileUtils.mv(tempfile.path, to)
|
49
49
|
info "<=== moving export to #{to}"
|
@@ -53,8 +53,7 @@ protected
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def export_to(to)
|
56
|
-
|
57
|
-
|
56
|
+
|
58
57
|
start = Time.now
|
59
58
|
info "===> start generate export #{to}, type: #{type}"
|
60
59
|
|
@@ -63,11 +62,11 @@ protected
|
|
63
62
|
case type
|
64
63
|
|
65
64
|
when :file
|
66
|
-
|
65
|
+
check_str(to)
|
67
66
|
File.open(to, 'w', &exporter)
|
68
67
|
|
69
68
|
when :gzip
|
70
|
-
|
69
|
+
check_str(to)
|
71
70
|
Zlib::GzipWriter.open(to, &exporter)
|
72
71
|
|
73
72
|
when :stream
|
@@ -86,13 +85,17 @@ protected
|
|
86
85
|
result
|
87
86
|
end
|
88
87
|
|
89
|
-
def
|
88
|
+
def check_str(to)
|
90
89
|
raise "'to' should be an string" unless to.is_a?(String)
|
91
90
|
end
|
92
91
|
|
92
|
+
def exporter
|
93
|
+
method(:export_to_stream).to_proc
|
94
|
+
end
|
95
|
+
|
93
96
|
def export_to_stream(stream)
|
94
97
|
write_csv(stream)
|
95
|
-
stream.flush
|
98
|
+
stream.flush if stream.respond_to?(:flush)
|
96
99
|
end
|
97
100
|
|
98
101
|
def write_csv(stream)
|
@@ -129,7 +132,7 @@ protected
|
|
129
132
|
def query
|
130
133
|
<<-SQL
|
131
134
|
COPY (
|
132
|
-
#{
|
135
|
+
#{sql}
|
133
136
|
) TO STDOUT
|
134
137
|
WITH CSV
|
135
138
|
DELIMITER '#{delimiter}'
|
@@ -182,4 +185,16 @@ DELIMITER '#{delimiter}'
|
|
182
185
|
o(:delimiter) || ','
|
183
186
|
end
|
184
187
|
|
188
|
+
def sql
|
189
|
+
o(:sql)
|
190
|
+
end
|
191
|
+
|
192
|
+
def temp_file
|
193
|
+
o(:temp_file)
|
194
|
+
end
|
195
|
+
|
196
|
+
def temp_dir
|
197
|
+
o(:temp_dir) || '/tmp'
|
198
|
+
end
|
199
|
+
|
185
200
|
end
|
data/lib/pg_csv_version.rb
CHANGED
data/pg_csv.gemspec
CHANGED
data/spec/pg_csv_spec.rb
CHANGED
@@ -135,4 +135,15 @@ describe PgCsv do
|
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
|
+
it "custom prepare row" do
|
139
|
+
e = PgCsv.new(:sql => @sql)
|
140
|
+
|
141
|
+
def e.prepare_row(row)
|
142
|
+
row.split(",").join("-|-")
|
143
|
+
end
|
144
|
+
|
145
|
+
e.export(@name)
|
146
|
+
with_file(@name){|d| d.should == "4-|-5-|-6\n1-|-2-|-3\n" }
|
147
|
+
end
|
148
|
+
|
138
149
|
end
|
data/spec/spec_support.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
|
3
|
-
conn = {'adapter' => 'postgresql', 'database' => 'pgcsv_test'
|
3
|
+
conn = {'adapter' => 'postgresql', 'database' => 'pgcsv_test'}
|
4
4
|
ActiveRecord::Base.establish_connection conn
|
5
5
|
|
6
6
|
class Test < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,88 +1,88 @@
|
|
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.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 1
|
10
|
-
version: 0.1.1
|
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-12 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
|
@@ -98,36 +98,27 @@ files:
|
|
98
98
|
- spec/tmp/.gitkeep
|
99
99
|
homepage: http://github.com/kostya/pg_csv
|
100
100
|
licenses: []
|
101
|
-
|
102
101
|
post_install_message:
|
103
102
|
rdoc_options: []
|
104
|
-
|
105
|
-
require_paths:
|
103
|
+
require_paths:
|
106
104
|
- lib
|
107
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
106
|
none: false
|
109
|
-
requirements:
|
110
|
-
- -
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
|
113
|
-
|
114
|
-
- 0
|
115
|
-
version: "0"
|
116
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
112
|
none: false
|
118
|
-
requirements:
|
119
|
-
- -
|
120
|
-
- !ruby/object:Gem::Version
|
121
|
-
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
version: "0"
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
125
117
|
requirements: []
|
126
|
-
|
127
118
|
rubyforge_project:
|
128
119
|
rubygems_version: 1.8.24
|
129
120
|
signing_key:
|
130
121
|
specification_version: 3
|
131
|
-
summary: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
|
122
|
+
summary: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
|
123
|
+
on millions rows.
|
132
124
|
test_files: []
|
133
|
-
|