csv_to_sqlite3 0.1.0 → 0.1.3.2
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.lock +1 -1
- data/README.md +12 -16
- data/Rakefile +9 -10
- data/bin/csv_to_sqlite +0 -1
- data/csv_to_sqlite3.gemspec +4 -4
- data/exe/csv_to_sqlite +3 -1
- data/lib/argument_handler_service.rb +29 -0
- data/lib/csv_reader.rb +14 -2
- data/lib/csv_to_sqlite.rb +30 -3
- data/lib/csv_to_sqlite/version.rb +1 -1
- data/lib/sql/column.rb +8 -1
- metadata +12 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 709acaceea756a2fab95c45bc156de01f41050fa6ec82461659a2779728e50f4
|
4
|
+
data.tar.gz: 16d5d03ec204d8f1f4fc2fe51a629be1333f60cc788187ac01c966de674c8b52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f69c8c7f24386a62b21cc6c21a775b335b86312cb81bb8ddb58bf171036a87f5e08799b2a7e42c6043ba50dd836edae20e96c278a501ce7c072d0ba9ff78e8f5
|
7
|
+
data.tar.gz: 07053a0b2a2d559ea7cf2e276c2372b3517557e1174b13d6f2ad17c09aa86f7bf18f090021db3b1647e6442a1b5730eabc5b82fc8ad77421359b1428af95d6bc
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
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,14 +6,14 @@ 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
|
11
|
-
spec.homepage = "https://github.com/joaofelipe1294/
|
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
|
+
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")
|
14
14
|
|
15
15
|
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
-
spec.metadata["source_code_uri"] = "https://github.com/joaofelipe1294/
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/joaofelipe1294/csv_to_sqlite3"
|
17
17
|
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
18
18
|
|
19
19
|
# Specify which files should be added to the gem when it is released.
|
data/exe/csv_to_sqlite
CHANGED
@@ -0,0 +1,29 @@
|
|
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 "Some customisations will be aviable on next versions :)"
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def empty
|
24
|
+
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."
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
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,17 +3,44 @@ 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
|
-
CsvToSqlite::SQL::CreateTable.new(name:
|
16
|
-
CsvToSqlite::SQL::Insert.new(name:
|
24
|
+
csv_table = CsvToSqlite::CsvReader.new(file_path).load_file
|
25
|
+
CsvToSqlite::SQL::CreateTable.new(name: table_name, csv_table: csv_table, connection: connection).run
|
26
|
+
CsvToSqlite::SQL::Insert.new(name: table_name, csv_table: csv_table, connection: connection).run
|
27
|
+
end
|
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
|
17
44
|
end
|
18
45
|
|
19
46
|
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
|
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.2
|
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-25 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
|
@@ -38,12 +42,12 @@ files:
|
|
38
42
|
- lib/sql/column.rb
|
39
43
|
- lib/sql/create_table.rb
|
40
44
|
- lib/sql/insert.rb
|
41
|
-
homepage: https://github.com/joaofelipe1294/
|
45
|
+
homepage: https://github.com/joaofelipe1294/csv_to_sqlite3
|
42
46
|
licenses:
|
43
47
|
- MIT
|
44
48
|
metadata:
|
45
|
-
homepage_uri: https://github.com/joaofelipe1294/
|
46
|
-
source_code_uri: https://github.com/joaofelipe1294/
|
49
|
+
homepage_uri: https://github.com/joaofelipe1294/csv_to_sqlite3
|
50
|
+
source_code_uri: https://github.com/joaofelipe1294/csv_to_sqlite3
|
47
51
|
post_install_message:
|
48
52
|
rdoc_options: []
|
49
53
|
require_paths:
|
@@ -59,8 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
63
|
- !ruby/object:Gem::Version
|
60
64
|
version: '0'
|
61
65
|
requirements: []
|
62
|
-
rubygems_version: 3.
|
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: []
|