csvql 0.2.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8058641699d9c3f61519e20c5c82bf025d63b2bf
4
- data.tar.gz: 6ce31e76053b3864bc1f15d34ce93556c9161e6e
3
+ metadata.gz: 0dde4233623d84d9417709ae86a5cc854ed8a6e5
4
+ data.tar.gz: 36e69e39b2f9c5a5dce0699942afff427f4ab753
5
5
  SHA512:
6
- metadata.gz: 73f1c1962c82f3724b310c81ccdf62892f67cd6ad98c4106bab16232d6a62ba7db89dc058319f00b60633bbda794f02979357653642b846beb7339b830502d99
7
- data.tar.gz: 47a073fe0eccc876cb515db3b894823a0a7149e5be8e1a6eccb00820b38a0d2783cdd8cc4ba77a17586bfdc8b643c66e1826357dd6c4034bb1296ca83926760a
6
+ metadata.gz: 2673f91dd259536d838e20d4e1d12e9493d972ce2a0151be669cbb20be983f1481a4d111ade364d5a72aab92719eec00d051b6943f91373ed1667dbac54b4bd6
7
+ data.tar.gz: 072b5d5f0aae880362953600b288f3c412d771f0313eb213137e21209a269160dd7f3b8f2e7b2ec01eaecc08842b5c9c5f33bb3079f796f0c24064945a6db7f6
data/lib/csvql/csvql.rb CHANGED
@@ -25,6 +25,7 @@ module Csvql
25
25
  end
26
26
 
27
27
  def create_alias(table, view="tbl")
28
+ return if table == view
28
29
  exec "DROP VIEW IF EXISTS #{view}"
29
30
  exec "CREATE VIEW #{view} AS SELECT * FROM #{table}"
30
31
  end
data/lib/csvql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Csvql
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
data/lib/csvql.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'csv'
4
4
  require 'nkf'
5
5
  require 'optparse'
6
+ require 'ostruct'
6
7
 
7
8
  require "csvql/csvql"
8
9
  require "csvql/version"
@@ -10,62 +11,62 @@ require "csvql/version"
10
11
  module Csvql
11
12
  class << self
12
13
  def option_parse(argv)
13
- opt = OptionParser.new("Usage: csvql [csvfile] [options]")
14
- option = {}
14
+ opt_parser = OptionParser.new("Usage: csvql [csvfile] [options]")
15
+ opt = OpenStruct.new
15
16
 
16
17
  # default
17
18
  # option[:header] = true
18
19
 
19
- opt.on("--console", "After all commands are run, open sqlite3 console with this data") {|v| option[:console] = v }
20
- opt.on("--[no-]header", "Treat file as having the first row as a header row") {|v| option[:header] = v }
21
- opt.on('--output-dlm="|"', "Output delimiter (|)") {|v| option[:output_dlm] = v }
22
- opt.on("--save-to=FILE", "If set, sqlite3 db is left on disk at this path") {|v| option[:save_to] = v }
23
- opt.on("--append", "Append mode (not dropping any tables)") {|v| option[:append] = v }
24
- opt.on("--skip-comment", "Skip comment lines start with '#'") {|v| option[:skip_comment] = v }
25
- opt.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| option[:source] = v }
26
- opt.on("--sql=SQL", "SQL Command(s) to run on the data") {|v| option[:sql] = v }
27
- opt.on("--select=COLUMN", "Select column (*)") {|v| option[:select] = v }
28
- opt.on("--schema=FILE or STRING", "Specify a table schema") {|v| option[:schema] = v }
29
- opt.on("--strip", "Strip spaces around columns") {|v| option[:strip] = v }
30
- opt.on("--where=COND", "Where clause") {|v| option[:where] = v }
31
- opt.on("--table-name=NAME", "Override the default table name (tbl)") {|v| option[:table_name] = v }
32
- opt.on("--verbose", "Enable verbose logging") {|v| option[:verbose] = v }
33
- opt.parse!(argv)
20
+ opt_parser.on("--console", "After all commands are run, open sqlite3 console with this data") {|v| opt.console = v }
21
+ opt_parser.on("--[no-]header", "Treat file as having the first row as a header row") {|v| opt.header = v }
22
+ opt_parser.on('--output-dlm="|"', "Output delimiter (|)") {|v| opt.output_dlm = v }
23
+ opt_parser.on("--save-to=FILE", "If set, sqlite3 db is left on disk at this path") {|v| opt.save_to = v }
24
+ opt_parser.on("--append", "Append mode (not dropping any tables)") {|v| opt.append = v }
25
+ opt_parser.on("--skip-comment", "Skip comment lines start with '#'") {|v| opt.skip_comment = v }
26
+ opt_parser.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| opt.source = v }
27
+ opt_parser.on("--sql=SQL", "SQL Command(s) to run on the data") {|v| opt.sql = v }
28
+ opt_parser.on("--select=COLUMN", "Select column (*)") {|v| opt.select = v }
29
+ opt_parser.on("--schema=FILE or STRING", "Specify a table schema") {|v| opt.schema = v }
30
+ opt_parser.on("--strip", "Strip spaces around columns") {|v| opt.strip = v }
31
+ opt_parser.on("--where=COND", "Where clause") {|v| opt.where = v }
32
+ opt_parser.on("--table-name=NAME", "Override the default table name (tbl)") {|v| opt.table_name = v }
33
+ opt_parser.on("--verbose", "Enable verbose logging") {|v| opt.verbose = v }
34
+ opt_parser.parse!(argv)
34
35
 
35
- option[:source] ||= argv[0]
36
- # option[:where] ||= argv[1]
37
- option[:table_name] ||= if option[:save_to] && option[:source] != nil
38
- File.basename(option[:source].downcase, ".csv").gsub(/\./, "_")
36
+ opt.source ||= argv[0]
37
+ # opt.where] ||= argv[1]
38
+ opt.table_name ||= if opt.save_to && opt.source != nil
39
+ File.basename(opt.source.downcase, ".csv").gsub(/\./, "_")
39
40
  else
40
41
  "tbl"
41
42
  end
42
- if option[:output_dlm] == 'tab'
43
- option[:output_dlm] = "\t"
43
+ if opt.output_dlm == 'tab'
44
+ opt.output_dlm = "\t"
44
45
  end
45
- option[:output_dlm] ||= "|"
46
+ opt.output_dlm ||= "|"
46
47
 
47
- if option[:completion]
48
+ if opt.completion
48
49
  puts opt.compsys('csvql')
49
50
  exit 0
50
51
  end
51
- option
52
+ opt
52
53
  end
53
54
 
54
55
  def run(argv)
55
- option = option_parse(argv)
56
- if option[:console] && option[:source] == nil
56
+ opt = option_parse(argv)
57
+ if opt.console && opt.source == nil
57
58
  puts "Can not open console with pipe input, read a file instead"
58
59
  exit 1
59
60
  end
60
- if option[:sql] && (option[:select] || option[:where])
61
+ if opt.sql && (opt.select || opt.where)
61
62
  puts "Can not use --sql option and --select|--where option at the same time."
62
63
  exit 1
63
64
  end
64
65
 
65
- csvfile = option[:source] ? File.open(option[:source]) : $stdin
66
+ csvfile = opt.source ? File.open(opt.source) : $stdin
66
67
  first_line = csvfile.readline
67
68
 
68
- schema = option[:schema]
69
+ schema = opt.schema
69
70
  if schema
70
71
  file = File.expand_path(schema)
71
72
  if File.exist?(file)
@@ -73,43 +74,43 @@ module Csvql
73
74
  end
74
75
  else
75
76
  cols = first_line.parse_csv
76
- col_name = if option[:header]
77
+ col_name = if opt.header
77
78
  cols
78
79
  else
79
80
  cols.size.times.map {|i| "c#{i}" }
80
81
  end
81
82
  schema = col_name.map {|c| "#{c} NONE" }.join(",")
82
83
  end
83
- csvfile.rewind unless option[:header]
84
+ csvfile.rewind unless opt.header
84
85
 
85
- tbl = TableHandler.new(option[:save_to], option[:console])
86
- tbl.drop_table(option[:table_name]) unless option[:append]
87
- tbl.create_table(schema, option[:table_name])
88
- tbl.create_alias(option[:table_name]) if option[:save_to] && option[:table_name] != "tbl"
86
+ tbl = TableHandler.new(opt.save_to, opt.console)
87
+ tbl.drop_table(opt.table_name) unless opt.append
88
+ tbl.create_table(schema, opt.table_name)
89
+ tbl.create_alias(opt.table_name) if opt.save_to
89
90
  tbl.exec("PRAGMA synchronous=OFF")
90
91
  tbl.exec("BEGIN TRANSACTION")
91
92
  csvfile.each.with_index(1) do |line,i|
92
93
  line = NKF.nkf('-w', line).strip
93
94
  next if line.size == 0
94
- next if option[:skip_comment] && line.start_with?("#")
95
+ next if opt.skip_comment && line.start_with?("#")
95
96
  row = line.parse_csv
96
- row.map!(&:strip) if option[:strip]
97
+ row.map!(&:strip) if opt.strip
97
98
  tbl.insert(row, i)
98
99
  end
99
100
  tbl.exec("COMMIT TRANSACTION")
100
101
 
101
- if option[:sql]
102
- sql = option[:sql]
103
- elsif option[:select] || option[:where]
104
- option[:select] ||= "*"
105
- sql = "select #{option[:select]} from #{option[:table_name]}"
106
- if option[:where]
107
- sql += " where (#{option[:where]})"
102
+ if opt.sql
103
+ sql = opt.sql
104
+ elsif opt.select || opt.where
105
+ opt.select ||= "*"
106
+ sql = "select #{opt.select} from #{opt.table_name}"
107
+ if opt.where
108
+ sql += " where (#{opt.where})"
108
109
  end
109
110
  end
110
111
 
111
- tbl.exec(sql).each {|row| puts row.join(option[:output_dlm]) } if sql
112
- tbl.open_console if option[:console]
112
+ tbl.exec(sql).each {|row| puts row.join(opt.output_dlm) } if sql
113
+ tbl.open_console if opt.console
113
114
  end
114
115
  end
115
116
  end
data/spec/csvql_spec.rb CHANGED
@@ -9,7 +9,7 @@ describe Csvql do
9
9
 
10
10
  it 'select name' do
11
11
  expect(capture {
12
- Csvql.run([csvfile, "--select", "name"])
12
+ Csvql.run([csvfile, "--header", "--select", "name"])
13
13
  }).to eq(<<EOL)
14
14
  Anne
15
15
  Bob
@@ -21,7 +21,7 @@ EOL
21
21
 
22
22
  it 'where age > 40' do
23
23
  expect(capture {
24
- Csvql.run([csvfile, "--where", "age > 40"])
24
+ Csvql.run([csvfile, "--header", "--where", "age > 40"])
25
25
  }).to eq(<<EOL)
26
26
  3|Charry|48
27
27
  5|Edward|52
@@ -30,7 +30,7 @@ EOL
30
30
 
31
31
  it 'sql option' do
32
32
  expect(capture {
33
- Csvql.run([csvfile, "--sql", "select name,age from tbl where age between 20 and 40"])
33
+ Csvql.run([csvfile, "--header", "--sql", "select name,age from tbl where age between 20 and 40"])
34
34
  }).to eq(<<EOL)
35
35
  Anne|33
36
36
  Bob|25
@@ -39,7 +39,7 @@ EOL
39
39
 
40
40
  it 'change output delimiter' do
41
41
  expect(capture {
42
- Csvql.run([csvfile, "--where", "id = 3", "--output-dlm", ","])
42
+ Csvql.run([csvfile, "--header", "--where", "id = 3", "--output-dlm", ","])
43
43
  }).to eq(<<EOL)
44
44
  3,Charry,48
45
45
  EOL
@@ -47,7 +47,7 @@ EOL
47
47
 
48
48
  it 'change table name' do
49
49
  expect(capture {
50
- Csvql.run([csvfile, "--sql", "select id,name from users where id >= 4", "--table-name", "users"])
50
+ Csvql.run([csvfile, "--header", "--sql", "select id,name from users where id >= 4", "--table-name", "users"])
51
51
  }).to eq(<<EOL)
52
52
  4|Daniel
53
53
  5|Edward
@@ -56,7 +56,7 @@ EOL
56
56
 
57
57
  it 'save-to db file' do
58
58
  dbfile = "csvql_test.db"
59
- Csvql.run([csvfile, "--save-to", dbfile])
59
+ Csvql.run([csvfile, "--header", "--save-to", dbfile])
60
60
  expect(`sqlite3 #{dbfile} "select * from tbl"`).to eq(<<EOL)
61
61
  1|Anne|33
62
62
  2|Bob|25
@@ -77,7 +77,7 @@ EOL
77
77
 
78
78
  it 'source option' do
79
79
  expect(capture {
80
- Csvql.run(["--source", csvfile, "--select", "count(*)"])
80
+ Csvql.run(["--source", csvfile, "--header", "--select", "count(*)"])
81
81
  }).to eq(<<EOL)
82
82
  5
83
83
  EOL
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - YANO Satoru