csvql 0.0.2 → 0.1.0
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 +40 -8
- 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: 0bc6c5546cff40bfbbd72f590c17320a5dad50ef
|
4
|
+
data.tar.gz: b16cf555382b960fe85243d71778ba763e211625
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1f35e0cc16d38a2609937aad3691f24c903cda38dcd5a091e32065eb4dce27131e6aa3a32ec4c874cb99eb2e5f4c077b03bf1aeb6a0ac730081b22751db87ff
|
7
|
+
data.tar.gz: 9cec49221a509984052448cc08fd33eb5e08020c214fc0d13e0e4608c24d9dd21aece125832f549942fa56eae4a0b9f58cd4d6848a19e16dcf0c0ddadc143f7e
|
data/lib/csvql/csvql.rb
CHANGED
@@ -24,16 +24,17 @@ module Csvql
|
|
24
24
|
def create_table(cols, header, table_name="tbl")
|
25
25
|
@col_size = cols.size
|
26
26
|
@table_name = table_name
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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} TEXT" }
|
32
33
|
sql = "CREATE TABLE #{@table_name} (#{col.join(",")});"
|
33
34
|
@db.execute(sql)
|
34
35
|
end
|
35
36
|
|
36
|
-
def
|
37
|
+
def single_insert(cols, line)
|
37
38
|
if cols.size != @col_size
|
38
39
|
puts "line #{line}: wrong number of fields in line"
|
39
40
|
return
|
@@ -43,6 +44,31 @@ module Csvql
|
|
43
44
|
@db.execute(sql)
|
44
45
|
end
|
45
46
|
|
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
|
55
|
+
@db.execute(sql)
|
56
|
+
end
|
57
|
+
|
58
|
+
def prepare(cols)
|
59
|
+
sql = "INSERT INTO #{@table_name} (#{@col_name.join(",")}) VALUES (#{cols.map{"?"}.join(",")});"
|
60
|
+
@pre = @db.prepare(sql)
|
61
|
+
end
|
62
|
+
|
63
|
+
def insert(cols, line)
|
64
|
+
if cols.size != @col_size
|
65
|
+
puts "line #{line}: wrong number of fields in line"
|
66
|
+
return
|
67
|
+
end
|
68
|
+
@pre ||= prepare(cols)
|
69
|
+
@pre.execute(cols)
|
70
|
+
end
|
71
|
+
|
46
72
|
def exec(sql)
|
47
73
|
@db.execute(sql) do |row|
|
48
74
|
puts row.join("|")
|
@@ -80,8 +106,12 @@ module Csvql
|
|
80
106
|
exit 1
|
81
107
|
end
|
82
108
|
|
83
|
-
tbl = TableHandler.new(option[:save_to], option[:console])
|
84
109
|
csvfile = option[:source] ? File.open(option[:source]) : $stdin
|
110
|
+
|
111
|
+
tbl = TableHandler.new(option[:save_to], option[:console])
|
112
|
+
bulk = []
|
113
|
+
tbl.exec("PRAGMA synchronous=OFF")
|
114
|
+
tbl.exec("BEGIN TRANSACTION")
|
85
115
|
csvfile.each.with_index(1) do |line,i|
|
86
116
|
next if option[:skip_comment] && line.start_with?("#")
|
87
117
|
row = NKF.nkf('-w', line).parse_csv
|
@@ -89,11 +119,13 @@ module Csvql
|
|
89
119
|
tbl.create_table(row, option[:header], option[:table_name]||"tbl")
|
90
120
|
next if option[:header]
|
91
121
|
end
|
92
|
-
tbl.insert(row,
|
122
|
+
tbl.insert(row,i)
|
93
123
|
end
|
124
|
+
tbl.exec("COMMIT TRANSACTION")
|
94
125
|
|
95
126
|
tbl.exec(option[:sql]) if option[:sql]
|
96
127
|
tbl.open_console if option[:console]
|
97
128
|
end
|
98
129
|
end
|
99
130
|
end
|
131
|
+
|
data/lib/csvql/version.rb
CHANGED