csv_to_sqlite3 0.1.1 → 0.1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +11 -1
- data/README.md +12 -16
- data/Rakefile +9 -10
- data/bin/csv_to_sqlite +0 -1
- data/csv_to_sqlite3.gemspec +2 -2
- data/exe/csv_to_sqlite +3 -1
- data/lib/argument_handler_service.rb +30 -0
- data/lib/csv_reader.rb +14 -2
- data/lib/csv_to_sqlite.rb +28 -3
- data/lib/csv_to_sqlite/version.rb +1 -1
- data/lib/sql/column.rb +8 -1
- data/lib/sql/insert.rb +5 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 210e5e06c4338355c22dc3c8d84b7026b4878a4b6b5a4e34712e311e226a7b24
|
4
|
+
data.tar.gz: d8d5eae006f16edf17fb90a40cec72e8ecfe653258883161692be39f4b430be5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ab7649056cc987ff15015f8d542b43df7eab74cf63e94edd9cc4fa15ba4a0560ef0e03af33db3d0208665912a0d0b8756c56bd365222570f2ebb613b5859c36
|
7
|
+
data.tar.gz: f7272550f558ba70220e7f0fac18e273cd2a0b57a0a8571d7c2a76dd58176136ae1b8d10ee42b8c7e19d9b6a7987ba5a9c0ec940ab4b48f76cee59caea361a36
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
csv_to_sqlite3 (0.1.
|
4
|
+
csv_to_sqlite3 (0.1.3.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -22,6 +22,15 @@ GEM
|
|
22
22
|
rspec-support (~> 3.9.0)
|
23
23
|
rspec-support (3.9.3)
|
24
24
|
sqlite3 (1.4.2)
|
25
|
+
strings-ansi (0.1.0)
|
26
|
+
tty-cursor (0.7.1)
|
27
|
+
tty-progressbar (0.17.0)
|
28
|
+
strings-ansi (~> 0.1.0)
|
29
|
+
tty-cursor (~> 0.7)
|
30
|
+
tty-screen (~> 0.7)
|
31
|
+
unicode-display_width (~> 1.6)
|
32
|
+
tty-screen (0.8.1)
|
33
|
+
unicode-display_width (1.7.0)
|
25
34
|
|
26
35
|
PLATFORMS
|
27
36
|
ruby
|
@@ -31,6 +40,7 @@ DEPENDENCIES
|
|
31
40
|
rake (~> 12.0)
|
32
41
|
rspec (~> 3.9)
|
33
42
|
sqlite3 (~> 1.4, >= 1.4.2)
|
43
|
+
tty-progressbar
|
34
44
|
|
35
45
|
BUNDLED WITH
|
36
46
|
2.1.4
|
data/README.md
CHANGED
@@ -1,28 +1,24 @@
|
|
1
|
-
#
|
1
|
+
# CsvToSqlite3
|
2
2
|
|
3
|
-
|
3
|
+
CsvToSqlite3 is a gem that aims to work as a executable like rake or bundler, it servs as a Sqlite3 database generator from csv files. It is used to create and insert all data from one or more csv files to the same Sqlite3 database.
|
4
4
|
|
5
|
-
|
5
|
+
The ideia comes from a specific cenario when I needed to run complex sql queries on a huge database, and once I was afraid to run it directly in prolduction and it impacts in the operation I chose to run simples selects (select * from some_table when created_at > some_date) from each table used on my query, export to csv files than create a specific ruby script that create a sqlite3 database and run the inserts, thereby i can run any havy query on my local computer without generate any impact in the operation.
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
7
|
+
Once this cenario is quite common I decided to rewrite the script to work with any csv file exported from any table.
|
10
8
|
|
11
|
-
|
12
|
-
gem 'csv_to_sqlite'
|
13
|
-
```
|
9
|
+
## Installation
|
14
10
|
|
15
|
-
|
11
|
+
Installed with gem:
|
16
12
|
|
17
|
-
$
|
13
|
+
$ gem install csv_to_sqlite3
|
18
14
|
|
19
|
-
|
15
|
+
## Usage
|
20
16
|
|
21
|
-
|
17
|
+
Once the gem is installed it is aviable the command "csv_to_sqlite", to generate a new database you need to run on terminal "csv_to_sqlite path/to/file.csv'". It will create a file named data.sqlite3 and create a table with name equal to file name, case the table already exists it will only insert all data from this file.
|
22
18
|
|
23
|
-
|
19
|
+
$ csv_to_sqlite stores.csv
|
24
20
|
|
25
|
-
|
21
|
+
By running the command above it will be generated a database data.sqlite3 with a table named stores with all data included.
|
26
22
|
|
27
23
|
## Development
|
28
24
|
|
@@ -32,7 +28,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
28
|
|
33
29
|
## Contributing
|
34
30
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/joaofelipe1294/csv_to_sqlite3.
|
36
32
|
|
37
33
|
|
38
34
|
## License
|
data/Rakefile
CHANGED
@@ -6,17 +6,16 @@
|
|
6
6
|
# task :default => :spec
|
7
7
|
|
8
8
|
|
9
|
-
require_relative "lib/database"
|
10
|
-
require_relative "lib/csv_reader"
|
11
|
-
require_relative "lib/sql/create_table"
|
12
|
-
require_relative "lib/sql/insert"
|
9
|
+
# require_relative "lib/database"
|
10
|
+
# require_relative "lib/csv_reader"
|
11
|
+
# require_relative "lib/sql/create_table"
|
12
|
+
# require_relative "lib/sql/insert"
|
13
13
|
|
14
|
-
|
14
|
+
require_relative "lib/csv_to_sqlite"
|
15
|
+
|
16
|
+
task default: :convert
|
15
17
|
|
16
18
|
desc "Create tables used in sqlite"
|
17
|
-
task :
|
18
|
-
|
19
|
-
csv_table = CsvToSQLite::CsvReader.load_file './spec/test_files/test_file.csv'
|
20
|
-
CsvToSQLite::SQL::CreateTable.new(name: 'persons', csv_table: csv_table, connection: connection).run
|
21
|
-
CsvToSQLite::SQL::Insert.new(name: 'persons', csv_table: csv_table, connection: connection).run
|
19
|
+
task :convert do
|
20
|
+
CsvToSqlite::CsvToSQLite.new.convert(ARGV[1])
|
22
21
|
end
|
data/bin/csv_to_sqlite
CHANGED
data/csv_to_sqlite3.gemspec
CHANGED
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = ["joaofelipelopes"]
|
7
7
|
spec.email = ["joaofelipe1294@gmail.com"]
|
8
8
|
|
9
|
-
spec.summary = %q{An easy way to convert a csv file into
|
10
|
-
spec.description = %q{Add a
|
9
|
+
spec.summary = %q{An easy way to convert a csv file into SQLite3 database.}
|
10
|
+
spec.description = %q{Add the executable "csv_to_sqlite" to your terminal, wich is used to create a SQLite3 database with all data from a csv file by running "csv_to_sqlite file.csv". It was developed to be used as a command line.}
|
11
11
|
spec.homepage = "https://github.com/joaofelipe1294/csv_to_sqlite3"
|
12
12
|
spec.license = "MIT"
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
data/exe/csv_to_sqlite
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
module CsvToSqlite
|
2
|
+
class ArgumentHandlerService
|
3
|
+
|
4
|
+
def initialize argv
|
5
|
+
@argv = argv
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
return empty if @argv.empty?
|
10
|
+
return help if @argv.include? "-h"
|
11
|
+
:convert
|
12
|
+
end
|
13
|
+
|
14
|
+
def help
|
15
|
+
puts "Run csv_lto_sqlite3 is easy!"
|
16
|
+
puts "To convert a csv file into a sqlite3 database you just need to run the follow command from the same directory of the csv file:"
|
17
|
+
puts "csv_to_sqlite your_csv_file.csv"
|
18
|
+
puts "==============================="
|
19
|
+
puts "-t <table-name> -> parameter used to set wich table will be created/used"
|
20
|
+
puts "Some customisations will be aviable on next versions :)"
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def empty
|
25
|
+
puts "Run 'csv_to_sqlite -h' to receive some help or csv_to_sqlite csv_file.csv to generate a SQLite3 database from a csv file."
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/csv_reader.rb
CHANGED
@@ -4,8 +4,20 @@ module CsvToSqlite
|
|
4
4
|
|
5
5
|
class CsvReader
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def initialize file_path
|
8
|
+
@file_path = file_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def load_file
|
12
|
+
CSV.parse(File.read(@file_path), headers: true, col_sep: separator)
|
13
|
+
end
|
14
|
+
|
15
|
+
def separator
|
16
|
+
first_line = File.open(@file_path) do |file|
|
17
|
+
line = file.readline
|
18
|
+
return ";" if line.include? ";"
|
19
|
+
return "," if line.include? ","
|
20
|
+
end
|
9
21
|
end
|
10
22
|
|
11
23
|
end
|
data/lib/csv_to_sqlite.rb
CHANGED
@@ -3,21 +3,46 @@ require_relative "./database"
|
|
3
3
|
require_relative "./csv_reader"
|
4
4
|
require_relative "./sql/create_table"
|
5
5
|
require_relative "./sql/insert"
|
6
|
+
require_relative "./argument_handler_service"
|
6
7
|
|
7
8
|
module CsvToSqlite
|
8
9
|
# class Error < StandardError; end
|
9
10
|
|
10
11
|
class CsvToSQLite
|
11
12
|
|
13
|
+
def initialize argv
|
14
|
+
@args = argv
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
method = CsvToSqlite::ArgumentHandlerService.new(@args).call
|
19
|
+
self.convert(@args.first) unless method.nil?
|
20
|
+
end
|
21
|
+
|
12
22
|
def convert file_path
|
13
23
|
connection = CsvToSqlite::Database.new().connect
|
14
|
-
csv_table = CsvToSqlite::CsvReader.load_file
|
15
|
-
puts file_path
|
16
|
-
table_name = file_path.split("/").last.split(".csv").first
|
24
|
+
csv_table = CsvToSqlite::CsvReader.new(file_path).load_file
|
17
25
|
CsvToSqlite::SQL::CreateTable.new(name: table_name, csv_table: csv_table, connection: connection).run
|
18
26
|
CsvToSqlite::SQL::Insert.new(name: table_name, csv_table: csv_table, connection: connection).run
|
19
27
|
end
|
20
28
|
|
29
|
+
def table_name
|
30
|
+
if @args.include? "-t"
|
31
|
+
table_name_index = @args.find_index "-t"
|
32
|
+
return @args[table_name_index + 1]
|
33
|
+
else
|
34
|
+
return csv_file_name.split("/").last.split(".csv").first
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def csv_file_name
|
39
|
+
file_name = nil
|
40
|
+
@args.each_with_index do |param, index|
|
41
|
+
file_name = param if param.include? ".csv"
|
42
|
+
end
|
43
|
+
file_name
|
44
|
+
end
|
45
|
+
|
21
46
|
end
|
22
47
|
|
23
48
|
end
|
data/lib/sql/column.rb
CHANGED
@@ -8,7 +8,9 @@ module CsvToSqlite::SQL
|
|
8
8
|
|
9
9
|
def sql_for column
|
10
10
|
@data = @csv_table[column][0..2]
|
11
|
-
if
|
11
|
+
if null?
|
12
|
+
type = "TEXT DEFAULT NULL"
|
13
|
+
elsif int?
|
12
14
|
type = "INTEGER"
|
13
15
|
elsif float?
|
14
16
|
type = "FLOAT"
|
@@ -24,6 +26,11 @@ module CsvToSqlite::SQL
|
|
24
26
|
"#{column} #{type},"
|
25
27
|
end
|
26
28
|
|
29
|
+
def null?
|
30
|
+
match_cases = @data.map { |value| value == nil }
|
31
|
+
match_cases.any? { |value| value == true }
|
32
|
+
end
|
33
|
+
|
27
34
|
def int?
|
28
35
|
meta_comparing :Integer
|
29
36
|
end
|
data/lib/sql/insert.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "tty-progressbar"
|
2
|
+
|
1
3
|
module CsvToSqlite::SQL
|
2
4
|
|
3
5
|
class Insert
|
@@ -10,11 +12,14 @@ module CsvToSqlite::SQL
|
|
10
12
|
|
11
13
|
def run
|
12
14
|
columns = @csv_table.headers.sort
|
15
|
+
bar = TTY::ProgressBar.new("Inserting data [:bar] :percent", total: @csv_table.by_row.count)
|
13
16
|
@csv_table.by_row.each do |row|
|
14
17
|
values = columns.map { |column| row[column] }
|
15
18
|
question_marks = ("?," * columns.size).chop!
|
16
19
|
@connection.execute("INSERT INTO #{@name} (#{columns.join(',')}) VALUES (#{question_marks})", values)
|
20
|
+
bar.advance(1)
|
17
21
|
end
|
22
|
+
bar.finish
|
18
23
|
end
|
19
24
|
|
20
25
|
end
|
metadata
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_to_sqlite3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joaofelipelopes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Add
|
13
|
+
description: Add the executable "csv_to_sqlite" to your terminal, wich is used to
|
14
|
+
create a SQLite3 database with all data from a csv file by running "csv_to_sqlite
|
15
|
+
file.csv". It was developed to be used as a command line.
|
14
16
|
email:
|
15
17
|
- joaofelipe1294@gmail.com
|
16
18
|
executables:
|
@@ -18,6 +20,7 @@ executables:
|
|
18
20
|
extensions: []
|
19
21
|
extra_rdoc_files: []
|
20
22
|
files:
|
23
|
+
- ".DS_Store"
|
21
24
|
- ".gitignore"
|
22
25
|
- ".rspec"
|
23
26
|
- ".travis.yml"
|
@@ -31,6 +34,7 @@ files:
|
|
31
34
|
- bin/setup
|
32
35
|
- csv_to_sqlite3.gemspec
|
33
36
|
- exe/csv_to_sqlite
|
37
|
+
- lib/argument_handler_service.rb
|
34
38
|
- lib/csv_reader.rb
|
35
39
|
- lib/csv_to_sqlite.rb
|
36
40
|
- lib/csv_to_sqlite/version.rb
|
@@ -62,5 +66,5 @@ requirements: []
|
|
62
66
|
rubygems_version: 3.0.3
|
63
67
|
signing_key:
|
64
68
|
specification_version: 4
|
65
|
-
summary: An easy way to convert a csv file into
|
69
|
+
summary: An easy way to convert a csv file into SQLite3 database.
|
66
70
|
test_files: []
|