mysql2 0.3.21-x86-mingw32 → 0.4.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -0
- data/README.md +28 -6
- data/examples/threaded.rb +4 -6
- data/ext/mysql2/client.c +54 -46
- data/ext/mysql2/client.h +21 -0
- data/ext/mysql2/extconf.rb +20 -23
- data/ext/mysql2/mysql2_ext.c +1 -0
- data/ext/mysql2/mysql2_ext.h +1 -0
- data/ext/mysql2/mysql_enc_name_to_ruby.h +6 -6
- data/ext/mysql2/result.c +473 -94
- data/ext/mysql2/result.h +8 -1
- data/ext/mysql2/statement.c +454 -0
- data/ext/mysql2/statement.h +22 -0
- data/lib/mysql2.rb +2 -18
- data/lib/mysql2/1.8/mysql2.so +0 -0
- data/lib/mysql2/1.9/mysql2.so +0 -0
- data/lib/mysql2/2.0/mysql2.so +0 -0
- data/lib/mysql2/2.1/mysql2.so +0 -0
- data/lib/mysql2/2.2/mysql2.so +0 -0
- data/lib/mysql2/client.rb +9 -2
- data/lib/mysql2/error.rb +8 -20
- data/lib/mysql2/field.rb +4 -0
- data/lib/mysql2/statement.rb +5 -0
- data/lib/mysql2/version.rb +1 -1
- data/spec/em/em_spec.rb +13 -13
- data/spec/mysql2/client_spec.rb +284 -277
- data/spec/mysql2/error_spec.rb +37 -36
- data/spec/mysql2/result_spec.rb +184 -193
- data/spec/mysql2/statement_spec.rb +598 -0
- data/spec/spec_helper.rb +7 -0
- metadata +15 -48
- data/lib/mysql2/2.3/mysql2.so +0 -0
@@ -0,0 +1,598 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require './spec/spec_helper.rb'
|
3
|
+
|
4
|
+
RSpec.describe Mysql2::Statement do
|
5
|
+
before :each do
|
6
|
+
@client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should create a statement" do
|
10
|
+
statement = nil
|
11
|
+
expect { statement = @client.prepare 'SELECT 1' }.not_to raise_error
|
12
|
+
expect(statement).to be_kind_of Mysql2::Statement
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should raise an exception when server disconnects" do
|
16
|
+
@client.close
|
17
|
+
expect { @client.prepare 'SELECT 1' }.to raise_error(Mysql2::Error)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should tell us the param count" do
|
21
|
+
statement = @client.prepare 'SELECT ?, ?'
|
22
|
+
expect(statement.param_count).to eq(2)
|
23
|
+
|
24
|
+
statement2 = @client.prepare 'SELECT 1'
|
25
|
+
expect(statement2.param_count).to eq(0)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should tell us the field count" do
|
29
|
+
statement = @client.prepare 'SELECT ?, ?'
|
30
|
+
expect(statement.field_count).to eq(2)
|
31
|
+
|
32
|
+
statement2 = @client.prepare 'SELECT 1'
|
33
|
+
expect(statement2.field_count).to eq(1)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should let us execute our statement" do
|
37
|
+
statement = @client.prepare 'SELECT 1'
|
38
|
+
expect(statement.execute).not_to eq(nil)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should raise an exception without a block" do
|
42
|
+
statement = @client.prepare 'SELECT 1'
|
43
|
+
expect { statement.execute.each }.to raise_error(LocalJumpError)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should tell us the result count" do
|
47
|
+
statement = @client.prepare 'SELECT 1'
|
48
|
+
result = statement.execute
|
49
|
+
expect(result.count).to eq(1)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should let us iterate over results" do
|
53
|
+
statement = @client.prepare 'SELECT 1'
|
54
|
+
result = statement.execute
|
55
|
+
rows = []
|
56
|
+
result.each {|r| rows << r}
|
57
|
+
expect(rows).to eq([{"1"=>1}])
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should keep its result after other query" do
|
61
|
+
@client.query 'USE test'
|
62
|
+
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
|
63
|
+
@client.query 'INSERT INTO mysql2_stmt_q (a) VALUES (1), (2)'
|
64
|
+
stmt = @client.prepare('SELECT a FROM mysql2_stmt_q WHERE a = ?')
|
65
|
+
result1 = stmt.execute(1)
|
66
|
+
result2 = stmt.execute(2)
|
67
|
+
expect(result2.first).to eq({"a"=>2})
|
68
|
+
expect(result1.first).to eq({"a"=>1})
|
69
|
+
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should be reusable 1000 times" do
|
73
|
+
statement = @client.prepare 'SELECT 1'
|
74
|
+
1000.times do
|
75
|
+
result = statement.execute
|
76
|
+
expect(result.to_a.length).to eq(1)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be reusable 10000 times" do
|
81
|
+
statement = @client.prepare 'SELECT 1'
|
82
|
+
10000.times do
|
83
|
+
result = statement.execute
|
84
|
+
expect(result.to_a.length).to eq(1)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should handle comparisons and likes" do
|
89
|
+
@client.query 'USE test'
|
90
|
+
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int, b varchar(10))'
|
91
|
+
@client.query 'INSERT INTO mysql2_stmt_q (a, b) VALUES (1, "Hello"), (2, "World")'
|
92
|
+
statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE a < ?'
|
93
|
+
results = statement.execute(2)
|
94
|
+
expect(results.first).to eq({"a" => 1, "b" => "Hello"})
|
95
|
+
|
96
|
+
statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE b LIKE ?'
|
97
|
+
results = statement.execute('%orld')
|
98
|
+
expect(results.first).to eq({"a" => 2, "b" => "World"})
|
99
|
+
|
100
|
+
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should select dates" do
|
104
|
+
statement = @client.prepare 'SELECT NOW()'
|
105
|
+
result = statement.execute
|
106
|
+
expect(result.first.first[1]).to be_kind_of Time
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should tell us about the fields" do
|
110
|
+
statement = @client.prepare 'SELECT 1 as foo, 2'
|
111
|
+
statement.execute
|
112
|
+
list = statement.fields
|
113
|
+
expect(list.length).to eq(2)
|
114
|
+
expect(list.first).to eq('foo')
|
115
|
+
expect(list[1]).to eq('2')
|
116
|
+
end
|
117
|
+
|
118
|
+
context "utf8_db" do
|
119
|
+
before(:each) do
|
120
|
+
@client.query("DROP DATABASE IF EXISTS test_mysql2_stmt_utf8")
|
121
|
+
@client.query("CREATE DATABASE test_mysql2_stmt_utf8")
|
122
|
+
@client.query("USE test_mysql2_stmt_utf8")
|
123
|
+
@client.query("CREATE TABLE テーブル (整数 int, 文字列 varchar(32)) charset=utf8")
|
124
|
+
@client.query("INSERT INTO テーブル (整数, 文字列) VALUES (1, 'イチ'), (2, '弐'), (3, 'さん')")
|
125
|
+
end
|
126
|
+
|
127
|
+
after(:each) do
|
128
|
+
@client.query("DROP DATABASE test_mysql2_stmt_utf8")
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should be able to retrieve utf8 field names correctly" do
|
132
|
+
stmt = @client.prepare 'SELECT * FROM `テーブル`'
|
133
|
+
expect(stmt.fields).to eq(['整数', '文字列'])
|
134
|
+
result = stmt.execute
|
135
|
+
|
136
|
+
expect(result.to_a).to eq([{"整数"=>1, "文字列"=>"イチ"}, {"整数"=>2, "文字列"=>"弐"}, {"整数"=>3, "文字列"=>"さん"}])
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be able to retrieve utf8 param query correctly" do
|
140
|
+
stmt = @client.prepare 'SELECT 整数 FROM テーブル WHERE 文字列 = ?'
|
141
|
+
expect(stmt.param_count).to eq(1)
|
142
|
+
|
143
|
+
result = stmt.execute 'イチ'
|
144
|
+
|
145
|
+
expect(result.to_a).to eq([{"整数"=>1}])
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should be able to retrieve query with param in different encoding correctly" do
|
149
|
+
stmt = @client.prepare 'SELECT 整数 FROM テーブル WHERE 文字列 = ?'
|
150
|
+
expect(stmt.param_count).to eq(1)
|
151
|
+
|
152
|
+
param = 'イチ'.encode("EUC-JP")
|
153
|
+
result = stmt.execute param
|
154
|
+
|
155
|
+
expect(result.to_a).to eq([{"整数"=>1}])
|
156
|
+
end
|
157
|
+
|
158
|
+
end if defined? Encoding
|
159
|
+
|
160
|
+
context "streaming result" do
|
161
|
+
it "should be able to stream query result" do
|
162
|
+
n = 1
|
163
|
+
stmt = @client.prepare("SELECT 1 UNION SELECT 2")
|
164
|
+
|
165
|
+
@client.query_options.merge!({:stream => true, :cache_rows => false, :as => :array})
|
166
|
+
|
167
|
+
stmt.execute.each do |r|
|
168
|
+
case n
|
169
|
+
when 1
|
170
|
+
expect(r).to eq([1])
|
171
|
+
when 2
|
172
|
+
expect(r).to eq([2])
|
173
|
+
else
|
174
|
+
violated "returned more than two rows"
|
175
|
+
end
|
176
|
+
n += 1
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context "#each" do
|
182
|
+
# note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
|
183
|
+
# The drawback of this is that args of Result#each is ignored...
|
184
|
+
|
185
|
+
it "should yield rows as hash's" do
|
186
|
+
@result = @client.prepare("SELECT 1").execute
|
187
|
+
@result.each do |row|
|
188
|
+
expect(row.class).to eql(Hash)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
|
193
|
+
@client.query_options[:symbolize_keys] = true
|
194
|
+
@result = @client.prepare("SELECT 1").execute
|
195
|
+
@result.each do |row|
|
196
|
+
expect(row.keys.first.class).to eql(Symbol)
|
197
|
+
end
|
198
|
+
@client.query_options[:symbolize_keys] = false
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should be able to return results as an array" do
|
202
|
+
@client.query_options[:as] = :array
|
203
|
+
|
204
|
+
@result = @client.prepare("SELECT 1").execute
|
205
|
+
@result.each do |row|
|
206
|
+
expect(row.class).to eql(Array)
|
207
|
+
end
|
208
|
+
|
209
|
+
@client.query_options[:as] = :hash
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should cache previously yielded results by default" do
|
213
|
+
@result = @client.prepare("SELECT 1").execute
|
214
|
+
expect(@result.first.object_id).to eql(@result.first.object_id)
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should yield different value for #first if streaming" do
|
218
|
+
@client.query_options[:stream] = true
|
219
|
+
@client.query_options[:cache_rows] = false
|
220
|
+
|
221
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
222
|
+
expect(result.first).not_to eql(result.first)
|
223
|
+
|
224
|
+
@client.query_options[:stream] = false
|
225
|
+
@client.query_options[:cache_rows] = true
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should yield the same value for #first if streaming is disabled" do
|
229
|
+
@client.query_options[:stream] = false
|
230
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
231
|
+
expect(result.first).to eql(result.first)
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should throw an exception if we try to iterate twice when streaming is enabled" do
|
235
|
+
@client.query_options[:stream] = true
|
236
|
+
@client.query_options[:cache_rows] = false
|
237
|
+
|
238
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
239
|
+
|
240
|
+
expect {
|
241
|
+
result.each {}
|
242
|
+
result.each {}
|
243
|
+
}.to raise_exception(Mysql2::Error)
|
244
|
+
|
245
|
+
@client.query_options[:stream] = false
|
246
|
+
@client.query_options[:cache_rows] = true
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context "#fields" do
|
251
|
+
before(:each) do
|
252
|
+
@client.query "USE test"
|
253
|
+
@test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute
|
254
|
+
end
|
255
|
+
|
256
|
+
it "method should exist" do
|
257
|
+
expect(@test_result).to respond_to(:fields)
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should return an array of field names in proper order" do
|
261
|
+
result = @client.prepare("SELECT 'a', 'b', 'c'").execute
|
262
|
+
expect(result.fields).to eql(['a', 'b', 'c'])
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context "row data type mapping" do
|
267
|
+
before(:each) do
|
268
|
+
@client.query "USE test"
|
269
|
+
@test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute.first
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should return nil for a NULL value" do
|
273
|
+
expect(@test_result['null_test'].class).to eql(NilClass)
|
274
|
+
expect(@test_result['null_test']).to eql(nil)
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should return String for a BIT(64) value" do
|
278
|
+
expect(@test_result['bit_test'].class).to eql(String)
|
279
|
+
expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should return String for a BIT(1) value" do
|
283
|
+
expect(@test_result['single_bit_test'].class).to eql(String)
|
284
|
+
expect(@test_result['single_bit_test']).to eql("\001")
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should return Fixnum for a TINYINT value" do
|
288
|
+
expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
|
289
|
+
expect(@test_result['tiny_int_test']).to eql(1)
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
|
293
|
+
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
|
294
|
+
id1 = @client.last_id
|
295
|
+
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
|
296
|
+
id2 = @client.last_id
|
297
|
+
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
|
298
|
+
id3 = @client.last_id
|
299
|
+
|
300
|
+
result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
|
301
|
+
result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
|
302
|
+
result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
|
303
|
+
expect(result1.first['bool_cast_test']).to be true
|
304
|
+
expect(result2.first['bool_cast_test']).to be false
|
305
|
+
expect(result3.first['bool_cast_test']).to be true
|
306
|
+
|
307
|
+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
|
311
|
+
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
|
312
|
+
id1 = @client.last_id
|
313
|
+
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
|
314
|
+
id2 = @client.last_id
|
315
|
+
|
316
|
+
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
|
317
|
+
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
|
318
|
+
expect(result1.first['single_bit_test']).to be true
|
319
|
+
expect(result2.first['single_bit_test']).to be false
|
320
|
+
|
321
|
+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should return Fixnum for a SMALLINT value" do
|
325
|
+
expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
|
326
|
+
expect(@test_result['small_int_test']).to eql(10)
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should return Fixnum for a MEDIUMINT value" do
|
330
|
+
expect([Fixnum, Bignum]).to include(@test_result['medium_int_test'].class)
|
331
|
+
expect(@test_result['medium_int_test']).to eql(10)
|
332
|
+
end
|
333
|
+
|
334
|
+
it "should return Fixnum for an INT value" do
|
335
|
+
expect([Fixnum, Bignum]).to include(@test_result['int_test'].class)
|
336
|
+
expect(@test_result['int_test']).to eql(10)
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should return Fixnum for a BIGINT value" do
|
340
|
+
expect([Fixnum, Bignum]).to include(@test_result['big_int_test'].class)
|
341
|
+
expect(@test_result['big_int_test']).to eql(10)
|
342
|
+
end
|
343
|
+
|
344
|
+
it "should return Fixnum for a YEAR value" do
|
345
|
+
expect([Fixnum, Bignum]).to include(@test_result['year_test'].class)
|
346
|
+
expect(@test_result['year_test']).to eql(2009)
|
347
|
+
end
|
348
|
+
|
349
|
+
it "should return BigDecimal for a DECIMAL value" do
|
350
|
+
expect(@test_result['decimal_test'].class).to eql(BigDecimal)
|
351
|
+
expect(@test_result['decimal_test']).to eql(10.3)
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should return Float for a FLOAT value" do
|
355
|
+
expect(@test_result['float_test'].class).to eql(Float)
|
356
|
+
expect(@test_result['float_test']).to be_within(1e-5).of(10.3)
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should return Float for a DOUBLE value" do
|
360
|
+
expect(@test_result['double_test'].class).to eql(Float)
|
361
|
+
expect(@test_result['double_test']).to eql(10.3)
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should return Time for a DATETIME value when within the supported range" do
|
365
|
+
expect(@test_result['date_time_test'].class).to eql(Time)
|
366
|
+
expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
367
|
+
end
|
368
|
+
|
369
|
+
if 1.size == 4 # 32bit
|
370
|
+
unless RUBY_VERSION =~ /1.8/
|
371
|
+
klass = Time
|
372
|
+
else
|
373
|
+
klass = DateTime
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
|
377
|
+
# 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
|
378
|
+
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
379
|
+
expect(r.first['test'].class).to eql(klass)
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
|
383
|
+
# 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
|
384
|
+
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
385
|
+
expect(r.first['test'].class).to eql(klass)
|
386
|
+
end
|
387
|
+
elsif 1.size == 8 # 64bit
|
388
|
+
unless RUBY_VERSION =~ /1.8/
|
389
|
+
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
|
390
|
+
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
391
|
+
expect(r.first['test'].class).to eql(Time)
|
392
|
+
end
|
393
|
+
|
394
|
+
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
395
|
+
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
396
|
+
expect(r.first['test'].class).to eql(Time)
|
397
|
+
end
|
398
|
+
else
|
399
|
+
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
|
400
|
+
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
|
401
|
+
expect(r.first['test'].class).to eql(Time)
|
402
|
+
end
|
403
|
+
|
404
|
+
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
|
405
|
+
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
|
406
|
+
expect(r.first['test'].class).to eql(DateTime)
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
410
|
+
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
411
|
+
expect(r.first['test'].class).to eql(Time)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
it "should return Time for a TIMESTAMP value when within the supported range" do
|
417
|
+
expect(@test_result['timestamp_test'].class).to eql(Time)
|
418
|
+
expect(@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
419
|
+
end
|
420
|
+
|
421
|
+
it "should return Time for a TIME value" do
|
422
|
+
expect(@test_result['time_test'].class).to eql(Time)
|
423
|
+
expect(@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
|
424
|
+
end
|
425
|
+
|
426
|
+
it "should return Date for a DATE value" do
|
427
|
+
expect(@test_result['date_test'].class).to eql(Date)
|
428
|
+
expect(@test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
|
429
|
+
end
|
430
|
+
|
431
|
+
it "should return String for an ENUM value" do
|
432
|
+
expect(@test_result['enum_test'].class).to eql(String)
|
433
|
+
expect(@test_result['enum_test']).to eql('val1')
|
434
|
+
end
|
435
|
+
|
436
|
+
it "should raise an error given an invalid DATETIME" do
|
437
|
+
expect { @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each }.to \
|
438
|
+
raise_error(Mysql2::Error, "Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
|
439
|
+
end
|
440
|
+
|
441
|
+
context "string encoding for ENUM values" do
|
442
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
443
|
+
|
444
|
+
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
445
|
+
with_internal_encoding nil do
|
446
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
447
|
+
expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
|
448
|
+
|
449
|
+
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
450
|
+
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
451
|
+
expect(result['enum_test'].encoding).to eql(Encoding::US_ASCII)
|
452
|
+
client2.close
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
it "should use Encoding.default_internal" do
|
457
|
+
with_internal_encoding Encoding::UTF_8 do
|
458
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
459
|
+
expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
|
460
|
+
end
|
461
|
+
|
462
|
+
with_internal_encoding Encoding::ASCII do
|
463
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
464
|
+
expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
|
465
|
+
end
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
it "should return String for a SET value" do
|
470
|
+
expect(@test_result['set_test'].class).to eql(String)
|
471
|
+
expect(@test_result['set_test']).to eql('val1,val2')
|
472
|
+
end
|
473
|
+
|
474
|
+
context "string encoding for SET values" do
|
475
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
476
|
+
|
477
|
+
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
478
|
+
with_internal_encoding nil do
|
479
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
480
|
+
expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
|
481
|
+
|
482
|
+
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
483
|
+
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
484
|
+
expect(result['set_test'].encoding).to eql(Encoding::US_ASCII)
|
485
|
+
client2.close
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
it "should use Encoding.default_internal" do
|
490
|
+
with_internal_encoding Encoding::UTF_8 do
|
491
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
492
|
+
expect(result['set_test'].encoding).to eql(Encoding.default_internal)
|
493
|
+
end
|
494
|
+
|
495
|
+
with_internal_encoding Encoding::ASCII do
|
496
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
497
|
+
expect(result['set_test'].encoding).to eql(Encoding.default_internal)
|
498
|
+
end
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
it "should return String for a BINARY value" do
|
503
|
+
expect(@test_result['binary_test'].class).to eql(String)
|
504
|
+
expect(@test_result['binary_test']).to eql("test#{"\000"*6}")
|
505
|
+
end
|
506
|
+
|
507
|
+
context "string encoding for BINARY values" do
|
508
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
509
|
+
|
510
|
+
it "should default to binary if Encoding.default_internal is nil" do
|
511
|
+
with_internal_encoding nil do
|
512
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
513
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
it "should not use Encoding.default_internal" do
|
518
|
+
with_internal_encoding Encoding::UTF_8 do
|
519
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
520
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
521
|
+
end
|
522
|
+
|
523
|
+
with_internal_encoding Encoding::ASCII do
|
524
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
525
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
526
|
+
end
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
{
|
531
|
+
'char_test' => 'CHAR',
|
532
|
+
'varchar_test' => 'VARCHAR',
|
533
|
+
'varbinary_test' => 'VARBINARY',
|
534
|
+
'tiny_blob_test' => 'TINYBLOB',
|
535
|
+
'tiny_text_test' => 'TINYTEXT',
|
536
|
+
'blob_test' => 'BLOB',
|
537
|
+
'text_test' => 'TEXT',
|
538
|
+
'medium_blob_test' => 'MEDIUMBLOB',
|
539
|
+
'medium_text_test' => 'MEDIUMTEXT',
|
540
|
+
'long_blob_test' => 'LONGBLOB',
|
541
|
+
'long_text_test' => 'LONGTEXT'
|
542
|
+
}.each do |field, type|
|
543
|
+
it "should return a String for #{type}" do
|
544
|
+
expect(@test_result[field].class).to eql(String)
|
545
|
+
expect(@test_result[field]).to eql("test")
|
546
|
+
end
|
547
|
+
|
548
|
+
context "string encoding for #{type} values" do
|
549
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
550
|
+
|
551
|
+
if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
|
552
|
+
it "should default to binary if Encoding.default_internal is nil" do
|
553
|
+
with_internal_encoding nil do
|
554
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
555
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
556
|
+
end
|
557
|
+
end
|
558
|
+
|
559
|
+
it "should not use Encoding.default_internal" do
|
560
|
+
with_internal_encoding Encoding::UTF_8 do
|
561
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
562
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
563
|
+
end
|
564
|
+
|
565
|
+
with_internal_encoding Encoding::ASCII do
|
566
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
567
|
+
expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
|
568
|
+
end
|
569
|
+
end
|
570
|
+
else
|
571
|
+
it "should default to utf-8 if Encoding.default_internal is nil" do
|
572
|
+
with_internal_encoding nil do
|
573
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
574
|
+
expect(result[field].encoding).to eql(Encoding::UTF_8)
|
575
|
+
|
576
|
+
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
577
|
+
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
578
|
+
expect(result[field].encoding).to eql(Encoding::US_ASCII)
|
579
|
+
client2.close
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
it "should use Encoding.default_internal" do
|
584
|
+
with_internal_encoding Encoding::UTF_8 do
|
585
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
586
|
+
expect(result[field].encoding).to eql(Encoding.default_internal)
|
587
|
+
end
|
588
|
+
|
589
|
+
with_internal_encoding Encoding::ASCII do
|
590
|
+
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
591
|
+
expect(result[field].encoding).to eql(Encoding.default_internal)
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
end
|
596
|
+
end
|
597
|
+
end
|
598
|
+
end
|