csvql 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/bin/csvql +6 -0
- data/csvql.gemspec +25 -0
- data/lib/csvql/csvql.rb +100 -0
- data/lib/csvql/version.rb +3 -0
- data/lib/csvql.rb +6 -0
- data/spec/csvql_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cd5503b923231ad59d08bbc77597860c4f9a711
|
4
|
+
data.tar.gz: f92d26460d9e657a1ef0c3b0990060e00b5b9bb4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45f5ee87afaaf81547da46f33b18a372f35efab8275f8937cf12ff822a197fd7bf4498293cc418215a971ca9cb832a6c8e6efcc221de0ed7a91cf6e5a0106f7b
|
7
|
+
data.tar.gz: 0d7139ffbfea264e79e7b7f2aeced813454d652041db8a79bb5be8cff0c71a27a7c85769b238e30a21bfe608d40746c280aaf50cabcb3e45cbfbbfd5730b4d0d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 YANO Satoru
|
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,29 @@
|
|
1
|
+
# Csvql
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'csvql'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install csvql
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/csvql/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/csvql
ADDED
data/csvql.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 'csvql/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csvql"
|
8
|
+
spec.version = Csvql::VERSION
|
9
|
+
spec.authors = ["YANO Satoru"]
|
10
|
+
spec.email = ["s-yano@pb.jp.nec.com"]
|
11
|
+
spec.summary = %q{csvql}
|
12
|
+
spec.description = ""
|
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
|
+
|
21
|
+
# spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
# spec.add_development_dependency "rake"
|
23
|
+
# spec.add_development_dependency "rspec"
|
24
|
+
spec.add_runtime_dependency "sqlite3"
|
25
|
+
end
|
data/lib/csvql/csvql.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
require 'csv'
|
5
|
+
require 'nkf'
|
6
|
+
require 'tempfile'
|
7
|
+
require 'optparse'
|
8
|
+
require 'sqlite3'
|
9
|
+
|
10
|
+
module Csvql
|
11
|
+
class TableHandler
|
12
|
+
def initialize(path, console)
|
13
|
+
@db_file = if path && path.size > 0
|
14
|
+
path
|
15
|
+
elsif console
|
16
|
+
@tmp_file = true
|
17
|
+
Tempfile.new("csvql").path + ".sqlite3"
|
18
|
+
else
|
19
|
+
":memory:"
|
20
|
+
end
|
21
|
+
@db = SQLite3::Database.new(@db_file)
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_table(cols, header, table_name="tbl")
|
25
|
+
@col_size = cols.size
|
26
|
+
@table_name = table_name
|
27
|
+
col = if header
|
28
|
+
cols.map {|c| "#{c} TEXT" }
|
29
|
+
else
|
30
|
+
@col_size.times.map {|i| "c#{i} TEXT" }
|
31
|
+
end
|
32
|
+
sql = "CREATE TABLE #{@table_name} (#{col.join(",")});"
|
33
|
+
@db.execute(sql)
|
34
|
+
end
|
35
|
+
|
36
|
+
def insert(cols, line)
|
37
|
+
if cols.size != @col_size
|
38
|
+
puts "line #{line}: wrong number of fields in line"
|
39
|
+
return
|
40
|
+
end
|
41
|
+
col = cols.map {|c| "'#{c}'" }
|
42
|
+
sql = "INSERT INTO #{@table_name} VALUES(#{col.join(",")});"
|
43
|
+
@db.execute(sql)
|
44
|
+
end
|
45
|
+
|
46
|
+
def exec(sql)
|
47
|
+
@db.execute(sql) do |row|
|
48
|
+
puts row.join("|")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def open_console
|
53
|
+
system("sqlite3", @db_file)
|
54
|
+
File.delete(@db_file) if @tmp_file
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class << self
|
59
|
+
def option_parse(argv)
|
60
|
+
opt = OptionParser.new
|
61
|
+
option = {}
|
62
|
+
|
63
|
+
opt.on("--console", "After all commands are run, open sqlite3 console with this data") {|v| option[:console] = v }
|
64
|
+
opt.on("--header", "Treat file as having the first row as a header row") {|v| option[:header] = v }
|
65
|
+
opt.on("--save-to=FILE", "If set, sqlite3 db is left on disk at this path") {|v| option[:save_to] = v }
|
66
|
+
opt.on("--skip-comment", "Skip comment lines start with '#'") {|v| option[:skip_comment] = v }
|
67
|
+
opt.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| option[:source] = v }
|
68
|
+
opt.on("--sql=SQL", "SQL Command(s) to run on the data") {|v| option[:sql] = v }
|
69
|
+
opt.on("--table-name=NAME", "Override the default table name (tbl)") {|v| option[:table_name] = v }
|
70
|
+
opt.on("--verbose", "Enable verbose logging") {|v| option[:verbose] = v }
|
71
|
+
opt.parse!(argv)
|
72
|
+
|
73
|
+
option
|
74
|
+
end
|
75
|
+
|
76
|
+
def run(argv)
|
77
|
+
option = option_parse(argv)
|
78
|
+
if option[:console] && option[:source] == nil
|
79
|
+
puts "Can not open console with pipe input, read a file instead"
|
80
|
+
exit 1
|
81
|
+
end
|
82
|
+
|
83
|
+
tbl = TableHandler.new(option[:save_to], option[:console])
|
84
|
+
csvfile = option[:source] ? File.open(option[:source]) : STDIN
|
85
|
+
csvfile.each.with_index(1) do |line,i|
|
86
|
+
line = line.strip
|
87
|
+
next if option[:skip_comment] && line.start_with?("#")
|
88
|
+
row = NKF.nkf('-w', line).parse_csv
|
89
|
+
if i == 1
|
90
|
+
tbl.create_table(row, option[:header], option[:table_name]||"tbl")
|
91
|
+
next if option[:header]
|
92
|
+
end
|
93
|
+
tbl.insert(row, i)
|
94
|
+
end
|
95
|
+
|
96
|
+
tbl.exec(option[:sql]) if option[:sql]
|
97
|
+
tbl.open_console if option[:console]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/csvql.rb
ADDED
data/spec/csvql_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csvql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- YANO Satoru
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sqlite3
|
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
|
+
description: ''
|
28
|
+
email:
|
29
|
+
- s-yano@pb.jp.nec.com
|
30
|
+
executables:
|
31
|
+
- csvql
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- bin/csvql
|
43
|
+
- csvql.gemspec
|
44
|
+
- lib/csvql.rb
|
45
|
+
- lib/csvql/csvql.rb
|
46
|
+
- lib/csvql/version.rb
|
47
|
+
- spec/csvql_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: ''
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.2.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: csvql
|
73
|
+
test_files:
|
74
|
+
- spec/csvql_spec.rb
|
75
|
+
- spec/spec_helper.rb
|