ruby-mysql 2.9.12 → 2.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 202d13ae653ae79671fbe568f53f258bebcb453c
4
+ data.tar.gz: dc69e58cf536f1e10195296f247e81efd9503887
5
+ SHA512:
6
+ metadata.gz: 390ae9a82ceca9c89c30c4837cc2fa06c00c8e2570bbd4bce5896f83dfb5c8cf5a082434f13b0729daf6c5513bf220be1c59342d1c548525e860c0321b1fb573
7
+ data.tar.gz: 8986d9a99784a45f52d122426be397f7d9c956dce6f54303ba75311fa68d924e253abd9720379ae912d0ff52053e60ac915f6c35f08bde156f26296a51bec7fa
@@ -21,7 +21,7 @@ class Mysql
21
21
  rescue LoadError
22
22
  end
23
23
 
24
- VERSION = 20912 # Version number of this library
24
+ VERSION = 20913 # Version number of this library
25
25
  MYSQL_UNIX_PORT = "/tmp/mysql.sock" # UNIX domain socket filename
26
26
  MYSQL_TCP_PORT = 3306 # TCP socket port number
27
27
 
@@ -0,0 +1,1837 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test/unit'
3
+ require 'test/unit/rr'
4
+ begin
5
+ require 'test/unit/notify'
6
+ rescue LoadError
7
+ # ignore
8
+ end
9
+
10
+ require 'mysql'
11
+
12
+ # MYSQL_USER must have ALL privilege for MYSQL_DATABASE.* and RELOAD privilege for *.*
13
+ MYSQL_SERVER = ENV['MYSQL_SERVER']
14
+ MYSQL_USER = ENV['MYSQL_USER']
15
+ MYSQL_PASSWORD = ENV['MYSQL_PASSWORD']
16
+ MYSQL_DATABASE = ENV['MYSQL_DATABASE'] || "test_for_mysql_ruby"
17
+ MYSQL_PORT = ENV['MYSQL_PORT']
18
+ MYSQL_SOCKET = ENV['MYSQL_SOCKET']
19
+
20
+ class TestMysql < Test::Unit::TestCase
21
+ sub_test_case 'Mysql::VERSION' do
22
+ test 'returns client version' do
23
+ assert{ Mysql::VERSION == 20913 }
24
+ end
25
+ end
26
+
27
+ sub_test_case 'Mysql.init' do
28
+ test 'returns Mysql object' do
29
+ assert{ Mysql.init.kind_of? Mysql }
30
+ end
31
+ end
32
+
33
+ sub_test_case 'Mysql.real_connect' do
34
+ test 'connect to mysqld' do
35
+ @m = Mysql.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
36
+ assert{ @m.kind_of? Mysql }
37
+ end
38
+
39
+ test 'flag argument affects' do
40
+ @m = Mysql.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET, Mysql::CLIENT_FOUND_ROWS)
41
+ @m.query 'create temporary table t (c int)'
42
+ @m.query 'insert into t values (123)'
43
+ @m.query 'update t set c=123'
44
+ assert{ @m.affected_rows == 1 }
45
+ end
46
+
47
+ teardown do
48
+ @m.close if @m
49
+ end
50
+ end
51
+
52
+ sub_test_case 'Mysql.connect' do
53
+ test 'connect to mysqld' do
54
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
55
+ assert{ @m.kind_of? Mysql }
56
+ end
57
+
58
+ teardown do
59
+ @m.close if @m
60
+ end
61
+ end
62
+
63
+ sub_test_case 'Mysql.new' do
64
+ test 'connect to mysqld' do
65
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
66
+ assert{ @m.kind_of? Mysql }
67
+ end
68
+ teardown do
69
+ @m.close if @m
70
+ end
71
+ end
72
+
73
+ sub_test_case 'Mysql.escape_string' do
74
+ test 'escape special character' do
75
+ assert{ Mysql.escape_string("abc'def\"ghi\0jkl%mno") == "abc\\'def\\\"ghi\\0jkl%mno" }
76
+ end
77
+ end
78
+
79
+ sub_test_case 'Mysql.quote' do
80
+ test 'escape special character' do
81
+ assert{ Mysql.quote("abc'def\"ghi\0jkl%mno") == "abc\\'def\\\"ghi\\0jkl%mno" }
82
+ end
83
+ end
84
+
85
+ sub_test_case 'Mysql.client_info' do
86
+ test 'returns client version as string' do
87
+ assert{ Mysql.client_info == '5.0.0' }
88
+ end
89
+ end
90
+
91
+ sub_test_case 'Mysql.get_client_info' do
92
+ test 'returns client version as string' do
93
+ assert{ Mysql.get_client_info == '5.0.0' }
94
+ end
95
+ end
96
+
97
+ sub_test_case 'Mysql.client_version' do
98
+ test 'returns client version as Integer' do
99
+ assert{ Mysql.client_version == 50000 }
100
+ end
101
+ end
102
+
103
+ sub_test_case 'Mysql.get_client_version' do
104
+ test 'returns client version as Integer' do
105
+ assert{ Mysql.client_version == 50000 }
106
+ end
107
+ end
108
+
109
+ sub_test_case 'Mysql#real_connect' do
110
+ test 'connect to mysqld' do
111
+ @m = Mysql.init
112
+ assert{ @m.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
113
+ end
114
+ teardown do
115
+ @m.close if @m
116
+ end
117
+ end
118
+
119
+ sub_test_case 'Mysql#connect' do
120
+ test 'connect to mysqld' do
121
+ @m = Mysql.init
122
+ assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
123
+ end
124
+ teardown do
125
+ @m.close if @m
126
+ end
127
+ end
128
+
129
+ sub_test_case 'Mysql#options' do
130
+ setup do
131
+ @m = Mysql.init
132
+ end
133
+ teardown do
134
+ @m.close
135
+ end
136
+ test 'INIT_COMMAND: execute query when connecting' do
137
+ assert{ @m.options(Mysql::INIT_COMMAND, "SET AUTOCOMMIT=0") == @m }
138
+ assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
139
+ assert{ @m.query('select @@AUTOCOMMIT').fetch_row == ["0"] }
140
+ end
141
+ test 'OPT_CONNECT_TIMEOUT: set timeout for connecting' do
142
+ assert{ @m.options(Mysql::OPT_CONNECT_TIMEOUT, 0.1) == @m }
143
+ stub(UNIXSocket).new{ sleep 1}
144
+ stub(TCPSocket).new{ sleep 1}
145
+ assert_raise Mysql::ClientError, 'connection timeout' do
146
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
147
+ end
148
+ assert_raise Mysql::ClientError, 'connection timeout' do
149
+ @m.connect
150
+ end
151
+ end
152
+ test 'OPT_LOCAL_INFILE: client can execute LOAD DATA LOCAL INFILE query' do
153
+ require 'tempfile'
154
+ tmpf = Tempfile.new 'mysql_spec'
155
+ tmpf.puts "123\tabc\n"
156
+ tmpf.close
157
+ assert{ @m.options(Mysql::OPT_LOCAL_INFILE, true) == @m }
158
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
159
+ @m.query('create temporary table t (i int, c char(10))')
160
+ @m.query("load data local infile '#{tmpf.path}' into table t")
161
+ assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
162
+ end
163
+ test 'OPT_READ_TIMEOUT: set timeout for reading packet' do
164
+ assert{ @m.options(Mysql::OPT_READ_TIMEOUT, 10) == @m }
165
+ end
166
+ test 'OPT_WRITE_TIMEOUT: set timeout for writing packet' do
167
+ assert{ @m.options(Mysql::OPT_WRITE_TIMEOUT, 10) == @m }
168
+ end
169
+ test 'SET_CHARSET_NAME: set charset for connection' do
170
+ assert{ @m.options(Mysql::SET_CHARSET_NAME, 'utf8') == @m }
171
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
172
+ assert{ @m.query('select @@character_set_connection').fetch_row == ['utf8'] }
173
+ end
174
+ end
175
+
176
+ sub_test_case 'Mysql' do
177
+ setup do
178
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
179
+ end
180
+
181
+ teardown do
182
+ @m.close if @m rescue nil
183
+ end
184
+
185
+ sub_test_case '#escape_string' do
186
+ if defined? ::Encoding
187
+ test 'escape special character for charset' do
188
+ @m.charset = 'cp932'
189
+ assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
190
+ end
191
+ else
192
+ test 'raise error if charset is multibyte' do
193
+ @m.charset = 'cp932'
194
+ assert_raise Mysql::ClientError, 'Mysql#escape_string is called for unsafe multibyte charset' do
195
+ @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\")
196
+ end
197
+ end
198
+ test 'not warn if charset is singlebyte' do
199
+ @m.charset = 'latin1'
200
+ assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\") == "abc\\'def\\\"ghi\\0jkl%mno_\x95\\\\" }
201
+ end
202
+ end
203
+ end
204
+
205
+ sub_test_case '#quote' do
206
+ test 'is alias of #escape_string' do
207
+ assert{ @m.method(:quote) == @m.method(:escape_string) }
208
+ end
209
+ end
210
+
211
+ sub_test_case '#client_info' do
212
+ test 'returns client version as string' do
213
+ assert{ @m.client_info == '5.0.0' }
214
+ end
215
+ end
216
+
217
+ sub_test_case '#get_client_info' do
218
+ test 'returns client version as string' do
219
+ assert{ @m.get_client_info == '5.0.0' }
220
+ end
221
+ end
222
+
223
+ sub_test_case '#affected_rows' do
224
+ test 'returns number of affected rows' do
225
+ @m.query 'create temporary table t (id int)'
226
+ @m.query 'insert into t values (1),(2)'
227
+ assert{ @m.affected_rows == 2 }
228
+ end
229
+ end
230
+
231
+ sub_test_case '#character_set_name' do
232
+ test 'returns charset name' do
233
+ m = Mysql.init
234
+ m.options Mysql::SET_CHARSET_NAME, 'cp932'
235
+ m.connect MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET
236
+ assert{ m.character_set_name == 'cp932' }
237
+ end
238
+ end
239
+
240
+ sub_test_case '#close' do
241
+ test 'returns self' do
242
+ assert{ @m.close == @m }
243
+ end
244
+ end
245
+
246
+ sub_test_case '#close!' do
247
+ test 'returns self' do
248
+ assert{ @m.close! == @m }
249
+ end
250
+ end
251
+
252
+ # sub_test_case '#create_db' do
253
+ # end
254
+
255
+ # sub_test_case '#drop_db' do
256
+ # end
257
+
258
+ sub_test_case '#errno' do
259
+ test 'default value is 0' do
260
+ assert{ @m.errno == 0 }
261
+ end
262
+ test 'returns error number of latest error' do
263
+ @m.query('hogehoge') rescue nil
264
+ assert{ @m.errno == 1064 }
265
+ end
266
+ end
267
+
268
+ sub_test_case '#error' do
269
+ test 'returns error message of latest error' do
270
+ @m.query('hogehoge') rescue nil
271
+ assert{ @m.error == "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hogehoge' at line 1" }
272
+ end
273
+ end
274
+
275
+ sub_test_case '#field_count' do
276
+ test 'returns number of fields for latest query' do
277
+ @m.query 'select 1,2,3'
278
+ assert{ @m.field_count == 3 }
279
+ end
280
+ end
281
+
282
+ sub_test_case '#client_version' do
283
+ test 'returns client version as Integer' do
284
+ assert{ @m.client_version.kind_of? Integer }
285
+ end
286
+ end
287
+
288
+ sub_test_case '#get_client_version' do
289
+ test 'returns client version as Integer' do
290
+ assert{ @m.get_client_version.kind_of? Integer }
291
+ end
292
+ end
293
+
294
+ sub_test_case '#get_host_info' do
295
+ test 'returns connection type as String' do
296
+ if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
297
+ assert{ @m.get_host_info == 'Localhost via UNIX socket' }
298
+ else
299
+ assert{ @m.get_host_info == "#{MYSQL_SERVER} via TCP/IP" }
300
+ end
301
+ end
302
+ end
303
+
304
+ sub_test_case '#host_info' do
305
+ test 'returns connection type as String' do
306
+ if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
307
+ assert{ @m.host_info == 'Localhost via UNIX socket' }
308
+ else
309
+ assert{ @m.host_info == "#{MYSQL_SERVER} via TCP/IP" }
310
+ end
311
+ end
312
+ end
313
+
314
+ sub_test_case '#get_proto_info' do
315
+ test 'returns version of connection as Integer' do
316
+ assert{ @m.get_proto_info == 10 }
317
+ end
318
+ end
319
+
320
+ sub_test_case '#proto_info' do
321
+ test 'returns version of connection as Integer' do
322
+ assert{ @m.proto_info == 10 }
323
+ end
324
+ end
325
+
326
+ sub_test_case '#get_server_info' do
327
+ test 'returns server version as String' do
328
+ assert{ @m.get_server_info =~ /\A\d+\.\d+\.\d+/ }
329
+ end
330
+ end
331
+
332
+ sub_test_case '#server_info' do
333
+ test 'returns server version as String' do
334
+ assert{ @m.server_info =~ /\A\d+\.\d+\.\d+/ }
335
+ end
336
+ end
337
+
338
+ sub_test_case '#info' do
339
+ test 'returns information of latest query' do
340
+ @m.query 'create temporary table t (id int)'
341
+ @m.query 'insert into t values (1),(2),(3)'
342
+ assert{ @m.info == 'Records: 3 Duplicates: 0 Warnings: 0' }
343
+ end
344
+ end
345
+
346
+ sub_test_case '#insert_id' do
347
+ test 'returns latest auto_increment value' do
348
+ @m.query 'create temporary table t (id int auto_increment, unique (id))'
349
+ @m.query 'insert into t values (0)'
350
+ assert{ @m.insert_id == 1 }
351
+ @m.query 'alter table t auto_increment=1234'
352
+ @m.query 'insert into t values (0)'
353
+ assert{ @m.insert_id == 1234 }
354
+ end
355
+ end
356
+
357
+ sub_test_case '#kill' do
358
+ setup do
359
+ @m2 = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
360
+ end
361
+ teardown do
362
+ @m2.close rescue nil
363
+ end
364
+ test 'returns self' do
365
+ assert{ @m.kill(@m2.thread_id) == @m }
366
+ end
367
+ end
368
+
369
+ sub_test_case '#list_dbs' do
370
+ test 'returns database list' do
371
+ ret = @m.list_dbs
372
+ assert{ ret.kind_of? Array }
373
+ assert{ ret.include? MYSQL_DATABASE }
374
+ end
375
+ test 'with pattern returns databases that matches pattern' do
376
+ assert{ @m.list_dbs('info%').include? 'information_schema' }
377
+ end
378
+ end
379
+
380
+ sub_test_case '#list_fields' do
381
+ setup do
382
+ @m.query 'create temporary table t (i int, c char(10), d date)'
383
+ end
384
+ test 'returns result set that contains information of fields' do
385
+ ret = @m.list_fields('t')
386
+ assert{ ret.kind_of? Mysql::Result }
387
+ assert{ ret.num_rows == 0 }
388
+ assert{ ret.fetch_fields.map{|f|f.name} == ['i','c','d'] }
389
+ end
390
+ test 'with pattern returns result set that contains information of fields that matches pattern' do
391
+ ret = @m.list_fields('t', 'i')
392
+ assert{ ret.kind_of? Mysql::Result }
393
+ assert{ ret.num_rows == 0 }
394
+ ret.fetch_fields.map{|f|f.name} == ['i']
395
+ end
396
+ end
397
+
398
+ sub_test_case '#list_processes' do
399
+ test 'returns result set that contains information of all connections' do
400
+ ret = @m.list_processes
401
+ assert{ ret.kind_of? Mysql::Result }
402
+ assert{ ret.find{|r|r[0].to_i == @m.thread_id}[4] == "Processlist" }
403
+ end
404
+ end
405
+
406
+ sub_test_case '#list_tables' do
407
+ setup do
408
+ @m.query 'create table test_mysql_list_tables (id int)'
409
+ end
410
+ teardown do
411
+ @m.query 'drop table if exists test_mysql_list_tables'
412
+ end
413
+ test 'returns table list' do
414
+ ret = @m.list_tables
415
+ assert{ ret.kind_of? Array }
416
+ assert{ ret.include? 'test_mysql_list_tables' }
417
+ end
418
+ test 'with pattern returns lists that matches pattern' do
419
+ ret = @m.list_tables '%mysql\_list\_t%'
420
+ assert{ ret.include? 'test_mysql_list_tables' }
421
+ end
422
+ end
423
+
424
+ sub_test_case '#ping' do
425
+ test 'returns self' do
426
+ assert{ @m.ping == @m }
427
+ end
428
+ end
429
+
430
+ sub_test_case '#query' do
431
+ test 'returns Mysql::Result if query returns results' do
432
+ assert{ @m.query('select 123').kind_of? Mysql::Result }
433
+ end
434
+ test 'returns nil if query returns no results' do
435
+ assert{ @m.query('set @hoge:=123') == nil }
436
+ end
437
+ test 'returns self if query_with_result is false' do
438
+ @m.query_with_result = false
439
+ assert{ @m.query('select 123') == @m }
440
+ @m.store_result
441
+ assert{ @m.query('set @hoge:=123') == @m }
442
+ end
443
+ end
444
+
445
+ sub_test_case '#real_query' do
446
+ test 'is same as #query' do
447
+ assert{ @m.real_query('select 123').kind_of? Mysql::Result }
448
+ end
449
+ end
450
+
451
+ sub_test_case '#refresh' do
452
+ test 'returns self' do
453
+ assert{ @m.refresh(Mysql::REFRESH_HOSTS) == @m }
454
+ end
455
+ end
456
+
457
+ sub_test_case '#reload' do
458
+ test 'returns self' do
459
+ assert{ @m.reload == @m }
460
+ end
461
+ end
462
+
463
+ sub_test_case '#select_db' do
464
+ test 'changes default database' do
465
+ @m.select_db 'information_schema'
466
+ assert{ @m.query('select database()').fetch_row.first == 'information_schema' }
467
+ end
468
+ end
469
+
470
+ # sub_test_case '#shutdown' do
471
+ # end
472
+
473
+ sub_test_case '#stat' do
474
+ test 'returns server status' do
475
+ assert{ @m.stat =~ /\AUptime: \d+ Threads: \d+ Questions: \d+ Slow queries: \d+ Opens: \d+ Flush tables: \d+ Open tables: \d+ Queries per second avg: \d+\.\d+\z/ }
476
+ end
477
+ end
478
+
479
+ sub_test_case '#store_result' do
480
+ test 'returns Mysql::Result' do
481
+ @m.query_with_result = false
482
+ @m.query 'select 1,2,3'
483
+ ret = @m.store_result
484
+ assert{ ret.kind_of? Mysql::Result }
485
+ assert{ ret.fetch_row == ['1','2','3'] }
486
+ end
487
+ test 'raises error when no query' do
488
+ assert_raise Mysql::ClientError, 'invalid usage' do
489
+ @m.store_result
490
+ end
491
+ end
492
+ test 'raises error when query does not return results' do
493
+ @m.query 'set @hoge:=123'
494
+ assert_raise Mysql::ClientError, 'invalid usage' do
495
+ @m.store_result
496
+ end
497
+ end
498
+ end
499
+
500
+ sub_test_case '#thread_id' do
501
+ test 'returns thread id as Integer' do
502
+ assert{ @m.thread_id.kind_of? Integer }
503
+ end
504
+ end
505
+
506
+ sub_test_case '#use_result' do
507
+ test 'returns Mysql::Result' do
508
+ @m.query_with_result = false
509
+ @m.query 'select 1,2,3'
510
+ ret = @m.use_result
511
+ assert{ ret.kind_of? Mysql::Result }
512
+ assert{ ret.fetch_row == ['1','2','3'] }
513
+ end
514
+ test 'raises error when no query' do
515
+ assert_raise Mysql::ClientError, 'invalid usage' do
516
+ @m.use_result
517
+ end
518
+ end
519
+ test 'raises error when query does not return results' do
520
+ @m.query 'set @hoge:=123'
521
+ assert_raise Mysql::ClientError, 'invalid usage' do
522
+ @m.use_result
523
+ end
524
+ end
525
+ end
526
+
527
+ sub_test_case '#get_server_version' do
528
+ test 'returns server version as Integer' do
529
+ assert{ @m.get_server_version.kind_of? Integer }
530
+ end
531
+ end
532
+
533
+ sub_test_case '#server_version' do
534
+ test 'returns server version as Integer' do
535
+ assert{ @m.server_version.kind_of? Integer }
536
+ end
537
+ end
538
+
539
+ sub_test_case '#warning_count' do
540
+ test 'default values is zero' do
541
+ assert{ @m.warning_count == 0 }
542
+ end
543
+ test 'returns number of warnings' do
544
+ @m.query 'create temporary table t (i tinyint)'
545
+ @m.query 'insert into t values (1234567)'
546
+ assert{ @m.warning_count == 1 }
547
+ end
548
+ end
549
+
550
+ sub_test_case '#commit' do
551
+ test 'returns self' do
552
+ assert{ @m.commit == @m }
553
+ end
554
+ end
555
+
556
+ sub_test_case '#rollback' do
557
+ test 'returns self' do
558
+ assert{ @m.rollback == @m }
559
+ end
560
+ end
561
+
562
+ sub_test_case '#autocommit' do
563
+ test 'returns self' do
564
+ assert{ @m.autocommit(true) == @m }
565
+ end
566
+
567
+ test 'change auto-commit mode' do
568
+ @m.autocommit(true)
569
+ assert{ @m.query('select @@autocommit').fetch_row == ['1'] }
570
+ @m.autocommit(false)
571
+ assert{ @m.query('select @@autocommit').fetch_row == ['0'] }
572
+ end
573
+ end
574
+
575
+ sub_test_case '#set_server_option' do
576
+ test 'returns self' do
577
+ assert{ @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON) == @m }
578
+ end
579
+ end
580
+
581
+ sub_test_case '#sqlstate' do
582
+ test 'default values is "00000"' do
583
+ assert{ @m.sqlstate == "00000" }
584
+ end
585
+ test 'returns sqlstate code' do
586
+ assert_raise do
587
+ @m.query("hoge")
588
+ end
589
+ assert{ @m.sqlstate == "42000" }
590
+ end
591
+ end
592
+
593
+ sub_test_case '#query_with_result' do
594
+ test 'default value is true' do
595
+ assert{ @m.query_with_result == true }
596
+ end
597
+ test 'can set value' do
598
+ assert{ (@m.query_with_result=true) == true }
599
+ assert{ @m.query_with_result == true }
600
+ assert{ (@m.query_with_result=false) == false }
601
+ assert{ @m.query_with_result == false }
602
+ end
603
+ end
604
+
605
+ sub_test_case '#query_with_result is false' do
606
+ test 'Mysql#query returns self and Mysql#store_result returns result set' do
607
+ @m.query_with_result = false
608
+ assert{ @m.query('select 1,2,3') == @m }
609
+ res = @m.store_result
610
+ assert{ res.fetch_row == ['1','2','3'] }
611
+ end
612
+ end
613
+
614
+ sub_test_case '#query with block' do
615
+ test 'returns self' do
616
+ assert{ @m.query('select 1'){} == @m }
617
+ end
618
+ test 'evaluate block with Mysql::Result' do
619
+ assert{ @m.query('select 1'){|res| res.kind_of? Mysql::Result} == @m }
620
+ end
621
+ test 'evaluate block multiple times if multiple query is specified' do
622
+ @m.set_server_option Mysql::OPTION_MULTI_STATEMENTS_ON
623
+ cnt = 0
624
+ expect = [["1"], ["2"]]
625
+ assert{ @m.query('select 1; select 2'){|res|
626
+ assert{ res.fetch_row == expect.shift }
627
+ cnt += 1
628
+ } == @m }
629
+ assert{ cnt == 2 }
630
+ end
631
+ test 'evaluate block only when query has result' do
632
+ @m.set_server_option Mysql::OPTION_MULTI_STATEMENTS_ON
633
+ cnt = 0
634
+ expect = [["1"], ["2"]]
635
+ assert{ @m.query('select 1; set @hoge:=1; select 2'){|res|
636
+ assert{ res.fetch_row == expect.shift }
637
+ cnt += 1
638
+ } == @m }
639
+ assert{ cnt == 2 }
640
+ end
641
+ end
642
+ end
643
+
644
+ sub_test_case 'multiple statement query:' do
645
+ setup do
646
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
647
+ @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
648
+ @res = @m.query 'select 1,2; select 3,4,5'
649
+ end
650
+ test 'Mysql#query returns results for first query' do
651
+ assert{ @res.entries == [['1','2']] }
652
+ end
653
+ test 'Mysql#more_results is true' do
654
+ assert{ @m.more_results == true }
655
+ end
656
+ test 'Mysql#more_results? is true' do
657
+ assert{ @m.more_results? == true }
658
+ end
659
+ test 'Mysql#next_result is true' do
660
+ assert{ @m.next_result == true }
661
+ end
662
+ sub_test_case 'for next query:' do
663
+ setup do
664
+ @m.next_result
665
+ @res = @m.store_result
666
+ end
667
+ test 'Mysql#store_result returns results' do
668
+ assert{ @res.entries == [['3','4','5']] }
669
+ end
670
+ test 'Mysql#more_results is false' do
671
+ assert{ @m.more_results == false }
672
+ end
673
+ test 'Mysql#more_results? is false' do
674
+ assert{ @m.more_results? == false }
675
+ end
676
+ test 'Mysql#next_result is false' do
677
+ assert{ @m.next_result == false }
678
+ end
679
+ end
680
+ end
681
+
682
+ sub_test_case 'Mysql::Result' do
683
+ setup do
684
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
685
+ @m.charset = 'latin1'
686
+ @m.query 'create temporary table t (id int, str char(10), primary key (id))'
687
+ @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
688
+ @res = @m.query 'select * from t'
689
+ end
690
+
691
+ teardown do
692
+ @m.close if @m
693
+ end
694
+
695
+ test '#data_seek set position of current record' do
696
+ assert{ @res.fetch_row == ['1', 'abc'] }
697
+ assert{ @res.fetch_row == ['2', 'defg'] }
698
+ assert{ @res.fetch_row == ['3', 'hi'] }
699
+ @res.data_seek 1
700
+ assert{ @res.fetch_row == ['2', 'defg'] }
701
+ end
702
+
703
+ test '#fetch_field return current field' do
704
+ f = @res.fetch_field
705
+ assert{ f.name == 'id' }
706
+ assert{ f.table == 't' }
707
+ assert{ f.def == nil }
708
+ assert{ f.type == Mysql::Field::TYPE_LONG }
709
+ assert{ f.length == 11 }
710
+ f.max_length == 1
711
+ assert{ f.flags == Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG }
712
+ assert{ f.decimals == 0 }
713
+
714
+ f = @res.fetch_field
715
+ assert{ f.name == 'str' }
716
+ assert{ f.table == 't' }
717
+ assert{ f.def == nil }
718
+ assert{ f.type == Mysql::Field::TYPE_STRING }
719
+ assert{ f.length == 10 }
720
+ f.max_length == 4
721
+ assert{ f.flags == 0 }
722
+ assert{ f.decimals == 0 }
723
+
724
+ assert{ @res.fetch_field == nil }
725
+ end
726
+
727
+ test '#fetch_fields returns array of fields' do
728
+ ret = @res.fetch_fields
729
+ assert{ ret.size == 2 }
730
+ assert{ ret[0].name == 'id' }
731
+ assert{ ret[1].name == 'str' }
732
+ end
733
+
734
+ test '#fetch_field_direct returns field' do
735
+ f = @res.fetch_field_direct 0
736
+ assert{ f.name == 'id' }
737
+ f = @res.fetch_field_direct 1
738
+ assert{ f.name == 'str' }
739
+ assert_raise Mysql::ClientError, 'invalid argument: -1' do
740
+ @res.fetch_field_direct(-1)
741
+ end
742
+ assert_raise Mysql::ClientError, 'invalid argument: 2' do
743
+ @res.fetch_field_direct 2
744
+ end
745
+ end
746
+
747
+ test '#fetch_lengths returns array of length of field data' do
748
+ assert{ @res.fetch_lengths == nil }
749
+ @res.fetch_row
750
+ assert{ @res.fetch_lengths == [1, 3] }
751
+ @res.fetch_row
752
+ assert{ @res.fetch_lengths == [1, 4] }
753
+ @res.fetch_row
754
+ assert{ @res.fetch_lengths == [1, 2] }
755
+ @res.fetch_row
756
+ assert{ @res.fetch_lengths == [1, 0] }
757
+ @res.fetch_row
758
+ assert{ @res.fetch_lengths == nil }
759
+ end
760
+
761
+ test '#fetch_row returns one record as array for current record' do
762
+ assert{ @res.fetch_row == ['1', 'abc'] }
763
+ assert{ @res.fetch_row == ['2', 'defg'] }
764
+ assert{ @res.fetch_row == ['3', 'hi'] }
765
+ assert{ @res.fetch_row == ['4', nil] }
766
+ assert{ @res.fetch_row == nil }
767
+ end
768
+
769
+ test '#fetch_hash returns one record as hash for current record' do
770
+ assert{ @res.fetch_hash == {'id'=>'1', 'str'=>'abc'} }
771
+ assert{ @res.fetch_hash == {'id'=>'2', 'str'=>'defg'} }
772
+ assert{ @res.fetch_hash == {'id'=>'3', 'str'=>'hi'} }
773
+ assert{ @res.fetch_hash == {'id'=>'4', 'str'=>nil} }
774
+ assert{ @res.fetch_hash == nil }
775
+ end
776
+
777
+ test '#fetch_hash(true) returns with table name' do
778
+ assert{ @res.fetch_hash(true) == {'t.id'=>'1', 't.str'=>'abc'} }
779
+ assert{ @res.fetch_hash(true) == {'t.id'=>'2', 't.str'=>'defg'} }
780
+ assert{ @res.fetch_hash(true) == {'t.id'=>'3', 't.str'=>'hi'} }
781
+ assert{ @res.fetch_hash(true) == {'t.id'=>'4', 't.str'=>nil} }
782
+ assert{ @res.fetch_hash(true) == nil }
783
+ end
784
+
785
+ test '#num_fields returns number of fields' do
786
+ assert{ @res.num_fields == 2 }
787
+ end
788
+
789
+ test '#num_rows returns number of records' do
790
+ assert{ @res.num_rows == 4 }
791
+ end
792
+
793
+ test '#each iterate block with a record' do
794
+ expect = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
795
+ @res.each do |a|
796
+ assert{ a == expect.shift }
797
+ end
798
+ end
799
+
800
+ test '#each_hash iterate block with a hash' do
801
+ expect = [{"id"=>"1","str"=>"abc"}, {"id"=>"2","str"=>"defg"}, {"id"=>"3","str"=>"hi"}, {"id"=>"4","str"=>nil}]
802
+ @res.each_hash do |a|
803
+ assert{ a == expect.shift }
804
+ end
805
+ end
806
+
807
+ test '#each_hash(true): hash key has table name' do
808
+ expect = [{"t.id"=>"1","t.str"=>"abc"}, {"t.id"=>"2","t.str"=>"defg"}, {"t.id"=>"3","t.str"=>"hi"}, {"t.id"=>"4","t.str"=>nil}]
809
+ @res.each_hash(true) do |a|
810
+ assert{ a == expect.shift }
811
+ end
812
+ end
813
+
814
+ test '#row_tell returns position of current record, #row_seek set position of current record' do
815
+ assert{ @res.fetch_row == ['1', 'abc'] }
816
+ pos = @res.row_tell
817
+ assert{ @res.fetch_row == ['2', 'defg'] }
818
+ assert{ @res.fetch_row == ['3', 'hi'] }
819
+ @res.row_seek pos
820
+ assert{ @res.fetch_row == ['2', 'defg'] }
821
+ end
822
+
823
+ test '#field_tell returns position of current field, #field_seek set position of current field' do
824
+ assert{ @res.field_tell == 0 }
825
+ @res.fetch_field
826
+ assert{ @res.field_tell == 1 }
827
+ @res.fetch_field
828
+ assert{ @res.field_tell == 2 }
829
+ @res.field_seek 1
830
+ assert{ @res.field_tell == 1 }
831
+ end
832
+
833
+ test '#free returns nil' do
834
+ assert{ @res.free == nil }
835
+ end
836
+ end
837
+
838
+ sub_test_case 'Mysql::Field' do
839
+ setup do
840
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
841
+ @m.charset = 'latin1'
842
+ @m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
843
+ @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
844
+ @res = @m.query 'select * from t'
845
+ end
846
+
847
+ teardown do
848
+ @m.close if @m
849
+ end
850
+
851
+ test '#name is name of field' do
852
+ assert{ @res.fetch_field.name == 'id' }
853
+ end
854
+
855
+ test '#table is name of table for field' do
856
+ assert{ @res.fetch_field.table == 't' }
857
+ end
858
+
859
+ test '#def for result set is null' do
860
+ assert{ @res.fetch_field.def == nil }
861
+ end
862
+
863
+ test '#def for field information is default value' do
864
+ assert{ @m.list_fields('t').fetch_field.def == '0' }
865
+ end
866
+
867
+ test '#type is type of field as Integer' do
868
+ assert{ @res.fetch_field.type == Mysql::Field::TYPE_LONG }
869
+ assert{ @res.fetch_field.type == Mysql::Field::TYPE_STRING }
870
+ end
871
+
872
+ test '#length is length of field' do
873
+ assert{ @res.fetch_field.length == 11 }
874
+ assert{ @res.fetch_field.length == 10 }
875
+ end
876
+
877
+ test '#max_length is maximum length of field value' do
878
+ assert{ @res.fetch_field.max_length == 1 }
879
+ assert{ @res.fetch_field.max_length == 4 }
880
+ end
881
+
882
+ test '#flags is flag of field as Integer' do
883
+ assert{ @res.fetch_field.flags == Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG }
884
+ assert{ @res.fetch_field.flags == 0 }
885
+ end
886
+
887
+ test '#decimals is number of decimal digits' do
888
+ assert{ @m.query('select 1.23').fetch_field.decimals == 2 }
889
+ end
890
+
891
+ test '#hash return field as hash' do
892
+ assert{ @res.fetch_field.hash == {
893
+ 'name' => 'id',
894
+ 'table' => 't',
895
+ 'def' => nil,
896
+ 'type' => Mysql::Field::TYPE_LONG,
897
+ 'length' => 11,
898
+ 'max_length' => 1,
899
+ 'flags' => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
900
+ 'decimals' => 0,
901
+ }
902
+ }
903
+ assert{ @res.fetch_field.hash == {
904
+ 'name' => 'str',
905
+ 'table' => 't',
906
+ 'def' => nil,
907
+ 'type' => Mysql::Field::TYPE_STRING,
908
+ 'length' => 10,
909
+ 'max_length' => 4,
910
+ 'flags' => 0,
911
+ 'decimals' => 0,
912
+ }
913
+ }
914
+ end
915
+
916
+ test '#inspect returns "#<Mysql::Field:name>"' do
917
+ assert{ @res.fetch_field.inspect == '#<Mysql::Field:id>' }
918
+ assert{ @res.fetch_field.inspect == '#<Mysql::Field:str>' }
919
+ end
920
+
921
+ test '#is_num? returns true if the field is numeric' do
922
+ assert{ @res.fetch_field.is_num? == true }
923
+ assert{ @res.fetch_field.is_num? == false }
924
+ end
925
+
926
+ test '#is_not_null? returns true if the field is not null' do
927
+ assert{ @res.fetch_field.is_not_null? == true }
928
+ assert{ @res.fetch_field.is_not_null? == false }
929
+ end
930
+
931
+ test '#is_pri_key? returns true if the field is primary key' do
932
+ assert{ @res.fetch_field.is_pri_key? == true }
933
+ assert{ @res.fetch_field.is_pri_key? == false }
934
+ end
935
+ end
936
+
937
+ sub_test_case 'create Mysql::Stmt object:' do
938
+ setup do
939
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
940
+ end
941
+
942
+ teardown do
943
+ @m.close if @m
944
+ end
945
+
946
+ test 'Mysql#stmt_init returns Mysql::Stmt object' do
947
+ assert{ @m.stmt_init.kind_of? Mysql::Stmt }
948
+ end
949
+
950
+ test 'Mysq;#prepare returns Mysql::Stmt object' do
951
+ assert{ @m.prepare("select 1").kind_of? Mysql::Stmt }
952
+ end
953
+ end
954
+
955
+ sub_test_case 'Mysql::Stmt' do
956
+ setup do
957
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
958
+ @s = @m.stmt_init
959
+ end
960
+
961
+ teardown do
962
+ @s.close if @s rescue nil
963
+ @m.close if @m
964
+ end
965
+
966
+ test '#affected_rows returns number of affected records' do
967
+ @m.query 'create temporary table t (i int, c char(10))'
968
+ @s.prepare 'insert into t values (?,?)'
969
+ @s.execute 1, 'hoge'
970
+ assert{ @s.affected_rows == 1 }
971
+ @s.execute 2, 'hoge'
972
+ @s.execute 3, 'hoge'
973
+ @s.prepare 'update t set c=?'
974
+ @s.execute 'fuga'
975
+ assert{ @s.affected_rows == 3 }
976
+ end
977
+
978
+ sub_test_case '#bind_result' do
979
+ setup do
980
+ @m.query 'create temporary table t (i int, c char(10), d double, t datetime)'
981
+ @m.query 'insert into t values (123,"9abcdefg",1.2345,20091208100446)'
982
+ @s.prepare 'select * from t'
983
+ end
984
+
985
+ test '(nil) make result format to be standard value' do
986
+ @s.bind_result nil, nil, nil, nil
987
+ @s.execute
988
+ assert{ @s.fetch == [123, '9abcdefg', 1.2345, Mysql::Time.new(2009,12,8,10,4,46)] }
989
+ end
990
+
991
+ test '(Numeric) make result format to be Integer value' do
992
+ @s.bind_result Numeric, Numeric, Numeric, Numeric
993
+ @s.execute
994
+ assert{ @s.fetch == [123, 9, 1, 20091208100446] }
995
+ end
996
+
997
+ test '(Integer) make result format to be Integer value' do
998
+ @s.bind_result Integer, Integer, Integer, Integer
999
+ @s.execute
1000
+ assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1001
+ end
1002
+
1003
+ test '(Fixnum) make result format to be Integer value' do
1004
+ @s.bind_result Fixnum, Fixnum, Fixnum, Fixnum
1005
+ @s.execute
1006
+ assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1007
+ end
1008
+
1009
+ test '(String) make result format to be String value' do
1010
+ @s.bind_result String, String, String, String
1011
+ @s.execute
1012
+ assert{ @s.fetch == ["123", "9abcdefg", "1.2345", "2009-12-08 10:04:46"] }
1013
+ end
1014
+
1015
+ test '(Float) make result format to be Float value' do
1016
+ @s.bind_result Float, Float, Float, Float
1017
+ @s.execute
1018
+ assert{ @s.fetch == [123.0, 9.0, 1.2345 , 20091208100446.0] }
1019
+ end
1020
+
1021
+ test '(Mysql::Time) make result format to be Mysql::Time value' do
1022
+ @s.bind_result Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time
1023
+ @s.execute
1024
+ assert{ @s.fetch == [Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2009,12,8,10,4,46)] }
1025
+ end
1026
+
1027
+ test '(invalid) raises error' do
1028
+ assert_raise TypeError do
1029
+ @s.bind_result(Time, nil, nil, nil)
1030
+ end
1031
+ end
1032
+
1033
+ test 'with mismatch argument count raise error' do
1034
+ assert_raise Mysql::ClientError, 'bind_result: result value count(4) != number of argument(1)' do
1035
+ @s.bind_result(nil)
1036
+ end
1037
+ end
1038
+ end
1039
+
1040
+ test '#close returns nil' do
1041
+ assert{ @s.close == nil }
1042
+ end
1043
+
1044
+ test '#data_seek set position of current record' do
1045
+ @m.query 'create temporary table t (i int)'
1046
+ @m.query 'insert into t values (0),(1),(2),(3),(4),(5),(6)'
1047
+ @s.prepare 'select i from t'
1048
+ @s.execute
1049
+ assert{ @s.fetch == [0] }
1050
+ assert{ @s.fetch == [1] }
1051
+ assert{ @s.fetch == [2] }
1052
+ @s.data_seek 5
1053
+ assert{ @s.fetch == [5] }
1054
+ @s.data_seek 1
1055
+ assert{ @s.fetch == [1] }
1056
+ end
1057
+
1058
+ test '#each iterate block with a record' do
1059
+ @m.query 'create temporary table t (i int, c char(255), d datetime)'
1060
+ @m.query "insert into t values (1,'abc','19701224235905'),(2,'def','21120903123456'),(3,'123',null)"
1061
+ @s.prepare 'select * from t'
1062
+ @s.execute
1063
+ expect = [
1064
+ [1, 'abc', Mysql::Time.new(1970,12,24,23,59,05)],
1065
+ [2, 'def', Mysql::Time.new(2112,9,3,12,34,56)],
1066
+ [3, '123', nil],
1067
+ ]
1068
+ @s.each do |a|
1069
+ assert{ a == expect.shift }
1070
+ end
1071
+ end
1072
+
1073
+ test '#execute returns self' do
1074
+ @s.prepare 'select 1'
1075
+ assert{ @s.execute == @s }
1076
+ end
1077
+
1078
+ test '#execute pass arguments to query' do
1079
+ @m.query 'create temporary table t (i int)'
1080
+ @s.prepare 'insert into t values (?)'
1081
+ @s.execute 123
1082
+ @s.execute '456'
1083
+ assert{ @m.query('select * from t').entries == [['123'], ['456']] }
1084
+ end
1085
+
1086
+ test '#execute with various arguments' do
1087
+ @m.query 'create temporary table t (i int, c char(255), t timestamp)'
1088
+ @s.prepare 'insert into t values (?,?,?)'
1089
+ @s.execute 123, 'hoge', Time.local(2009,12,8,19,56,21)
1090
+ assert{ @m.query('select * from t').fetch_row == ['123', 'hoge', '2009-12-08 19:56:21'] }
1091
+ end
1092
+
1093
+ test '#execute with arguments that is invalid count raise error' do
1094
+ @s.prepare 'select ?'
1095
+ assert_raise Mysql::ClientError, 'parameter count mismatch' do
1096
+ @s.execute 123, 456
1097
+ end
1098
+ end
1099
+
1100
+ test '#execute with huge value' do
1101
+ [30, 31, 32, 62, 63].each do |i|
1102
+ assert{ @m.prepare('select cast(? as signed)').execute(2**i-1).fetch == [2**i-1] }
1103
+ assert{ @m.prepare('select cast(? as signed)').execute(-(2**i)).fetch == [-2**i] }
1104
+ end
1105
+ end
1106
+
1107
+ sub_test_case '#execute with various integer value:' do
1108
+ setup do
1109
+ @m.query('create temporary table t (i bigint)')
1110
+ end
1111
+ [
1112
+ -9223372036854775808,
1113
+ -9223372036854775807,
1114
+ -4294967297,
1115
+ -4294967296,
1116
+ -4294967295,
1117
+ -2147483649,
1118
+ -2147483648,
1119
+ -2147483647,
1120
+ -65537,
1121
+ -65536,
1122
+ -65535,
1123
+ -32769,
1124
+ -32768,
1125
+ -32767,
1126
+ -257,
1127
+ -256,
1128
+ -255,
1129
+ -129,
1130
+ -128,
1131
+ -127,
1132
+ 0,
1133
+ 126,
1134
+ 127,
1135
+ 128,
1136
+ 254,
1137
+ 255,
1138
+ 256,
1139
+ 32766,
1140
+ 32767,
1141
+ 32768,
1142
+ 65534,
1143
+ 65535,
1144
+ 65536,
1145
+ 2147483646,
1146
+ 2147483647,
1147
+ 2147483648,
1148
+ 4294967294,
1149
+ 4294967295,
1150
+ 4294967296,
1151
+ 9223372036854775806,
1152
+ 9223372036854775807,
1153
+ ].each do |n|
1154
+ test "#{n} is #{n}" do
1155
+ @s.prepare 'insert into t values (?)'
1156
+ @s.execute n
1157
+ assert{ @m.query('select i from t').fetch == ["#{n}"] }
1158
+ end
1159
+ end
1160
+ end
1161
+
1162
+ sub_test_case '#execute with various unsigned integer value:' do
1163
+ setup do
1164
+ @m.query('create temporary table t (i bigint unsigned)')
1165
+ end
1166
+ [
1167
+ 0,
1168
+ 126,
1169
+ 127,
1170
+ 128,
1171
+ 254,
1172
+ 255,
1173
+ 256,
1174
+ 32766,
1175
+ 32767,
1176
+ 32768,
1177
+ 65534,
1178
+ 65535,
1179
+ 65536,
1180
+ 2147483646,
1181
+ 2147483647,
1182
+ 2147483648,
1183
+ 4294967294,
1184
+ 4294967295,
1185
+ 4294967296,
1186
+ 9223372036854775806,
1187
+ 9223372036854775807,
1188
+ 9223372036854775808,
1189
+ 18446744073709551614,
1190
+ 18446744073709551615,
1191
+ ].each do |n|
1192
+ test "#{n} is #{n}" do
1193
+ @s.prepare 'insert into t values (?)'
1194
+ @s.execute n
1195
+ assert{ @m.query('select i from t').fetch == ["#{n}"] }
1196
+ end
1197
+ end
1198
+ end
1199
+
1200
+ test '#fetch returns result-record' do
1201
+ @s.prepare 'select 123, "abc", null'
1202
+ @s.execute
1203
+ assert{ @s.fetch == [123, 'abc', nil] }
1204
+ end
1205
+
1206
+ test '#fetch bit column (8bit)' do
1207
+ @m.query 'create temporary table t (i bit(8))'
1208
+ @m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
1209
+ @s.prepare 'select i from t'
1210
+ @s.execute
1211
+ if defined? Encoding
1212
+ assert{ @s.entries == [
1213
+ ["\x00".force_encoding('ASCII-8BIT')],
1214
+ ["\xff".force_encoding('ASCII-8BIT')],
1215
+ ["\x7f".force_encoding('ASCII-8BIT')],
1216
+ ["\xff".force_encoding('ASCII-8BIT')],
1217
+ ["\xff".force_encoding('ASCII-8BIT')],
1218
+ ["\xff".force_encoding('ASCII-8BIT')],
1219
+ ["\xff".force_encoding('ASCII-8BIT')],
1220
+ ]
1221
+ }
1222
+ else
1223
+ assert{ @s.entries == [["\x00"], ["\xff"], ["\x7f"], ["\xff"], ["\xff"], ["\xff"], ["\xff"]] }
1224
+ end
1225
+ end
1226
+
1227
+ test '#fetch bit column (64bit)' do
1228
+ @m.query 'create temporary table t (i bit(64))'
1229
+ @m.query 'insert into t values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)'
1230
+ @s.prepare 'select i from t'
1231
+ @s.execute
1232
+ if defined? Encoding
1233
+ assert{ @s.entries == [
1234
+ ["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1235
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1236
+ ["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1237
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1238
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1239
+ ]
1240
+ }
1241
+ else
1242
+ assert{ @s.entries == [
1243
+ ["\x00\x00\x00\x00\x00\x00\x00\x00"],
1244
+ ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1245
+ ["\x00\x00\x00\x01\x00\x00\x00\x00"],
1246
+ ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1247
+ ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1248
+ ]
1249
+ }
1250
+ end
1251
+ end
1252
+
1253
+ test '#fetch tinyint column' do
1254
+ @m.query 'create temporary table t (i tinyint)'
1255
+ @m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255)'
1256
+ @s.prepare 'select i from t'
1257
+ @s.execute
1258
+ assert{ @s.entries == [[0], [-1], [127], [-128], [127], [-128]] }
1259
+ end
1260
+
1261
+ test '#fetch tinyint unsigned column' do
1262
+ @m.query 'create temporary table t (i tinyint unsigned)'
1263
+ @m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
1264
+ @s.prepare 'select i from t'
1265
+ @s.execute
1266
+ assert{ @s.entries == [[0], [0], [127], [0], [255], [0], [255]] }
1267
+ end
1268
+
1269
+ test '#fetch smallint column' do
1270
+ @m.query 'create temporary table t (i smallint)'
1271
+ @m.query 'insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)'
1272
+ @s.prepare 'select i from t'
1273
+ @s.execute
1274
+ assert{ @s.entries == [[0], [-1], [32767], [-32768], [32767], [-32768], [32767]] }
1275
+ end
1276
+
1277
+ test '#fetch smallint unsigned column' do
1278
+ @m.query 'create temporary table t (i smallint unsigned)'
1279
+ @m.query 'insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)'
1280
+ @s.prepare 'select i from t'
1281
+ @s.execute
1282
+ assert{ @s.entries == [[0], [0], [32767], [0], [65535], [0], [65535]] }
1283
+ end
1284
+
1285
+ test '#fetch mediumint column' do
1286
+ @m.query 'create temporary table t (i mediumint)'
1287
+ @m.query 'insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)'
1288
+ @s.prepare 'select i from t'
1289
+ @s.execute
1290
+ assert{ @s.entries == [[0], [-1], [8388607], [-8388608], [8388607], [-8388608], [8388607]] }
1291
+ end
1292
+
1293
+ test '#fetch mediumint unsigned column' do
1294
+ @m.query 'create temporary table t (i mediumint unsigned)'
1295
+ @m.query 'insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)'
1296
+ @s.prepare 'select i from t'
1297
+ @s.execute
1298
+ assert{ @s.entries == [[0], [0], [8388607], [0], [16777215], [0], [16777215]] }
1299
+ end
1300
+
1301
+ test '#fetch int column' do
1302
+ @m.query 'create temporary table t (i int)'
1303
+ @m.query 'insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)'
1304
+ @s.prepare 'select i from t'
1305
+ @s.execute
1306
+ assert{ @s.entries == [[0], [-1], [2147483647], [-2147483648], [2147483647], [-2147483648], [2147483647]] }
1307
+ end
1308
+
1309
+ test '#fetch int unsigned column' do
1310
+ @m.query 'create temporary table t (i int unsigned)'
1311
+ @m.query 'insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)'
1312
+ @s.prepare 'select i from t'
1313
+ @s.execute
1314
+ assert{ @s.entries == [[0], [0], [2147483647], [0], [4294967295], [0], [4294967295]] }
1315
+ end
1316
+
1317
+ test '#fetch bigint column' do
1318
+ @m.query 'create temporary table t (i bigint)'
1319
+ @m.query 'insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)'
1320
+ @s.prepare 'select i from t'
1321
+ @s.execute
1322
+ assert{ @s.entries == [[0], [-1], [9223372036854775807], [-9223372036854775808], [9223372036854775807], [-9223372036854775808], [9223372036854775807]] }
1323
+ end
1324
+
1325
+ test '#fetch bigint unsigned column' do
1326
+ @m.query 'create temporary table t (i bigint unsigned)'
1327
+ @m.query 'insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)'
1328
+ @s.prepare 'select i from t'
1329
+ @s.execute
1330
+ assert{ @s.entries == [[0], [0], [9223372036854775807], [0], [18446744073709551615], [0], [18446744073709551615]] }
1331
+ end
1332
+
1333
+ test '#fetch float column' do
1334
+ @m.query 'create temporary table t (i float)'
1335
+ @m.query 'insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)'
1336
+ @s.prepare 'select i from t'
1337
+ @s.execute
1338
+ assert{ @s.fetch[0] == 0.0 }
1339
+ assert{ (@s.fetch[0] - -3.402823466E+38).abs < 0.000000001E+38 }
1340
+ assert{ (@s.fetch[0] - -1.175494351E-38).abs < 0.000000001E-38 }
1341
+ assert{ (@s.fetch[0] - 1.175494351E-38).abs < 0.000000001E-38 }
1342
+ assert{ (@s.fetch[0] - 3.402823466E+38).abs < 0.000000001E+38 }
1343
+ end
1344
+
1345
+ test '#fetch float unsigned column' do
1346
+ @m.query 'create temporary table t (i float unsigned)'
1347
+ @m.query 'insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)'
1348
+ @s.prepare 'select i from t'
1349
+ @s.execute
1350
+ assert{ @s.fetch[0] == 0.0 }
1351
+ assert{ @s.fetch[0] == 0.0 }
1352
+ assert{ @s.fetch[0] == 0.0 }
1353
+ assert{ (@s.fetch[0] - 1.175494351E-38).abs < 0.000000001E-38 }
1354
+ assert{ (@s.fetch[0] - 3.402823466E+38).abs < 0.000000001E+38 }
1355
+ end
1356
+
1357
+ test '#fetch double column' do
1358
+ @m.query 'create temporary table t (i double)'
1359
+ @m.query 'insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)'
1360
+ @s.prepare 'select i from t'
1361
+ @s.execute
1362
+ assert{ @s.fetch[0] == 0.0 }
1363
+ assert{ (@s.fetch[0] - -Float::MAX).abs < Float::EPSILON }
1364
+ assert{ (@s.fetch[0] - -Float::MIN).abs < Float::EPSILON }
1365
+ assert{ (@s.fetch[0] - Float::MIN).abs < Float::EPSILON }
1366
+ assert{ (@s.fetch[0] - Float::MAX).abs < Float::EPSILON }
1367
+ end
1368
+
1369
+ test '#fetch double unsigned column' do
1370
+ @m.query 'create temporary table t (i double unsigned)'
1371
+ @m.query 'insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)'
1372
+ @s.prepare 'select i from t'
1373
+ @s.execute
1374
+ assert{ @s.fetch[0] == 0.0 }
1375
+ assert{ @s.fetch[0] == 0.0 }
1376
+ assert{ @s.fetch[0] == 0.0 }
1377
+ assert{ (@s.fetch[0] - Float::MIN).abs < Float::EPSILON }
1378
+ assert{ (@s.fetch[0] - Float::MAX).abs < Float::EPSILON }
1379
+ end
1380
+
1381
+ test '#fetch decimal column' do
1382
+ @m.query 'create temporary table t (i decimal)'
1383
+ @m.query 'insert into t values (0),(9999999999),(-9999999999),(10000000000),(-10000000000)'
1384
+ @s.prepare 'select i from t'
1385
+ @s.execute
1386
+ assert{ @s.entries == [["0"], ["9999999999"], ["-9999999999"], ["9999999999"], ["-9999999999"]] }
1387
+ end
1388
+
1389
+ test '#fetch decimal unsigned column' do
1390
+ @m.query 'create temporary table t (i decimal unsigned)'
1391
+ @m.query 'insert into t values (0),(9999999998),(9999999999),(-9999999998),(-9999999999),(10000000000),(-10000000000)'
1392
+ @s.prepare 'select i from t'
1393
+ @s.execute
1394
+ assert{ @s.entries == [["0"], ["9999999998"], ["9999999999"], ["0"], ["0"], ["9999999999"], ["0"]] }
1395
+ end
1396
+
1397
+ test '#fetch date column' do
1398
+ @m.query 'create temporary table t (i date)'
1399
+ @m.query "insert into t values ('0000-00-00'),('1000-01-01'),('9999-12-31')"
1400
+ @s.prepare 'select i from t'
1401
+ @s.execute
1402
+ cols = @s.fetch
1403
+ assert{ cols == [Mysql::Time.new] }
1404
+ assert{ cols.first.to_s == '0000-00-00' }
1405
+ cols = @s.fetch
1406
+ assert{ cols == [Mysql::Time.new(1000,1,1)] }
1407
+ assert{ cols.first.to_s == '1000-01-01' }
1408
+ cols = @s.fetch
1409
+ assert{ cols == [Mysql::Time.new(9999,12,31)] }
1410
+ assert{ cols.first.to_s == '9999-12-31' }
1411
+ end
1412
+
1413
+ test '#fetch datetime column' do
1414
+ @m.query 'create temporary table t (i datetime)'
1415
+ @m.query "insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')"
1416
+ @s.prepare 'select i from t'
1417
+ @s.execute
1418
+ assert{ @s.fetch == [Mysql::Time.new] }
1419
+ assert{ @s.fetch == [Mysql::Time.new(1000,1,1)] }
1420
+ assert{ @s.fetch == [Mysql::Time.new(9999,12,31,23,59,59)] }
1421
+ end
1422
+
1423
+ test '#fetch timestamp column' do
1424
+ @m.query 'create temporary table t (i timestamp)'
1425
+ @m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
1426
+ @s.prepare 'select i from t'
1427
+ @s.execute
1428
+ assert{ @s.fetch == [Mysql::Time.new(1970,1,2)] }
1429
+ assert{ @s.fetch == [Mysql::Time.new(2037,12,30,23,59,59)] }
1430
+ end
1431
+
1432
+ test '#fetch time column' do
1433
+ @m.query 'create temporary table t (i time)'
1434
+ @m.query "insert into t values ('-838:59:59'),(0),('838:59:59')"
1435
+ @s.prepare 'select i from t'
1436
+ @s.execute
1437
+ assert{ @s.fetch == [Mysql::Time.new(0,0,0,838,59,59,true)] }
1438
+ assert{ @s.fetch == [Mysql::Time.new(0,0,0,0,0,0,false)] }
1439
+ assert{ @s.fetch == [Mysql::Time.new(0,0,0,838,59,59,false)] }
1440
+ end
1441
+
1442
+ test '#fetch year column' do
1443
+ @m.query 'create temporary table t (i year)'
1444
+ @m.query 'insert into t values (0),(70),(69),(1901),(2155)'
1445
+ @s.prepare 'select i from t'
1446
+ @s.execute
1447
+ assert{ @s.entries == [[0], [1970], [2069], [1901], [2155]] }
1448
+ end
1449
+
1450
+ test '#fetch char column' do
1451
+ @m.query 'create temporary table t (i char(10))'
1452
+ @m.query "insert into t values (null),('abc')"
1453
+ @s.prepare 'select i from t'
1454
+ @s.execute
1455
+ assert{ @s.entries == [[nil], ['abc']] }
1456
+ end
1457
+
1458
+ test '#fetch varchar column' do
1459
+ @m.query 'create temporary table t (i varchar(10))'
1460
+ @m.query "insert into t values (null),('abc')"
1461
+ @s.prepare 'select i from t'
1462
+ @s.execute
1463
+ assert{ @s.entries == [[nil], ['abc']] }
1464
+ end
1465
+
1466
+ test '#fetch binary column' do
1467
+ @m.query 'create temporary table t (i binary(10))'
1468
+ @m.query "insert into t values (null),('abc')"
1469
+ @s.prepare 'select i from t'
1470
+ @s.execute
1471
+ assert{ @s.entries == [[nil], ["abc\0\0\0\0\0\0\0"]] }
1472
+ end
1473
+
1474
+ test '#fetch varbinary column' do
1475
+ @m.query 'create temporary table t (i varbinary(10))'
1476
+ @m.query "insert into t values (null),('abc')"
1477
+ @s.prepare 'select i from t'
1478
+ @s.execute
1479
+ assert{ @s.entries == [[nil], ["abc"]] }
1480
+ end
1481
+
1482
+ test '#fetch tinyblob column' do
1483
+ @m.query 'create temporary table t (i tinyblob)'
1484
+ @m.query "insert into t values (null),('#{"a"*255}')"
1485
+ @s.prepare 'select i from t'
1486
+ @s.execute
1487
+ assert{ @s.entries == [[nil], ["a"*255]] }
1488
+ end
1489
+
1490
+ test '#fetch tinytext column' do
1491
+ @m.query 'create temporary table t (i tinytext)'
1492
+ @m.query "insert into t values (null),('#{"a"*255}')"
1493
+ @s.prepare 'select i from t'
1494
+ @s.execute
1495
+ assert{ @s.entries == [[nil], ["a"*255]] }
1496
+ end
1497
+
1498
+ test '#fetch blob column' do
1499
+ @m.query 'create temporary table t (i blob)'
1500
+ @m.query "insert into t values (null),('#{"a"*65535}')"
1501
+ @s.prepare 'select i from t'
1502
+ @s.execute
1503
+ assert{ @s.entries == [[nil], ["a"*65535]] }
1504
+ end
1505
+
1506
+ test '#fetch text column' do
1507
+ @m.query 'create temporary table t (i text)'
1508
+ @m.query "insert into t values (null),('#{"a"*65535}')"
1509
+ @s.prepare 'select i from t'
1510
+ @s.execute
1511
+ assert{ @s.entries == [[nil], ["a"*65535]] }
1512
+ end
1513
+
1514
+ test '#fetch mediumblob column' do
1515
+ @m.query 'create temporary table t (i mediumblob)'
1516
+ @m.query "insert into t values (null),('#{"a"*16777215}')"
1517
+ @s.prepare 'select i from t'
1518
+ @s.execute
1519
+ assert{ @s.entries == [[nil], ['a'*16777215]] }
1520
+ end
1521
+
1522
+ test '#fetch mediumtext column' do
1523
+ @m.query 'create temporary table t (i mediumtext)'
1524
+ @m.query "insert into t values (null),('#{"a"*16777215}')"
1525
+ @s.prepare 'select i from t'
1526
+ @s.execute
1527
+ assert{ @s.entries == [[nil], ['a'*16777215]] }
1528
+ end
1529
+
1530
+ test '#fetch longblob column' do
1531
+ @m.query 'create temporary table t (i longblob)'
1532
+ @m.query "insert into t values (null),('#{"a"*16777216}')"
1533
+ @s.prepare 'select i from t'
1534
+ @s.execute
1535
+ assert{ @s.entries == [[nil], ["a"*16777216]] }
1536
+ end
1537
+
1538
+ test '#fetch longtext column' do
1539
+ @m.query 'create temporary table t (i longtext)'
1540
+ @m.query "insert into t values (null),('#{"a"*16777216}')"
1541
+ @s.prepare 'select i from t'
1542
+ @s.execute
1543
+ assert{ @s.entries == [[nil], ["a"*16777216]] }
1544
+ end
1545
+
1546
+ test '#fetch enum column' do
1547
+ @m.query "create temporary table t (i enum('abc','def'))"
1548
+ @m.query "insert into t values (null),(0),(1),(2),('abc'),('def'),('ghi')"
1549
+ @s.prepare 'select i from t'
1550
+ @s.execute
1551
+ assert{ @s.entries == [[nil], [''], ['abc'], ['def'], ['abc'], ['def'], ['']] }
1552
+ end
1553
+
1554
+ test '#fetch set column' do
1555
+ @m.query "create temporary table t (i set('abc','def'))"
1556
+ @m.query "insert into t values (null),(0),(1),(2),(3),('abc'),('def'),('abc,def'),('ghi')"
1557
+ @s.prepare 'select i from t'
1558
+ @s.execute
1559
+ assert{ @s.entries == [[nil], [''], ['abc'], ['def'], ['abc,def'], ['abc'], ['def'], ['abc,def'], ['']] }
1560
+ end
1561
+
1562
+ test '#field_count' do
1563
+ @s.prepare 'select 1,2,3'
1564
+ assert{ @s.field_count == 3 }
1565
+ @s.prepare 'set @a=1'
1566
+ assert{ @s.field_count == 0 }
1567
+ end
1568
+
1569
+ test '#free_result' do
1570
+ @s.free_result
1571
+ @s.prepare 'select 1,2,3'
1572
+ @s.execute
1573
+ @s.free_result
1574
+ end
1575
+
1576
+ test '#insert_id' do
1577
+ @m.query 'create temporary table t (i int auto_increment, unique(i))'
1578
+ @s.prepare 'insert into t values (0)'
1579
+ @s.execute
1580
+ assert{ @s.insert_id == 1 }
1581
+ @s.execute
1582
+ assert{ @s.insert_id == 2 }
1583
+ end
1584
+
1585
+ test '#num_rows' do
1586
+ @m.query 'create temporary table t (i int)'
1587
+ @m.query 'insert into t values (1),(2),(3),(4)'
1588
+ @s.prepare 'select * from t'
1589
+ @s.execute
1590
+ assert{ @s.num_rows == 4 }
1591
+ end
1592
+
1593
+ test '#param_count' do
1594
+ @m.query 'create temporary table t (a int, b int, c int)'
1595
+ @s.prepare 'select * from t'
1596
+ assert{ @s.param_count == 0 }
1597
+ @s.prepare 'insert into t values (?,?,?)'
1598
+ assert{ @s.param_count == 3 }
1599
+ end
1600
+
1601
+ test '#prepare' do
1602
+ assert{ @s.prepare('select 1').kind_of? Mysql::Stmt }
1603
+ assert_raise Mysql::ParseError do
1604
+ @s.prepare 'invalid syntax'
1605
+ end
1606
+ end
1607
+
1608
+ test '#prepare returns self' do
1609
+ assert{ @s.prepare('select 1') == @s }
1610
+ end
1611
+
1612
+ test '#prepare with invalid query raises error' do
1613
+ assert_raise Mysql::ParseError do
1614
+ @s.prepare 'invalid query'
1615
+ end
1616
+ end
1617
+
1618
+ test '#result_metadata' do
1619
+ @s.prepare 'select 1 foo, 2 bar'
1620
+ f = @s.result_metadata.fetch_fields
1621
+ assert{ f[0].name == 'foo' }
1622
+ assert{ f[1].name == 'bar' }
1623
+ end
1624
+
1625
+ test '#result_metadata forn no data' do
1626
+ @s.prepare 'set @a=1'
1627
+ assert{ @s.result_metadata == nil }
1628
+ end
1629
+
1630
+ test '#row_seek and #row_tell' do
1631
+ @m.query 'create temporary table t (i int)'
1632
+ @m.query 'insert into t values (0),(1),(2),(3),(4)'
1633
+ @s.prepare 'select * from t'
1634
+ @s.execute
1635
+ row0 = @s.row_tell
1636
+ assert{ @s.fetch == [0] }
1637
+ assert{ @s.fetch == [1] }
1638
+ row2 = @s.row_seek row0
1639
+ assert{ @s.fetch == [0] }
1640
+ @s.row_seek row2
1641
+ assert{ @s.fetch == [2] }
1642
+ end
1643
+
1644
+ test '#sqlstate' do
1645
+ @s.prepare 'select 1'
1646
+ assert{ @s.sqlstate == '00000' }
1647
+ assert_raise Mysql::ParseError do
1648
+ @s.prepare 'hogehoge'
1649
+ end
1650
+ assert{ @s.sqlstate == '42000' }
1651
+ end
1652
+ end
1653
+
1654
+ sub_test_case 'Mysql::Time' do
1655
+ setup do
1656
+ @t = Mysql::Time.new
1657
+ end
1658
+
1659
+ test '.new with no arguments returns 0' do
1660
+ assert{ @t.year == 0 }
1661
+ assert{ @t.month == 0 }
1662
+ assert{ @t.day == 0 }
1663
+ assert{ @t.hour == 0 }
1664
+ assert{ @t.minute == 0 }
1665
+ assert{ @t.second == 0 }
1666
+ assert{ @t.neg == false }
1667
+ assert{ @t.second_part == 0 }
1668
+ end
1669
+
1670
+ test '#inspect' do
1671
+ assert{ Mysql::Time.new(2009,12,8,23,35,21).inspect == '#<Mysql::Time:2009-12-08 23:35:21>' }
1672
+ end
1673
+
1674
+ test '#to_s' do
1675
+ assert{ Mysql::Time.new(2009,12,8,23,35,21).to_s == '2009-12-08 23:35:21' }
1676
+ end
1677
+
1678
+ test '#to_i' do
1679
+ assert{ Mysql::Time.new(2009,12,8,23,35,21).to_i == 20091208233521 }
1680
+ end
1681
+
1682
+ test '#year' do
1683
+ assert{ (@t.year = 2009) == 2009 }
1684
+ assert{ @t.year == 2009 }
1685
+ end
1686
+
1687
+ test '#month' do
1688
+ assert{ (@t.month = 12) == 12 }
1689
+ assert{ @t.month == 12 }
1690
+ end
1691
+
1692
+ test '#day' do
1693
+ assert{ (@t.day = 8) == 8 }
1694
+ assert{ @t.day == 8 }
1695
+ end
1696
+
1697
+ test '#hour' do
1698
+ assert{ (@t.hour = 23) == 23 }
1699
+ assert{ @t.hour == 23 }
1700
+ end
1701
+
1702
+ test '#minute' do
1703
+ assert{ (@t.minute = 35) == 35 }
1704
+ assert{ @t.minute == 35 }
1705
+ end
1706
+
1707
+ test '#second' do
1708
+ assert{ (@t.second = 21) == 21 }
1709
+ assert{ @t.second == 21 }
1710
+ end
1711
+
1712
+ test '#neg' do
1713
+ assert{ @t.neg == false }
1714
+ end
1715
+
1716
+ test '#second_part' do
1717
+ assert{ @t.second_part == 0 }
1718
+ end
1719
+
1720
+ test '#==' do
1721
+ t1 = Mysql::Time.new 2009,12,8,23,35,21
1722
+ t2 = Mysql::Time.new 2009,12,8,23,35,21
1723
+ assert{ t1 == t2 }
1724
+ end
1725
+ end
1726
+
1727
+ sub_test_case 'Mysql::Error' do
1728
+ setup do
1729
+ m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1730
+ begin
1731
+ m.query('hogehoge')
1732
+ rescue => @e
1733
+ end
1734
+ end
1735
+
1736
+ test '#error is error message' do
1737
+ assert{ @e.error == "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hogehoge' at line 1" }
1738
+ end
1739
+
1740
+ test '#errno is error number' do
1741
+ assert{ @e.errno == 1064 }
1742
+ end
1743
+
1744
+ test '#sqlstate is sqlstate value as String' do
1745
+ assert{ @e.sqlstate == '42000' }
1746
+ end
1747
+ end
1748
+
1749
+ if defined? Encoding
1750
+ sub_test_case 'Connection charset is UTF-8:' do
1751
+ setup do
1752
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1753
+ @m.charset = "utf8"
1754
+ @m.query "create temporary table t (utf8 char(10) charset utf8, cp932 char(10) charset cp932, eucjp char(10) charset eucjpms, bin varbinary(10))"
1755
+ @utf8 = "いろは"
1756
+ @cp932 = @utf8.encode "CP932"
1757
+ @eucjp = @utf8.encode "EUC-JP-MS"
1758
+ @bin = "\x00\x01\x7F\x80\xFE\xFF".force_encoding("ASCII-8BIT")
1759
+ @default_internal = Encoding.default_internal
1760
+ end
1761
+
1762
+ teardown do
1763
+ Encoding.default_internal = @default_internal
1764
+ end
1765
+
1766
+ sub_test_case 'default_internal is CP932' do
1767
+ setup do
1768
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1769
+ Encoding.default_internal = 'CP932'
1770
+ end
1771
+ test 'is converted to CP932' do
1772
+ assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
1773
+ end
1774
+ test 'data is stored as is' do
1775
+ assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
1776
+ end
1777
+ test 'By simple query, charset of retrieved data is connection charset' do
1778
+ assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
1779
+ end
1780
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1781
+ assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
1782
+ end
1783
+ end
1784
+
1785
+ sub_test_case 'query with CP932 encoding' do
1786
+ test 'is converted to UTF-8' do
1787
+ assert @m.query('select HEX("あいう")'.encode("CP932")).fetch == ["E38182E38184E38186"]
1788
+ end
1789
+ end
1790
+
1791
+ sub_test_case 'prepared statement with CP932 encoding' do
1792
+ test 'is converted to UTF-8' do
1793
+ assert @m.prepare('select HEX("あいう")'.encode("CP932")).execute.fetch == ["E38182E38184E38186"]
1794
+ end
1795
+ end
1796
+
1797
+ sub_test_case 'The encoding of data are correspond to charset of column:' do
1798
+ setup do
1799
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1800
+ end
1801
+ test 'data is stored as is' do
1802
+ assert{ @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF'] }
1803
+ end
1804
+ test 'By simple query, charset of retrieved data is connection charset' do
1805
+ assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
1806
+ end
1807
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1808
+ assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
1809
+ end
1810
+ end
1811
+
1812
+ sub_test_case 'The encoding of data are different from charset of column:' do
1813
+ setup do
1814
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @utf8, @utf8, @utf8
1815
+ end
1816
+ test 'stored data is converted' do
1817
+ assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
1818
+ end
1819
+ test 'By simple query, charset of retrieved data is connection charset' do
1820
+ assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
1821
+ end
1822
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1823
+ assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
1824
+ end
1825
+ end
1826
+
1827
+ sub_test_case 'The data include invalid byte code:' do
1828
+ test 'raises Encoding::InvalidByteSequenceError' do
1829
+ cp932 = "\x01\xFF\x80".force_encoding("CP932")
1830
+ assert_raise Encoding::InvalidByteSequenceError do
1831
+ @m.prepare("insert into t (cp932) values (?)").execute cp932
1832
+ end
1833
+ end
1834
+ end
1835
+ end
1836
+ end
1837
+ end