csv2psql 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc32b319264dee9105efb40ad0091403d8ecd6e3
4
- data.tar.gz: 6e20b3c108466bf8c171d417156cd25e17afe267
3
+ metadata.gz: b8617b9028668a6a28cd5185dc0eb6a79effda90
4
+ data.tar.gz: b7808d63c2ec216005dc407aa8b200638b5344e5
5
5
  SHA512:
6
- metadata.gz: 8784cbe3de9285e9d78b81b8e69d628488839cafe6557cc8a0602d9c321f49b4fbd36037d01652e2fcceb5ac687bfc428005c34c76232850c61269a60728237f
7
- data.tar.gz: 6657977d8372f140ee242be56889840ef97969d8a043244be4d721b8de3da2ac5d04e357fb913f20377d4eab121027c0af9cfe64984142eedad9951ace6880a0
6
+ metadata.gz: 7ad111c1e05e5b12d191d9e160d248280f76d3768933b03ac41c4ee42c1f0e2962ed5251bade181c52b217fa14da66e1a58acaa6303d1bcb0e778ddb95bebf04
7
+ data.tar.gz: 9b75d29f5a8bb8c412b719df00aa88056053f0b209a376166b05f8cdf616e1f400d1f2d872d94fa4ba5afbd38f5339657bf35d792d626cc188486596af988537
data/README.md CHANGED
@@ -56,13 +56,15 @@ SYNOPSIS
56
56
  csv2psql [global options] convert [command options]
57
57
 
58
58
  COMMAND OPTIONS
59
- --[no-]create-table - Crate SQL Table before inserts
60
- -d, --delimiter=arg - Column delimiter (default: ,)
61
- -h, --[no-]header - Header row included (default: enabled)
62
- -q, --quote=arg - Quoting character (default: ")
63
- -s, --separator=arg - Line separator (default: auto)
64
- -t, --table=arg - Table to insert to (default: my_table)
65
- --[no-]transaction - Import in transaction block (default: enabled)
59
+ --[no-]create-table - Crate SQL Table before inserts
60
+ -d, --delimiter=arg - Column delimiter (default: ,)
61
+ --[no-]drop-table - Drop SQL Table before inserts
62
+ -h, --[no-]header - Header row included (default: enabled)
63
+ -q, --quote=arg - Quoting character (default: ")
64
+ -s, --separator=arg - Line separator (default: auto)
65
+ -t, --table=arg - Table to insert to (default: my_table)
66
+ --[no-]transaction - Import in transaction block (default: enabled)
67
+ --[no-]truncate-table - Truncate SQL Table before inserts
66
68
  ```
67
69
 
68
70
  ## Example
@@ -126,6 +128,33 @@ INSERT 0 1
126
128
  COMMIT
127
129
  ```
128
130
 
131
+ **Convert CSV - Full load**
132
+
133
+ ```
134
+ csv2psql convert --create-table --drop-table --truncate-table -t test data/sample.csv
135
+
136
+ BEGIN;
137
+ DROP TABLE IF EXISTS test;
138
+
139
+ CREATE TABLE test(
140
+ id TEXT,
141
+ firstname TEXT,
142
+ lastname TEXT,
143
+ address_street TEXT,
144
+ address_city TEXT,
145
+ address_details_note TEXT
146
+ )
147
+ WITH (
148
+ OIDS=FALSE
149
+ );
150
+
151
+ TRUNCATE test;
152
+
153
+ INSERT INTO test(id, firstname, lastname, address_street, address_city, address_details_note) VALUES('12345', 'Joe', 'Doe', '#2140 Taylor Street, 94133', 'San Francisco', 'Pool available');
154
+ INSERT INTO test(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');
155
+ COMMIT;
156
+ ```
157
+
129
158
  ## Contributing to csv2psql
130
159
 
131
160
  - Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -34,6 +34,7 @@ module Csv2Psql
34
34
  TEMPLATE_DIR = File.join(BASE_DIR, 'templates')
35
35
  CREATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'create_table.sql.erb')
36
36
  DROP_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'drop_table.sql.erb')
37
+ HEADER_TEMPLATE = File.join(TEMPLATE_DIR, 'header.sql.erb')
37
38
  TRUNCATE_TABLE_TEMPLATE = File.join(TEMPLATE_DIR, 'truncate_table.sql.erb')
38
39
 
39
40
  def convert(paths, opts = {})
@@ -54,8 +55,22 @@ module Csv2Psql
54
55
  end
55
56
 
56
57
  def create_header(path, row, opts = {})
58
+ ctx = create_erb_context(path, row, opts)
59
+ erb = ErbHelper.new
60
+ erb.process(HEADER_TEMPLATE, ctx)
61
+ end
62
+
63
+ def create_table(path, row, opts = {})
64
+ ctx = create_erb_context(path, row, opts)
65
+ erb = ErbHelper.new
66
+ erb.process(CREATE_TABLE_TEMPLATE, ctx)
67
+ end
68
+
69
+ def create_sql_script(path, row, opts = {})
57
70
  return unless @first_row
58
71
 
72
+ puts create_header(path, row, opts)
73
+
59
74
  TABLE_FUNCTIONS.each do |k, v|
60
75
  t = DEFAULT_OPTIONS[k]
61
76
  t = opts[k] unless opts[k].nil?
@@ -65,12 +80,6 @@ module Csv2Psql
65
80
  @first_row = false
66
81
  end
67
82
 
68
- def create_table(path, row, opts = {})
69
- ctx = create_erb_context(path, row, opts)
70
- erb = ErbHelper.new
71
- erb.process(CREATE_TABLE_TEMPLATE, ctx)
72
- end
73
-
74
83
  def drop_table(path, row, opts = {})
75
84
  ctx = create_erb_context(path, row, opts)
76
85
  erb = ErbHelper.new
@@ -148,7 +157,7 @@ module Csv2Psql
148
157
  end
149
158
 
150
159
  def with_row(path, row, opts = {}, &block)
151
- create_header(path, row, opts)
160
+ create_sql_script(path, row, opts)
152
161
 
153
162
  args = { path: path, row: row }
154
163
  block.call(args) if block_given?
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Csv2Psql module
4
4
  module Csv2Psql
5
- VERSION = '0.0.5'
5
+ VERSION = '0.0.6'
6
6
  end
@@ -0,0 +1,2 @@
1
+ -- Table <%= ctx[:table] + "\n" %>
2
+
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Korcak
@@ -208,6 +208,7 @@ files:
208
208
  - spec/spec_helper.rb
209
209
  - templates/create_table.sql.erb
210
210
  - templates/drop_table.sql.erb
211
+ - templates/header.sql.erb
211
212
  - templates/truncate_table.sql.erb
212
213
  homepage: https://github.com/korczis/csv2psql
213
214
  licenses: