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 CHANGED
@@ -1,5 +1,3 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gemspec
4
-
5
- gem 'activerecord', :require => "active_record"
3
+ gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_csv (0.1.3)
4
+ pg_csv (0.1.4)
5
5
  activerecord
6
6
  pg
7
7
 
@@ -40,7 +40,6 @@ PLATFORMS
40
40
  ruby
41
41
 
42
42
  DEPENDENCIES
43
- activerecord
44
43
  pg_csv!
45
44
  rake
46
45
  rspec
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| FastPgCsv.new(:sql => "select * from users").\
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)
@@ -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 = {}, &block)
11
- @block = block || Proc.new{|x|x}
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 |_to|
18
- export_to(_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(to, use_temp_file, tmp_dir)
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 "block should be" unless @block
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
- yield(@block[columns_str]) if columns_str
116
+ if columns_str
117
+ yield(@row_proc ? @row_proc[columns_str] : columns_str)
118
+ end
109
119
 
110
120
  count = 0
111
- while row = raw.get_copy_data()
112
- yield(@block[row])
113
- count += 1
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
 
@@ -1,3 +1,3 @@
1
1
  class PgCsv
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -20,9 +20,16 @@ class PgCsv
20
20
  def load_data
21
21
  n = o(:times).to_i
22
22
  c = 0
23
- n.times do
24
- c += 1
25
- yield(@block["#{c},#{c*2},#{c * 249},#{rand(100)},#{rand(n)},blablabla,hahah,ahah,ahaha,ahahah,ah,1.55234143\n"])
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
- hash: 29
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
- date: 2012-06-13 00:00:00 Z
19
- dependencies:
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
- prerelease: false
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
- hash: 3
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
- requirement: &id002 !ruby/object:Gem::Requirement
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
- hash: 3
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
- requirement: &id003 !ruby/object:Gem::Requirement
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
- hash: 3
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
- requirement: &id004 !ruby/object:Gem::Requirement
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
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
74
70
  type: :development
75
- version_requirements: *id004
76
- description: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective on millions rows.
77
- email:
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
- hash: 3
114
- segments:
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
- hash: 3
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 on millions rows.
123
+ summary: Fast AR/PostgreSQL csv export. Used pg function 'copy to csv'. Effective
124
+ on millions rows.
133
125
  test_files: []
134
-