dohmysql 0.2.22 → 0.2.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,10 +76,18 @@ class Handle
76
76
  insert_hash_helper(hash, table, 'INSERT', quote_strings)
77
77
  end
78
78
 
79
+ def insert_hashes(hashes, table, quote_strings = true)
80
+ insert_hashes_helper(hashes, table, 'INSERT', quote_strings)
81
+ end
82
+
79
83
  def insert_ignore_hash(hash, table, quote_strings = true)
80
84
  insert_hash_helper(hash, table, 'INSERT IGNORE', quote_strings)
81
85
  end
82
86
 
87
+ def insert_ignore_hashes(hash, table, quote_strings = true)
88
+ insert_hashes_helper(hash, table, 'INSERT IGNORE', quote_strings)
89
+ end
90
+
83
91
  def replace_hash(hash, table, quote_strings = true)
84
92
  insert_hash_helper(hash, table, 'REPLACE', quote_strings)
85
93
  end
@@ -190,6 +198,10 @@ private
190
198
  raise
191
199
  end
192
200
 
201
+ def get_key_insert_str(keys)
202
+ "(`#{keys.join('`,`')}`)"
203
+ end
204
+
193
205
  def insert_hash_helper(hash, table, keyword, quote_strings)
194
206
  names = []
195
207
  values = []
@@ -201,6 +213,23 @@ private
201
213
  insert("#{keyword} INTO #{table} (`#{names.join('`,`')}`) VALUES (#{values.join(',')})")
202
214
  end
203
215
 
216
+ def insert_hashes_helper(hashes, table, keyword, quote_strings)
217
+ valuestrs = []
218
+ keys = hashes[0].keys
219
+ keystr = get_key_insert_str(keys)
220
+ hashes.each do |hash|
221
+ values = []
222
+ keys.each do |key|
223
+ value = hash[key]
224
+ values.push(if quote_strings || !value.is_a?(String) then value.to_sql else value end)
225
+ end
226
+ valuestrs.push("(#{values.join(',')})")
227
+ end
228
+
229
+ insert("#{keyword} INTO #{table} #{keystr} VALUES #{valuestrs.join(",")}")
230
+ end
231
+
232
+
204
233
  def get_row_builder(row_builder = nil)
205
234
  if row_builder.nil?
206
235
  TypedRowBuilder.new
data/test/handle.dt.rb CHANGED
@@ -52,18 +52,24 @@ class Test_Handle < DohTest::TestGroup
52
52
  dbh = get_dbh
53
53
  dbh.query("CREATE TEMPORARY TABLE #{tbl} (value INT KEY)")
54
54
  hash1 = {'value' => 1}
55
+ hash2 = {'value' => 2}
56
+ hash3 = {'value' => 3}
57
+ hash4 = {'value' => 4}
55
58
  assert_equal(0, dbh.insert_hash(hash1, tbl))
56
59
  assert_equal(0, dbh.insert_ignore_hash(hash1, tbl))
60
+ assert_equal(1, dbh.select_field("SELECT COUNT(*) from #{tbl}"))
61
+ assert_equal(0, dbh.insert_hashes([hash2, hash3, hash4], tbl))
62
+ assert_equal(4, dbh.select_field("SELECT COUNT(*) from #{tbl}"))
57
63
  assert_raises(Mysql2::Error) { dbh.insert_hash(hash1, tbl) }
58
64
  end
59
65
 
60
66
  def test_bad_handle_reconnect
61
67
  dbh = get_dbh
62
68
  begin
63
- Timeout.timeout(0.001) { Doh.db.select_field("SELECT SLEEP(1), 0 FROM mysql.user GROUP BY 2") }
69
+ Timeout.timeout(0.001) { dbh.select_field("SELECT SLEEP(1), 0 FROM mysql.user GROUP BY 2") }
64
70
  rescue => _
65
71
  end
66
- assert_equal(3, Doh.db.select_field("SELECT 3 FROM mysql.user GROUP BY 1"))
72
+ assert_equal(3, dbh.select_field("SELECT 3 FROM mysql.user GROUP BY 1"))
67
73
  end
68
74
  end
69
75
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dohmysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.22
4
+ version: 0.2.23
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: