csv2psql 0.0.3 → 0.0.4
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 +27 -0
- data/lib/csv2psql/cli/cmd/convert_cmd.rb +17 -6
- data/lib/csv2psql/helpers/erb_helper.rb +30 -0
- data/lib/csv2psql/processor/processor.rb +32 -7
- data/lib/csv2psql/version.rb +1 -1
- data/templates/create_table.sql.erb +15 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9f2aaa26746a4f627968755685e09b4de8eca7f
|
4
|
+
data.tar.gz: 511b57240f7c0588d6c3d366e5f807229b0a6664
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 169542faf9dadeef826a4205cb2cc7f59d836cbd71b5bb9930d58e7e14d641c8bb2b5b77dd540854e2dbcf67bc4a9f9945e21df1c92fbdb937e81116d7e7762d
|
7
|
+
data.tar.gz: eb29f050adcceace67b611cd6c65649b4e775a1e6a0ae921734922aad5fd752dbc79c31489709c8606fc32afa7d9ae1780e251ebe7a11ebeb75c8fe84d90e52c
|
data/README.md
CHANGED
@@ -56,6 +56,7 @@ SYNOPSIS
|
|
56
56
|
csv2psql [global options] convert [command options]
|
57
57
|
|
58
58
|
COMMAND OPTIONS
|
59
|
+
--[no-]create-table - Crate SQL Table before inserts
|
59
60
|
-d, --delimiter=arg - Column delimiter (default: ,)
|
60
61
|
-h, --[no-]header - Header row included (default: enabled)
|
61
62
|
-q, --quote=arg - Quoting character (default: ")
|
@@ -87,6 +88,32 @@ INSERT INTO my_table(id, firstname, lastname, address_street, address_city, addr
|
|
87
88
|
COMMIT;
|
88
89
|
```
|
89
90
|
|
91
|
+
**Convert CSV - Create table**
|
92
|
+
|
93
|
+
```
|
94
|
+
csv2psql convert --create-table -t pokus data/sample.csv
|
95
|
+
|
96
|
+
BEGIN;
|
97
|
+
-- Table: pokus
|
98
|
+
-- DROP TABLE pokus;
|
99
|
+
|
100
|
+
CREATE TABLE pokus(
|
101
|
+
id TEXT,
|
102
|
+
firstname TEXT,
|
103
|
+
lastname TEXT,
|
104
|
+
address_street TEXT,
|
105
|
+
address_city TEXT,
|
106
|
+
address_details_note TEXT
|
107
|
+
)
|
108
|
+
WITH (
|
109
|
+
OIDS=FALSE
|
110
|
+
);
|
111
|
+
|
112
|
+
INSERT INTO pokus(id, firstname, lastname, address_street, address_city, address_details_note) VALUES('12345', 'Joe', 'Doe', '#2140 Taylor Street, 94133', 'San Francisco', 'Pool available');
|
113
|
+
INSERT INTO pokus(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');
|
114
|
+
COMMIT;
|
115
|
+
```
|
116
|
+
|
90
117
|
## Contributing to csv2psql
|
91
118
|
|
92
119
|
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -6,36 +6,46 @@ include GLI::App
|
|
6
6
|
|
7
7
|
require_relative '../shared'
|
8
8
|
require_relative '../../convert/convert'
|
9
|
+
require_relative '../../processor/processor'
|
9
10
|
|
10
11
|
cmds = {
|
11
12
|
h: {
|
12
13
|
desc: 'Header row included',
|
13
|
-
default_value:
|
14
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:header]
|
14
15
|
},
|
15
16
|
|
16
17
|
d: {
|
17
18
|
desc: 'Column delimiter',
|
18
|
-
type: String,
|
19
|
+
type: String,
|
20
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:delimiter]
|
19
21
|
},
|
20
22
|
|
21
23
|
t: {
|
22
24
|
desc: 'Table to insert to',
|
23
|
-
type: String,
|
25
|
+
type: String,
|
26
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:table]
|
24
27
|
},
|
25
28
|
|
26
29
|
q: {
|
27
30
|
desc: 'Quoting character',
|
28
|
-
type: String,
|
31
|
+
type: String,
|
32
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:quote]
|
29
33
|
},
|
30
34
|
|
31
35
|
s: {
|
32
36
|
desc: 'Line separator',
|
33
|
-
type: String,
|
37
|
+
type: String,
|
38
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:separator]
|
34
39
|
},
|
35
40
|
|
36
41
|
transaction: {
|
37
42
|
desc: 'Import in transaction block',
|
38
|
-
|
43
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS[:transaction]
|
44
|
+
},
|
45
|
+
|
46
|
+
'create-table' => {
|
47
|
+
desc: 'Crate SQL Table before inserts',
|
48
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS['create-table']
|
39
49
|
}
|
40
50
|
}
|
41
51
|
|
@@ -47,6 +57,7 @@ command :convert do |c|
|
|
47
57
|
c.flag [:q, :quote], cmds[:q]
|
48
58
|
c.flag [:s, :separator], cmds[:s]
|
49
59
|
c.switch [:transaction], cmds[:transaction]
|
60
|
+
c.switch ['create-table'], cmds['create-table']
|
50
61
|
|
51
62
|
c.action do |global_options, options, args|
|
52
63
|
fail ArgumentError, 'No file to convert specified' if args.empty?
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
module Csv2Psql
|
7
|
+
# ERB Helper
|
8
|
+
class ErbHelper
|
9
|
+
BASE_DIR = File.join(File.dirname(__FILE__), '..')
|
10
|
+
|
11
|
+
def render(filename)
|
12
|
+
path = File.join(BASE_DIR, filename)
|
13
|
+
File.read(path)
|
14
|
+
end
|
15
|
+
|
16
|
+
def process(filename, ctx = {})
|
17
|
+
ctx ||= {} # rubocop:disable Lint/UselessAssignment
|
18
|
+
b = binding
|
19
|
+
erb = ERB.new(File.read(filename), 0, '>')
|
20
|
+
erb.filename = filename
|
21
|
+
erb.result b
|
22
|
+
end
|
23
|
+
|
24
|
+
def run
|
25
|
+
FILES.each do |files|
|
26
|
+
process(files)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -6,6 +6,7 @@ require 'pathname'
|
|
6
6
|
require 'pp'
|
7
7
|
|
8
8
|
require_relative '../version'
|
9
|
+
require_relative '../helpers/erb_helper'
|
9
10
|
|
10
11
|
module Csv2Psql
|
11
12
|
# Csv2Psql processor class
|
@@ -13,18 +14,38 @@ module Csv2Psql
|
|
13
14
|
attr_reader :path
|
14
15
|
|
15
16
|
DEFAULT_OPTIONS = {
|
17
|
+
'create-table' => false,
|
16
18
|
delimiter: ',',
|
17
19
|
header: true,
|
18
20
|
separator: :auto,
|
21
|
+
table: 'my_table',
|
22
|
+
transaction: true,
|
19
23
|
quote: '"'
|
20
24
|
}
|
21
25
|
|
26
|
+
BASE_DIR = File.join(File.dirname(__FILE__), '..', '..', '..')
|
27
|
+
TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
|
28
|
+
CREATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'create_table.sql.erb')
|
29
|
+
|
22
30
|
def convert(paths, opts = {})
|
23
31
|
with_paths(paths, opts) do |data|
|
24
32
|
puts format_row(data[:row], opts)
|
25
33
|
end
|
26
34
|
end
|
27
35
|
|
36
|
+
def create_table(path, row, opts = {})
|
37
|
+
header = get_header(row, opts)
|
38
|
+
columns = get_columns(row, opts, header)
|
39
|
+
erb = ErbHelper.new
|
40
|
+
ctx = {
|
41
|
+
path: path,
|
42
|
+
header: header,
|
43
|
+
columns: columns,
|
44
|
+
table: opts[:table] || DEFAULT_OPTIONS[:table]
|
45
|
+
}
|
46
|
+
erb.process(CREATE_TABLE_TEMPLATE, ctx)
|
47
|
+
end
|
48
|
+
|
28
49
|
def format_row(row, opts = {})
|
29
50
|
header = get_header(row, opts)
|
30
51
|
columns = get_columns(row, opts, header).join(', ')
|
@@ -67,15 +88,16 @@ module Csv2Psql
|
|
67
88
|
end
|
68
89
|
|
69
90
|
def sanitize_header(header_column)
|
70
|
-
header_column.downcase.gsub(
|
91
|
+
header_column.downcase.gsub(/[^0-9a-z ]/i, '_')
|
71
92
|
end
|
72
93
|
|
73
94
|
def with_path(path, opts = {}, &block)
|
74
95
|
puts 'BEGIN;' if opts[:transaction]
|
75
96
|
csv_opts = merge_csv_options(opts)
|
97
|
+
@first_row = true
|
76
98
|
CSV.open(path, 'rt', csv_opts) do |csv|
|
77
99
|
csv.each do |row|
|
78
|
-
with_row(path, row, &block)
|
100
|
+
with_row(path, row, opts, &block)
|
79
101
|
end
|
80
102
|
end
|
81
103
|
puts 'COMMIT;' if opts[:transaction]
|
@@ -88,11 +110,14 @@ module Csv2Psql
|
|
88
110
|
end
|
89
111
|
end
|
90
112
|
|
91
|
-
def with_row(path, row, &block)
|
92
|
-
args = {
|
93
|
-
|
94
|
-
|
95
|
-
|
113
|
+
def with_row(path, row, opts = {}, &block)
|
114
|
+
args = { path: path, row: row }
|
115
|
+
if @first_row
|
116
|
+
ct = DEFAULT_OPTIONS['create-table']
|
117
|
+
ct = opts['create-table'] unless opts['create-table'].nil?
|
118
|
+
puts create_table(path, row, opts) if ct
|
119
|
+
@first_row = false
|
120
|
+
end
|
96
121
|
block.call(args) if block_given?
|
97
122
|
end
|
98
123
|
end
|
data/lib/csv2psql/version.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
-- Table: <%= ctx[:table] %>
|
2
|
+
|
3
|
+
-- DROP TABLE <%= ctx[:table] %>;
|
4
|
+
|
5
|
+
CREATE TABLE <%= ctx[:table] %>
|
6
|
+
(
|
7
|
+
<% ctx[:columns].each_with_index do |item, index| %>
|
8
|
+
<%= item %> TEXT<%= ", " if index < ctx[:columns].length - 1%>
|
9
|
+
|
10
|
+
<% end %>
|
11
|
+
)
|
12
|
+
WITH (
|
13
|
+
OIDS=FALSE
|
14
|
+
);
|
15
|
+
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Korcak
|
@@ -201,10 +201,12 @@ files:
|
|
201
201
|
- lib/csv2psql/cli/cmd/version_cmd.rb
|
202
202
|
- lib/csv2psql/cli/shared.rb
|
203
203
|
- lib/csv2psql/convert/convert.rb
|
204
|
+
- lib/csv2psql/helpers/erb_helper.rb
|
204
205
|
- lib/csv2psql/lib.rb
|
205
206
|
- lib/csv2psql/processor/processor.rb
|
206
207
|
- lib/csv2psql/version.rb
|
207
208
|
- spec/spec_helper.rb
|
209
|
+
- templates/create_table.sql.erb
|
208
210
|
homepage: https://github.com/korczis/csv2psql
|
209
211
|
licenses:
|
210
212
|
- MIT
|