mysql 2.8.1-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1488 @@
1
+ #!/usr/local/bin/ruby
2
+ # $Id: test.rb 244 2009-02-01 08:43:39Z tommy $
3
+
4
+ require "test/unit"
5
+ require 'ostruct'
6
+ require 'mysql'
7
+
8
+ CONFIG = OpenStruct.new
9
+ CONFIG.host = ENV['MYSQL_HOST'] || 'localhost'
10
+ CONFIG.port = ENV['MYSQL_PORT'] || '3306'
11
+ CONFIG.user = ENV['MYSQL_USER'] || 'root'
12
+ CONFIG.pass = ENV['MYSQL_PASS'] || ''
13
+ CONFIG.sock = ENV['MYSQL_SOCK']
14
+ CONFIG.flag = ENV['MYSQL_FLAG']
15
+ CONFIG.database = ENV['MYSQL_DATABASE'] || 'test'
16
+
17
+ class TC_Mysql < Test::Unit::TestCase
18
+ def setup()
19
+ @host = CONFIG.host
20
+ @user = CONFIG.user
21
+ @pass = CONFIG.pass
22
+ @db = CONFIG.database
23
+
24
+ @port = CONFIG.port.to_i
25
+ @sock = CONFIG.sock
26
+ @flag = CONFIG.flag.to_i
27
+ end
28
+
29
+ def teardown()
30
+ end
31
+
32
+ def test_version()
33
+ assert_equal(20801, Mysql::VERSION)
34
+ end
35
+
36
+ def test_init()
37
+ assert_nothing_raised{@m = Mysql.init}
38
+ assert_nothing_raised{@m.close}
39
+ end
40
+
41
+ def test_real_connect()
42
+ assert_nothing_raised{@m = Mysql.real_connect(@host, @user, @pass, @db, @port, @sock, @flag)}
43
+ assert_nothing_raised{@m.close}
44
+ end
45
+
46
+ def test_connect()
47
+ assert_nothing_raised{@m = Mysql.connect(@host, @user, @pass, @db, @port, @sock, @flag)}
48
+ assert_nothing_raised{@m.close}
49
+ end
50
+
51
+ def test_new()
52
+ assert_nothing_raised{@m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)}
53
+ assert_nothing_raised{@m.close}
54
+ end
55
+
56
+ def test_escape_string()
57
+ assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.escape_string("abc'def\"ghi\0jkl%mno"))
58
+ end
59
+
60
+ def test_quote()
61
+ assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.quote("abc'def\"ghi\0jkl%mno"))
62
+ end
63
+
64
+ def test_get_client_info()
65
+ assert_match(/^\d.\d+.\d+[a-z]?(-.*)?$/, Mysql.get_client_info())
66
+ end
67
+
68
+ def test_client_info()
69
+ assert_match(/^\d.\d+.\d+[a-z]?(-.*)?$/, Mysql.client_info())
70
+ end
71
+
72
+ def test_options()
73
+ @m = Mysql.init
74
+ assert_equal(@m, @m.options(Mysql::INIT_COMMAND, "SET AUTOCOMMIT=0"))
75
+ assert_equal(@m, @m.options(Mysql::OPT_COMPRESS))
76
+ assert_equal(@m, @m.options(Mysql::OPT_CONNECT_TIMEOUT, 10))
77
+ assert_equal(@m, @m.options(Mysql::GUESS_CONNECTION)) if defined? Mysql::GUESS_CONNECTION
78
+ assert_equal(@m, @m.options(Mysql::OPT_LOCAL_INFILE, true))
79
+ # assert_equal(@m, @m.options(Mysql::OPT_NAMED_PIPE))
80
+ # assert_equal(@m, @m.options(Mysql::OPT_PROTOCOL, 1))
81
+ assert_equal(@m, @m.options(Mysql::OPT_READ_TIMEOUT, 10)) if defined? Mysql::OPT_READ_TIMEOUT
82
+ assert_equal(@m, @m.options(Mysql::OPT_USE_EMBEDDED_CONNECTION)) if defined? Mysql::OPT_USE_EMBEDDED_CONNECTION
83
+ assert_equal(@m, @m.options(Mysql::OPT_USE_REMOTE_CONNECTION)) if defined? Mysql::OPT_USE_REMOTE_CONNECTION
84
+ assert_equal(@m, @m.options(Mysql::OPT_WRITE_TIMEOUT, 10)) if defined? Mysql::OPT_WRITE_TIMEOUT
85
+ # assert_equal(@m, @m.options(Mysql::READ_DEFAULT_FILE, "/tmp/hoge"))
86
+ assert_equal(@m, @m.options(Mysql::READ_DEFAULT_GROUP, "test"))
87
+ assert_equal(@m, @m.options(Mysql::SECURE_AUTH, true)) if defined? Mysql::SECURE_AUTH
88
+ # assert_equal(@m, @m.options(Mysql::SET_CHARSET_DIR, "??"))
89
+ assert_equal(@m, @m.options(Mysql::SET_CHARSET_NAME, "latin1"))
90
+ assert_equal(@m, @m.options(Mysql::SET_CLIENT_IP, "127.0.0.1")) if defined? Mysql::SET_CLIENT_IP
91
+ # assert_equal(@m, @m.options(Mysql::SHARED_MEMORY_BASE_NAME, "xxx"))
92
+ assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
93
+ @m.close
94
+ end
95
+
96
+ def test_real_connect2()
97
+ @m = Mysql.init
98
+ assert_equal(@m, @m.real_connect(@host, @user, @pass, @db, @port, @sock, @flag))
99
+ @m.close
100
+ end
101
+
102
+ def test_connect2()
103
+ @m = Mysql.init
104
+ assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
105
+ @m.close
106
+ end
107
+
108
+ end
109
+
110
+ class TC_Mysql2 < Test::Unit::TestCase
111
+ def setup()
112
+ @host = CONFIG.host
113
+ @user = CONFIG.user
114
+ @pass = CONFIG.pass
115
+ @db = CONFIG.database
116
+
117
+ @port = CONFIG.port.to_i
118
+ @sock = CONFIG.sock
119
+ @flag = CONFIG.flag.to_i
120
+
121
+ @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
122
+ end
123
+
124
+ def teardown()
125
+ @m.close if @m
126
+ end
127
+
128
+ def test_affected_rows()
129
+ @m.query("create temporary table t (id int)")
130
+ @m.query("insert into t values (1)")
131
+ assert_equal(1, @m.affected_rows)
132
+ end
133
+
134
+ def test_autocommit()
135
+ if @m.methods.include? "autocommit" then
136
+ assert_equal(@m, @m.autocommit(true))
137
+ assert_equal(@m, @m.autocommit(false))
138
+ end
139
+ end
140
+
141
+ # def test_ssl_set()
142
+ # end
143
+
144
+ def test_more_results_next_result()
145
+ if @m.server_version >= 40100 then
146
+ @m.query_with_result = false
147
+ @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON) if defined? Mysql::OPTION_MULTI_STATEMENTS_ON
148
+ @m.query("select 1,2,3; select 4,5,6")
149
+ res = @m.store_result
150
+ assert_equal(["1","2","3"], res.fetch_row)
151
+ assert_equal(nil, res.fetch_row)
152
+ assert_equal(true, @m.more_results)
153
+ assert_equal(true, @m.more_results?)
154
+ assert_equal(true, @m.next_result)
155
+ res = @m.store_result
156
+ assert_equal(["4","5","6"], res.fetch_row)
157
+ assert_equal(nil, res.fetch_row)
158
+ assert_equal(false, @m.more_results)
159
+ assert_equal(false, @m.more_results?)
160
+ assert_equal(false, @m.next_result)
161
+ end
162
+ end if Mysql.client_version >= 40100
163
+
164
+ def test_query_with_block()
165
+ if @m.server_version >= 40100 then
166
+ @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
167
+ expect = [["1","2","3"], ["4","5","6"]]
168
+ @m.query("select 1,2,3; select 4,5,6") {|res|
169
+ assert_equal(1, res.num_rows)
170
+ assert_equal(expect.shift, res.fetch_row)
171
+ }
172
+ assert(expect.empty?)
173
+ expect = [["1","2","3"], ["4","5","6"]]
174
+ assert_raises(Mysql::Error) {
175
+ @m.query("select 1,2,3; hoge; select 4,5,6") {|res|
176
+ assert_equal(1, res.num_rows)
177
+ assert_equal(expect.shift, res.fetch_row)
178
+ }
179
+ }
180
+ assert_equal(1, expect.size)
181
+ expect = [["1","2","3"], ["4","5","6"]]
182
+ assert_raises(Mysql::Error) {
183
+ @m.query("select 1,2,3; select 4,5,6; hoge") {|res|
184
+ assert_equal(1, res.num_rows)
185
+ assert_equal(expect.shift, res.fetch_row)
186
+ }
187
+ }
188
+ assert(expect.empty?)
189
+ end
190
+ end
191
+
192
+ def test_query_with_block_single()
193
+ @m.query("select 1,2,3") {|res|
194
+ assert_equal(1, res.num_rows)
195
+ assert_equal(["1","2","3"], res.fetch_row)
196
+ }
197
+ end
198
+
199
+ def test_set_server_option()
200
+ if @m.server_version >= 40101 then
201
+ assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON))
202
+ assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_OFF))
203
+ end
204
+ end if Mysql.client_version >= 40101
205
+
206
+ def test_sqlstate()
207
+ if @m.server_version >= 40100 then
208
+ if RUBY_PLATFORM !~ /mingw|mswin/ then
209
+ assert_equal("00000", @m.sqlstate)
210
+ else
211
+ assert_equal("HY000", @m.sqlstate)
212
+ end
213
+ assert_raises(Mysql::Error){@m.query("hogehoge")}
214
+ assert_equal("42000", @m.sqlstate)
215
+ end
216
+ end
217
+
218
+ def test_query_with_result()
219
+ assert_equal(true, @m.query_with_result)
220
+ assert_equal(false, @m.query_with_result = false)
221
+ assert_equal(false, @m.query_with_result)
222
+ assert_equal(true, @m.query_with_result = true)
223
+ assert_equal(true, @m.query_with_result)
224
+ end
225
+
226
+ def test_reconnect()
227
+ assert_equal(false, @m.reconnect)
228
+ assert_equal(true, @m.reconnect = true)
229
+ assert_equal(true, @m.reconnect)
230
+ assert_equal(false, @m.reconnect = false)
231
+ assert_equal(false, @m.reconnect)
232
+ end
233
+ end
234
+
235
+ class TC_MysqlRes < Test::Unit::TestCase
236
+ def setup()
237
+ @host = CONFIG.host
238
+ @user = CONFIG.user
239
+ @pass = CONFIG.pass
240
+ @db = CONFIG.database
241
+
242
+ @port = CONFIG.port.to_i
243
+ @sock = CONFIG.sock
244
+ @flag = CONFIG.flag.to_i
245
+
246
+ @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
247
+ @m.query("create temporary table t (id int, str char(10), primary key (id))")
248
+ @m.query("insert into t values (1, 'abc'), (2, 'defg'), (3, 'hi'), (4, null)")
249
+ @res = @m.query("select * from t")
250
+ end
251
+
252
+ def teardown()
253
+ @res.free
254
+ @m.close
255
+ end
256
+
257
+ def test_num_fields()
258
+ assert_equal(2, @res.num_fields)
259
+ end
260
+
261
+ def test_num_rows()
262
+ assert_equal(4, @res.num_rows)
263
+ end
264
+
265
+ def test_fetch_row()
266
+ assert_equal(["1","abc"], @res.fetch_row)
267
+ assert_equal(["2","defg"], @res.fetch_row)
268
+ assert_equal(["3","hi"], @res.fetch_row)
269
+ assert_equal(["4",nil], @res.fetch_row)
270
+ assert_equal(nil, @res.fetch_row)
271
+ end
272
+
273
+ def test_fetch_hash()
274
+ assert_equal({"id"=>"1", "str"=>"abc"}, @res.fetch_hash)
275
+ assert_equal({"id"=>"2", "str"=>"defg"}, @res.fetch_hash)
276
+ assert_equal({"id"=>"3", "str"=>"hi"}, @res.fetch_hash)
277
+ assert_equal({"id"=>"4", "str"=>nil}, @res.fetch_hash)
278
+ assert_equal(nil, @res.fetch_hash)
279
+ end
280
+
281
+ def test_fetch_hash2()
282
+ assert_equal({"t.id"=>"1", "t.str"=>"abc"}, @res.fetch_hash(true))
283
+ assert_equal({"t.id"=>"2", "t.str"=>"defg"}, @res.fetch_hash(true))
284
+ assert_equal({"t.id"=>"3", "t.str"=>"hi"}, @res.fetch_hash(true))
285
+ assert_equal({"t.id"=>"4", "t.str"=>nil}, @res.fetch_hash(true))
286
+ assert_equal(nil, @res.fetch_hash)
287
+ end
288
+
289
+ def test_each()
290
+ ary = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
291
+ @res.each do |a|
292
+ assert_equal(ary.shift, a)
293
+ end
294
+ end
295
+
296
+ def test_each_hash()
297
+ hash = [{"id"=>"1","str"=>"abc"}, {"id"=>"2","str"=>"defg"}, {"id"=>"3","str"=>"hi"}, {"id"=>"4","str"=>nil}]
298
+ @res.each_hash do |h|
299
+ assert_equal(hash.shift, h)
300
+ end
301
+ end
302
+
303
+ def test_data_seek()
304
+ assert_equal(["1","abc"], @res.fetch_row)
305
+ assert_equal(["2","defg"], @res.fetch_row)
306
+ assert_equal(["3","hi"], @res.fetch_row)
307
+ @res.data_seek(1)
308
+ assert_equal(["2","defg"], @res.fetch_row)
309
+ end
310
+
311
+ def test_row_seek()
312
+ assert_equal(["1","abc"], @res.fetch_row)
313
+ pos = @res.row_tell
314
+ assert_equal(["2","defg"], @res.fetch_row)
315
+ assert_equal(["3","hi"], @res.fetch_row)
316
+ @res.row_seek(pos)
317
+ assert_equal(["2","defg"], @res.fetch_row)
318
+ end
319
+
320
+ def test_field_seek()
321
+ assert_equal(0, @res.field_tell)
322
+ @res.fetch_field
323
+ assert_equal(1, @res.field_tell)
324
+ @res.fetch_field
325
+ assert_equal(2, @res.field_tell)
326
+ @res.field_seek(1)
327
+ assert_equal(1, @res.field_tell)
328
+ end
329
+
330
+ def test_fetch_field()
331
+ f = @res.fetch_field
332
+ assert_equal("id", f.name)
333
+ assert_equal("t", f.table)
334
+ assert_equal(nil, f.def)
335
+ assert_equal(Mysql::Field::TYPE_LONG, f.type)
336
+ assert_equal(11, f.length)
337
+ assert_equal(1, f.max_length)
338
+ assert_equal(Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG, f.flags)
339
+ assert_equal(0, f.decimals)
340
+ f = @res.fetch_field
341
+ assert_equal("str", f.name)
342
+ assert_equal("t", f.table)
343
+ assert_equal(nil, f.def)
344
+ assert_equal(Mysql::Field::TYPE_STRING, f.type)
345
+ assert_equal(10, f.length)
346
+ assert_equal(4, f.max_length)
347
+ assert_equal(0, f.flags)
348
+ assert_equal(0, f.decimals)
349
+ f = @res.fetch_field
350
+ assert_equal(nil, f)
351
+ end
352
+
353
+ def test_fetch_fields()
354
+ a = @res.fetch_fields
355
+ assert_equal(2, a.size)
356
+ assert_equal("id", a[0].name)
357
+ assert_equal("str", a[1].name)
358
+ end
359
+
360
+ def test_fetch_field_direct()
361
+ f = @res.fetch_field_direct(0)
362
+ assert_equal("id", f.name)
363
+ f = @res.fetch_field_direct(1)
364
+ assert_equal("str", f.name)
365
+ assert_raises(Mysql::Error){@res.fetch_field_direct(-1)}
366
+ assert_raises(Mysql::Error){@res.fetch_field_direct(2)}
367
+ end
368
+
369
+ def test_fetch_lengths()
370
+ assert_equal(nil, @res.fetch_lengths())
371
+ @res.fetch_row
372
+ assert_equal([1, 3], @res.fetch_lengths())
373
+ @res.fetch_row
374
+ assert_equal([1, 4], @res.fetch_lengths())
375
+ @res.fetch_row
376
+ assert_equal([1, 2], @res.fetch_lengths())
377
+ @res.fetch_row
378
+ assert_equal([1, 0], @res.fetch_lengths())
379
+ @res.fetch_row
380
+ assert_equal(nil, @res.fetch_lengths())
381
+ end
382
+
383
+ def test_field_hash()
384
+ f = @res.fetch_field
385
+ h = {
386
+ "name" => "id",
387
+ "table" => "t",
388
+ "def" => nil,
389
+ "type" => Mysql::Field::TYPE_LONG,
390
+ "length" => 11,
391
+ "max_length" => 1,
392
+ "flags" => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
393
+ "decimals" => 0,
394
+ }
395
+ assert_equal(h, f.hash)
396
+ f = @res.fetch_field
397
+ h = {
398
+ "name" => "str",
399
+ "table" => "t",
400
+ "def" => nil,
401
+ "type" => Mysql::Field::TYPE_STRING,
402
+ "length" => 10,
403
+ "max_length" => 4,
404
+ "flags" => 0,
405
+ "decimals" => 0,
406
+ }
407
+ assert_equal(h, f.hash)
408
+ end
409
+
410
+ def test_field_inspect()
411
+ f = @res.fetch_field
412
+ assert_equal("#<Mysql::Field:id>", f.inspect)
413
+ f = @res.fetch_field
414
+ assert_equal("#<Mysql::Field:str>", f.inspect)
415
+ end
416
+
417
+ def test_is_num()
418
+ f = @res.fetch_field
419
+ assert_equal(true, f.is_num?)
420
+ f = @res.fetch_field
421
+ assert_equal(false, f.is_num?)
422
+ end
423
+
424
+ def test_is_not_null()
425
+ f = @res.fetch_field
426
+ assert_equal(true, f.is_not_null?)
427
+ f = @res.fetch_field
428
+ assert_equal(false, f.is_not_null?)
429
+ end
430
+
431
+ def test_is_pri_key()
432
+ f = @res.fetch_field
433
+ assert_equal(true, f.is_pri_key?)
434
+ f = @res.fetch_field
435
+ assert_equal(false, f.is_pri_key?)
436
+ end
437
+
438
+ end
439
+
440
+ class TC_MysqlStmt < Test::Unit::TestCase
441
+ def setup()
442
+ @host = CONFIG.host
443
+ @user = CONFIG.user
444
+ @pass = CONFIG.pass
445
+ @db = CONFIG.database
446
+
447
+ @port = CONFIG.port.to_i
448
+ @sock = CONFIG.sock
449
+ @flag = CONFIG.flag.to_i
450
+
451
+ @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
452
+ end
453
+
454
+ def teardown()
455
+ end
456
+
457
+ def test_init()
458
+ if @m.server_version >= 40100 then
459
+ s = @m.stmt_init()
460
+ assert_equal(Mysql::Stmt, s.class)
461
+ s.close
462
+ end
463
+ end
464
+
465
+ def test_prepare()
466
+ if @m.server_version >= 40100 then
467
+ s = @m.prepare("select 1")
468
+ assert_equal(Mysql::Stmt, s.class)
469
+ s.close
470
+ end
471
+ end
472
+
473
+ end if Mysql.client_version >= 40100
474
+
475
+ class TC_MysqlStmt2 < Test::Unit::TestCase
476
+ def setup()
477
+ @host = CONFIG.host
478
+ @user = CONFIG.user
479
+ @pass = CONFIG.pass
480
+ @db = CONFIG.database
481
+
482
+ @port = CONFIG.port.to_i
483
+ @sock = CONFIG.sock
484
+ @flag = CONFIG.flag.to_i
485
+
486
+ @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
487
+ @s = @m.stmt_init()
488
+ end
489
+
490
+ def teardown()
491
+ @s.close
492
+ @m.close
493
+ end
494
+
495
+ def test_affected_rows()
496
+ if @m.server_version >= 40100 then
497
+ @m.query("create temporary table t (i int, c char(10))")
498
+ @s.prepare("insert into t values (?,?)")
499
+ @s.execute(1, "hoge")
500
+ assert_equal(1, @s.affected_rows())
501
+ @s.execute(2, "hoge")
502
+ @s.execute(3, "hoge")
503
+ @s.prepare("update t set c=?")
504
+ @s.execute("fuga")
505
+ assert_equal(3, @s.affected_rows())
506
+ end
507
+ end
508
+
509
+ =begin
510
+ def test_attr_get()
511
+ assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
512
+ assert_raises(Mysql::Error){@s.attr_get(999)}
513
+ end
514
+
515
+ def test_attr_set()
516
+ @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, true)
517
+ assert_equal(true, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
518
+ @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, false)
519
+ assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
520
+ assert_raises(Mysql::Error){@s.attr_set(999, true)}
521
+ end
522
+
523
+ def test_bind_param()
524
+ @s.prepare("insert into t values (?,?)")
525
+ @s.bind_param(123, "abc")
526
+ @s.bind_param(Time.now, nil)
527
+ assert_raises(Mysql::Error){@s.bind_param(1, 2, 3)}
528
+ b = @s.bind_param(Bind.new(Mysql::TYPE_TINY, 99, false))
529
+ @s.bind_param(98.765, b)
530
+ end
531
+ =end
532
+
533
+ def test_bind_result_nil()
534
+ if @m.server_version >= 40100 then
535
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
536
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
537
+ @s.prepare("select * from t")
538
+ @s.bind_result(nil,nil,nil,nil)
539
+ @s.execute
540
+ a = @s.fetch
541
+ assert_equal([123, "9abcdefg", 1.2345, Mysql::Time.new(2005,8,2,23,50,11)], a)
542
+ end
543
+ end
544
+
545
+ def test_bind_result_numeric()
546
+ if @m.server_version >= 40100 then
547
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
548
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
549
+ @s.prepare("select * from t")
550
+ @s.bind_result(Numeric, Numeric, Numeric, Numeric)
551
+ @s.execute
552
+ a = @s.fetch
553
+ if Mysql.client_version < 50000 then
554
+ assert_equal([123, 9, 1, 2005], a)
555
+ else
556
+ assert_equal([123, 9, 1, 20050802235011], a)
557
+ end
558
+ end
559
+ end
560
+
561
+ def test_bind_result_integer()
562
+ if @m.server_version >= 40100 then
563
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
564
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
565
+ @s.prepare("select * from t")
566
+ @s.bind_result(Integer, Integer, Integer, Integer)
567
+ @s.execute
568
+ a = @s.fetch
569
+ if Mysql.client_version < 50000 then
570
+ assert_equal([123, 9, 1, 2005], a)
571
+ else
572
+ assert_equal([123, 9, 1, 20050802235011], a)
573
+ end
574
+ end
575
+ end
576
+
577
+ def test_bind_result_fixnum()
578
+ if @m.server_version >= 40100 then
579
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
580
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
581
+ @s.prepare("select * from t")
582
+ @s.bind_result(Fixnum, Fixnum, Fixnum, Fixnum)
583
+ @s.execute
584
+ a = @s.fetch
585
+ if Mysql.client_version < 50000 then
586
+ assert_equal([123, 9, 1, 2005], a)
587
+ else
588
+ assert_equal([123, 9, 1, 20050802235011.0], a)
589
+ end
590
+ end
591
+ end
592
+
593
+ def test_bind_result_string()
594
+ if @m.server_version >= 40100 then
595
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
596
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
597
+ @s.prepare("select * from t")
598
+ @s.bind_result(String, String, String, String)
599
+ @s.execute
600
+ a = @s.fetch
601
+ assert_equal(["123", "9abcdefg", "1.2345", "2005-08-02 23:50:11"], a)
602
+ end
603
+ end
604
+
605
+ def test_bind_result_float()
606
+ if @m.server_version >= 40100 then
607
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
608
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
609
+ @s.prepare("select * from t")
610
+ @s.bind_result(Float, Float, Float, Float)
611
+ @s.execute
612
+ a = @s.fetch
613
+ if Mysql.client_version < 50000 then
614
+ assert_equal([123.0, 9.0, 1.2345, 2005.0], a)
615
+ else
616
+ assert_equal([123.0, 9.0, 1.2345, 20050802235011.0], a)
617
+ end
618
+ end
619
+ end
620
+
621
+ def test_bind_result_mysqltime()
622
+ if @m.server_version >= 40100 then
623
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
624
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
625
+ @s.prepare("select * from t")
626
+ @s.bind_result(Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time)
627
+ @s.execute
628
+ a = @s.fetch
629
+ if Mysql.client_version < 50000 then
630
+ assert_equal([Mysql::Time.new, Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
631
+ else
632
+ assert_equal([Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
633
+ end
634
+ end
635
+ end
636
+
637
+ def test_bind_result_unknown()
638
+ if @m.server_version >= 40100 then
639
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
640
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
641
+ @s.prepare("select * from t")
642
+ assert_raises(TypeError){@s.bind_result(Time, nil, nil, nil)}
643
+ end
644
+ end
645
+
646
+ def test_bind_result_unmatch_count()
647
+ if @m.server_version >= 40100 then
648
+ @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
649
+ @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
650
+ @s.prepare("select * from t")
651
+ assert_raises(Mysql::Error){@s.bind_result(nil, nil)}
652
+ end
653
+ end
654
+
655
+ def test_data_seek()
656
+ if @m.server_version >= 40100 then
657
+ @m.query("create temporary table t (i int)")
658
+ @m.query("insert into t values (0),(1),(2),(3),(4),(5)")
659
+ @s.prepare("select i from t")
660
+ @s.execute
661
+ assert_equal([0], @s.fetch)
662
+ assert_equal([1], @s.fetch)
663
+ assert_equal([2], @s.fetch)
664
+ @s.data_seek(5)
665
+ assert_equal([5], @s.fetch)
666
+ @s.data_seek(1)
667
+ assert_equal([1], @s.fetch)
668
+ end
669
+ end
670
+
671
+ =begin
672
+ def test_errno()
673
+ @s.errno()
674
+ end
675
+
676
+ def test_error()
677
+ @s.error()
678
+ end
679
+ =end
680
+
681
+ def test_execute()
682
+ if @m.server_version >= 40100 then
683
+ @m.query("create temporary table t (i int)")
684
+ @s.prepare("insert into t values (123)")
685
+ @s.execute()
686
+ assert_equal(1, @s.affected_rows)
687
+ @s.execute()
688
+ assert_equal(1, @s.affected_rows)
689
+ assert_equal(2, @m.query("select count(*) from t").fetch_row[0].to_i)
690
+ end
691
+ end
692
+
693
+ def test_execute2()
694
+ if @m.server_version >= 40100 then
695
+ @m.query("create temporary table t (i int)")
696
+ @s.prepare("insert into t values (?)")
697
+ @s.execute(123)
698
+ @s.execute("456")
699
+ @s.prepare("select * from t")
700
+ @s.execute
701
+ assert_equal([123], @s.fetch)
702
+ assert_equal([456], @s.fetch)
703
+ end
704
+ end
705
+
706
+ def test_execute3()
707
+ if @m.server_version >= 40100 then
708
+ @m.query("create temporary table t (i int, c char(255), t timestamp)")
709
+ @s.prepare("insert into t values (?,?,?)")
710
+ @s.execute(123, "hoge", Time.local(2005,7,19,23,53,0));
711
+ assert_raises(Mysql::Error){@s.execute(123, "hoge")}
712
+ assert_raises(Mysql::Error){@s.execute(123, "hoge", 0, "fuga")}
713
+ @s.prepare("select * from t")
714
+ @s.execute
715
+ assert_equal([123, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
716
+ end
717
+ end
718
+
719
+ def test_execute4()
720
+ if @m.server_version >= 40100 then
721
+ @m.query("create temporary table t (i int, c char(255), t timestamp)")
722
+ @s.prepare("insert into t values (?,?,?)")
723
+ @s.execute(nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0));
724
+ @s.prepare("select * from t")
725
+ @s.execute
726
+ assert_equal([nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
727
+ end
728
+ end
729
+
730
+ def test_execute5()
731
+ if @m.server_version >= 40100 then
732
+ [30, 31, 32, 62, 63].each do |i|
733
+ v, = @m.prepare("select cast(? as signed)").execute(2**i-1).fetch
734
+ assert_equal(2**i-1, v)
735
+ v, = @m.prepare("select cast(? as signed)").execute(-(2**i)).fetch
736
+ assert_equal(-(2**i), v)
737
+ end
738
+ end
739
+ end
740
+
741
+ def test_fetch()
742
+ if @m.server_version >= 40100 then
743
+ @s.prepare("select 123, 'abc', null")
744
+ @s.execute()
745
+ assert_equal([123, "abc", nil], @s.fetch())
746
+ end
747
+ end
748
+
749
+ def test_fetch_bit()
750
+ if @m.client_version >= 50003 and @m.server_version >= 50003 then
751
+ @m.query("create temporary table t (i bit(8))")
752
+ @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
753
+ @s.prepare("select i from t")
754
+ @s.execute
755
+ assert_equal(["\x00"], @s.fetch)
756
+ assert_equal(["\xff"], @s.fetch)
757
+ assert_equal(["\x7f"], @s.fetch)
758
+ assert_equal(["\xff"], @s.fetch)
759
+ assert_equal(["\xff"], @s.fetch)
760
+ assert_equal(["\xff"], @s.fetch)
761
+ assert_equal(["\xff"], @s.fetch)
762
+ @m.query("create temporary table t2 (i bit(64))")
763
+ @m.query("insert into t2 values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)")
764
+ @s.prepare("select i from t2")
765
+ @s.execute
766
+ assert_equal(["\x00\x00\x00\x00\x00\x00\x00\x00"], @s.fetch)
767
+ assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
768
+ assert_equal(["\x00\x00\x00\x01\x00\x00\x00\x00"], @s.fetch)
769
+ assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
770
+ assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
771
+ end
772
+ end
773
+
774
+ def test_fetch_tinyint()
775
+ if @m.server_version >= 40100 then
776
+ @m.query("create temporary table t (i tinyint)")
777
+ @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255)")
778
+ @s.prepare("select i from t")
779
+ @s.execute
780
+ assert_equal([0], @s.fetch)
781
+ assert_equal([-1], @s.fetch)
782
+ assert_equal([127], @s.fetch)
783
+ assert_equal([-128], @s.fetch)
784
+ assert_equal([127], @s.fetch)
785
+ assert_equal([-128], @s.fetch)
786
+ end
787
+ end
788
+
789
+ def test_fetch_tinyint_unsigned()
790
+ if @m.server_version >= 40100 then
791
+ @m.query("create temporary table t (i tinyint unsigned)")
792
+ @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
793
+ @s.prepare("select i from t")
794
+ @s.execute
795
+ assert_equal([0], @s.fetch)
796
+ assert_equal([0], @s.fetch)
797
+ assert_equal([127], @s.fetch)
798
+ assert_equal([0], @s.fetch)
799
+ assert_equal([255], @s.fetch)
800
+ assert_equal([0], @s.fetch)
801
+ assert_equal([255], @s.fetch)
802
+ end
803
+ end
804
+
805
+ def test_fetch_smallint()
806
+ if @m.server_version >= 40100 then
807
+ @m.query("create temporary table t (i smallint)")
808
+ @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
809
+ @s.prepare("select i from t")
810
+ @s.execute
811
+ assert_equal([0], @s.fetch)
812
+ assert_equal([-1], @s.fetch)
813
+ assert_equal([32767], @s.fetch)
814
+ assert_equal([-32768], @s.fetch)
815
+ assert_equal([32767], @s.fetch)
816
+ assert_equal([-32768], @s.fetch)
817
+ end
818
+ end
819
+
820
+ def test_fetch_smallint_unsigned()
821
+ if @m.server_version >= 40100 then
822
+ @m.query("create temporary table t (i smallint unsigned)")
823
+ @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
824
+ @s.prepare("select i from t")
825
+ @s.execute
826
+ assert_equal([0], @s.fetch)
827
+ assert_equal([0], @s.fetch)
828
+ assert_equal([32767], @s.fetch)
829
+ assert_equal([0], @s.fetch)
830
+ assert_equal([65535], @s.fetch)
831
+ assert_equal([0], @s.fetch)
832
+ assert_equal([65535], @s.fetch)
833
+ end
834
+ end
835
+
836
+ def test_fetch_mediumint()
837
+ if @m.server_version >= 40100 then
838
+ @m.query("create temporary table t (i mediumint)")
839
+ @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
840
+ @s.prepare("select i from t")
841
+ @s.execute
842
+ assert_equal([0], @s.fetch)
843
+ assert_equal([-1], @s.fetch)
844
+ assert_equal([8388607], @s.fetch)
845
+ assert_equal([-8388608], @s.fetch)
846
+ assert_equal([8388607], @s.fetch)
847
+ assert_equal([-8388608], @s.fetch)
848
+ end
849
+ end
850
+
851
+ def test_fetch_mediumint_unsigned()
852
+ if @m.server_version >= 40100 then
853
+ @m.query("create temporary table t (i mediumint unsigned)")
854
+ @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
855
+ @s.prepare("select i from t")
856
+ @s.execute
857
+ assert_equal([0], @s.fetch)
858
+ assert_equal([0], @s.fetch)
859
+ assert_equal([8388607], @s.fetch)
860
+ assert_equal([0], @s.fetch)
861
+ assert_equal([16777215], @s.fetch)
862
+ assert_equal([0], @s.fetch)
863
+ assert_equal([16777215], @s.fetch)
864
+ end
865
+ end
866
+
867
+ def test_fetch_int()
868
+ if @m.server_version >= 40100 then
869
+ @m.query("create temporary table t (i int)")
870
+ @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
871
+ @s.prepare("select i from t")
872
+ @s.execute
873
+ assert_equal([0], @s.fetch)
874
+ assert_equal([-1], @s.fetch)
875
+ assert_equal([2147483647], @s.fetch)
876
+ assert_equal([-2147483648], @s.fetch)
877
+ assert_equal([2147483647], @s.fetch)
878
+ assert_equal([-2147483648], @s.fetch)
879
+ end
880
+ end
881
+
882
+ def test_fetch_int_unsigned()
883
+ if @m.server_version >= 40100 then
884
+ @m.query("create temporary table t (i int unsigned)")
885
+ @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
886
+ @s.prepare("select i from t")
887
+ @s.execute
888
+ assert_equal([0], @s.fetch)
889
+ assert_equal([0], @s.fetch)
890
+ assert_equal([2147483647], @s.fetch)
891
+ assert_equal([0], @s.fetch)
892
+ assert_equal([4294967295], @s.fetch)
893
+ assert_equal([0], @s.fetch)
894
+ assert_equal([4294967295], @s.fetch)
895
+ end
896
+ end
897
+
898
+ def test_fetch_bigint()
899
+ if @m.server_version >= 40100 then
900
+ @m.query("create temporary table t (i bigint)")
901
+ @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
902
+ @s.prepare("select i from t")
903
+ @s.execute
904
+ assert_equal([0], @s.fetch)
905
+ assert_equal([-1], @s.fetch)
906
+ assert_equal([9223372036854775807], @s.fetch)
907
+ assert_equal([-9223372036854775808], @s.fetch)
908
+ if @m.server_version >= 50000 then
909
+ assert_equal([9223372036854775807], @s.fetch)
910
+ else
911
+ assert_equal([-1], @s.fetch) # MySQL problem
912
+ end
913
+ assert_equal([-9223372036854775808], @s.fetch)
914
+ assert_equal([9223372036854775807], @s.fetch)
915
+ end
916
+ end
917
+
918
+ def test_fetch_bigint_unsigned()
919
+ if @m.server_version >= 40100 then
920
+ @m.query("create temporary table t (i bigint unsigned)")
921
+ @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
922
+ @s.prepare("select i from t")
923
+ @s.execute
924
+ assert_equal([0], @s.fetch)
925
+ if @m.server_version >= 50000 then
926
+ assert_equal([0], @s.fetch)
927
+ else
928
+ assert_equal([18446744073709551615], @s.fetch) # MySQL problem
929
+ end
930
+ assert_equal([9223372036854775807], @s.fetch)
931
+ if @m.server_version >= 50000 then
932
+ assert_equal([0], @s.fetch)
933
+ else
934
+ assert_equal([9223372036854775808], @s.fetch) # MySQL problem
935
+ end
936
+ assert_equal([18446744073709551615], @s.fetch)
937
+ assert_equal([0], @s.fetch)
938
+ assert_equal([18446744073709551615], @s.fetch)
939
+ end
940
+ end
941
+
942
+ def test_fetch_float()
943
+ if @m.server_version >= 40100 then
944
+ @m.query("create temporary table t (i float)")
945
+ @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
946
+ @s.prepare("select i from t")
947
+ @s.execute
948
+ assert_equal([0], @s.fetch)
949
+ assert_in_delta(-3.402823466E+38, @s.fetch[0], 0.000000001E+38)
950
+ assert_in_delta(-1.175494351E-38, @s.fetch[0], 0.000000001E-38)
951
+ assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
952
+ assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
953
+ end
954
+ end
955
+
956
+ def test_fetch_float_unsigned()
957
+ if @m.server_version >= 40100 then
958
+ @m.query("create temporary table t (i float unsigned)")
959
+ @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
960
+ @s.prepare("select i from t")
961
+ @s.execute
962
+ assert_equal([0], @s.fetch)
963
+ assert_equal([0], @s.fetch)
964
+ assert_equal([0], @s.fetch)
965
+ assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
966
+ assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
967
+ end
968
+ end
969
+
970
+ def test_fetch_double()
971
+ if @m.server_version >= 40100 then
972
+ @m.query("create temporary table t (i double)")
973
+ @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
974
+ @s.prepare("select i from t")
975
+ @s.execute
976
+ assert_equal([0], @s.fetch)
977
+ assert_in_delta(-Float::MAX, @s.fetch[0], Float::EPSILON)
978
+ assert_in_delta(-Float::MIN, @s.fetch[0], Float::EPSILON)
979
+ assert_in_delta(Float::MIN, @s.fetch[0], Float::EPSILON)
980
+ assert_in_delta(Float::MAX, @s.fetch[0], Float::EPSILON)
981
+ end
982
+ end
983
+
984
+ def test_fetch_double_unsigned()
985
+ if @m.server_version >= 40100 then
986
+ @m.query("create temporary table t (i double unsigned)")
987
+ @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
988
+ @s.prepare("select i from t")
989
+ @s.execute
990
+ assert_equal([0], @s.fetch)
991
+ assert_equal([0], @s.fetch)
992
+ assert_equal([0], @s.fetch)
993
+ assert_in_delta(Float::MIN, @s.fetch[0], Float::EPSILON)
994
+ assert_in_delta(Float::MAX, @s.fetch[0], Float::EPSILON)
995
+ end
996
+ end
997
+
998
+ def test_fetch_decimal()
999
+ if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
1000
+ @m.query("create temporary table t (i decimal)")
1001
+ @m.query("insert into t values (0),(9999999999),(-9999999999),(10000000000),(-10000000000)")
1002
+ @s.prepare("select i from t")
1003
+ @s.execute
1004
+ assert_equal(["0"], @s.fetch)
1005
+ assert_equal(["9999999999"], @s.fetch)
1006
+ assert_equal(["-9999999999"], @s.fetch)
1007
+ if @m.server_version < 50000 then
1008
+ assert_equal(["10000000000"], @s.fetch) # MySQL problem
1009
+ else
1010
+ assert_equal(["9999999999"], @s.fetch)
1011
+ end
1012
+ assert_equal(["-9999999999"], @s.fetch)
1013
+ end
1014
+ end
1015
+
1016
+ def test_fetch_decimal_unsigned()
1017
+ if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
1018
+ @m.query("create temporary table t (i decimal unsigned)")
1019
+ @m.query("insert into t values (0),(9999999998),(9999999999),(-9999999998),(-9999999999),(10000000000),(-10000000000)")
1020
+ @s.prepare("select i from t")
1021
+ @s.execute
1022
+ assert_equal(["0"], @s.fetch)
1023
+ assert_equal(["9999999998"], @s.fetch)
1024
+ assert_equal(["9999999999"], @s.fetch)
1025
+ assert_equal(["0"], @s.fetch)
1026
+ assert_equal(["0"], @s.fetch)
1027
+ assert_equal(["9999999999"], @s.fetch)
1028
+ assert_equal(["0"], @s.fetch)
1029
+ end
1030
+ end
1031
+
1032
+ def test_fetch_date()
1033
+ if @m.server_version >= 40100 then
1034
+ @m.query("create temporary table t (i date)")
1035
+ @m.query("insert into t values ('0000-00-00'),('1000-01-01'),('9999-12-31')")
1036
+ @s.prepare("select i from t")
1037
+ @s.execute
1038
+ assert_equal([Mysql::Time.new(0,0,0)], @s.fetch)
1039
+ assert_equal([Mysql::Time.new(1000,1,1)], @s.fetch)
1040
+ assert_equal([Mysql::Time.new(9999,12,31)], @s.fetch)
1041
+ end
1042
+ end
1043
+
1044
+ def test_fetch_datetime()
1045
+ if @m.server_version >= 40100 then
1046
+ @m.query("create temporary table t (i datetime)")
1047
+ @m.query("insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')")
1048
+ @s.prepare("select i from t")
1049
+ @s.execute
1050
+ assert_equal([Mysql::Time.new(0,0,0,0,0,0)], @s.fetch)
1051
+ assert_equal([Mysql::Time.new(1000,1,1,0,0,0)], @s.fetch)
1052
+ assert_equal([Mysql::Time.new(9999,12,31,23,59,59)], @s.fetch)
1053
+ end
1054
+ end
1055
+
1056
+ def test_fetch_timestamp()
1057
+ if @m.server_version >= 40100 then
1058
+ @m.query("create temporary table t (i timestamp)")
1059
+ @m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
1060
+ @s.prepare("select i from t")
1061
+ @s.execute
1062
+ assert_equal([Mysql::Time.new(1970,1,2,0,0,0)], @s.fetch)
1063
+ assert_equal([Mysql::Time.new(2037,12,30,23,59,59)], @s.fetch)
1064
+ end
1065
+ end
1066
+
1067
+ def test_fetch_time()
1068
+ if @m.server_version >= 40100 then
1069
+ @m.query("create temporary table t (i time)")
1070
+ @m.query("insert into t values ('-838:59:59'),(0),('838:59:59')")
1071
+ @s.prepare("select i from t")
1072
+ @s.execute
1073
+ assert_equal([Mysql::Time.new(0,0,0,838,59,59,true)], @s.fetch)
1074
+ assert_equal([Mysql::Time.new(0,0,0,0,0,0,false)], @s.fetch)
1075
+ assert_equal([Mysql::Time.new(0,0,0,838,59,59,false)], @s.fetch)
1076
+ end
1077
+ end
1078
+
1079
+ def test_fetch_year()
1080
+ if @m.server_version >= 40100 then
1081
+ @m.query("create temporary table t (i year)")
1082
+ @m.query("insert into t values (0),(70),(69),(1901),(2155)")
1083
+ @s.prepare("select i from t")
1084
+ @s.execute
1085
+ assert_equal([0], @s.fetch)
1086
+ assert_equal([1970], @s.fetch)
1087
+ assert_equal([2069], @s.fetch)
1088
+ assert_equal([1901], @s.fetch)
1089
+ assert_equal([2155], @s.fetch)
1090
+ end
1091
+ end
1092
+
1093
+ def test_fetch_char()
1094
+ if @m.server_version >= 40100 then
1095
+ @m.query("create temporary table t (i char(10))")
1096
+ @m.query("insert into t values (null),('abc')")
1097
+ @s.prepare("select i from t")
1098
+ @s.execute
1099
+ assert_equal([nil], @s.fetch)
1100
+ assert_equal(["abc"], @s.fetch)
1101
+ end
1102
+ end
1103
+
1104
+ def test_fetch_varchar()
1105
+ if @m.server_version >= 40100 then
1106
+ @m.query("create temporary table t (i varchar(10))")
1107
+ @m.query("insert into t values (null),('abc')")
1108
+ @s.prepare("select i from t")
1109
+ @s.execute
1110
+ assert_equal([nil], @s.fetch)
1111
+ assert_equal(["abc"], @s.fetch)
1112
+ end
1113
+ end
1114
+
1115
+ def test_fetch_binary()
1116
+ if @m.server_version >= 40100 then
1117
+ @m.query("create temporary table t (i binary(10))")
1118
+ @m.query("insert into t values (null),('abc')")
1119
+ @s.prepare("select i from t")
1120
+ @s.execute
1121
+ assert_equal([nil], @s.fetch)
1122
+ if @m.server_version >= 50000 then
1123
+ assert_equal(["abc\0\0\0\0\0\0\0"], @s.fetch)
1124
+ else
1125
+ assert_equal(["abc"], @s.fetch)
1126
+ end
1127
+ end
1128
+ end
1129
+
1130
+ def test_fetch_varbinary()
1131
+ if @m.server_version >= 40100 then
1132
+ @m.query("create temporary table t (i varbinary(10))")
1133
+ @m.query("insert into t values (null),('abc')")
1134
+ @s.prepare("select i from t")
1135
+ @s.execute
1136
+ assert_equal([nil], @s.fetch)
1137
+ assert_equal(["abc"], @s.fetch)
1138
+ end
1139
+ end
1140
+
1141
+ def test_fetch_tinyblob()
1142
+ if @m.server_version >= 40100 then
1143
+ @m.query("create temporary table t (i tinyblob)")
1144
+ @m.query("insert into t values (null),('abc')")
1145
+ @s.prepare("select i from t")
1146
+ @s.execute
1147
+ assert_equal([nil], @s.fetch)
1148
+ assert_equal(["abc"], @s.fetch)
1149
+ end
1150
+ end
1151
+
1152
+ def test_fetch_tinytext()
1153
+ if @m.server_version >= 40100 then
1154
+ @m.query("create temporary table t (i tinytext)")
1155
+ @m.query("insert into t values (null),('abc')")
1156
+ @s.prepare("select i from t")
1157
+ @s.execute
1158
+ assert_equal([nil], @s.fetch)
1159
+ assert_equal(["abc"], @s.fetch)
1160
+ end
1161
+ end
1162
+
1163
+ def test_fetch_blob()
1164
+ if @m.server_version >= 40100 then
1165
+ @m.query("create temporary table t (i blob)")
1166
+ @m.query("insert into t values (null),('abc')")
1167
+ @s.prepare("select i from t")
1168
+ @s.execute
1169
+ assert_equal([nil], @s.fetch)
1170
+ assert_equal(["abc"], @s.fetch)
1171
+ end
1172
+ end
1173
+
1174
+ def test_fetch_text()
1175
+ if @m.server_version >= 40100 then
1176
+ @m.query("create temporary table t (i text)")
1177
+ @m.query("insert into t values (null),('abc')")
1178
+ @s.prepare("select i from t")
1179
+ @s.execute
1180
+ assert_equal([nil], @s.fetch)
1181
+ assert_equal(["abc"], @s.fetch)
1182
+ end
1183
+ end
1184
+
1185
+ def test_fetch_mediumblob()
1186
+ if @m.server_version >= 40100 then
1187
+ @m.query("create temporary table t (i mediumblob)")
1188
+ @m.query("insert into t values (null),('abc')")
1189
+ @s.prepare("select i from t")
1190
+ @s.execute
1191
+ assert_equal([nil], @s.fetch)
1192
+ assert_equal(["abc"], @s.fetch)
1193
+ end
1194
+ end
1195
+
1196
+ def test_fetch_mediumtext()
1197
+ if @m.server_version >= 40100 then
1198
+ @m.query("create temporary table t (i mediumtext)")
1199
+ @m.query("insert into t values (null),('abc')")
1200
+ @s.prepare("select i from t")
1201
+ @s.execute
1202
+ assert_equal([nil], @s.fetch)
1203
+ assert_equal(["abc"], @s.fetch)
1204
+ end
1205
+ end
1206
+
1207
+ def test_fetch_longblob()
1208
+ if @m.server_version >= 40100 then
1209
+ @m.query("create temporary table t (i longblob)")
1210
+ @m.query("insert into t values (null),('abc')")
1211
+ @s.prepare("select i from t")
1212
+ @s.execute
1213
+ assert_equal([nil], @s.fetch)
1214
+ assert_equal(["abc"], @s.fetch)
1215
+ end
1216
+ end
1217
+
1218
+ def test_fetch_longtext()
1219
+ if @m.server_version >= 40100 then
1220
+ @m.query("create temporary table t (i longtext)")
1221
+ @m.query("insert into t values (null),('abc')")
1222
+ @s.prepare("select i from t")
1223
+ @s.execute
1224
+ assert_equal([nil], @s.fetch)
1225
+ assert_equal(["abc"], @s.fetch)
1226
+ end
1227
+ end
1228
+
1229
+ def test_fetch_enum()
1230
+ if @m.server_version >= 40100 then
1231
+ @m.query("create temporary table t (i enum('abc','def'))")
1232
+ @m.query("insert into t values (null),(0),(1),(2),('abc'),('def'),('ghi')")
1233
+ @s.prepare("select i from t")
1234
+ @s.execute
1235
+ assert_equal([nil], @s.fetch)
1236
+ assert_equal([""], @s.fetch)
1237
+ assert_equal(["abc"], @s.fetch)
1238
+ assert_equal(["def"], @s.fetch)
1239
+ assert_equal(["abc"], @s.fetch)
1240
+ assert_equal(["def"], @s.fetch)
1241
+ assert_equal([""], @s.fetch)
1242
+ end
1243
+ end
1244
+
1245
+ def test_fetch_set()
1246
+ if @m.server_version >= 40100 then
1247
+ @m.query("create temporary table t (i set('abc','def'))")
1248
+ @m.query("insert into t values (null),(0),(1),(2),(3),('abc'),('def'),('abc,def'),('ghi')")
1249
+ @s.prepare("select i from t")
1250
+ @s.execute
1251
+ assert_equal([nil], @s.fetch)
1252
+ assert_equal([""], @s.fetch)
1253
+ assert_equal(["abc"], @s.fetch)
1254
+ assert_equal(["def"], @s.fetch)
1255
+ assert_equal(["abc,def"], @s.fetch)
1256
+ assert_equal(["abc"], @s.fetch)
1257
+ assert_equal(["def"], @s.fetch)
1258
+ assert_equal(["abc,def"], @s.fetch)
1259
+ assert_equal([""], @s.fetch)
1260
+ end
1261
+ end
1262
+
1263
+ def test_each()
1264
+ if @m.server_version >= 40100 then
1265
+ @m.query("create temporary table t (i int, c char(255), d datetime)")
1266
+ @m.query("insert into t values (1,'abc','19701224235905'),(2,'def','21120903123456'),(3,'123',null)")
1267
+ @s.prepare("select * from t")
1268
+ @s.execute
1269
+ c = 0
1270
+ @s.each do |a|
1271
+ case c
1272
+ when 0
1273
+ assert_equal([1,"abc",Mysql::Time.new(1970,12,24,23,59,05)], a)
1274
+ when 1
1275
+ assert_equal([2,"def",Mysql::Time.new(2112,9,3,12,34,56)], a)
1276
+ when 2
1277
+ assert_equal([3,"123",nil], a)
1278
+ else
1279
+ raise
1280
+ end
1281
+ c += 1
1282
+ end
1283
+ end
1284
+ end
1285
+
1286
+ def test_field_count()
1287
+ if @m.server_version >= 40100 then
1288
+ @s.prepare("select 1,2,3")
1289
+ @s.execute()
1290
+ assert_equal(3, @s.field_count())
1291
+ @s.prepare("set @a=1")
1292
+ @s.execute()
1293
+ assert_equal(0, @s.field_count())
1294
+ end
1295
+ end
1296
+
1297
+ def test_free_result()
1298
+ if @m.server_version >= 40100 then
1299
+ @s.free_result()
1300
+ @s.prepare("select 1,2,3")
1301
+ @s.execute()
1302
+ @s.free_result()
1303
+ end
1304
+ end
1305
+
1306
+ def test_insert_id()
1307
+ if @m.server_version >= 40100 then
1308
+ @m.query("create temporary table t (i int auto_increment, unique(i))")
1309
+ @s.prepare("insert into t values (0)")
1310
+ @s.execute()
1311
+ assert_equal(1, @s.insert_id())
1312
+ @s.execute()
1313
+ assert_equal(2, @s.insert_id())
1314
+ end
1315
+ end
1316
+
1317
+ def test_num_rows()
1318
+ if @m.server_version >= 40100 then
1319
+ @m.query("create temporary table t (i int)")
1320
+ @m.query("insert into t values (1),(2),(3),(4)")
1321
+ @s.prepare("select * from t")
1322
+ @s.execute
1323
+ assert_equal(4, @s.num_rows())
1324
+ end
1325
+ end
1326
+
1327
+ def test_param_count()
1328
+ if @m.server_version >= 40100 then
1329
+ @m.query("create temporary table t (a int, b int, c int)")
1330
+ @s.prepare("select * from t")
1331
+ assert_equal(0, @s.param_count())
1332
+ @s.prepare("insert into t values (?,?,?)")
1333
+ assert_equal(3, @s.param_count())
1334
+ end
1335
+ end
1336
+
1337
+ =begin
1338
+ def test_param_metadata()
1339
+ @s.param_metadata()
1340
+ end
1341
+ =end
1342
+
1343
+ def test_prepare()
1344
+ if @m.server_version >= 40100 then
1345
+ @s.prepare("select 1")
1346
+ assert_raises(Mysql::Error){@s.prepare("invalid syntax")}
1347
+ end
1348
+ end
1349
+
1350
+ =begin
1351
+ def test_reset()
1352
+ @s.reset()
1353
+ end
1354
+ =end
1355
+
1356
+ def test_result_metadata()
1357
+ if @m.server_version >= 40100 then
1358
+ @s.prepare("select 1 foo, 2 bar")
1359
+ res = @s.result_metadata()
1360
+ f = res.fetch_fields
1361
+ assert_equal("foo", f[0].name)
1362
+ assert_equal("bar", f[1].name)
1363
+ end
1364
+ end
1365
+
1366
+ def test_result_metadata_nodata()
1367
+ if @m.server_version >= 40100 then
1368
+ @m.query("create temporary table t (i int)")
1369
+ @s.prepare("insert into t values (1)")
1370
+ assert_equal(nil, @s.result_metadata())
1371
+ end
1372
+ end
1373
+
1374
+ def test_row_seek_tell()
1375
+ if @m.server_version >= 40100 then
1376
+ @m.query("create temporary table t (i int)")
1377
+ @m.query("insert into t values (0),(1),(2),(3),(4)")
1378
+ @s.prepare("select * from t")
1379
+ @s.execute
1380
+ row0 = @s.row_tell
1381
+ assert_equal([0], @s.fetch)
1382
+ assert_equal([1], @s.fetch)
1383
+ row2 = @s.row_seek(row0)
1384
+ assert_equal([0], @s.fetch)
1385
+ @s.row_seek(row2)
1386
+ assert_equal([2], @s.fetch)
1387
+ end
1388
+ end
1389
+
1390
+ =begin
1391
+ def test_send_long_data()
1392
+ @m.query("create temporary table t (i int, t text)")
1393
+ @s.prepare("insert into t values (?,?)")
1394
+ @s.send_long_data(1, "long long data ")
1395
+ @s.send_long_data(1, "long long data2")
1396
+ assert_raises(Mysql::Error){@s.send_long_data(9, "invalid param number")}
1397
+ @s.execute(99, "hoge")
1398
+ assert_equal("long long data long long data2", @m.query("select t from t").fetch_row[0])
1399
+ end
1400
+ =end
1401
+
1402
+ def test_sqlstate()
1403
+ if @m.server_version >= 40100 then
1404
+ @s.prepare("select 1")
1405
+ if @m.client_version >= 50000 then
1406
+ assert_equal("00000", @s.sqlstate)
1407
+ else
1408
+ assert_equal("", @s.sqlstate)
1409
+ end
1410
+ assert_raises(Mysql::Error){@s.prepare("hogehoge")}
1411
+ assert_equal("42000", @s.sqlstate)
1412
+ end
1413
+ end
1414
+
1415
+ =begin
1416
+ def test_store_result()
1417
+ @s.store_result()
1418
+ end
1419
+ =end
1420
+
1421
+ end if Mysql.client_version >= 40100
1422
+
1423
+ class TC_MysqlTime < Test::Unit::TestCase
1424
+ def setup()
1425
+ end
1426
+ def teardown()
1427
+ end
1428
+
1429
+ def test_init()
1430
+ t = Mysql::Time.new
1431
+ assert_equal(0, t.year);
1432
+ assert_equal(0, t.month);
1433
+ assert_equal(0, t.day);
1434
+ assert_equal(0, t.hour);
1435
+ assert_equal(0, t.minute);
1436
+ assert_equal(0, t.second);
1437
+ assert_equal(false, t.neg);
1438
+ assert_equal(0, t.second_part);
1439
+ end
1440
+
1441
+ def test_year()
1442
+ t = Mysql::Time.new
1443
+ assert_equal(2005, t.year = 2005)
1444
+ assert_equal(2005, t.year)
1445
+ end
1446
+
1447
+ def test_month()
1448
+ t = Mysql::Time.new
1449
+ assert_equal(11, t.month = 11)
1450
+ assert_equal(11, t.month)
1451
+ end
1452
+
1453
+ def test_day()
1454
+ t = Mysql::Time.new
1455
+ assert_equal(23, t.day = 23)
1456
+ assert_equal(23, t.day)
1457
+ end
1458
+
1459
+ def test_hour()
1460
+ t = Mysql::Time.new
1461
+ assert_equal(15, t.hour = 15)
1462
+ assert_equal(15, t.hour)
1463
+ end
1464
+
1465
+ def test_minute()
1466
+ t = Mysql::Time.new
1467
+ assert_equal(58, t.month = 58)
1468
+ assert_equal(58, t.month)
1469
+ end
1470
+
1471
+ def test_second()
1472
+ t = Mysql::Time.new
1473
+ assert_equal(34, t.second = 34)
1474
+ assert_equal(34, t.second)
1475
+ end
1476
+
1477
+ def test_tos()
1478
+ t = Mysql::Time.new(2005, 7, 19, 10, 15, 49)
1479
+ assert_equal("2005-07-19 10:15:49", t.to_s)
1480
+ end
1481
+
1482
+ def test_eql()
1483
+ t1 = Mysql::Time.new(2005,7,19,23,56,13)
1484
+ t2 = Mysql::Time.new(2005,7,19,23,56,13)
1485
+ assert_equal(t1, t2)
1486
+ end
1487
+
1488
+ end if Mysql.client_version >= 40100