csv2psql 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/csv2psql/cli/cmd/convert_cmd.rb +12 -0
- data/lib/csv2psql/processor/processor.rb +43 -10
- data/lib/csv2psql/version.rb +1 -1
- data/templates/create_table.sql.erb +0 -4
- data/templates/drop_table.sql.erb +2 -0
- data/templates/truncate_table.sql.erb +2 -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: fc32b319264dee9105efb40ad0091403d8ecd6e3
|
4
|
+
data.tar.gz: 6e20b3c108466bf8c171d417156cd25e17afe267
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8784cbe3de9285e9d78b81b8e69d628488839cafe6557cc8a0602d9c321f49b4fbd36037d01652e2fcceb5ac687bfc428005c34c76232850c61269a60728237f
|
7
|
+
data.tar.gz: 6657977d8372f140ee242be56889840ef97969d8a043244be4d721b8de3da2ac5d04e357fb913f20377d4eab121027c0af9cfe64984142eedad9951ace6880a0
|
data/README.md
CHANGED
@@ -114,6 +114,18 @@ INSERT INTO pokus(id, firstname, lastname, address_street, address_city, address
|
|
114
114
|
COMMIT;
|
115
115
|
```
|
116
116
|
|
117
|
+
**Convert CSV - Stream directly to Postgre client (psql)**
|
118
|
+
|
119
|
+
```
|
120
|
+
csv2psql convert --create-table -t hokus data/sample.csv | psql -h apollocrawler.com -U datathon
|
121
|
+
|
122
|
+
BEGIN
|
123
|
+
CREATE TABLE
|
124
|
+
INSERT 0 1
|
125
|
+
INSERT 0 1
|
126
|
+
COMMIT
|
127
|
+
```
|
128
|
+
|
117
129
|
## Contributing to csv2psql
|
118
130
|
|
119
131
|
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -46,6 +46,16 @@ cmds = {
|
|
46
46
|
'create-table' => {
|
47
47
|
desc: 'Crate SQL Table before inserts',
|
48
48
|
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS['create-table']
|
49
|
+
},
|
50
|
+
|
51
|
+
'drop-table' => {
|
52
|
+
desc: 'Drop SQL Table before inserts',
|
53
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS['drop-table']
|
54
|
+
},
|
55
|
+
|
56
|
+
'truncate-table' => {
|
57
|
+
desc: 'Truncate SQL Table before inserts',
|
58
|
+
default_value: Csv2Psql::Processor::DEFAULT_OPTIONS['truncate-table']
|
49
59
|
}
|
50
60
|
}
|
51
61
|
|
@@ -58,6 +68,8 @@ command :convert do |c|
|
|
58
68
|
c.flag [:s, :separator], cmds[:s]
|
59
69
|
c.switch [:transaction], cmds[:transaction]
|
60
70
|
c.switch ['create-table'], cmds['create-table']
|
71
|
+
c.switch ['drop-table'], cmds['drop-table']
|
72
|
+
c.switch ['truncate-table'], cmds['truncate-table']
|
61
73
|
|
62
74
|
c.action do |global_options, options, args|
|
63
75
|
fail ArgumentError, 'No file to convert specified' if args.empty?
|
@@ -10,11 +10,12 @@ require_relative '../helpers/erb_helper'
|
|
10
10
|
|
11
11
|
module Csv2Psql
|
12
12
|
# Csv2Psql processor class
|
13
|
-
class Processor
|
13
|
+
class Processor # rubocop:disable Metrics/ClassLength
|
14
14
|
attr_reader :path
|
15
15
|
|
16
16
|
DEFAULT_OPTIONS = {
|
17
17
|
'create-table' => false,
|
18
|
+
'drop-table' => false,
|
18
19
|
delimiter: ',',
|
19
20
|
header: true,
|
20
21
|
separator: :auto,
|
@@ -23,9 +24,17 @@ module Csv2Psql
|
|
23
24
|
quote: '"'
|
24
25
|
}
|
25
26
|
|
27
|
+
TABLE_FUNCTIONS = {
|
28
|
+
'drop-table' => :drop_table,
|
29
|
+
'create-table' => :create_table,
|
30
|
+
'truncate-table' => :truncate_table
|
31
|
+
}
|
32
|
+
|
26
33
|
BASE_DIR = File.join(File.dirname(__FILE__), '..', '..', '..')
|
27
34
|
TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
|
28
35
|
CREATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'create_table.sql.erb')
|
36
|
+
DROP_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'drop_table.sql.erb')
|
37
|
+
TRUNCATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'truncate_table.sql.erb')
|
29
38
|
|
30
39
|
def convert(paths, opts = {})
|
31
40
|
with_paths(paths, opts) do |data|
|
@@ -33,19 +42,41 @@ module Csv2Psql
|
|
33
42
|
end
|
34
43
|
end
|
35
44
|
|
36
|
-
def
|
45
|
+
def create_erb_context(path, row, opts = {})
|
37
46
|
header = get_header(row, opts)
|
38
47
|
columns = get_columns(row, opts, header)
|
39
|
-
|
40
|
-
ctx = {
|
48
|
+
{
|
41
49
|
path: path,
|
42
50
|
header: header,
|
43
51
|
columns: columns,
|
44
52
|
table: opts[:table] || DEFAULT_OPTIONS[:table]
|
45
53
|
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_header(path, row, opts = {})
|
57
|
+
return unless @first_row
|
58
|
+
|
59
|
+
TABLE_FUNCTIONS.each do |k, v|
|
60
|
+
t = DEFAULT_OPTIONS[k]
|
61
|
+
t = opts[k] unless opts[k].nil?
|
62
|
+
puts send(v, path, row, opts) if t
|
63
|
+
end
|
64
|
+
|
65
|
+
@first_row = false
|
66
|
+
end
|
67
|
+
|
68
|
+
def create_table(path, row, opts = {})
|
69
|
+
ctx = create_erb_context(path, row, opts)
|
70
|
+
erb = ErbHelper.new
|
46
71
|
erb.process(CREATE_TABLE_TEMPLATE, ctx)
|
47
72
|
end
|
48
73
|
|
74
|
+
def drop_table(path, row, opts = {})
|
75
|
+
ctx = create_erb_context(path, row, opts)
|
76
|
+
erb = ErbHelper.new
|
77
|
+
erb.process(DROP_TABLE_TEMPLATE, ctx)
|
78
|
+
end
|
79
|
+
|
49
80
|
def format_row(row, opts = {})
|
50
81
|
header = get_header(row, opts)
|
51
82
|
columns = get_columns(row, opts, header).join(', ')
|
@@ -91,6 +122,12 @@ module Csv2Psql
|
|
91
122
|
header_column.downcase.gsub(/[^0-9a-z ]/i, '_')
|
92
123
|
end
|
93
124
|
|
125
|
+
def truncate_table(path, row, opts = {})
|
126
|
+
ctx = create_erb_context(path, row, opts)
|
127
|
+
erb = ErbHelper.new
|
128
|
+
erb.process(TRUNCATE_TABLE_TEMPLATE, ctx)
|
129
|
+
end
|
130
|
+
|
94
131
|
def with_path(path, opts = {}, &block)
|
95
132
|
puts 'BEGIN;' if opts[:transaction]
|
96
133
|
csv_opts = merge_csv_options(opts)
|
@@ -111,13 +148,9 @@ module Csv2Psql
|
|
111
148
|
end
|
112
149
|
|
113
150
|
def with_row(path, row, opts = {}, &block)
|
151
|
+
create_header(path, row, opts)
|
152
|
+
|
114
153
|
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
|
121
154
|
block.call(args) if block_given?
|
122
155
|
end
|
123
156
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Korcak
|
@@ -207,6 +207,8 @@ files:
|
|
207
207
|
- lib/csv2psql/version.rb
|
208
208
|
- spec/spec_helper.rb
|
209
209
|
- templates/create_table.sql.erb
|
210
|
+
- templates/drop_table.sql.erb
|
211
|
+
- templates/truncate_table.sql.erb
|
210
212
|
homepage: https://github.com/korczis/csv2psql
|
211
213
|
licenses:
|
212
214
|
- MIT
|