csv2psql 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -23
- data/bin/csv2psql +0 -1
- data/lib/csv2psql/analyzer/types/bigint.rb +3 -1
- data/lib/csv2psql/analyzer/types/boolean.rb +22 -8
- data/lib/csv2psql/analyzer/types/character.rb +1 -1
- data/lib/csv2psql/analyzer/types/date.rb +26 -0
- data/lib/csv2psql/analyzer/types/decimal.rb +2 -2
- data/lib/csv2psql/analyzer/types/string.rb +1 -1
- data/lib/csv2psql/analyzer/types/time.rb +26 -0
- data/lib/csv2psql/analyzer/types/uuid.rb +2 -5
- data/lib/csv2psql/cli/cmd/schema_cmd.rb +1 -1
- data/lib/csv2psql/generator/generator.rb +1 -1
- data/lib/csv2psql/processor/processor.rb +5 -1
- data/lib/csv2psql/schema/schema_generator.rb +8 -2
- data/{templates → lib/csv2psql/templates}/create_table.sql.erb +0 -0
- data/{templates → lib/csv2psql/templates}/drop_table.sql.erb +0 -0
- data/{templates → lib/csv2psql/templates}/header.sql.erb +0 -0
- data/{templates → lib/csv2psql/templates}/schema.sql.erb +0 -0
- data/{templates → lib/csv2psql/templates}/truncate_table.sql.erb +0 -0
- data/lib/csv2psql/version.rb +1 -1
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22ed4a3c0fd8d982937fd5ff0fd94a28096b53ee
|
4
|
+
data.tar.gz: 69be2825f4e5073e8b1176e781c5c6be185c430d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e83da80ea836915620c2da1093dbaf7246ad64456eb390d2a89085cef5a5a851b2f54c62b3893054c5af480e72d1d9a50ea4df643751a9681698884b99afb25a
|
7
|
+
data.tar.gz: 940af7ddd4b901109e9435b780441fbaed0327f59c5c871b841deb177cb59519b871fb436081b6aed94e6b917d5db86ec89d61bf39f5b18337deae1212b215a5
|
data/README.md
CHANGED
@@ -33,7 +33,7 @@ See **[examples](https://gist.github.com/korczis/2f7f54c7bd53554f44df)**
|
|
33
33
|
## Getting started
|
34
34
|
|
35
35
|
```
|
36
|
-
gem install csv2psql
|
36
|
+
$ gem install csv2psql
|
37
37
|
```
|
38
38
|
|
39
39
|
## Usage
|
@@ -41,20 +41,22 @@ gem install csv2psql
|
|
41
41
|
**Simple conversion**
|
42
42
|
|
43
43
|
```
|
44
|
-
csv2psql convert data/sample.csv
|
44
|
+
$ csv2psql convert data/sample.csv
|
45
45
|
```
|
46
46
|
|
47
47
|
**Global help**
|
48
48
|
|
49
49
|
```
|
50
|
-
csv2psql
|
51
|
-
|
50
|
+
$ csv2psql
|
52
51
|
NAME
|
53
|
-
csv2psql -
|
52
|
+
csv2psql - Tool for converting CSV files to PSQL statements
|
54
53
|
|
55
54
|
SYNOPSIS
|
56
55
|
csv2psql [global options] command [command options] [arguments...]
|
57
56
|
|
57
|
+
VERSION
|
58
|
+
0.0.15
|
59
|
+
|
58
60
|
GLOBAL OPTIONS
|
59
61
|
-d, --delimiter=arg - Column delimiter (default: ,)
|
60
62
|
-h, --[no-]header - Header row included (default: enabled)
|
@@ -63,20 +65,20 @@ GLOBAL OPTIONS
|
|
63
65
|
-q, --quote=arg - Quoting character (default: ")
|
64
66
|
-s, --separator=arg - Line separator (default: none)
|
65
67
|
--skip=arg - How many rows skip (default: -1)
|
68
|
+
--version - Display the program version
|
66
69
|
|
67
70
|
COMMANDS
|
68
71
|
analyze - Analyze csv file
|
69
72
|
convert - Convert csv file
|
70
73
|
help - Shows a list of commands or help for one command
|
71
74
|
schema - Generate schema for file
|
72
|
-
version - Print version info
|
75
|
+
version - Print version info
|
73
76
|
```
|
74
77
|
|
75
78
|
**Analyze help**
|
76
79
|
|
77
80
|
```
|
78
|
-
csv2psql help analyze
|
79
|
-
|
81
|
+
$ csv2psql help analyze
|
80
82
|
NAME
|
81
83
|
analyze - Analyze csv file
|
82
84
|
|
@@ -84,14 +86,13 @@ SYNOPSIS
|
|
84
86
|
csv2psql [global options] analyze [command options]
|
85
87
|
|
86
88
|
COMMAND OPTIONS
|
87
|
-
-f, --format=arg - Output format (default: json)
|
89
|
+
-f, --format=arg - Output format json, table (default: json)
|
88
90
|
```
|
89
91
|
|
90
92
|
**Convert help**
|
91
93
|
|
92
94
|
```
|
93
|
-
csv2psql help convert
|
94
|
-
|
95
|
+
$ csv2psql help convert
|
95
96
|
NAME
|
96
97
|
convert - Convert csv file
|
97
98
|
|
@@ -102,15 +103,14 @@ COMMAND OPTIONS
|
|
102
103
|
--[no-]create-table - Crate SQL Table before inserts
|
103
104
|
--[no-]drop-table - Drop SQL Table before inserts
|
104
105
|
-t, --table=arg - Table to insert to (default: none)
|
105
|
-
--[no-]transaction - Import in transaction block
|
106
|
+
--[no-]transaction - Import in transaction block
|
106
107
|
--[no-]truncate-table - Truncate SQL Table before inserts
|
107
108
|
```
|
108
109
|
|
109
110
|
**Schema help**
|
110
111
|
|
111
112
|
```
|
112
|
-
csv2psql help schema
|
113
|
-
|
113
|
+
$ csv2psql help schema
|
114
114
|
NAME
|
115
115
|
schema - Generate schema for file
|
116
116
|
|
@@ -118,7 +118,7 @@ SYNOPSIS
|
|
118
118
|
csv2psql [global options] schema [command options]
|
119
119
|
|
120
120
|
COMMAND OPTIONS
|
121
|
-
-f, --format=arg - Output format (default: json)
|
121
|
+
-f, --format=arg - Output format - json, sql, table (default: json)
|
122
122
|
```
|
123
123
|
|
124
124
|
## Example
|
@@ -126,7 +126,7 @@ COMMAND OPTIONS
|
|
126
126
|
**Input CSV**
|
127
127
|
|
128
128
|
```
|
129
|
-
cat data/sample.csv
|
129
|
+
$ cat data/sample.csv
|
130
130
|
|
131
131
|
id,Firstname,Lastname,Address.Street,Address.City,Address.Details.Note
|
132
132
|
12345,Joe,Doe,"#2140 Taylor Street, 94133",San Francisco,Pool available
|
@@ -136,7 +136,7 @@ id,Firstname,Lastname,Address.Street,Address.City,Address.Details.Note
|
|
136
136
|
**Convert CSV**
|
137
137
|
|
138
138
|
```
|
139
|
-
csv2psql convert data/sample.csv
|
139
|
+
$ csv2psql convert data/sample.csv
|
140
140
|
|
141
141
|
BEGIN;
|
142
142
|
-- Table: my_table
|
@@ -149,7 +149,7 @@ COMMIT;
|
|
149
149
|
**Convert CSV - Create table**
|
150
150
|
|
151
151
|
```
|
152
|
-
csv2psql convert --create-table -t pokus data/sample.csv
|
152
|
+
$ csv2psql convert --create-table -t pokus data/sample.csv
|
153
153
|
|
154
154
|
BEGIN;
|
155
155
|
-- Table: pokus
|
@@ -174,7 +174,7 @@ COMMIT;
|
|
174
174
|
**Convert CSV - Stream directly to Postgres client (psql)**
|
175
175
|
|
176
176
|
```
|
177
|
-
csv2psql convert --create-table -t hokus data/sample.csv | psql
|
177
|
+
$ csv2psql convert --create-table -t hokus data/sample.csv | psql
|
178
178
|
|
179
179
|
BEGIN
|
180
180
|
CREATE TABLE
|
@@ -183,10 +183,17 @@ INSERT 0 1
|
|
183
183
|
COMMIT
|
184
184
|
```
|
185
185
|
|
186
|
+
```
|
187
|
+
$ csv2psql convert --create-table -t hokus data/sample.csv | psql -h 127.0.0.1 -U jetel
|
188
|
+
Password for user jetel:
|
189
|
+
CREATE TABLE
|
190
|
+
INSERT 0 1
|
191
|
+
INSERT 0 1
|
192
|
+
```
|
186
193
|
**Convert CSV - Full load**
|
187
194
|
|
188
195
|
```
|
189
|
-
csv2psql convert --create-table --drop-table --truncate-table -t test data/sample.csv
|
196
|
+
$ csv2psql convert --create-table --drop-table --truncate-table -t test data/sample.csv
|
190
197
|
|
191
198
|
BEGIN;
|
192
199
|
DROP TABLE IF EXISTS test;
|
@@ -213,13 +220,13 @@ COMMIT;
|
|
213
220
|
**Convert CSV - Load CIA Factbook automagically**
|
214
221
|
|
215
222
|
```
|
216
|
-
csv2psql convert --create-table --drop-table --truncate-table --no-transaction -t test data/cia-data-all.csv | psql
|
223
|
+
$ csv2psql convert --create-table --drop-table --truncate-table --no-transaction -t test data/cia-data-all.csv | psql
|
217
224
|
```
|
218
225
|
|
219
226
|
**Analyze CSV - Show as table**
|
220
227
|
|
221
228
|
```
|
222
|
-
csv2psql analyze --format=table tmp/sfpd_incident_2013.csv
|
229
|
+
$ csv2psql analyze --format=table tmp/sfpd_incident_2013.csv
|
223
230
|
|
224
231
|
+------------+--------+-----------+---------+------+--------+------+
|
225
232
|
| tmp/sfpd_incident_2013.csv |
|
@@ -243,7 +250,7 @@ csv2psql analyze --format=table tmp/sfpd_incident_2013.csv
|
|
243
250
|
**Schema guess**
|
244
251
|
|
245
252
|
```
|
246
|
-
csv2psql schema -f table ./data/census_SFOH_2010.csv
|
253
|
+
$ csv2psql schema -f table ./data/census_SFOH_2010.csv
|
247
254
|
|
248
255
|
+-------------------------------------------------+---------+---------+
|
249
256
|
| ./data/census_SFOH_2010.csv |
|
data/bin/csv2psql
CHANGED
@@ -10,9 +10,11 @@ module Csv2Psql
|
|
10
10
|
CLASS = :numeric
|
11
11
|
WEIGHT = 4
|
12
12
|
|
13
|
+
RE = Regexp.new(/^\d+$/)
|
14
|
+
|
13
15
|
class << self
|
14
16
|
def analyze(val)
|
15
|
-
val.is_a?(Integer) || (val &&
|
17
|
+
val.is_a?(Integer) || (val && RE.match(val))
|
16
18
|
end
|
17
19
|
|
18
20
|
def convert(val)
|
@@ -10,20 +10,34 @@ module Csv2Psql
|
|
10
10
|
CLASS = :special
|
11
11
|
WEIGHT = 5
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
TRUE_VALUES = [
|
14
|
+
true,
|
15
|
+
't',
|
16
|
+
'true',
|
17
|
+
'y',
|
18
|
+
'yes',
|
19
|
+
'on',
|
20
|
+
'1',
|
21
|
+
]
|
22
|
+
FALSE_VALUES = [
|
23
|
+
false,
|
24
|
+
'f',
|
25
|
+
'false',
|
26
|
+
'n',
|
27
|
+
'no',
|
28
|
+
'off',
|
29
|
+
'0'
|
30
|
+
]
|
31
|
+
BOOLEAN_VALUES = TRUE_VALUES + FALSE_VALUES
|
18
32
|
|
19
33
|
class << self
|
20
34
|
def analyze(val)
|
21
|
-
return if val.nil? || val.empty?
|
22
|
-
|
35
|
+
return false if val.nil? || val.empty?
|
36
|
+
BOOLEAN_VALUES.index(val) != nil
|
23
37
|
end
|
24
38
|
|
25
39
|
def convert(val)
|
26
|
-
val.
|
40
|
+
val.downcase
|
27
41
|
end
|
28
42
|
end
|
29
43
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative 'base_analyzer'
|
4
|
+
|
5
|
+
module Csv2Psql
|
6
|
+
module Analyzers
|
7
|
+
# Bigint value matcher
|
8
|
+
class Date < BaseAnalyzer
|
9
|
+
TYPE = :date
|
10
|
+
CLASS = :date
|
11
|
+
WEIGHT = 3
|
12
|
+
|
13
|
+
RE = Regexp.new(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def analyze(val)
|
17
|
+
val && RE.match(val)
|
18
|
+
end
|
19
|
+
|
20
|
+
def convert(val)
|
21
|
+
val.to_i
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -9,12 +9,12 @@ module Csv2Psql
|
|
9
9
|
TYPE = :decimal
|
10
10
|
CLASS = :numeric
|
11
11
|
WEIGHT = 3
|
12
|
-
RE = /^[-+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)?$/
|
12
|
+
RE = Regexp.new(/^[-+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)?$/)
|
13
13
|
|
14
14
|
class << self
|
15
15
|
def analyze(val)
|
16
16
|
return true if val.is_a?(Float)
|
17
|
-
res = val &&
|
17
|
+
res = val && RE.match(val)
|
18
18
|
!res.nil?
|
19
19
|
end
|
20
20
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative 'base_analyzer'
|
4
|
+
|
5
|
+
module Csv2Psql
|
6
|
+
module Analyzers
|
7
|
+
# Bigint value matcher
|
8
|
+
class Time < BaseAnalyzer
|
9
|
+
TYPE = :time
|
10
|
+
CLASS = :date
|
11
|
+
WEIGHT = 3
|
12
|
+
|
13
|
+
RE = Regexp.new(/^[0-9]{2}:[0-9]{2}(:[0-9]{2})?$/)
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def analyze(val)
|
17
|
+
val && RE.match(val)
|
18
|
+
end
|
19
|
+
|
20
|
+
def convert(val)
|
21
|
+
val.to_i
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -10,14 +10,11 @@ module Csv2Psql
|
|
10
10
|
CLASS = :special
|
11
11
|
WEIGHT = 5
|
12
12
|
|
13
|
-
RE = /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/ # rubocop:disable Metrics/LineLength
|
13
|
+
RE = Regexp.new(/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/) # rubocop:disable Metrics/LineLength
|
14
14
|
|
15
15
|
class << self
|
16
16
|
def analyze(val)
|
17
|
-
|
18
|
-
|
19
|
-
match = val && val.match(RE)
|
20
|
-
!match.nil?
|
17
|
+
val && val.match(RE)
|
21
18
|
end
|
22
19
|
end
|
23
20
|
end
|
@@ -8,7 +8,7 @@ require_relative '../../convert/convert'
|
|
8
8
|
require_relative '../../helpers/erb_helper'
|
9
9
|
require_relative '../../processor/processor'
|
10
10
|
|
11
|
-
BASE_DIR = File.join(File.dirname(__FILE__), '..', '..'
|
11
|
+
BASE_DIR = File.join(File.dirname(__FILE__), '..', '..')
|
12
12
|
TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
|
13
13
|
SCHEMA_TEMPLATE = File.join(TEMPLATE_DIR, 'schema.sql.erb')
|
14
14
|
|
@@ -10,7 +10,7 @@ require_relative '../helpers/erb_helper'
|
|
10
10
|
module Csv2Psql
|
11
11
|
# SQL Code generator
|
12
12
|
class Generator
|
13
|
-
BASE_DIR = File.join(File.dirname(__FILE__), '..'
|
13
|
+
BASE_DIR = File.join(File.dirname(__FILE__), '..')
|
14
14
|
TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
|
15
15
|
CREATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'create_table.sql.erb')
|
16
16
|
DROP_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'drop_table.sql.erb')
|
@@ -70,7 +70,11 @@ module Csv2Psql
|
|
70
70
|
analyzer.analyze(path, data[:row], opts)
|
71
71
|
end
|
72
72
|
|
73
|
-
|
73
|
+
analysis = analyzer.files[path]
|
74
|
+
if analysis
|
75
|
+
res[path] = SchemaGenerator.generate(analysis)
|
76
|
+
end
|
77
|
+
|
74
78
|
end
|
75
79
|
res
|
76
80
|
end
|
@@ -7,6 +7,7 @@ module Csv2Psql
|
|
7
7
|
def select_analyzers_by_match(analyzers, match)
|
8
8
|
null_count = analyzers['Null'][:results][:count]
|
9
9
|
analyzers.select do |_k, v|
|
10
|
+
next if _k != 'String' && v[:results][:count] == 0
|
10
11
|
v[:results][:count] + null_count == match
|
11
12
|
end
|
12
13
|
end
|
@@ -15,8 +16,13 @@ module Csv2Psql
|
|
15
16
|
analyzers.select { |_k, v| v[:class].sql_class?(class_name) }
|
16
17
|
end
|
17
18
|
|
18
|
-
def select_best(
|
19
|
-
analyzers = select_analyzers_by_match(
|
19
|
+
def select_best(in_analyzers, lines)
|
20
|
+
analyzers = select_analyzers_by_match(in_analyzers, lines)
|
21
|
+
|
22
|
+
# matched_analyzers = analyzers.select do |name, analyzer|
|
23
|
+
# analyzer[:results][:count] === lines
|
24
|
+
# end
|
25
|
+
|
20
26
|
sorted = analyzers.sort do |a, b|
|
21
27
|
a[1][:class].weight <=> b[1][:class].weight
|
22
28
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/csv2psql/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Korcak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gli
|
@@ -289,9 +289,11 @@ files:
|
|
289
289
|
- lib/csv2psql/analyzer/types/bigint.rb
|
290
290
|
- lib/csv2psql/analyzer/types/boolean.rb
|
291
291
|
- lib/csv2psql/analyzer/types/character.rb
|
292
|
+
- lib/csv2psql/analyzer/types/date.rb
|
292
293
|
- lib/csv2psql/analyzer/types/decimal.rb
|
293
294
|
- lib/csv2psql/analyzer/types/null.rb
|
294
295
|
- lib/csv2psql/analyzer/types/string.rb
|
296
|
+
- lib/csv2psql/analyzer/types/time.rb
|
295
297
|
- lib/csv2psql/analyzer/types/uuid.rb
|
296
298
|
- lib/csv2psql/cache/cache.rb
|
297
299
|
- lib/csv2psql/cli/app.rb
|
@@ -317,6 +319,11 @@ files:
|
|
317
319
|
- lib/csv2psql/output/output.rb
|
318
320
|
- lib/csv2psql/processor/processor.rb
|
319
321
|
- lib/csv2psql/schema/schema_generator.rb
|
322
|
+
- lib/csv2psql/templates/create_table.sql.erb
|
323
|
+
- lib/csv2psql/templates/drop_table.sql.erb
|
324
|
+
- lib/csv2psql/templates/header.sql.erb
|
325
|
+
- lib/csv2psql/templates/schema.sql.erb
|
326
|
+
- lib/csv2psql/templates/truncate_table.sql.erb
|
320
327
|
- lib/csv2psql/version.rb
|
321
328
|
- spec/cli/app_spec.rb
|
322
329
|
- spec/cli/cmd/analyze_cmd_spec.rb
|
@@ -324,11 +331,6 @@ files:
|
|
324
331
|
- spec/cli/cmd/version_cmd_spec.rb
|
325
332
|
- spec/helpers/cli_helper.rb
|
326
333
|
- spec/spec_helper.rb
|
327
|
-
- templates/create_table.sql.erb
|
328
|
-
- templates/drop_table.sql.erb
|
329
|
-
- templates/header.sql.erb
|
330
|
-
- templates/schema.sql.erb
|
331
|
-
- templates/truncate_table.sql.erb
|
332
334
|
homepage: https://github.com/korczis/csv2psql
|
333
335
|
licenses:
|
334
336
|
- MIT
|