csvql 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/csvql/csvql.rb +35 -36
- data/lib/csvql/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 810630ed534c79a2a4fa99ccf6757950352ae6eb
|
4
|
+
data.tar.gz: e11f9a88105893d4aea8eef4b1c47d949fd83ca4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1419f89fb4e2a4d484169348ffa1ff26ae8d6e2f465680c2651ea1f38810d13fa78dce29f63ee3a4e69974f70f1dc6e8e912050e3aa5e3cda07dabb61e69492a
|
7
|
+
data.tar.gz: da76917a1a2e45ebd1b11cce12a02e0da70dfded8c52fcb239a769a6b4e13004652e96b8520a9199d806828566c70a0349eebc922874b4e0435ddfd885d35056
|
data/lib/csvql/csvql.rb
CHANGED
@@ -13,45 +13,38 @@ module Csvql
|
|
13
13
|
@db_file = if path && path.size > 0
|
14
14
|
path
|
15
15
|
elsif console
|
16
|
-
@tmp_file =
|
17
|
-
Tempfile.new("csvql").path + ".sqlite3"
|
16
|
+
@tmp_file = Tempfile.new("csvql").path
|
18
17
|
else
|
19
18
|
":memory:"
|
20
19
|
end
|
21
20
|
@db = SQLite3::Database.new(@db_file)
|
22
21
|
end
|
23
22
|
|
24
|
-
def create_table(cols, header, table_name="tbl")
|
23
|
+
def create_table(cols, header, table_name="tbl", schema=nil)
|
25
24
|
@col_size = cols.size
|
26
25
|
@table_name = table_name
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
26
|
+
if schema
|
27
|
+
file = File.expand_path(schema)
|
28
|
+
col = if File.exist?(file)
|
29
|
+
File.open(file).read
|
30
|
+
else
|
31
|
+
schema
|
32
|
+
end
|
33
|
+
@col_name = col.split(",").map {|c| c.split.first.strip }
|
34
|
+
else
|
35
|
+
@col_name = if header
|
36
|
+
cols
|
37
|
+
else
|
38
|
+
@col_size.times.map {|i| "c#{i}" }
|
39
|
+
end
|
40
|
+
col = @col_name.map {|c| "#{c} NONE" }.join(",")
|
41
41
|
end
|
42
|
-
|
43
|
-
sql = "INSERT INTO #{@table_name} VALUES(#{col.join(",")});"
|
42
|
+
sql = "CREATE TABLE IF NOT EXISTS #{@table_name} (#{col})"
|
44
43
|
@db.execute(sql)
|
45
44
|
end
|
46
45
|
|
47
|
-
def
|
48
|
-
|
49
|
-
bulk.each do |cols|
|
50
|
-
col = cols.map {|c| "'#{c}'" }
|
51
|
-
rows << "(#{col.join(',')})"
|
52
|
-
end
|
53
|
-
sql = "INSERT INTO #{@table_name} VALUES#{rows.join(",")};"
|
54
|
-
# puts sql
|
46
|
+
def drop_table(table_name="tbl")
|
47
|
+
sql = "DROP TABLE IF EXISTS #{table_name}"
|
55
48
|
@db.execute(sql)
|
56
49
|
end
|
57
50
|
|
@@ -62,7 +55,7 @@ module Csvql
|
|
62
55
|
|
63
56
|
def insert(cols, line)
|
64
57
|
if cols.size != @col_size
|
65
|
-
puts "line #{line}: wrong number of fields in line"
|
58
|
+
puts "line #{line}: wrong number of fields in line (skipping)"
|
66
59
|
return
|
67
60
|
end
|
68
61
|
@pre ||= prepare(cols)
|
@@ -80,7 +73,7 @@ module Csvql
|
|
80
73
|
|
81
74
|
def open_console
|
82
75
|
system("sqlite3", @db_file)
|
83
|
-
File.delete(@
|
76
|
+
File.delete(@tmp_file) if @tmp_file
|
84
77
|
end
|
85
78
|
end
|
86
79
|
|
@@ -97,17 +90,21 @@ module Csvql
|
|
97
90
|
opt.on("--[no-]header", "Treat file as having the first row as a header row") {|v| option[:header] = v }
|
98
91
|
opt.on('--output-dlm="|"', "Output delimiter (|)") {|v| option[:output_dlm] = v }
|
99
92
|
opt.on("--save-to=FILE", "If set, sqlite3 db is left on disk at this path") {|v| option[:save_to] = v }
|
93
|
+
opt.on("--append", "Append mode (not dropping any tables)") {|v| option[:append] = v }
|
100
94
|
opt.on("--skip-comment", "Skip comment lines start with '#'") {|v| option[:skip_comment] = v }
|
101
95
|
opt.on("--source=FILE", "Source file to load, or defaults to stdin") {|v| option[:source] = v }
|
102
96
|
opt.on("--sql=SQL", "SQL Command(s) to run on the data") {|v| option[:sql] = v }
|
103
97
|
opt.on("--select=COLUMN", "Select column (*)") {|v| option[:select] = v }
|
98
|
+
opt.on("--schema=FILE or STRING", "Specify a table schema") {|v| option[:schema] = v }
|
104
99
|
opt.on("--where=COND", "Where clause") {|v| option[:where] = v }
|
105
100
|
opt.on("--table-name=NAME", "Override the default table name (tbl)") {|v| option[:table_name] = v }
|
106
101
|
opt.on("--verbose", "Enable verbose logging") {|v| option[:verbose] = v }
|
107
102
|
opt.parse!(argv)
|
108
103
|
|
109
104
|
option[:source] ||= argv[0]
|
105
|
+
# option[:where] ||= argv[1]
|
110
106
|
option[:table_name] ||= "tbl"
|
107
|
+
option[:output_dlm] ||= "|"
|
111
108
|
option
|
112
109
|
end
|
113
110
|
|
@@ -125,7 +122,7 @@ module Csvql
|
|
125
122
|
csvfile = option[:source] ? File.open(option[:source]) : $stdin
|
126
123
|
|
127
124
|
tbl = TableHandler.new(option[:save_to], option[:console])
|
128
|
-
|
125
|
+
tbl.drop_table(option[:table_name]) unless option[:append]
|
129
126
|
tbl.exec("PRAGMA synchronous=OFF")
|
130
127
|
tbl.exec("BEGIN TRANSACTION")
|
131
128
|
csvfile.each.with_index(1) do |line,i|
|
@@ -134,22 +131,24 @@ module Csvql
|
|
134
131
|
next if option[:skip_comment] && line.start_with?("#")
|
135
132
|
row = line.parse_csv
|
136
133
|
if i == 1
|
137
|
-
tbl.create_table(row, option[:header], option[:table_name])
|
134
|
+
tbl.create_table(row, option[:header], option[:table_name], option[:schema])
|
138
135
|
next if option[:header]
|
139
136
|
end
|
140
|
-
tbl.insert(row,i)
|
137
|
+
tbl.insert(row, i)
|
141
138
|
end
|
142
139
|
tbl.exec("COMMIT TRANSACTION")
|
143
140
|
|
144
|
-
if option[:
|
141
|
+
if option[:sql]
|
142
|
+
sql = option[:sql]
|
143
|
+
elsif option[:select] || option[:where]
|
145
144
|
option[:select] ||= "*"
|
146
|
-
|
145
|
+
sql = "select #{option[:select]} from #{option[:table_name]}"
|
147
146
|
if option[:where]
|
148
|
-
|
147
|
+
sql += " where (#{option[:where]})"
|
149
148
|
end
|
150
149
|
end
|
151
150
|
|
152
|
-
tbl.exec(
|
151
|
+
tbl.exec(sql, option[:output_dlm]) if sql
|
153
152
|
tbl.open_console if option[:console]
|
154
153
|
end
|
155
154
|
end
|
data/lib/csvql/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.7
|
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-
|
11
|
+
date: 2014-07-10 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.
|
69
|
+
rubygems_version: 2.2.2
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: csvql
|