csv2sqlite 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/bin/csv2sqlite +92 -0
- data/csv2sqlite.gemspec +25 -0
- data/lib/csv2sqlite/version.rb +3 -0
- data/lib/csv2sqlite.rb +5 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6b948d9fc0396bdd796e6e8138517e8f8119fdcb
|
4
|
+
data.tar.gz: 4b28da68119e15b3d2e9bc5c448c4521491221b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c6170c37d5b3f313e6e23ef8dcd8b1f035e83b90d80bba8faed83e404d530a24ef04bf51b7291de72e091fd91666efebe894b966ea358b52c439bebd3bdf641
|
7
|
+
data.tar.gz: 644db9190f6207416067844264ca2b6f0c86873aed5a815d9dbae23c9673b072aaf754fb0c4935dd659ab1c8cb9b8b9563d0acf9ae5c5d36b193cfe48e72e581
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Pete Brumm
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Csv2sqlite
|
2
|
+
|
3
|
+
Very simple script to convert folder of csv's to sqlite db. it doesn't detect column types. everything right now is a string.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install csv2sqlite
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
$ csv2sqlite <db> <csv> [<csv> ...]
|
12
|
+
|
13
|
+
## Additional
|
14
|
+
|
15
|
+
If you install gnumeric you can also provide xls and xlsx files
|
16
|
+
|
17
|
+
brew install gnumeric
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it ( http://github.com/<my-github-username>/csv2sqlite/fork )
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/csv2sqlite
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'csv'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_support/all'
|
6
|
+
require 'sqlite3'
|
7
|
+
|
8
|
+
db = ARGV[0]
|
9
|
+
csvs = ARGV[1..-1]
|
10
|
+
if ARGV.size < 2
|
11
|
+
puts "csv2sqlite <dbname> <csvfile> [<csvfile> ...]"
|
12
|
+
exit 0
|
13
|
+
end
|
14
|
+
config = %{
|
15
|
+
development:
|
16
|
+
adapter: sqlite3
|
17
|
+
database: #{db}
|
18
|
+
pool: 5
|
19
|
+
timeout: 5000
|
20
|
+
}
|
21
|
+
ActiveRecord::Base.configurations = YAML::load(config)
|
22
|
+
ActiveRecord::Base.establish_connection('development')
|
23
|
+
|
24
|
+
csvs.each {|file|
|
25
|
+
file_io = nil
|
26
|
+
if file.include?(".xls")
|
27
|
+
name = File.basename(file, ".xls").classify
|
28
|
+
temp_file = Tempfile.new(["conv_", ".csv"])
|
29
|
+
`ssconvert --import-type=Gnumeric_Excel:excel #{file} #{temp_file.path} > /dev/null 2>&1`
|
30
|
+
|
31
|
+
temp_file.rewind
|
32
|
+
file = temp_file.path
|
33
|
+
elsif file.include?(".xlsx")
|
34
|
+
name = File.basename(file, ".xlsx").classify
|
35
|
+
temp_file = Tempfile.new(["conv_", ".csv"])
|
36
|
+
`ssconvert --import-type=Gnumeric_Excel:xlsx #{file} #{temp_file.path} > /dev/null 2>&1`
|
37
|
+
|
38
|
+
temp_file.rewind
|
39
|
+
file = temp_file.path
|
40
|
+
else
|
41
|
+
name = File.basename(file, ".csv").classify
|
42
|
+
end
|
43
|
+
puts name
|
44
|
+
count = 0
|
45
|
+
ActiveRecord::Base.transaction do
|
46
|
+
cols = {}
|
47
|
+
|
48
|
+
CSV.foreach file, :headers => true, encoding: "ISO-8859-1:UTF-8", header_converters: ->(col){
|
49
|
+
col = col.downcase.gsub(/[^a-z0-9_]/, "")
|
50
|
+
if cols[col]
|
51
|
+
col += "#{cols.size}"
|
52
|
+
end
|
53
|
+
if col == "type"
|
54
|
+
col += "_#{cols.size}"
|
55
|
+
end
|
56
|
+
if col == "add"
|
57
|
+
col += "_#{cols.size}"
|
58
|
+
end
|
59
|
+
if col == "order"
|
60
|
+
col += "_#{cols.size}"
|
61
|
+
end
|
62
|
+
if col == "guid"
|
63
|
+
col += "_#{cols.size}"
|
64
|
+
end
|
65
|
+
cols[col] = true
|
66
|
+
#p col
|
67
|
+
col
|
68
|
+
} do |row|
|
69
|
+
if count == 0
|
70
|
+
columns = (row.to_hash.keys + ['guid']).map {|r| "#{r} varchar" }.join(", ")
|
71
|
+
insert_statement = "CREATE TABLE #{name.downcase.pluralize} (#{columns});"
|
72
|
+
ActiveRecord::Base.connection.execute(insert_statement)
|
73
|
+
end
|
74
|
+
s = %{
|
75
|
+
class #{name} < ActiveRecord::Base
|
76
|
+
self.table_name = "#{name.downcase.pluralize}"
|
77
|
+
end
|
78
|
+
}
|
79
|
+
Kernel.eval(s)
|
80
|
+
klass = name.constantize
|
81
|
+
print "."
|
82
|
+
hash = Hash[row]
|
83
|
+
hash['guid'] = SecureRandom.uuid
|
84
|
+
#p hash
|
85
|
+
klass.reset_column_information
|
86
|
+
klass.create! hash
|
87
|
+
count += 1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
puts "\nDone."
|
91
|
+
}
|
92
|
+
|
data/csv2sqlite.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv2sqlite/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv2sqlite"
|
8
|
+
spec.version = Csv2sqlite::VERSION
|
9
|
+
spec.authors = ["Pete Brumm"]
|
10
|
+
spec.email = ["pete@petebrumm.com"]
|
11
|
+
spec.summary = %q{Provides a simple command line tool for converting directory of csv's to sqlite tables}
|
12
|
+
spec.description = %q{if ssconvert from gnumeric is installed it can convert xls and xlsx as well (single sheet)}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.add_dependency "activesupport"
|
21
|
+
spec.add_dependency "activerecord"
|
22
|
+
spec.add_dependency "sqlite3"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/lib/csv2sqlite.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv2sqlite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pete Brumm
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: if ssconvert from gnumeric is installed it can convert xls and xlsx as
|
84
|
+
well (single sheet)
|
85
|
+
email:
|
86
|
+
- pete@petebrumm.com
|
87
|
+
executables:
|
88
|
+
- csv2sqlite
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/csv2sqlite
|
98
|
+
- csv2sqlite.gemspec
|
99
|
+
- lib/csv2sqlite.rb
|
100
|
+
- lib/csv2sqlite/version.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.0.0
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Provides a simple command line tool for converting directory of csv's to
|
125
|
+
sqlite tables
|
126
|
+
test_files: []
|