mysql 2.7.1-mswin32

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