jberkel-mysql-ruby 2.8.1

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