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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da996b0856a8d3474dfe2bd58b34bc96956874c16cb58281bec0d0fb36f81cf7
4
- data.tar.gz: 92e9180d09351aa8de077b8d6da2f8176a64d2d62123c2c7d5feb93ae0712c4a
3
+ metadata.gz: 210e5e06c4338355c22dc3c8d84b7026b4878a4b6b5a4e34712e311e226a7b24
4
+ data.tar.gz: d8d5eae006f16edf17fb90a40cec72e8ecfe653258883161692be39f4b430be5
5
5
  SHA512:
6
- metadata.gz: 82f5f4921be989aec8fdda9dbca21475bfde61295eab6a8c810c53b31f821a76637f8a8970e99d75a32816f9892327bfb1e303df0b3e7ab644981c2be9f33795
7
- data.tar.gz: 926f816917dae6073a2218129fb149c5ea09dd894937babac3b18bcf61dd0f9611e97f4d5004878e17896d881f4e1c92b3cf1afbd9d1199c9f7c351c01b4adb3
6
+ metadata.gz: 7ab7649056cc987ff15015f8d542b43df7eab74cf63e94edd9cc4fa15ba4a0560ef0e03af33db3d0208665912a0d0b8756c56bd365222570f2ebb613b5859c36
7
+ data.tar.gz: f7272550f558ba70220e7f0fac18e273cd2a0b57a0a8571d7c2a76dd58176136ae1b8d10ee42b8c7e19d9b6a7987ba5a9c0ec940ab4b48f76cee59caea361a36
Binary file
data/.gitignore CHANGED
@@ -13,3 +13,4 @@
13
13
  *.gem
14
14
 
15
15
  *.sqlite3
16
+ examples/
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gemspec
6
6
  gem "rake", "~> 12.0"
7
7
  gem 'rspec', '~> 3.9'
8
8
  gem 'sqlite3', '~> 1.4', '>= 1.4.2'
9
+ gem 'tty-progressbar'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csv_to_sqlite3 (0.1.0)
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
- # CsvToSqlite
1
+ # CsvToSqlite3
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csv_to_sqlite`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- ## Installation
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
- ```ruby
12
- gem 'csv_to_sqlite'
13
- ```
9
+ ## Installation
14
10
 
15
- And then execute:
11
+ Installed with gem:
16
12
 
17
- $ bundle install
13
+ $ gem install csv_to_sqlite3
18
14
 
19
- Or install it yourself as:
15
+ ## Usage
20
16
 
21
- $ gem install csv_to_sqlite
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
- ## Usage
19
+ $ csv_to_sqlite stores.csv
24
20
 
25
- TODO: Write usage instructions here
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/[USERNAME]/csv_to_sqlite.
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
- task default: :setup
14
+ require_relative "lib/csv_to_sqlite"
15
+
16
+ task default: :convert
15
17
 
16
18
  desc "Create tables used in sqlite"
17
- task :setup do
18
- connection = CsvToSQLite::Database.new().connect
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
@@ -1,4 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'csv_to_sqlite'
4
- CsvToSqlite::CsvToSqlite.convert(ARGV[0])
@@ -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 SQLite database.}
10
- spec.description = %q{Add a better description.}
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")
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'csv_to_sqlite'
4
- CsvToSqlite::CsvToSQLite.new.convert(ARGV[0])
4
+
5
+ CsvToSqlite::CsvToSQLite.new(ARGV).run
6
+
@@ -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
@@ -4,8 +4,20 @@ module CsvToSqlite
4
4
 
5
5
  class CsvReader
6
6
 
7
- def self.load_file file_path
8
- CSV.parse(File.read(file_path), headers: true, col_sep: ';')
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
@@ -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 file_path
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
@@ -1,3 +1,3 @@
1
1
  module CsvToSqlite
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3.3"
3
3
  end
@@ -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 int?
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
@@ -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.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-09-21 00:00:00.000000000 Z
11
+ date: 2020-10-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Add a better description.
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 SQLite database.
69
+ summary: An easy way to convert a csv file into SQLite3 database.
66
70
  test_files: []