csv2psql 0.0.1 → 0.0.2

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: c7cf1fee8ae3b9863f3e0fc1c34353d93289f167
4
- data.tar.gz: c2679fdf68f83282630724958b6345b658b5d5c0
3
+ metadata.gz: 10897155d2898ad210bce318b945f7d468873908
4
+ data.tar.gz: b3e56271eee38410818e889e92d8d22411966a6f
5
5
  SHA512:
6
- metadata.gz: 9893af6b95b14d28fb61b270585a028e00d7c176f5e3d0e1b77a9c5059f6aa70ae37f9f7522803811a3f1115f7bdbc913e4c35fe5441a51cdacabefcba11ce72
7
- data.tar.gz: a6598639b04050e381a759a099187a1c69f10ab87487cb58fb5dcc29ca1e3d3261000131bd58450f54b9af41df60eb46a92075917c313f7021209519ff2e57d6
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
- [![Gem Version](https://badge.fury.io/rb/csv2psql.svg)](http://badge.fury.io/rb/csv2psql)
7
+ [![Gem Version](https://badge.fury.io/rb/csv2psql.svg)](http://badge.fury.io/rb/csv2psql)
8
8
  [![Downloads](http://img.shields.io/gem/dt/csv2psql.svg)](http://rubygems.org/gems/csv2psql)
9
9
  [![Build Status](https://travis-ci.org/korczis/csv2psql.svg?branch=master)](https://travis-ci.org/korczis/csv2psql)
10
10
  [![Code Climate](https://codeclimate.com/github/korczis/csv2psql/badges/gpa.svg)](https://codeclimate.com/github/korczis/csv2psql)
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ namespace :test do
36
36
 
37
37
  desc "Run coding style tests"
38
38
  RSpec::Core::RakeTask.new(:cop) do |t|
39
- # Rake::Task['cop'].invoke
39
+ Rake::Task['cop'].invoke
40
40
  end
41
41
 
42
42
  task :all => [:unit, :cop]
@@ -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], desc: 'Header row included', default_value: true
13
- c.flag [:d, :delimiter], type: String, default_value: ','
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
- paths = [paths] unless paths.is_a?(Array)
33
- paths.each do |path|
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Csv2Psql module
4
4
  module Csv2Psql
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
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.1
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