csv2psql 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,115 +5,54 @@ require 'multi_json'
5
5
  require 'pathname'
6
6
  require 'pp'
7
7
 
8
- require_relative '../version'
8
+ require_relative '../analyzer/analyzer'
9
+ require_relative '../generator/generator'
10
+ require_relative '../helpers/csv_helper'
9
11
  require_relative '../helpers/erb_helper'
12
+ require_relative '../output/output'
13
+ require_relative '../version'
10
14
 
11
15
  module Csv2Psql
12
16
  # Csv2Psql processor class
13
- class Processor # rubocop:disable Metrics/ClassLength
14
- attr_reader :path
17
+ class Processor
18
+ attr_reader :analyzer, :generator, :output, :path
15
19
 
16
20
  DEFAULT_OPTIONS = {
17
- 'create-table' => false,
18
- 'drop-table' => false,
19
21
  delimiter: ',',
20
22
  header: true,
21
23
  separator: :auto,
22
- table: 'my_table',
23
- transaction: true,
24
+ transaction: false,
24
25
  quote: '"'
25
26
  }
26
27
 
27
- TABLE_FUNCTIONS = {
28
- 'drop-table' => :drop_table,
29
- 'create-table' => :create_table,
30
- 'truncate-table' => :truncate_table
31
- }
32
-
33
- BASE_DIR = File.join(File.dirname(__FILE__), '..', '..', '..')
34
- TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
35
- CREATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'create_table.sql.erb')
36
- DROP_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'drop_table.sql.erb')
37
- HEADER_TEMPLATE = File.join(TEMPLATE_DIR, 'header.sql.erb')
38
- TRUNCATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'truncate_table.sql.erb')
39
-
40
- def convert(paths, opts = {})
41
- with_paths(paths, opts) do |data|
42
- puts format_row(data[:row], opts)
43
- end
28
+ def initialize
29
+ @output = Output.new
30
+ @generator = Generator.new(@output)
31
+ @analyzer = Analyzer.new
44
32
  end
45
33
 
46
- def create_erb_context(path, row, opts = {})
47
- header = get_header(row, opts)
48
- columns = get_columns(row, opts, header)
49
- {
50
- path: path,
51
- header: header,
52
- columns: columns,
53
- table: opts[:table] || DEFAULT_OPTIONS[:table]
54
- }
55
- end
56
-
57
- def create_header(path, row, opts = {})
58
- ctx = create_erb_context(path, row, opts)
59
- erb = ErbHelper.new
60
- erb.process(HEADER_TEMPLATE, ctx)
61
- end
62
-
63
- def create_table(path, row, opts = {})
64
- ctx = create_erb_context(path, row, opts)
65
- erb = ErbHelper.new
66
- erb.process(CREATE_TABLE_TEMPLATE, ctx)
67
- end
68
-
69
- def create_sql_script(path, row, opts = {})
70
- return unless @first_row
71
-
72
- puts create_header(path, row, opts)
73
-
74
- TABLE_FUNCTIONS.each do |k, v|
75
- t = DEFAULT_OPTIONS[k]
76
- t = opts[k] unless opts[k].nil?
77
- puts send(v, path, row, opts) if t
34
+ def analyze(paths, opts = {})
35
+ with_paths(paths, opts) do |data|
36
+ path = data[:path]
37
+ row = data[:row]
38
+ analyzer.analyze(path, row, opts)
78
39
  end
79
-
80
- @first_row = false
40
+ analyzer
81
41
  end
82
42
 
83
- def drop_table(path, row, opts = {})
84
- ctx = create_erb_context(path, row, opts)
85
- erb = ErbHelper.new
86
- erb.process(DROP_TABLE_TEMPLATE, ctx)
87
- end
88
43
 
89
- def format_row(row, opts = {})
90
- header = get_header(row, opts)
91
- columns = get_columns(row, opts, header).join(', ')
92
- values = get_values(row, opts, header).join(', ')
93
- "INSERT INTO #{opts[:table]}(#{columns}) VALUES(#{values});"
94
- end
95
-
96
- def get_header(row, opts = {})
97
- if opts[:header]
98
- row.headers
99
- else
100
- row.map.with_index { |_item, i| i }
101
- end
102
- end
44
+ def convert(paths, opts = {})
45
+ file_headers = {}
103
46
 
104
- def get_columns(row, opts = {}, header = get_header(row, opts))
105
- if opts[:header]
106
- header.map { |h| sanitize_header(h) }
107
- else
108
- row.map.with_index do |_item, i|
109
- "col_#{i}"
47
+ with_paths(paths, opts) do |data|
48
+ path = data[:path]
49
+ row = data[:row]
50
+ unless file_headers.key?(path)
51
+ generator.create_sql_script(path, row, opts)
52
+ file_headers[path] = true
110
53
  end
111
- end
112
- end
113
54
 
114
- def get_values(row, opts = {}, header = get_header(row, opts))
115
- header.map do |h|
116
- "'#{row[h]}'"
55
+ output.write generator.format_row(row, opts)
117
56
  end
118
57
  end
119
58
 
@@ -127,18 +66,8 @@ module Csv2Psql
127
66
  }
128
67
  end
129
68
 
130
- def sanitize_header(header_column)
131
- header_column.downcase.gsub(/[^0-9a-z ]/i, '_')
132
- end
133
-
134
- def truncate_table(path, row, opts = {})
135
- ctx = create_erb_context(path, row, opts)
136
- erb = ErbHelper.new
137
- erb.process(TRUNCATE_TABLE_TEMPLATE, ctx)
138
- end
139
-
140
69
  def with_path(path, opts = {}, &block)
141
- puts 'BEGIN;' if opts[:transaction]
70
+ output.write 'BEGIN;' if opts[:transaction]
142
71
  csv_opts = merge_csv_options(opts)
143
72
  @first_row = true
144
73
  CSV.open(path, 'rt', csv_opts) do |csv|
@@ -146,7 +75,7 @@ module Csv2Psql
146
75
  with_row(path, row, opts, &block)
147
76
  end
148
77
  end
149
- puts 'COMMIT;' if opts[:transaction]
78
+ output.write 'COMMIT;' if opts[:transaction]
150
79
  end
151
80
 
152
81
  def with_paths(paths, opts = {}, &block)
@@ -156,9 +85,7 @@ module Csv2Psql
156
85
  end
157
86
  end
158
87
 
159
- def with_row(path, row, opts = {}, &block)
160
- create_sql_script(path, row, opts)
161
-
88
+ def with_row(path, row, _opts = {}, &block)
162
89
  args = { path: path, row: row }
163
90
  block.call(args) if block_given?
164
91
  end
@@ -2,5 +2,6 @@
2
2
 
3
3
  # Csv2Psql module
4
4
  module Csv2Psql
5
- VERSION = '0.0.6'
5
+ CODENAME = 'Blurry bat'
6
+ VERSION = '0.0.8'
6
7
  end
@@ -1,2 +1,2 @@
1
- -- Table <%= ctx[:table] + "\n" %>
1
+ -- Table: <%= ctx[:table] + "\n" %>
2
2
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv2psql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Korcak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-31 00:00:00.000000000 Z
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli
@@ -192,17 +192,28 @@ files:
192
192
  - LICENSE
193
193
  - README.md
194
194
  - Rakefile
195
+ - TODO.md
195
196
  - bin/csv2psql
197
+ - data/cia-data-all.csv
196
198
  - data/sample.csv
197
199
  - lib/csv2psql.rb
200
+ - lib/csv2psql/analyzer/analyzer.rb
201
+ - lib/csv2psql/analyzer/types/bigint.rb
202
+ - lib/csv2psql/analyzer/types/decimal.rb
198
203
  - lib/csv2psql/cli/app.rb
199
204
  - lib/csv2psql/cli/cli.rb
205
+ - lib/csv2psql/cli/cmd/analyze_cmd.rb
200
206
  - lib/csv2psql/cli/cmd/convert_cmd.rb
201
207
  - lib/csv2psql/cli/cmd/version_cmd.rb
202
208
  - lib/csv2psql/cli/shared.rb
203
209
  - lib/csv2psql/convert/convert.rb
210
+ - lib/csv2psql/dialects/psql.rb
211
+ - lib/csv2psql/extensions/string.rb
212
+ - lib/csv2psql/generator/generator.rb
213
+ - lib/csv2psql/helpers/csv_helper.rb
204
214
  - lib/csv2psql/helpers/erb_helper.rb
205
215
  - lib/csv2psql/lib.rb
216
+ - lib/csv2psql/output/output.rb
206
217
  - lib/csv2psql/processor/processor.rb
207
218
  - lib/csv2psql/version.rb
208
219
  - spec/spec_helper.rb