mysql-pr 2.9.11

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