csv2psql 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/lib/csv2psql/cli/cmd/convert_cmd.rb +32 -2
- data/lib/csv2psql/convert/convert.rb +3 -39
- data/lib/csv2psql/processor/processor.rb +82 -0
- data/lib/csv2psql/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10897155d2898ad210bce318b945f7d468873908
|
4
|
+
data.tar.gz: b3e56271eee38410818e889e92d8d22411966a6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d6283c4da46766cac76785dc0b4961d1cc2d0b874a53e7767ed640ce1e55a81b0196dc8faecc079f859efbd3607013cfe76873164839758551ac23bcb933792
|
7
|
+
data.tar.gz: ea96a5231e5dcaef6a6b75245c0b7318bf7ab2fe9b80de384a956bd4df91728584cd5147378d9da894fc2c53036f98ec3db488ba6bf255760711ee471275c7f7
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Tool for transforming CSV into SQL statements
|
|
4
4
|
|
5
5
|
## Status
|
6
6
|
|
7
|
-
[](http://badge.fury.io/rb/csv2psql)
|
7
|
+
[](http://badge.fury.io/rb/csv2psql)
|
8
8
|
[](http://rubygems.org/gems/csv2psql)
|
9
9
|
[](https://travis-ci.org/korczis/csv2psql)
|
10
10
|
[](https://codeclimate.com/github/korczis/csv2psql)
|
data/Rakefile
CHANGED
@@ -7,10 +7,40 @@ include GLI::App
|
|
7
7
|
require_relative '../shared'
|
8
8
|
require_relative '../../convert/convert'
|
9
9
|
|
10
|
+
cmds = {
|
11
|
+
h: {
|
12
|
+
desc: 'Header row included',
|
13
|
+
default_value: true
|
14
|
+
},
|
15
|
+
|
16
|
+
d: {
|
17
|
+
desc: 'Column delimiter',
|
18
|
+
type: String, default_value: ','
|
19
|
+
},
|
20
|
+
|
21
|
+
t: {
|
22
|
+
desc: 'Table to insert to',
|
23
|
+
type: String, default_value: 'my_table'
|
24
|
+
},
|
25
|
+
|
26
|
+
q: {
|
27
|
+
desc: 'Quoting character',
|
28
|
+
type: String, default_value: '"'
|
29
|
+
},
|
30
|
+
|
31
|
+
s: {
|
32
|
+
desc: 'Line separator',
|
33
|
+
type: String, default_value: :auto
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
10
37
|
desc 'Convert csv file'
|
11
38
|
command :convert do |c|
|
12
|
-
c.switch [:h, :header],
|
13
|
-
c.flag [:d, :delimiter],
|
39
|
+
c.switch [:h, :header], cmds[:h]
|
40
|
+
c.flag [:d, :delimiter], cmds[:d]
|
41
|
+
c.flag [:t, :table], cmds[:t]
|
42
|
+
c.flag [:q, :quote], cmds[:q]
|
43
|
+
c.flag [:s, :separator], cmds[:s]
|
14
44
|
|
15
45
|
c.action do |global_options, options, args|
|
16
46
|
fail ArgumentError, 'No file to convert specified' if args.empty?
|
@@ -6,51 +6,15 @@ require 'pathname'
|
|
6
6
|
require 'pp'
|
7
7
|
|
8
8
|
require_relative '../version'
|
9
|
+
require_relative '../processor/processor'
|
9
10
|
|
10
11
|
module Csv2Psql
|
11
12
|
# Csv2Psql convert module
|
12
13
|
module Convert
|
13
|
-
DEFAULT_OPTIONS = {
|
14
|
-
delimiter: ',',
|
15
|
-
header: true
|
16
|
-
}
|
17
|
-
|
18
14
|
class << self
|
19
|
-
def format_row(row)
|
20
|
-
headers = row.headers.map do |h|
|
21
|
-
h.downcase.gsub(/\./, '_')
|
22
|
-
end
|
23
|
-
|
24
|
-
values = row.headers.map do |h|
|
25
|
-
"'#{row[h]}'"
|
26
|
-
end
|
27
|
-
|
28
|
-
"INSERT INTO aaa(#{headers.join(', ')}) VALUES(#{values.join(', ')});"
|
29
|
-
end
|
30
|
-
|
31
15
|
def convert(paths, opts = {})
|
32
|
-
|
33
|
-
paths
|
34
|
-
puts "Converting #{path}"
|
35
|
-
|
36
|
-
header = !opts[:header].nil? ? opts[:header] : DEFAULT_OPTIONS[:header]
|
37
|
-
csv_opts = {
|
38
|
-
col_sep: opts[:delimiter] || DEFAULT_OPTIONS[:delimiter],
|
39
|
-
:headers => header
|
40
|
-
}
|
41
|
-
|
42
|
-
CSV.open(path, 'rt', csv_opts) do |csv|
|
43
|
-
csv.each do |row|
|
44
|
-
puts format_row(row)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def sanitize_header(header)
|
51
|
-
header.map do |h|
|
52
|
-
h.downcase
|
53
|
-
end
|
16
|
+
p = Processor.new
|
17
|
+
p.convert(paths, opts)
|
54
18
|
end
|
55
19
|
end
|
56
20
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'multi_json'
|
5
|
+
require 'pathname'
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
require_relative '../version'
|
9
|
+
|
10
|
+
module Csv2Psql
|
11
|
+
# Csv2Psql processor class
|
12
|
+
class Processor
|
13
|
+
attr_reader :path
|
14
|
+
|
15
|
+
DEFAULT_OPTIONS = {
|
16
|
+
delimiter: ',',
|
17
|
+
header: true,
|
18
|
+
separator: :auto,
|
19
|
+
quote: '"'
|
20
|
+
}
|
21
|
+
|
22
|
+
def format_row(row, opts = {})
|
23
|
+
headers = row.headers.map do |h|
|
24
|
+
h.downcase.gsub(/\./, '_')
|
25
|
+
end
|
26
|
+
|
27
|
+
values = row.headers.map do |h|
|
28
|
+
"'#{row[h]}'"
|
29
|
+
end
|
30
|
+
|
31
|
+
h_str = headers.join(', ')
|
32
|
+
v_str = values.join(', ')
|
33
|
+
"INSERT INTO #{opts[:table]}(#{h_str}) VALUES(#{v_str});"
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert(paths, opts = {})
|
37
|
+
with_paths(paths, opts) do |data|
|
38
|
+
puts format_row(data[:row], opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def merge_csv_options(opts = {})
|
43
|
+
header = !opts[:header].nil? ? opts[:header] : DEFAULT_OPTIONS[:header]
|
44
|
+
{
|
45
|
+
col_sep: opts[:delimiter] || DEFAULT_OPTIONS[:delimiter],
|
46
|
+
headers: header,
|
47
|
+
row_sep: opts[:separator] || DEFAULT_OPTIONS[:separator],
|
48
|
+
quote_char: opts[:quote] || DEFAULT_OPTIONS[:quote]
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def sanitize_header(header)
|
53
|
+
header.map do |h|
|
54
|
+
h.downcase
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def with_path(path, opts = {}, &block)
|
59
|
+
csv_opts = merge_csv_options(opts)
|
60
|
+
CSV.open(path, 'rt', csv_opts) do |csv|
|
61
|
+
csv.each do |row|
|
62
|
+
with_row(path, row, &block)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def with_paths(paths, opts = {}, &block)
|
68
|
+
paths = [paths] unless paths.is_a?(Array)
|
69
|
+
paths.each do |path|
|
70
|
+
with_path(path, opts, &block)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def with_row(path, row, &block)
|
75
|
+
args = {
|
76
|
+
path: path,
|
77
|
+
row: row
|
78
|
+
}
|
79
|
+
block.call(args) if block_given?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/csv2psql/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv2psql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Korcak
|
@@ -202,6 +202,7 @@ files:
|
|
202
202
|
- lib/csv2psql/cli/shared.rb
|
203
203
|
- lib/csv2psql/convert/convert.rb
|
204
204
|
- lib/csv2psql/lib.rb
|
205
|
+
- lib/csv2psql/processor/processor.rb
|
205
206
|
- lib/csv2psql/version.rb
|
206
207
|
- spec/spec_helper.rb
|
207
208
|
homepage: https://github.com/korczis/csv2psql
|