ffi-mysql 0.0.1

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