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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e68503eec0dd74c109717ab1bedcb0ea3fc1a3f
4
- data.tar.gz: 61183771ab2a1c321121b823f87004687afff0d5
3
+ metadata.gz: 810630ed534c79a2a4fa99ccf6757950352ae6eb
4
+ data.tar.gz: e11f9a88105893d4aea8eef4b1c47d949fd83ca4
5
5
  SHA512:
6
- metadata.gz: faa2fa4062e48a66cdc05fa7be6228958b5523012b5c5e733fd8a0cb5a1b41ce943037321558b44cc9364cdc499412c64ed1e5adda591e2b2bafa849f05fc21f
7
- data.tar.gz: 810fa43a79cef5114f57b700edd9e8cc84ab62358ab61e3059111b8ee2532a05149a3828d63abc940a35cbfdc0fe7e5ad57374670b017cc02d9387c9e5d0ca5b
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 = true
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
- @col_name = if header
28
- cols
29
- else
30
- @col_size.times.map {|i| "c#{i}" }
31
- end
32
- col = @col_name.map {|c| "#{c} NONE" }
33
- sql = "CREATE TABLE #{@table_name} (#{col.join(",")})"
34
- @db.execute(sql)
35
- end
36
-
37
- def single_insert(cols, line)
38
- if cols.size != @col_size
39
- puts "line #{line}: wrong number of fields in line"
40
- return
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
- col = cols.map {|c| "'#{c}'" }
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 bulk_insert(bulk)
48
- rows = []
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(@db_file) if @tmp_file
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
- bulk = []
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[:select] || option[:where]
141
+ if option[:sql]
142
+ sql = option[:sql]
143
+ elsif option[:select] || option[:where]
145
144
  option[:select] ||= "*"
146
- option[:sql] = "select #{option[:select]} from #{option[:table_name]}"
145
+ sql = "select #{option[:select]} from #{option[:table_name]}"
147
146
  if option[:where]
148
- option[:sql] += " where #{option[:where]}"
147
+ sql += " where (#{option[:where]})"
149
148
  end
150
149
  end
151
150
 
152
- tbl.exec(option[:sql], option[:output_dlm]||"|") if option[:sql]
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
@@ -1,3 +1,3 @@
1
1
  module Csvql
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
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.6
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-09 00:00:00.000000000 Z
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.3.0
69
+ rubygems_version: 2.2.2
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: csvql