ffi-mysql 0.0.2 → 1.0.0

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