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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10897155d2898ad210bce318b945f7d468873908
4
- data.tar.gz: b3e56271eee38410818e889e92d8d22411966a6f
3
+ metadata.gz: 9744ca214c289da5109fb90290641c58da8c798d
4
+ data.tar.gz: 7a3579322104b35e9db4e1a3709e45fd5534d1f2
5
5
  SHA512:
6
- metadata.gz: 3d6283c4da46766cac76785dc0b4961d1cc2d0b874a53e7767ed640ce1e55a81b0196dc8faecc079f859efbd3607013cfe76873164839758551ac23bcb933792
7
- data.tar.gz: ea96a5231e5dcaef6a6b75245c0b7318bf7ab2fe9b80de384a956bd4df91728584cd5147378d9da894fc2c53036f98ec3db488ba6bf255760711ee471275c7f7
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 format_row(row, opts = {})
23
- headers = row.headers.map do |h|
24
- h.downcase.gsub(/\./, '_')
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
- values = row.headers.map do |h|
28
- "'#{row[h]}'"
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
- h_str = headers.join(', ')
32
- v_str = values.join(', ')
33
- "INSERT INTO #{opts[:table]}(#{h_str}) VALUES(#{v_str});"
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 convert(paths, opts = {})
37
- with_paths(paths, opts) do |data|
38
- puts format_row(data[:row], opts)
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(header)
53
- header.map do |h|
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)
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Csv2Psql module
4
4
  module Csv2Psql
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
  end
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Korcak