csvql 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -16
- data/lib/csvql/csvql.rb +18 -1
- data/lib/csvql/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97d8dadd3d422fd894edcd30cf17af06332d570a
|
4
|
+
data.tar.gz: 501c3926dccc65e86367aa9d82fe9192df4c0599
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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]
|
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