csv_to_sqlite3 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 146ef624bc9e0172c1ebdb287b6d906b51e9b34826b85d50937c244210148c50
4
+ data.tar.gz: 1353a3721872dbfe61334bf5bc4d6869799d5ab9439b955b421710a366d73ae1
5
+ SHA512:
6
+ metadata.gz: affe7184ffd215d3279735dfb3218253a271278eb36e8edb2173cae2afc892d486eb8fdf94cefca3eae291cb8739b06258217e581dac991851da3fe13dcfd1c2
7
+ data.tar.gz: 9bd61eab0dfe28211d5e5ce970627a029c9fa0209f70f1e03483e3c3a9d04cdfa2ecb68383bd622733b9fd96020d9a29b53a50df7d1503687aa8ddc51e99e0a2
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ *.gem
14
+
15
+ *.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in csv_to_sqlite.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem 'rspec', '~> 3.9'
8
+ gem 'sqlite3', '~> 1.4', '>= 1.4.2'
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ csv_to_sqlite3 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.2)
16
+ rspec-support (~> 3.9.3)
17
+ rspec-expectations (3.9.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.3)
24
+ sqlite3 (1.4.2)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ csv_to_sqlite3!
31
+ rake (~> 12.0)
32
+ rspec (~> 3.9)
33
+ sqlite3 (~> 1.4, >= 1.4.2)
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 joaofelipe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # CsvToSqlite
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'csv_to_sqlite'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install csv_to_sqlite
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csv_to_sqlite.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,22 @@
1
+ # require "bundler/gem_tasks"
2
+ # require "rspec/core/rake_task"
3
+ #
4
+ # RSpec::Core::RakeTask.new(:spec)
5
+ #
6
+ # task :default => :spec
7
+
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"
13
+
14
+ task default: :setup
15
+
16
+ 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
22
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csv_to_sqlite"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv_to_sqlite'
4
+ CsvToSqlite::CsvToSqlite.convert(ARGV[0])
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/csv_to_sqlite/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "csv_to_sqlite3"
5
+ spec.version = CsvToSqlite::VERSION
6
+ spec.authors = ["joaofelipelopes"]
7
+ spec.email = ["joaofelipe1294@gmail.com"]
8
+
9
+ spec.summary = %q{An easy way to convert a csv file into SQLite database.}
10
+ spec.description = %q{Add a better description.}
11
+ spec.homepage = "https://github.com/joaofelipe1294/csv_to_sqlite"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/joaofelipe1294/csv_to_sqlite"
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.executables << 'csv_to_sqlite'
27
+ spec.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv_to_sqlite'
4
+ CsvToSqlite::CsvToSQLite.new.convert(ARGV[0])
@@ -0,0 +1,13 @@
1
+ require "csv"
2
+
3
+ module CsvToSqlite
4
+
5
+ class CsvReader
6
+
7
+ def self.load_file file_path
8
+ CSV.parse(File.read(file_path), headers: true, col_sep: ';')
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,21 @@
1
+ require "csv_to_sqlite/version"
2
+ require_relative "./database"
3
+ require_relative "./csv_reader"
4
+ require_relative "./sql/create_table"
5
+ require_relative "./sql/insert"
6
+
7
+ module CsvToSqlite
8
+ # class Error < StandardError; end
9
+
10
+ class CsvToSQLite
11
+
12
+ def convert file_path
13
+ connection = CsvToSqlite::Database.new().connect
14
+ csv_table = CsvToSqlite::CsvReader.load_file file_path
15
+ CsvToSqlite::SQL::CreateTable.new(name: 'persons', csv_table: csv_table, connection: connection).run
16
+ CsvToSqlite::SQL::Insert.new(name: 'persons', csv_table: csv_table, connection: connection).run
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ module CsvToSqlite
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'sqlite3'
2
+
3
+ module CsvToSqlite
4
+ class Database
5
+
6
+ def initialize db_name: "data.sqlite3"
7
+ @db_name = db_name
8
+ @target_dir = target_dir
9
+ @database_path = "#{@target_dir}/#{@db_name}"
10
+ end
11
+
12
+ def create
13
+ if File.exist? @database_path
14
+ puts "Database found"
15
+ else
16
+ puts "Creating database"
17
+ SQLite3::Database.new @database_path
18
+ end
19
+ end
20
+
21
+ def connect
22
+ create
23
+ SQLite3::Database.new @database_path
24
+ end
25
+
26
+ private
27
+
28
+ def target_dir
29
+ return './spec/src/' if ENV.key? "test"
30
+ '.'
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,84 @@
1
+ module CsvToSqlite::SQL
2
+
3
+ class Column
4
+
5
+ def initialize csv_table:
6
+ @csv_table = csv_table
7
+ end
8
+
9
+ def sql_for column
10
+ @data = @csv_table[column][0..2]
11
+ if int?
12
+ type = "INTEGER"
13
+ elsif float?
14
+ type = "FLOAT"
15
+ elsif boolean?
16
+ type = "BOOLEAN"
17
+ elsif datetime?
18
+ type = "DATETIME"
19
+ elsif date?
20
+ type = "DATE"
21
+ else
22
+ type = "TEXT"
23
+ end
24
+ "#{column} #{type},"
25
+ end
26
+
27
+ def int?
28
+ meta_comparing :Integer
29
+ end
30
+
31
+ def float?
32
+ meta_comparing :Float
33
+ end
34
+
35
+ def boolean?
36
+ match_cases = @data.map do |value|
37
+ return true if value == 't' or value == 'f' or value == 'true' or value == 'false'
38
+ return false
39
+ end
40
+ match_cases.all { |value| value == true }
41
+ end
42
+
43
+ def date?
44
+ match_cases = @data.map do |value|
45
+ begin
46
+ Date.parse value
47
+ return true unless value.include? ":"
48
+ return false
49
+ rescue ArgumentError
50
+ return false
51
+ end
52
+ end
53
+ match_cases.all { |value| value == true }
54
+ end
55
+
56
+ def datetime?
57
+ match_cases = @data.map do |value|
58
+ begin
59
+ DateTime.parse value
60
+ return true if value.include? ":"
61
+ return false
62
+ rescue ArgumentError
63
+ return false
64
+ end
65
+ end
66
+ match_cases.all { |value| value == true }
67
+ end
68
+
69
+ def meta_comparing type
70
+ match_cases = @data.map do |value|
71
+ begin
72
+ method = Kernel.method(type)
73
+ method.call value
74
+ return true
75
+ rescue ArgumentError
76
+ return false
77
+ end
78
+ end
79
+ match_cases.all { |value| value == true }
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,38 @@
1
+ require_relative './column'
2
+ require_relative '../database'
3
+
4
+ module CsvToSqlite::SQL
5
+
6
+ class CreateTable
7
+
8
+ def initialize name:, csv_table:, connection:
9
+ @connection = connection
10
+ @name = name
11
+ @csv_table = csv_table
12
+ end
13
+
14
+ def run
15
+ puts "========== Creating table #{@name} =========="
16
+ @connection.execute sql
17
+ puts "=================================================="
18
+ end
19
+
20
+ def sql
21
+ "#{create_statement} ( #{columns} );"
22
+ end
23
+
24
+ def create_statement
25
+ "CREATE TABLE IF NOT EXISTS #{@name}"
26
+ end
27
+
28
+ def columns
29
+ @column_generator = CsvToSqlite::SQL::Column.new(csv_table: @csv_table)
30
+ columns_sql = @csv_table.headers.map do |column|
31
+ @column_generator.sql_for column
32
+ end
33
+ columns_sql.join.chop
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,22 @@
1
+ module CsvToSqlite::SQL
2
+
3
+ class Insert
4
+
5
+ def initialize connection:, csv_table:, name:
6
+ @connection = connection
7
+ @csv_table = csv_table
8
+ @name = name
9
+ end
10
+
11
+ def run
12
+ columns = @csv_table.headers.sort
13
+ @csv_table.by_row.each do |row|
14
+ values = columns.map { |column| row[column] }
15
+ question_marks = ("?," * columns.size).chop!
16
+ @connection.execute("INSERT INTO #{@name} (#{columns.join(',')}) VALUES (#{question_marks})", values)
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_to_sqlite3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joaofelipelopes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Add a better description.
14
+ email:
15
+ - joaofelipe1294@gmail.com
16
+ executables:
17
+ - csv_to_sqlite
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/csv_to_sqlite
31
+ - bin/setup
32
+ - csv_to_sqlite3.gemspec
33
+ - exe/csv_to_sqlite
34
+ - lib/csv_reader.rb
35
+ - lib/csv_to_sqlite.rb
36
+ - lib/csv_to_sqlite/version.rb
37
+ - lib/database.rb
38
+ - lib/sql/column.rb
39
+ - lib/sql/create_table.rb
40
+ - lib/sql/insert.rb
41
+ homepage: https://github.com/joaofelipe1294/csv_to_sqlite
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/joaofelipe1294/csv_to_sqlite
46
+ source_code_uri: https://github.com/joaofelipe1294/csv_to_sqlite
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: An easy way to convert a csv file into SQLite database.
66
+ test_files: []