csvql 0.0.2 → 0.1.0

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: a120305182e435bae2f10105391d3fa58715a84e
4
- data.tar.gz: e97b490524ad746db681ce228bdfdf0063dc2fef
3
+ metadata.gz: 0bc6c5546cff40bfbbd72f590c17320a5dad50ef
4
+ data.tar.gz: b16cf555382b960fe85243d71778ba763e211625
5
5
  SHA512:
6
- metadata.gz: da2c3be95c6d6f53f834386b75e80216119e31b3ba626be0e2665e895c7086ea71f8ba8e90d2820974542698c6c2df67a121d4cc2fd21b6b29499f7a64c3517d
7
- data.tar.gz: 96cea0878dcc53847cffe08c762cc0d2ca96514c0da5d86b13ed04bf1445fc708f977463adf1c52507c8d54693c0017082afe7d0c3d5badc8116433e1151f642
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
- col = if header
28
- cols.map {|c| "#{c} TEXT" }
29
- else
30
- @col_size.times.map {|i| "c#{i} TEXT" }
31
- end
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 insert(cols, line)
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, i)
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
@@ -1,3 +1,3 @@
1
1
  module Csvql
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YANO Satoru