csv2psql 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -0
- data/lib/csv2psql/cli/cmd/convert_cmd.rb +6 -0
- data/lib/csv2psql/processor/processor.rb +32 -15
- data/lib/csv2psql/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9744ca214c289da5109fb90290641c58da8c798d
|
4
|
+
data.tar.gz: 7a3579322104b35e9db4e1a3709e45fd5534d1f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4090eaeb4baf5498665affa16d3eecc276558a63a94519934eb5ecd8937588a44de958a00defcabd4cc3a2b6dca6d88a29a08a35d514edb28bdb46431e5478a6
|
7
|
+
data.tar.gz: 6ca73b4f105aeded11e69785842ae84d18b74060eec70be86d87c8d05ed2a4426a7fb70d3b97c2c17ef01b48d0cb38a546d32b12e9238578e3c83e15e8a662ef
|
data/README.md
CHANGED
@@ -24,6 +24,69 @@ gem install csv2psql
|
|
24
24
|
csv2psql convert data/sample.csv
|
25
25
|
```
|
26
26
|
|
27
|
+
**Global help**
|
28
|
+
|
29
|
+
```
|
30
|
+
csv2psql help
|
31
|
+
|
32
|
+
NAME
|
33
|
+
csv2psql - csv2psql 0.0.5
|
34
|
+
|
35
|
+
SYNOPSIS
|
36
|
+
csv2psql [global options] command [command options] [arguments...]
|
37
|
+
|
38
|
+
GLOBAL OPTIONS
|
39
|
+
--help - Show this message
|
40
|
+
|
41
|
+
COMMANDS
|
42
|
+
convert - Convert csv file
|
43
|
+
help - Shows a list of commands or help for one command
|
44
|
+
version - Print version info
|
45
|
+
```
|
46
|
+
|
47
|
+
**Convert help**
|
48
|
+
|
49
|
+
```
|
50
|
+
csv2psql help convert
|
51
|
+
|
52
|
+
NAME
|
53
|
+
convert - Convert csv file
|
54
|
+
|
55
|
+
SYNOPSIS
|
56
|
+
csv2psql [global options] convert [command options]
|
57
|
+
|
58
|
+
COMMAND OPTIONS
|
59
|
+
-d, --delimiter=arg - Column delimiter (default: ,)
|
60
|
+
-h, --[no-]header - Header row included (default: enabled)
|
61
|
+
-q, --quote=arg - Quoting character (default: ")
|
62
|
+
-s, --separator=arg - Line separator (default: auto)
|
63
|
+
-t, --table=arg - Table to insert to (default: my_table)
|
64
|
+
--[no-]transaction - Import in transaction block (default: enabled)
|
65
|
+
```
|
66
|
+
|
67
|
+
## Example
|
68
|
+
|
69
|
+
**Input CSV**
|
70
|
+
|
71
|
+
```
|
72
|
+
cat data/sample.csv
|
73
|
+
|
74
|
+
id,Firstname,Lastname,Address.Street,Address.City,Address.Details.Note
|
75
|
+
12345,Joe,Doe,"#2140 Taylor Street, 94133",San Francisco,Pool available
|
76
|
+
45678,Jack,Plumber,"#111 Sutter St, 94104",San Francisco,Korean Deli near to main entrance
|
77
|
+
```
|
78
|
+
|
79
|
+
**Convert CSV**
|
80
|
+
|
81
|
+
```
|
82
|
+
csv2psql convert data/sample.csv
|
83
|
+
|
84
|
+
BEGIN;
|
85
|
+
INSERT INTO my_table(id, firstname, lastname, address_street, address_city, address_details_note) VALUES('12345', 'Joe', 'Doe', '#2140 Taylor Street, 94133', 'San Francisco', 'Pool available');
|
86
|
+
INSERT INTO my_table(id, firstname, lastname, address_street, address_city, address_details_note) VALUES('45678', 'Jack', 'Plumber', '#111 Sutter St, 94104', 'San Francisco', 'Korean Deli near to main entrance');
|
87
|
+
COMMIT;
|
88
|
+
```
|
89
|
+
|
27
90
|
## Contributing to csv2psql
|
28
91
|
|
29
92
|
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -31,6 +31,11 @@ cmds = {
|
|
31
31
|
s: {
|
32
32
|
desc: 'Line separator',
|
33
33
|
type: String, default_value: :auto
|
34
|
+
},
|
35
|
+
|
36
|
+
transaction: {
|
37
|
+
desc: 'Import in transaction block',
|
38
|
+
type: String, default_value: true
|
34
39
|
}
|
35
40
|
}
|
36
41
|
|
@@ -41,6 +46,7 @@ command :convert do |c|
|
|
41
46
|
c.flag [:t, :table], cmds[:t]
|
42
47
|
c.flag [:q, :quote], cmds[:q]
|
43
48
|
c.flag [:s, :separator], cmds[:s]
|
49
|
+
c.switch [:transaction], cmds[:transaction]
|
44
50
|
|
45
51
|
c.action do |global_options, options, args|
|
46
52
|
fail ArgumentError, 'No file to convert specified' if args.empty?
|
@@ -19,23 +19,40 @@ module Csv2Psql
|
|
19
19
|
quote: '"'
|
20
20
|
}
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
22
|
+
def convert(paths, opts = {})
|
23
|
+
with_paths(paths, opts) do |data|
|
24
|
+
puts format_row(data[:row], opts)
|
25
25
|
end
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
def format_row(row, opts = {})
|
29
|
+
header = get_header(row, opts)
|
30
|
+
columns = get_columns(row, opts, header).join(', ')
|
31
|
+
values = get_values(row, opts, header).join(', ')
|
32
|
+
"INSERT INTO #{opts[:table]}(#{columns}) VALUES(#{values});"
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_header(row, opts = {})
|
36
|
+
if opts[:header]
|
37
|
+
row.headers
|
38
|
+
else
|
39
|
+
row.map.with_index { |_item, i| i }
|
29
40
|
end
|
41
|
+
end
|
30
42
|
|
31
|
-
|
32
|
-
|
33
|
-
|
43
|
+
def get_columns(row, opts = {}, header = get_header(row, opts))
|
44
|
+
if opts[:header]
|
45
|
+
header.map { |h| sanitize_header(h) }
|
46
|
+
else
|
47
|
+
row.map.with_index do |_item, i|
|
48
|
+
"col_#{i}"
|
49
|
+
end
|
50
|
+
end
|
34
51
|
end
|
35
52
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
53
|
+
def get_values(row, opts = {}, header = get_header(row, opts))
|
54
|
+
header.map do |h|
|
55
|
+
"'#{row[h]}'"
|
39
56
|
end
|
40
57
|
end
|
41
58
|
|
@@ -49,19 +66,19 @@ module Csv2Psql
|
|
49
66
|
}
|
50
67
|
end
|
51
68
|
|
52
|
-
def sanitize_header(
|
53
|
-
|
54
|
-
h.downcase
|
55
|
-
end
|
69
|
+
def sanitize_header(header_column)
|
70
|
+
header_column.downcase.gsub(/\./, '_')
|
56
71
|
end
|
57
72
|
|
58
73
|
def with_path(path, opts = {}, &block)
|
74
|
+
puts 'BEGIN;' if opts[:transaction]
|
59
75
|
csv_opts = merge_csv_options(opts)
|
60
76
|
CSV.open(path, 'rt', csv_opts) do |csv|
|
61
77
|
csv.each do |row|
|
62
78
|
with_row(path, row, &block)
|
63
79
|
end
|
64
80
|
end
|
81
|
+
puts 'COMMIT;' if opts[:transaction]
|
65
82
|
end
|
66
83
|
|
67
84
|
def with_paths(paths, opts = {}, &block)
|
data/lib/csv2psql/version.rb
CHANGED