csvql 0.1.4 → 0.1.5

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: 13dbfaea6b45323f789361d04dc5ad1889144ee5
4
- data.tar.gz: d9767a31cb0260261d446c18c8f8b3320a2649b8
3
+ metadata.gz: 97d8dadd3d422fd894edcd30cf17af06332d570a
4
+ data.tar.gz: 501c3926dccc65e86367aa9d82fe9192df4c0599
5
5
  SHA512:
6
- metadata.gz: 75e2cb12c515b7f96f96245a513d18c80b31b974a144f78e69807b895bc247b5441ae27966116e275e17df684725a5f286b36e3e956465008be494271bf3dfe8
7
- data.tar.gz: 0b0a405717981ef54ffa4f7b407fcda8f7af71864b043bbc9a5841e0953e5a6968d3d5b658574223e3d6be002edafbaa3ca9bb767b5bf9cb095b7a131e8e8372
6
+ metadata.gz: 5f96a8f0bf6162fcf2e710e1b79050aef0ab75bb04350a386da81c5cbf5d5077ee7dd1f9d96111956b61d040a6b4b68f2347286fa17ab57dbe4a1c8a4c1f15b6
7
+ data.tar.gz: 8a6559ecfdf815918602a8d0e96a1513896a730a1a34c2ce4db602aa04e893356faca9785abe8a46ccd54abb0c0c582291a205ee5b05e8de2ca46fd2700203b8
data/README.md CHANGED
@@ -56,6 +56,11 @@ From stdin:
56
56
  $ cat sample.csv | csvql --header --sql "select max(age) from tbl"
57
57
  52
58
58
 
59
+ And you can specify source csvfile at the first argment, instead of --source option or stdin:
60
+
61
+ $ csvql sample.csv --header --sql "select max(age) from tbl"
62
+ 52
63
+
59
64
  Save to db-file:
60
65
 
61
66
  $ csvql --source sample.csv --header --save-to test.db
@@ -68,26 +73,36 @@ Save to db-file:
68
73
 
69
74
  Change output delimiter:
70
75
 
71
- $ csvql --source sample.csv --header -sql "select * from tbl where age > 30" --output-dlm ","
72
- 2,Bob,25
73
- 4,Daniel,16
74
- $ csvql --source sample.csv --header -sql "select * from tbl where age > 30" --output-dlm=tab
75
- 2 Bob 25
76
- 4 Daniel 16
76
+ $ csvql --source sample.csv --header --sql "select * from tbl where age > 30" --output-dlm ","
77
+ 1,Anne,33
78
+ 3,Charry,48
79
+ 5,Edward,52
80
+ $ csvql --source sample.csv --header --sql "select * from tbl where age > 30" --output-dlm=tab
81
+ 1 Anne 33
82
+ 3 Charry 48
83
+ 5 Edward 52
84
+
85
+ Only where clause:
86
+
87
+ $ csvql sample.csv --header --where "age between 30 and 50"
88
+ 1|Anne|33
89
+ 3|Charry|48
77
90
 
78
91
  Options:
79
92
 
80
93
  $ svql --help
81
- Usage: csvql [options]
82
- --console After all commands are run, open sqlite3 console with this data
83
- --header Treat file as having the first row as a header row
84
- --output-dlm="|" Output delimiter (|)
85
- --save-to=FILE If set, sqlite3 db is left on disk at this path
86
- --skip-comment Skip comment lines start with '#'
87
- --source=FILE Source file to load, or defaults to stdin
88
- --sql=SQL SQL Command(s) to run on the data
89
- --table-name=NAME Override the default table name (tbl)
90
- --verbose Enable verbose logging
94
+ Usage: csvql [csvfile] [options]
95
+ --console After all commands are run, open sqlite3 console with this data
96
+ --header Treat file as having the first row as a header row
97
+ --output-dlm="|" Output delimiter (|)
98
+ --save-to=FILE If set, sqlite3 db is left on disk at this path
99
+ --skip-comment Skip comment lines start with '#'
100
+ --source=FILE Source file to load, or defaults to stdin
101
+ --sql=SQL SQL Command(s) to run on the data
102
+ --select=COLUMN Select column (*)
103
+ --where=COND Where clause
104
+ --table-name=NAME Override the default table name (tbl)
105
+ --verbose Enable verbose logging
91
106
 
92
107
  ## Contributing
93
108
 
data/lib/csvql/csvql.rb CHANGED
@@ -89,6 +89,7 @@ module Csvql
89
89
  opt = OptionParser.new
90
90
  option = {}
91
91
 
92
+ opt.banner = "Usage: csvql [csvfile] [options]"
92
93
  opt.on("--console", "After all commands are run, open sqlite3 console with this data") {|v| option[:console] = v }
93
94
  opt.on("--header", "Treat file as having the first row as a header row") {|v| option[:header] = v }
94
95
  opt.on('--output-dlm="|"', "Output delimiter (|)") {|v| option[:output_dlm] = v }
@@ -96,10 +97,14 @@ module Csvql
96
97
  opt.on("--skip-comment", "Skip comment lines start with '#'") {|v| option[:skip_comment] = v }
97
98
  opt.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| option[:source] = v }
98
99
  opt.on("--sql=SQL", "SQL Command(s) to run on the data") {|v| option[:sql] = v }
100
+ opt.on("--select=COLUMN", "Select column (*)") {|v| option[:select] = v }
101
+ opt.on("--where=COND", "Where clause") {|v| option[:where] = v }
99
102
  opt.on("--table-name=NAME", "Override the default table name (tbl)") {|v| option[:table_name] = v }
100
103
  opt.on("--verbose", "Enable verbose logging") {|v| option[:verbose] = v }
101
104
  opt.parse!(argv)
102
105
 
106
+ option[:source] ||= argv[0]
107
+ option[:table_name] ||= "tbl"
103
108
  option
104
109
  end
105
110
 
@@ -109,6 +114,10 @@ module Csvql
109
114
  puts "Can not open console with pipe input, read a file instead"
110
115
  exit 1
111
116
  end
117
+ if option[:sql] && (option[:select] || option[:where])
118
+ puts "Can not use --sql option and --select|--where option at the same time."
119
+ exit 1
120
+ end
112
121
 
113
122
  csvfile = option[:source] ? File.open(option[:source]) : $stdin
114
123
 
@@ -122,13 +131,21 @@ module Csvql
122
131
  next if option[:skip_comment] && line.start_with?("#")
123
132
  row = line.parse_csv
124
133
  if i == 1
125
- tbl.create_table(row, option[:header], option[:table_name]||"tbl")
134
+ tbl.create_table(row, option[:header], option[:table_name])
126
135
  next if option[:header]
127
136
  end
128
137
  tbl.insert(row,i)
129
138
  end
130
139
  tbl.exec("COMMIT TRANSACTION")
131
140
 
141
+ if option[:select] || option[:where]
142
+ option[:select] ||= "*"
143
+ option[:sql] = "select #{option[:select]} from #{option[:table_name]}"
144
+ if option[:where]
145
+ option[:sql] += " where #{option[:where]}"
146
+ end
147
+ end
148
+
132
149
  tbl.exec(option[:sql], option[:output_dlm]||"|") if option[:sql]
133
150
  tbl.open_console if option[:console]
134
151
  end
data/lib/csvql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Csvql
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
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.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - YANO Satoru