csvql 0.1.3 → 0.1.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: a46964f15635d66a86f53e9eaa9bcfaa317940ac
4
- data.tar.gz: e7db55edc02fb2579c0d5f7d30b5a74b620d9696
3
+ metadata.gz: 13dbfaea6b45323f789361d04dc5ad1889144ee5
4
+ data.tar.gz: d9767a31cb0260261d446c18c8f8b3320a2649b8
5
5
  SHA512:
6
- metadata.gz: 9e720c27ed1d0232827e7a14c118b3859c6ef2a595dab6d11371ae0068dc5740103020a1c280964b68f809e2197b388d16997c9b69bb027db3788324d0427c95
7
- data.tar.gz: 292372e315213dd80426ccaac005ba9bfd8b2ec5ba501dfede451f852e8e34c0d776b14e921ef30db0bbfd76145937084050c91b0cb85692ac951330baf3e7b7
6
+ metadata.gz: 75e2cb12c515b7f96f96245a513d18c80b31b974a144f78e69807b895bc247b5441ae27966116e275e17df684725a5f286b36e3e956465008be494271bf3dfe8
7
+ data.tar.gz: 0b0a405717981ef54ffa4f7b407fcda8f7af71864b043bbc9a5841e0953e5a6968d3d5b658574223e3d6be002edafbaa3ca9bb767b5bf9cb095b7a131e8e8372
data/README.md CHANGED
@@ -18,7 +18,76 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Usage by examples:
22
+
23
+ $ cat sample.csv
24
+ id,name,age
25
+ 1,Anne,33
26
+ 2,Bob,25
27
+ 3,Charry,48
28
+ 4,Daniel,16
29
+ 5,Edward,52
30
+
31
+ Simple query:
32
+
33
+ $ csvql --source sample.csv --sql "select count(*) from tbl"
34
+ 6
35
+ $ csvql --source sample.csv --header -sql "select count(*) from tbl"
36
+ 5
37
+ $ csvql --source sample.csv --header -sql "select * from tbl where age > 40"
38
+ 3|Charry|48
39
+ 5|Edward|52
40
+
41
+ Open console:
42
+
43
+ $ csvql --source sample.csv --header --console
44
+ SQLite version 3.7.7 2011-06-25 16:35:41
45
+ Enter ".help" for instructions
46
+ Enter SQL statements terminated with a ";"
47
+ sqlite> select * from tbl order by age desc;
48
+ 5|Edward|52
49
+ 3|Charry|48
50
+ 1|Anne|33
51
+ 2|Bob|25
52
+ 4|Daniel|16
53
+
54
+ From stdin:
55
+
56
+ $ cat sample.csv | csvql --header --sql "select max(age) from tbl"
57
+ 52
58
+
59
+ Save to db-file:
60
+
61
+ $ csvql --source sample.csv --header --save-to test.db
62
+ $ sqlite3 test.db
63
+ SQLite version 3.7.7 2011-06-25 16:35:41
64
+ Enter ".help" for instructions
65
+ Enter SQL statements terminated with a ";"
66
+ sqlite> select avg(age) from tbl;
67
+ 34.8
68
+
69
+ Change output delimiter:
70
+
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
77
+
78
+ Options:
79
+
80
+ $ 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
22
91
 
23
92
  ## Contributing
24
93
 
data/lib/csvql/csvql.rb CHANGED
@@ -69,9 +69,12 @@ module Csvql
69
69
  @pre.execute(cols)
70
70
  end
71
71
 
72
- def exec(sql)
72
+ def exec(sql, dlm="|")
73
+ if dlm.downcase == 'tab'
74
+ dlm = "\t"
75
+ end
73
76
  @db.execute(sql) do |row|
74
- puts row.join("|")
77
+ puts row.join(dlm)
75
78
  end
76
79
  end
77
80
 
@@ -88,6 +91,7 @@ module Csvql
88
91
 
89
92
  opt.on("--console", "After all commands are run, open sqlite3 console with this data") {|v| option[:console] = v }
90
93
  opt.on("--header", "Treat file as having the first row as a header row") {|v| option[:header] = v }
94
+ opt.on('--output-dlm="|"', "Output delimiter (|)") {|v| option[:output_dlm] = v }
91
95
  opt.on("--save-to=FILE", "If set, sqlite3 db is left on disk at this path") {|v| option[:save_to] = v }
92
96
  opt.on("--skip-comment", "Skip comment lines start with '#'") {|v| option[:skip_comment] = v }
93
97
  opt.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| option[:source] = v }
@@ -113,8 +117,10 @@ module Csvql
113
117
  tbl.exec("PRAGMA synchronous=OFF")
114
118
  tbl.exec("BEGIN TRANSACTION")
115
119
  csvfile.each.with_index(1) do |line,i|
120
+ line = NKF.nkf('-w', line).strip
121
+ next if line.size == 0
116
122
  next if option[:skip_comment] && line.start_with?("#")
117
- row = NKF.nkf('-w', line).parse_csv
123
+ row = line.parse_csv
118
124
  if i == 1
119
125
  tbl.create_table(row, option[:header], option[:table_name]||"tbl")
120
126
  next if option[:header]
@@ -123,7 +129,7 @@ module Csvql
123
129
  end
124
130
  tbl.exec("COMMIT TRANSACTION")
125
131
 
126
- tbl.exec(option[:sql]) if option[:sql]
132
+ tbl.exec(option[:sql], option[:output_dlm]||"|") if option[:sql]
127
133
  tbl.open_console if option[:console]
128
134
  end
129
135
  end
data/lib/csvql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Csvql
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - YANO Satoru
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-06 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.2.2
69
+ rubygems_version: 2.3.0
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: csvql