ruby-mysql 2.9.11 → 2.10.0

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