ruby-mysql 2.11.0 → 3.0.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +28 -0
- data/lib/mysql/protocol.rb +86 -149
- data/lib/mysql.rb +182 -359
- data/test/test_mysql.rb +255 -551
- metadata +10 -7
- data/README.rdoc +0 -69
data/test/test_mysql.rb
CHANGED
@@ -20,28 +20,71 @@ MYSQL_SOCKET = ENV['MYSQL_SOCKET']
|
|
20
20
|
class TestMysql < Test::Unit::TestCase
|
21
21
|
sub_test_case 'Mysql::VERSION' do
|
22
22
|
test 'returns client version' do
|
23
|
-
assert{ Mysql::VERSION ==
|
23
|
+
assert{ Mysql::VERSION == '3.0.1' }
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
sub_test_case 'Mysql.
|
27
|
+
sub_test_case 'Mysql.new' do
|
28
28
|
test 'returns Mysql object' do
|
29
|
-
assert{ Mysql.
|
29
|
+
assert{ Mysql.new.kind_of? Mysql }
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
sub_test_case '
|
34
|
-
test '
|
35
|
-
@m = Mysql.
|
36
|
-
assert{ @m.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@m
|
41
|
-
@m.
|
42
|
-
@m.
|
43
|
-
|
44
|
-
|
33
|
+
sub_test_case 'arguments' do
|
34
|
+
test 'with fixed arguments' do
|
35
|
+
@m = Mysql.new('127.0.0.1', 'hoge', 'abc&def', 'test', 3306, '/tmp/socket', 12345)
|
36
|
+
assert{ @m.host == '127.0.0.1' }
|
37
|
+
assert{ @m.username == 'hoge' }
|
38
|
+
assert{ @m.password == 'abc&def' }
|
39
|
+
assert{ @m.database == 'test' }
|
40
|
+
assert{ @m.port == 3306 }
|
41
|
+
assert{ @m.socket == '/tmp/socket' }
|
42
|
+
assert{ @m.flags == 12345 }
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'with keyword arguments' do
|
46
|
+
@m = Mysql.new(host: '127.0.0.1', username: 'hoge', password: 'abc&def', database: 'test', port: 3306, socket: '/tmp/socket', flags: 12345)
|
47
|
+
assert{ @m.host == '127.0.0.1' }
|
48
|
+
assert{ @m.username == 'hoge' }
|
49
|
+
assert{ @m.password == 'abc&def' }
|
50
|
+
assert{ @m.database == 'test' }
|
51
|
+
assert{ @m.port == 3306 }
|
52
|
+
assert{ @m.socket == '/tmp/socket' }
|
53
|
+
assert{ @m.flags == 12345 }
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'with URI' do
|
57
|
+
uri = URI.parse("mysql://hoge:abc%26def@127.0.0.1:3306/test?socket=/tmp/socket&flags=12345")
|
58
|
+
@m = Mysql.new(uri)
|
59
|
+
assert{ @m.host == '127.0.0.1' }
|
60
|
+
assert{ @m.username == 'hoge' }
|
61
|
+
assert{ @m.password == 'abc&def' }
|
62
|
+
assert{ @m.database == 'test' }
|
63
|
+
assert{ @m.port == 3306 }
|
64
|
+
assert{ @m.socket == '/tmp/socket' }
|
65
|
+
assert{ @m.flags == 12345 }
|
66
|
+
end
|
67
|
+
|
68
|
+
test 'with URI string' do
|
69
|
+
@m = Mysql.new("mysql://hoge:abc%26def@127.0.0.1:3306/test?socket=/tmp/socket&flags=12345")
|
70
|
+
assert{ @m.host == '127.0.0.1' }
|
71
|
+
assert{ @m.username == 'hoge' }
|
72
|
+
assert{ @m.password == 'abc&def' }
|
73
|
+
assert{ @m.database == 'test' }
|
74
|
+
assert{ @m.port == 3306 }
|
75
|
+
assert{ @m.socket == '/tmp/socket' }
|
76
|
+
assert{ @m.flags == 12345 }
|
77
|
+
end
|
78
|
+
|
79
|
+
test 'with URI string: host is filename' do
|
80
|
+
@m = Mysql.new("mysql://hoge:abc%26def@%2Ftmp%2Fsocket:3306/test?flags=12345")
|
81
|
+
assert{ @m.host == '' }
|
82
|
+
assert{ @m.username == 'hoge' }
|
83
|
+
assert{ @m.password == 'abc&def' }
|
84
|
+
assert{ @m.database == 'test' }
|
85
|
+
assert{ @m.port == 3306 }
|
86
|
+
assert{ @m.socket == '/tmp/socket' }
|
87
|
+
assert{ @m.flags == 12345 }
|
45
88
|
end
|
46
89
|
|
47
90
|
teardown do
|
@@ -55,16 +98,14 @@ class TestMysql < Test::Unit::TestCase
|
|
55
98
|
assert{ @m.kind_of? Mysql }
|
56
99
|
end
|
57
100
|
|
58
|
-
|
59
|
-
@m.
|
101
|
+
test 'flag argument affects' do
|
102
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET, Mysql::CLIENT_FOUND_ROWS)
|
103
|
+
@m.query 'create temporary table t (c int)'
|
104
|
+
@m.query 'insert into t values (123)'
|
105
|
+
@m.query 'update t set c=123'
|
106
|
+
assert{ @m.affected_rows == 1 }
|
60
107
|
end
|
61
|
-
end
|
62
108
|
|
63
|
-
sub_test_case 'Mysql.new' do
|
64
|
-
test 'connect to mysqld' do
|
65
|
-
@m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
66
|
-
assert{ @m.kind_of? Mysql }
|
67
|
-
end
|
68
109
|
teardown do
|
69
110
|
@m.close if @m
|
70
111
|
end
|
@@ -82,64 +123,41 @@ class TestMysql < Test::Unit::TestCase
|
|
82
123
|
end
|
83
124
|
end
|
84
125
|
|
85
|
-
sub_test_case 'Mysql
|
86
|
-
test '
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
sub_test_case 'Mysql.get_client_info' do
|
92
|
-
test 'returns client version as string' do
|
93
|
-
assert{ Mysql.get_client_info == '5.0.0' }
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
sub_test_case 'Mysql.client_version' do
|
98
|
-
test 'returns client version as Integer' do
|
99
|
-
assert{ Mysql.client_version == 50000 }
|
126
|
+
sub_test_case 'Mysql#connect' do
|
127
|
+
test 'connect to mysqld' do
|
128
|
+
@m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
129
|
+
assert{ @m.connect == @m }
|
100
130
|
end
|
101
|
-
end
|
102
131
|
|
103
|
-
|
104
|
-
|
105
|
-
assert{
|
132
|
+
test 'connect to mysqld by URI' do
|
133
|
+
@m = Mysql.new("mysql://#{MYSQL_USER}:#{MYSQL_PASSWORD}@#{MYSQL_SERVER}:#{MYSQL_PORT}/#{MYSQL_DATABASE}?socket=#{MYSQL_SOCKET}")
|
134
|
+
assert{ @m.connect == @m }
|
106
135
|
end
|
107
|
-
end
|
108
136
|
|
109
|
-
|
110
|
-
|
111
|
-
@m
|
112
|
-
assert{ @m.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
|
113
|
-
end
|
114
|
-
teardown do
|
115
|
-
@m.close if @m
|
137
|
+
test 'overrides arguments of new method' do
|
138
|
+
@m = Mysql.new('example.com', 12345)
|
139
|
+
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
116
140
|
end
|
117
|
-
end
|
118
141
|
|
119
|
-
sub_test_case 'Mysql#connect' do
|
120
|
-
test 'connect to mysqld' do
|
121
|
-
@m = Mysql.init
|
122
|
-
assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
|
123
|
-
end
|
124
142
|
teardown do
|
125
143
|
@m.close if @m
|
126
144
|
end
|
127
145
|
end
|
128
146
|
|
129
|
-
sub_test_case '
|
147
|
+
sub_test_case 'options' do
|
130
148
|
setup do
|
131
|
-
@m = Mysql.
|
149
|
+
@m = Mysql.new
|
132
150
|
end
|
133
151
|
teardown do
|
134
152
|
@m.close
|
135
153
|
end
|
136
|
-
test '
|
137
|
-
|
154
|
+
test 'init_command: execute query when connecting' do
|
155
|
+
@m.init_command = "SET AUTOCOMMIT=0"
|
138
156
|
assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
|
139
157
|
assert{ @m.query('select @@AUTOCOMMIT').fetch_row == ["0"] }
|
140
158
|
end
|
141
|
-
test '
|
142
|
-
|
159
|
+
test 'connect_timeout: set timeout for connecting' do
|
160
|
+
@m.connect_timeout = 0.1
|
143
161
|
stub(Socket).tcp{ raise Errno::ETIMEDOUT }
|
144
162
|
stub(Socket).unix{ raise Errno::ETIMEDOUT }
|
145
163
|
assert_raise Mysql::ClientError, 'connection timeout' do
|
@@ -149,46 +167,59 @@ class TestMysql < Test::Unit::TestCase
|
|
149
167
|
@m.connect
|
150
168
|
end
|
151
169
|
end
|
152
|
-
test '
|
170
|
+
test 'local_infile: client can execute LOAD DATA LOCAL INFILE query' do
|
153
171
|
require 'tempfile'
|
154
172
|
tmpf = Tempfile.new 'mysql_spec'
|
155
173
|
tmpf.puts "123\tabc\n"
|
156
174
|
tmpf.close
|
157
|
-
|
175
|
+
@m.local_infile = true
|
158
176
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
177
|
+
if @m.query('select @@local_infile').fetch[0] == '0'
|
178
|
+
omit 'skip because local_infile variable is false'
|
179
|
+
end
|
159
180
|
@m.query('create temporary table t (i int, c char(10))')
|
160
181
|
@m.query("load data local infile '#{tmpf.path}' into table t")
|
182
|
+
assert{ @m.info == 'Records: 1 Deleted: 0 Skipped: 0 Warnings: 0' }
|
161
183
|
assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
|
162
184
|
end
|
163
|
-
test '
|
185
|
+
test 'load_data_local_dir: client can execute LOAD DATA LOCAL INFILE query with specified directory' do
|
164
186
|
require 'tempfile'
|
165
187
|
tmpf = Tempfile.new 'mysql_spec'
|
166
188
|
tmpf.puts "123\tabc\n"
|
167
189
|
tmpf.close
|
168
|
-
|
190
|
+
@m.load_data_local_dir = File.dirname(tmpf.path)
|
169
191
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
192
|
+
if @m.query('select @@local_infile').fetch[0] == '0'
|
193
|
+
omit 'skip because local_infile variable is false'
|
194
|
+
end
|
170
195
|
@m.query('create temporary table t (i int, c char(10))')
|
171
196
|
@m.query("load data local infile '#{tmpf.path}' into table t")
|
172
197
|
assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
|
173
198
|
end
|
174
|
-
test '
|
199
|
+
test 'load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query without specified directory' do
|
175
200
|
require 'tempfile'
|
176
201
|
tmpf = Tempfile.new 'mysql_spec'
|
177
202
|
tmpf.puts "123\tabc\n"
|
178
203
|
tmpf.close
|
179
|
-
|
204
|
+
@m.load_data_local_dir = '/hoge'
|
180
205
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
206
|
+
if @m.query('select @@local_infile').fetch[0] == '0'
|
207
|
+
omit 'skip because local_infile variable is false'
|
208
|
+
end
|
181
209
|
@m.query('create temporary table t (i int, c char(10))')
|
182
210
|
assert_raise Mysql::ClientError::LoadDataLocalInfileRejected, 'LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.' do
|
183
211
|
@m.query("load data local infile '#{tmpf.path}' into table t")
|
184
212
|
end
|
185
213
|
end
|
186
|
-
test 'without
|
214
|
+
test 'without local_infile and load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query' do
|
187
215
|
require 'tempfile'
|
188
216
|
tmpf = Tempfile.new 'mysql_spec'
|
189
217
|
tmpf.puts "123\tabc\n"
|
190
218
|
tmpf.close
|
191
219
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
220
|
+
if @m.query('select @@local_infile').fetch[0] == '0'
|
221
|
+
omit 'skip because local_infile variable is false'
|
222
|
+
end
|
192
223
|
@m.query('create temporary table t (i int, c char(10))')
|
193
224
|
if @m.server_version >= 80000
|
194
225
|
assert_raise Mysql::ServerError, 'Loading local data is disabled; this must be enabled on both the client and server sides' do
|
@@ -200,18 +231,18 @@ class TestMysql < Test::Unit::TestCase
|
|
200
231
|
end
|
201
232
|
end
|
202
233
|
end
|
203
|
-
test '
|
204
|
-
|
234
|
+
test 'read_timeout: set timeout for reading packet' do
|
235
|
+
@m.read_timeout = 1
|
205
236
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
206
237
|
@m.query("select 123").entries
|
207
238
|
end
|
208
|
-
test '
|
209
|
-
|
239
|
+
test 'write_timeout: set timeout for writing packet' do
|
240
|
+
@m.write_timeout = 1
|
210
241
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
211
242
|
@m.query("select 123").entries
|
212
243
|
end
|
213
|
-
test '
|
214
|
-
|
244
|
+
test 'charset: set charset for connection' do
|
245
|
+
@m.charset = 'utf8mb3'
|
215
246
|
@m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
216
247
|
assert do
|
217
248
|
@m.query('select @@character_set_connection').fetch_row == ['utf8mb3'] ||
|
@@ -222,7 +253,7 @@ class TestMysql < Test::Unit::TestCase
|
|
222
253
|
|
223
254
|
sub_test_case 'Mysql' do
|
224
255
|
setup do
|
225
|
-
@m = Mysql.
|
256
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
226
257
|
end
|
227
258
|
|
228
259
|
teardown do
|
@@ -230,22 +261,9 @@ class TestMysql < Test::Unit::TestCase
|
|
230
261
|
end
|
231
262
|
|
232
263
|
sub_test_case '#escape_string' do
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
|
237
|
-
end
|
238
|
-
else
|
239
|
-
test 'raise error if charset is multibyte' do
|
240
|
-
@m.charset = 'cp932'
|
241
|
-
assert_raise Mysql::ClientError, 'Mysql#escape_string is called for unsafe multibyte charset' do
|
242
|
-
@m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\")
|
243
|
-
end
|
244
|
-
end
|
245
|
-
test 'not warn if charset is singlebyte' do
|
246
|
-
@m.charset = 'latin1'
|
247
|
-
assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\") == "abc\\'def\\\"ghi\\0jkl%mno_\x95\\\\" }
|
248
|
-
end
|
264
|
+
test 'escape special character for charset' do
|
265
|
+
@m.charset = 'cp932'
|
266
|
+
assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
|
249
267
|
end
|
250
268
|
end
|
251
269
|
|
@@ -255,18 +273,6 @@ class TestMysql < Test::Unit::TestCase
|
|
255
273
|
end
|
256
274
|
end
|
257
275
|
|
258
|
-
sub_test_case '#client_info' do
|
259
|
-
test 'returns client version as string' do
|
260
|
-
assert{ @m.client_info == '5.0.0' }
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
sub_test_case '#get_client_info' do
|
265
|
-
test 'returns client version as string' do
|
266
|
-
assert{ @m.get_client_info == '5.0.0' }
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
276
|
sub_test_case '#affected_rows' do
|
271
277
|
test 'returns number of affected rows' do
|
272
278
|
@m.query 'create temporary table t (id int)'
|
@@ -277,8 +283,8 @@ class TestMysql < Test::Unit::TestCase
|
|
277
283
|
|
278
284
|
sub_test_case '#character_set_name' do
|
279
285
|
test 'returns charset name' do
|
280
|
-
m = Mysql.
|
281
|
-
m.
|
286
|
+
m = Mysql.new
|
287
|
+
m.charset = 'cp932'
|
282
288
|
m.connect MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET
|
283
289
|
assert{ m.character_set_name == 'cp932' }
|
284
290
|
end
|
@@ -326,28 +332,6 @@ class TestMysql < Test::Unit::TestCase
|
|
326
332
|
end
|
327
333
|
end
|
328
334
|
|
329
|
-
sub_test_case '#client_version' do
|
330
|
-
test 'returns client version as Integer' do
|
331
|
-
assert{ @m.client_version.kind_of? Integer }
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
sub_test_case '#get_client_version' do
|
336
|
-
test 'returns client version as Integer' do
|
337
|
-
assert{ @m.get_client_version.kind_of? Integer }
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
sub_test_case '#get_host_info' do
|
342
|
-
test 'returns connection type as String' do
|
343
|
-
if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
|
344
|
-
assert{ @m.get_host_info == 'Localhost via UNIX socket' }
|
345
|
-
else
|
346
|
-
assert{ @m.get_host_info == "#{MYSQL_SERVER} via TCP/IP" }
|
347
|
-
end
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
335
|
sub_test_case '#host_info' do
|
352
336
|
test 'returns connection type as String' do
|
353
337
|
if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
|
@@ -358,24 +342,6 @@ class TestMysql < Test::Unit::TestCase
|
|
358
342
|
end
|
359
343
|
end
|
360
344
|
|
361
|
-
sub_test_case '#get_proto_info' do
|
362
|
-
test 'returns version of connection as Integer' do
|
363
|
-
assert{ @m.get_proto_info == 10 }
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
sub_test_case '#proto_info' do
|
368
|
-
test 'returns version of connection as Integer' do
|
369
|
-
assert{ @m.proto_info == 10 }
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
sub_test_case '#get_server_info' do
|
374
|
-
test 'returns server version as String' do
|
375
|
-
assert{ @m.get_server_info =~ /\A\d+\.\d+\.\d+/ }
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
345
|
sub_test_case '#server_info' do
|
380
346
|
test 'returns server version as String' do
|
381
347
|
assert{ @m.server_info =~ /\A\d+\.\d+\.\d+/ }
|
@@ -403,7 +369,7 @@ class TestMysql < Test::Unit::TestCase
|
|
403
369
|
|
404
370
|
sub_test_case '#kill' do
|
405
371
|
setup do
|
406
|
-
@m2 = Mysql.
|
372
|
+
@m2 = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
407
373
|
end
|
408
374
|
teardown do
|
409
375
|
@m2.close rescue nil
|
@@ -413,61 +379,6 @@ class TestMysql < Test::Unit::TestCase
|
|
413
379
|
end
|
414
380
|
end
|
415
381
|
|
416
|
-
sub_test_case '#list_dbs' do
|
417
|
-
test 'returns database list' do
|
418
|
-
ret = @m.list_dbs
|
419
|
-
assert{ ret.kind_of? Array }
|
420
|
-
assert{ ret.include? MYSQL_DATABASE }
|
421
|
-
end
|
422
|
-
test 'with pattern returns databases that matches pattern' do
|
423
|
-
assert{ @m.list_dbs('info%').include? 'information_schema' }
|
424
|
-
end
|
425
|
-
end
|
426
|
-
|
427
|
-
sub_test_case '#list_fields' do
|
428
|
-
setup do
|
429
|
-
@m.query 'create temporary table t (i int, c char(10), d date)'
|
430
|
-
end
|
431
|
-
test 'returns result set that contains information of fields' do
|
432
|
-
ret = @m.list_fields('t')
|
433
|
-
assert{ ret.kind_of? Mysql::Result }
|
434
|
-
assert{ ret.num_rows == 0 }
|
435
|
-
assert{ ret.fetch_fields.map{|f|f.name} == ['i','c','d'] }
|
436
|
-
end
|
437
|
-
test 'with pattern returns result set that contains information of fields that matches pattern' do
|
438
|
-
ret = @m.list_fields('t', 'i')
|
439
|
-
assert{ ret.kind_of? Mysql::Result }
|
440
|
-
assert{ ret.num_rows == 0 }
|
441
|
-
ret.fetch_fields.map{|f|f.name} == ['i']
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
sub_test_case '#list_processes' do
|
446
|
-
test 'returns result set that contains information of all connections' do
|
447
|
-
ret = @m.list_processes
|
448
|
-
assert{ ret.kind_of? Mysql::Result }
|
449
|
-
assert{ ret.find{|r|r[0].to_i == @m.thread_id}[4] == "Processlist" }
|
450
|
-
end
|
451
|
-
end
|
452
|
-
|
453
|
-
sub_test_case '#list_tables' do
|
454
|
-
setup do
|
455
|
-
@m.query 'create table test_mysql_list_tables (id int)'
|
456
|
-
end
|
457
|
-
teardown do
|
458
|
-
@m.query 'drop table if exists test_mysql_list_tables'
|
459
|
-
end
|
460
|
-
test 'returns table list' do
|
461
|
-
ret = @m.list_tables
|
462
|
-
assert{ ret.kind_of? Array }
|
463
|
-
assert{ ret.include? 'test_mysql_list_tables' }
|
464
|
-
end
|
465
|
-
test 'with pattern returns lists that matches pattern' do
|
466
|
-
ret = @m.list_tables '%mysql\_list\_t%'
|
467
|
-
assert{ ret.include? 'test_mysql_list_tables' }
|
468
|
-
end
|
469
|
-
end
|
470
|
-
|
471
382
|
sub_test_case '#ping' do
|
472
383
|
test 'returns self' do
|
473
384
|
assert{ @m.ping == @m }
|
@@ -481,17 +392,8 @@ class TestMysql < Test::Unit::TestCase
|
|
481
392
|
test 'returns nil if query returns no results' do
|
482
393
|
assert{ @m.query('set @hoge:=123') == nil }
|
483
394
|
end
|
484
|
-
test 'returns self if
|
485
|
-
@m.
|
486
|
-
assert{ @m.query('select 123') == @m }
|
487
|
-
@m.store_result
|
488
|
-
assert{ @m.query('set @hoge:=123') == @m }
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
sub_test_case '#real_query' do
|
493
|
-
test 'is same as #query' do
|
494
|
-
assert{ @m.real_query('select 123').kind_of? Mysql::Result }
|
395
|
+
test 'returns self if block is specified' do
|
396
|
+
assert{ @m.query('select 123'){} == @m }
|
495
397
|
end
|
496
398
|
end
|
497
399
|
|
@@ -523,60 +425,12 @@ class TestMysql < Test::Unit::TestCase
|
|
523
425
|
end
|
524
426
|
end
|
525
427
|
|
526
|
-
sub_test_case '#store_result' do
|
527
|
-
test 'returns Mysql::Result' do
|
528
|
-
@m.query_with_result = false
|
529
|
-
@m.query 'select 1,2,3'
|
530
|
-
ret = @m.store_result
|
531
|
-
assert{ ret.kind_of? Mysql::Result }
|
532
|
-
assert{ ret.fetch_row == ['1','2','3'] }
|
533
|
-
end
|
534
|
-
test 'raises error when no query' do
|
535
|
-
assert_raise Mysql::ClientError, 'invalid usage' do
|
536
|
-
@m.store_result
|
537
|
-
end
|
538
|
-
end
|
539
|
-
test 'raises error when query does not return results' do
|
540
|
-
@m.query 'set @hoge:=123'
|
541
|
-
assert_raise Mysql::ClientError, 'invalid usage' do
|
542
|
-
@m.store_result
|
543
|
-
end
|
544
|
-
end
|
545
|
-
end
|
546
|
-
|
547
428
|
sub_test_case '#thread_id' do
|
548
429
|
test 'returns thread id as Integer' do
|
549
430
|
assert{ @m.thread_id.kind_of? Integer }
|
550
431
|
end
|
551
432
|
end
|
552
433
|
|
553
|
-
sub_test_case '#use_result' do
|
554
|
-
test 'returns Mysql::Result' do
|
555
|
-
@m.query_with_result = false
|
556
|
-
@m.query 'select 1,2,3'
|
557
|
-
ret = @m.use_result
|
558
|
-
assert{ ret.kind_of? Mysql::Result }
|
559
|
-
assert{ ret.fetch_row == ['1','2','3'] }
|
560
|
-
end
|
561
|
-
test 'raises error when no query' do
|
562
|
-
assert_raise Mysql::ClientError, 'invalid usage' do
|
563
|
-
@m.use_result
|
564
|
-
end
|
565
|
-
end
|
566
|
-
test 'raises error when query does not return results' do
|
567
|
-
@m.query 'set @hoge:=123'
|
568
|
-
assert_raise Mysql::ClientError, 'invalid usage' do
|
569
|
-
@m.use_result
|
570
|
-
end
|
571
|
-
end
|
572
|
-
end
|
573
|
-
|
574
|
-
sub_test_case '#get_server_version' do
|
575
|
-
test 'returns server version as Integer' do
|
576
|
-
assert{ @m.get_server_version.kind_of? Integer }
|
577
|
-
end
|
578
|
-
end
|
579
|
-
|
580
434
|
sub_test_case '#server_version' do
|
581
435
|
test 'returns server version as Integer' do
|
582
436
|
assert{ @m.server_version.kind_of? Integer }
|
@@ -641,27 +495,6 @@ class TestMysql < Test::Unit::TestCase
|
|
641
495
|
end
|
642
496
|
end
|
643
497
|
|
644
|
-
sub_test_case '#query_with_result' do
|
645
|
-
test 'default value is true' do
|
646
|
-
assert{ @m.query_with_result == true }
|
647
|
-
end
|
648
|
-
test 'can set value' do
|
649
|
-
assert{ (@m.query_with_result=true) == true }
|
650
|
-
assert{ @m.query_with_result == true }
|
651
|
-
assert{ (@m.query_with_result=false) == false }
|
652
|
-
assert{ @m.query_with_result == false }
|
653
|
-
end
|
654
|
-
end
|
655
|
-
|
656
|
-
sub_test_case '#query_with_result is false' do
|
657
|
-
test 'Mysql#query returns self and Mysql#store_result returns result set' do
|
658
|
-
@m.query_with_result = false
|
659
|
-
assert{ @m.query('select 1,2,3') == @m }
|
660
|
-
res = @m.store_result
|
661
|
-
assert{ res.fetch_row == ['1','2','3'] }
|
662
|
-
end
|
663
|
-
end
|
664
|
-
|
665
498
|
sub_test_case '#query with block' do
|
666
499
|
test 'returns self' do
|
667
500
|
assert{ @m.query('select 1'){} == @m }
|
@@ -694,7 +527,7 @@ class TestMysql < Test::Unit::TestCase
|
|
694
527
|
|
695
528
|
sub_test_case 'multiple statement query:' do
|
696
529
|
setup do
|
697
|
-
@m = Mysql.
|
530
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
698
531
|
@m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
|
699
532
|
@res = @m.query 'select 1,2; select 3,4,5'
|
700
533
|
end
|
@@ -730,9 +563,33 @@ class TestMysql < Test::Unit::TestCase
|
|
730
563
|
end
|
731
564
|
end
|
732
565
|
|
566
|
+
test 'multiple statement error' do
|
567
|
+
m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
568
|
+
m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
|
569
|
+
res = m.query 'select 1; select hoge; select 2'
|
570
|
+
assert{ res.entries == [['1']] }
|
571
|
+
assert{ m.more_results? == true }
|
572
|
+
assert_raise(Mysql::ServerError::BadFieldError){ m.next_result }
|
573
|
+
assert{ m.more_results? == false }
|
574
|
+
end
|
575
|
+
|
576
|
+
test 'procedure returns multiple results' do
|
577
|
+
m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
578
|
+
m.query 'drop procedure if exists test_proc'
|
579
|
+
m.query 'create procedure test_proc() begin select 1 as a; select 2 as b; end'
|
580
|
+
res = m.query 'call test_proc()'
|
581
|
+
assert{ res.entries == [['1']] }
|
582
|
+
assert{ m.more_results? == true }
|
583
|
+
assert{ m.next_result == true }
|
584
|
+
assert{ m.store_result.entries == [['2']] }
|
585
|
+
assert{ m.more_results? == true }
|
586
|
+
assert{ m.next_result == true }
|
587
|
+
assert{ m.more_results? == false }
|
588
|
+
end
|
589
|
+
|
733
590
|
sub_test_case 'Mysql::Result' do
|
734
591
|
setup do
|
735
|
-
@m = Mysql.
|
592
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
736
593
|
@m.charset = 'latin1'
|
737
594
|
@m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
|
738
595
|
@m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
|
@@ -888,7 +745,7 @@ class TestMysql < Test::Unit::TestCase
|
|
888
745
|
|
889
746
|
sub_test_case 'Mysql::Field' do
|
890
747
|
setup do
|
891
|
-
@m = Mysql.
|
748
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
892
749
|
@m.charset = 'latin1'
|
893
750
|
@m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
|
894
751
|
@m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
|
@@ -911,10 +768,6 @@ class TestMysql < Test::Unit::TestCase
|
|
911
768
|
assert{ @res.fetch_field.def == nil }
|
912
769
|
end
|
913
770
|
|
914
|
-
test '#def for field information is default value' do
|
915
|
-
assert{ @m.list_fields('t').fetch_field.def == '0' }
|
916
|
-
end
|
917
|
-
|
918
771
|
test '#type is type of field as Integer' do
|
919
772
|
assert{ @res.fetch_field.type == Mysql::Field::TYPE_LONG }
|
920
773
|
assert{ @res.fetch_field.type == Mysql::Field::TYPE_STRING }
|
@@ -939,8 +792,8 @@ class TestMysql < Test::Unit::TestCase
|
|
939
792
|
assert{ @m.query('select 1.23').fetch_field.decimals == 2 }
|
940
793
|
end
|
941
794
|
|
942
|
-
test '#
|
943
|
-
assert{ @res.fetch_field.
|
795
|
+
test '#to_hash return field as hash' do
|
796
|
+
assert{ @res.fetch_field.to_hash == {
|
944
797
|
'name' => 'id',
|
945
798
|
'table' => 't',
|
946
799
|
'def' => nil,
|
@@ -951,7 +804,7 @@ class TestMysql < Test::Unit::TestCase
|
|
951
804
|
'decimals' => 0,
|
952
805
|
}
|
953
806
|
}
|
954
|
-
assert{ @res.fetch_field.
|
807
|
+
assert{ @res.fetch_field.to_hash == {
|
955
808
|
'name' => 'str',
|
956
809
|
'table' => 't',
|
957
810
|
'def' => nil,
|
@@ -987,15 +840,15 @@ class TestMysql < Test::Unit::TestCase
|
|
987
840
|
|
988
841
|
sub_test_case 'create Mysql::Stmt object:' do
|
989
842
|
setup do
|
990
|
-
@m = Mysql.
|
843
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
991
844
|
end
|
992
845
|
|
993
846
|
teardown do
|
994
847
|
@m.close if @m
|
995
848
|
end
|
996
849
|
|
997
|
-
test 'Mysql#
|
998
|
-
assert{ @m.
|
850
|
+
test 'Mysql#stmt returns Mysql::Stmt object' do
|
851
|
+
assert{ @m.stmt.kind_of? Mysql::Stmt }
|
999
852
|
end
|
1000
853
|
|
1001
854
|
test 'Mysq;#prepare returns Mysql::Stmt object' do
|
@@ -1005,14 +858,14 @@ class TestMysql < Test::Unit::TestCase
|
|
1005
858
|
|
1006
859
|
sub_test_case 'Mysql::Stmt' do
|
1007
860
|
setup do
|
1008
|
-
@m = Mysql.
|
861
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
1009
862
|
@m.query("set sql_mode=''")
|
1010
|
-
@s = @m.
|
863
|
+
@s = @m.stmt
|
1011
864
|
end
|
1012
865
|
|
1013
866
|
teardown do
|
1014
867
|
@s.close if @s rescue nil
|
1015
|
-
@m.close if @m
|
868
|
+
@m.close if @m rescue nil
|
1016
869
|
end
|
1017
870
|
|
1018
871
|
test '#affected_rows returns number of affected records' do
|
@@ -1027,62 +880,6 @@ class TestMysql < Test::Unit::TestCase
|
|
1027
880
|
assert{ @s.affected_rows == 3 }
|
1028
881
|
end
|
1029
882
|
|
1030
|
-
sub_test_case '#bind_result' do
|
1031
|
-
setup do
|
1032
|
-
@m.query 'create temporary table t (i int, c char(10), d double, t datetime)'
|
1033
|
-
@m.query 'insert into t values (123,"9abcdefg",1.2345,20091208100446)'
|
1034
|
-
@s.prepare 'select * from t'
|
1035
|
-
end
|
1036
|
-
|
1037
|
-
test '(nil) make result format to be standard value' do
|
1038
|
-
@s.bind_result nil, nil, nil, nil
|
1039
|
-
@s.execute
|
1040
|
-
assert{ @s.fetch == [123, '9abcdefg', 1.2345, Mysql::Time.new(2009,12,8,10,4,46)] }
|
1041
|
-
end
|
1042
|
-
|
1043
|
-
test '(Numeric) make result format to be Integer value' do
|
1044
|
-
@s.bind_result Numeric, Numeric, Numeric, Numeric
|
1045
|
-
@s.execute
|
1046
|
-
assert{ @s.fetch == [123, 9, 1, 20091208100446] }
|
1047
|
-
end
|
1048
|
-
|
1049
|
-
test '(Integer) make result format to be Integer value' do
|
1050
|
-
@s.bind_result Integer, Integer, Integer, Integer
|
1051
|
-
@s.execute
|
1052
|
-
assert{ @s.fetch == [123, 9, 1, 20091208100446] }
|
1053
|
-
end
|
1054
|
-
|
1055
|
-
test '(String) make result format to be String value' do
|
1056
|
-
@s.bind_result String, String, String, String
|
1057
|
-
@s.execute
|
1058
|
-
assert{ @s.fetch == ["123", "9abcdefg", "1.2345", "2009-12-08 10:04:46"] }
|
1059
|
-
end
|
1060
|
-
|
1061
|
-
test '(Float) make result format to be Float value' do
|
1062
|
-
@s.bind_result Float, Float, Float, Float
|
1063
|
-
@s.execute
|
1064
|
-
assert{ @s.fetch == [123.0, 9.0, 1.2345 , 20091208100446.0] }
|
1065
|
-
end
|
1066
|
-
|
1067
|
-
test '(Mysql::Time) make result format to be Mysql::Time value' do
|
1068
|
-
@s.bind_result Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time
|
1069
|
-
@s.execute
|
1070
|
-
assert{ @s.fetch == [Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2009,12,8,10,4,46)] }
|
1071
|
-
end
|
1072
|
-
|
1073
|
-
test '(invalid) raises error' do
|
1074
|
-
assert_raise TypeError do
|
1075
|
-
@s.bind_result(Time, nil, nil, nil)
|
1076
|
-
end
|
1077
|
-
end
|
1078
|
-
|
1079
|
-
test 'with mismatch argument count raise error' do
|
1080
|
-
assert_raise Mysql::ClientError, 'bind_result: result value count(4) != number of argument(1)' do
|
1081
|
-
@s.bind_result(nil)
|
1082
|
-
end
|
1083
|
-
end
|
1084
|
-
end
|
1085
|
-
|
1086
883
|
test '#close returns nil' do
|
1087
884
|
assert{ @s.close == nil }
|
1088
885
|
end
|
@@ -1107,8 +904,8 @@ class TestMysql < Test::Unit::TestCase
|
|
1107
904
|
@s.prepare 'select * from t'
|
1108
905
|
@s.execute
|
1109
906
|
expect = [
|
1110
|
-
[1, 'abc',
|
1111
|
-
[2, 'def',
|
907
|
+
[1, 'abc', Time.new(1970,12,24,23,59,05)],
|
908
|
+
[2, 'def', Time.new(2112,9,3,12,34,56)],
|
1112
909
|
[3, '123', nil],
|
1113
910
|
]
|
1114
911
|
@s.each do |a|
|
@@ -1254,20 +1051,16 @@ class TestMysql < Test::Unit::TestCase
|
|
1254
1051
|
@m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
|
1255
1052
|
@s.prepare 'select i from t'
|
1256
1053
|
@s.execute
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
}
|
1268
|
-
else
|
1269
|
-
assert{ @s.entries == [["\x00"], ["\xff"], ["\x7f"], ["\xff"], ["\xff"], ["\xff"], ["\xff"]] }
|
1270
|
-
end
|
1054
|
+
assert{ @s.entries == [
|
1055
|
+
["\x00".force_encoding('ASCII-8BIT')],
|
1056
|
+
["\xff".force_encoding('ASCII-8BIT')],
|
1057
|
+
["\x7f".force_encoding('ASCII-8BIT')],
|
1058
|
+
["\xff".force_encoding('ASCII-8BIT')],
|
1059
|
+
["\xff".force_encoding('ASCII-8BIT')],
|
1060
|
+
["\xff".force_encoding('ASCII-8BIT')],
|
1061
|
+
["\xff".force_encoding('ASCII-8BIT')],
|
1062
|
+
]
|
1063
|
+
}
|
1271
1064
|
end
|
1272
1065
|
|
1273
1066
|
test '#fetch bit column (64bit)' do
|
@@ -1275,25 +1068,14 @@ class TestMysql < Test::Unit::TestCase
|
|
1275
1068
|
@m.query 'insert into t values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)'
|
1276
1069
|
@s.prepare 'select i from t'
|
1277
1070
|
@s.execute
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
}
|
1287
|
-
else
|
1288
|
-
assert{ @s.entries == [
|
1289
|
-
["\x00\x00\x00\x00\x00\x00\x00\x00"],
|
1290
|
-
["\xff\xff\xff\xff\xff\xff\xff\xff"],
|
1291
|
-
["\x00\x00\x00\x01\x00\x00\x00\x00"],
|
1292
|
-
["\xff\xff\xff\xff\xff\xff\xff\xff"],
|
1293
|
-
["\xff\xff\xff\xff\xff\xff\xff\xff"],
|
1294
|
-
]
|
1295
|
-
}
|
1296
|
-
end
|
1071
|
+
assert{ @s.entries == [
|
1072
|
+
["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
|
1073
|
+
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
|
1074
|
+
["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
|
1075
|
+
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
|
1076
|
+
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
|
1077
|
+
]
|
1078
|
+
}
|
1297
1079
|
end
|
1298
1080
|
|
1299
1081
|
test '#fetch tinyint column' do
|
@@ -1446,14 +1228,11 @@ class TestMysql < Test::Unit::TestCase
|
|
1446
1228
|
@s.prepare 'select i from t'
|
1447
1229
|
@s.execute
|
1448
1230
|
cols = @s.fetch
|
1449
|
-
assert{ cols == [
|
1450
|
-
assert{ cols.first.to_s == '0000-00-00' }
|
1231
|
+
assert{ cols == [nil] }
|
1451
1232
|
cols = @s.fetch
|
1452
|
-
assert{ cols == [
|
1453
|
-
assert{ cols.first.to_s == '1000-01-01' }
|
1233
|
+
assert{ cols == [Time.new(1000,1,1)] }
|
1454
1234
|
cols = @s.fetch
|
1455
|
-
assert{ cols == [
|
1456
|
-
assert{ cols.first.to_s == '9999-12-31' }
|
1235
|
+
assert{ cols == [Time.new(9999,12,31)] }
|
1457
1236
|
end
|
1458
1237
|
|
1459
1238
|
test '#fetch datetime column' do
|
@@ -1461,9 +1240,9 @@ class TestMysql < Test::Unit::TestCase
|
|
1461
1240
|
@m.query "insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')"
|
1462
1241
|
@s.prepare 'select i from t'
|
1463
1242
|
@s.execute
|
1464
|
-
assert{ @s.fetch == [
|
1465
|
-
assert{ @s.fetch == [
|
1466
|
-
assert{ @s.fetch == [
|
1243
|
+
assert{ @s.fetch == [nil] }
|
1244
|
+
assert{ @s.fetch == [Time.new(1000,1,1)] }
|
1245
|
+
assert{ @s.fetch == [Time.new(9999,12,31,23,59,59)] }
|
1467
1246
|
end
|
1468
1247
|
|
1469
1248
|
test '#fetch timestamp column' do
|
@@ -1471,8 +1250,8 @@ class TestMysql < Test::Unit::TestCase
|
|
1471
1250
|
@m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
|
1472
1251
|
@s.prepare 'select i from t'
|
1473
1252
|
@s.execute
|
1474
|
-
assert{ @s.fetch == [
|
1475
|
-
assert{ @s.fetch == [
|
1253
|
+
assert{ @s.fetch == [Time.new(1970,1,2)] }
|
1254
|
+
assert{ @s.fetch == [Time.new(2037,12,30,23,59,59)] }
|
1476
1255
|
end
|
1477
1256
|
|
1478
1257
|
test '#fetch time column' do
|
@@ -1480,9 +1259,9 @@ class TestMysql < Test::Unit::TestCase
|
|
1480
1259
|
@m.query "insert into t values ('-838:59:59'),(0),('838:59:59')"
|
1481
1260
|
@s.prepare 'select i from t'
|
1482
1261
|
@s.execute
|
1483
|
-
assert{ @s.fetch == [
|
1484
|
-
assert{ @s.fetch == [
|
1485
|
-
assert{ @s.fetch == [
|
1262
|
+
assert{ @s.fetch == [-(838*3600+59*60+59)] }
|
1263
|
+
assert{ @s.fetch == [0] }
|
1264
|
+
assert{ @s.fetch == [838*3600+59*60+59] }
|
1486
1265
|
end
|
1487
1266
|
|
1488
1267
|
test '#fetch year column' do
|
@@ -1707,79 +1486,6 @@ class TestMysql < Test::Unit::TestCase
|
|
1707
1486
|
end
|
1708
1487
|
end
|
1709
1488
|
|
1710
|
-
sub_test_case 'Mysql::Time' do
|
1711
|
-
setup do
|
1712
|
-
@t = Mysql::Time.new
|
1713
|
-
end
|
1714
|
-
|
1715
|
-
test '.new with no arguments returns 0' do
|
1716
|
-
assert{ @t.year == 0 }
|
1717
|
-
assert{ @t.month == 0 }
|
1718
|
-
assert{ @t.day == 0 }
|
1719
|
-
assert{ @t.hour == 0 }
|
1720
|
-
assert{ @t.minute == 0 }
|
1721
|
-
assert{ @t.second == 0 }
|
1722
|
-
assert{ @t.neg == false }
|
1723
|
-
assert{ @t.second_part == 0 }
|
1724
|
-
end
|
1725
|
-
|
1726
|
-
test '#inspect' do
|
1727
|
-
assert{ Mysql::Time.new(2009,12,8,23,35,21).inspect == '#<Mysql::Time:2009-12-08 23:35:21>' }
|
1728
|
-
end
|
1729
|
-
|
1730
|
-
test '#to_s' do
|
1731
|
-
assert{ Mysql::Time.new(2009,12,8,23,35,21).to_s == '2009-12-08 23:35:21' }
|
1732
|
-
end
|
1733
|
-
|
1734
|
-
test '#to_i' do
|
1735
|
-
assert{ Mysql::Time.new(2009,12,8,23,35,21).to_i == 20091208233521 }
|
1736
|
-
end
|
1737
|
-
|
1738
|
-
test '#year' do
|
1739
|
-
assert{ (@t.year = 2009) == 2009 }
|
1740
|
-
assert{ @t.year == 2009 }
|
1741
|
-
end
|
1742
|
-
|
1743
|
-
test '#month' do
|
1744
|
-
assert{ (@t.month = 12) == 12 }
|
1745
|
-
assert{ @t.month == 12 }
|
1746
|
-
end
|
1747
|
-
|
1748
|
-
test '#day' do
|
1749
|
-
assert{ (@t.day = 8) == 8 }
|
1750
|
-
assert{ @t.day == 8 }
|
1751
|
-
end
|
1752
|
-
|
1753
|
-
test '#hour' do
|
1754
|
-
assert{ (@t.hour = 23) == 23 }
|
1755
|
-
assert{ @t.hour == 23 }
|
1756
|
-
end
|
1757
|
-
|
1758
|
-
test '#minute' do
|
1759
|
-
assert{ (@t.minute = 35) == 35 }
|
1760
|
-
assert{ @t.minute == 35 }
|
1761
|
-
end
|
1762
|
-
|
1763
|
-
test '#second' do
|
1764
|
-
assert{ (@t.second = 21) == 21 }
|
1765
|
-
assert{ @t.second == 21 }
|
1766
|
-
end
|
1767
|
-
|
1768
|
-
test '#neg' do
|
1769
|
-
assert{ @t.neg == false }
|
1770
|
-
end
|
1771
|
-
|
1772
|
-
test '#second_part' do
|
1773
|
-
assert{ @t.second_part == 0 }
|
1774
|
-
end
|
1775
|
-
|
1776
|
-
test '#==' do
|
1777
|
-
t1 = Mysql::Time.new 2009,12,8,23,35,21
|
1778
|
-
t2 = Mysql::Time.new 2009,12,8,23,35,21
|
1779
|
-
assert{ t1 == t2 }
|
1780
|
-
end
|
1781
|
-
end
|
1782
|
-
|
1783
1489
|
sub_test_case 'Mysql::Error' do
|
1784
1490
|
setup do
|
1785
1491
|
m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
@@ -1802,96 +1508,94 @@ class TestMysql < Test::Unit::TestCase
|
|
1802
1508
|
end
|
1803
1509
|
end
|
1804
1510
|
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
end
|
1511
|
+
sub_test_case 'Connection charset is UTF-8:' do
|
1512
|
+
setup do
|
1513
|
+
@m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
|
1514
|
+
@m.charset = "utf8"
|
1515
|
+
@m.query "create temporary table t (utf8 char(10) charset utf8, cp932 char(10) charset cp932, eucjp char(10) charset eucjpms, bin varbinary(10))"
|
1516
|
+
@utf8 = "いろは"
|
1517
|
+
@cp932 = @utf8.encode "CP932"
|
1518
|
+
@eucjp = @utf8.encode "EUC-JP-MS"
|
1519
|
+
@bin = "\x00\x01\x7F\x80\xFE\xFF".force_encoding("ASCII-8BIT")
|
1520
|
+
@default_internal = Encoding.default_internal
|
1521
|
+
end
|
1817
1522
|
|
1818
|
-
|
1523
|
+
teardown do
|
1524
|
+
v = $VERBOSE
|
1525
|
+
$VERBOSE = false
|
1526
|
+
Encoding.default_internal = @default_internal
|
1527
|
+
$VERBOSE = v
|
1528
|
+
end
|
1529
|
+
|
1530
|
+
sub_test_case 'default_internal is CP932' do
|
1531
|
+
setup do
|
1532
|
+
@m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
|
1819
1533
|
v = $VERBOSE
|
1820
1534
|
$VERBOSE = false
|
1821
|
-
Encoding.default_internal =
|
1535
|
+
Encoding.default_internal = 'CP932'
|
1822
1536
|
$VERBOSE = v
|
1823
1537
|
end
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
end
|
1833
|
-
test 'is converted to CP932' do
|
1834
|
-
assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
|
1835
|
-
end
|
1836
|
-
test 'data is stored as is' do
|
1837
|
-
assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
|
1838
|
-
end
|
1839
|
-
test 'By simple query, charset of retrieved data is connection charset' do
|
1840
|
-
assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
|
1841
|
-
end
|
1842
|
-
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1843
|
-
assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
|
1844
|
-
end
|
1538
|
+
test 'is converted to CP932' do
|
1539
|
+
assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
|
1540
|
+
end
|
1541
|
+
test 'data is stored as is' do
|
1542
|
+
assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
|
1543
|
+
end
|
1544
|
+
test 'By simple query, charset of retrieved data is connection charset' do
|
1545
|
+
assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
|
1845
1546
|
end
|
1547
|
+
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1548
|
+
assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
|
1549
|
+
end
|
1550
|
+
end
|
1846
1551
|
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
end
|
1552
|
+
sub_test_case 'query with CP932 encoding' do
|
1553
|
+
test 'is converted to UTF-8' do
|
1554
|
+
assert @m.query('select HEX("あいう")'.encode("CP932")).fetch == ["E38182E38184E38186"]
|
1851
1555
|
end
|
1556
|
+
end
|
1852
1557
|
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
end
|
1558
|
+
sub_test_case 'prepared statement with CP932 encoding' do
|
1559
|
+
test 'is converted to UTF-8' do
|
1560
|
+
assert @m.prepare('select HEX("あいう")'.encode("CP932")).execute.fetch == ["E38182E38184E38186"]
|
1857
1561
|
end
|
1562
|
+
end
|
1858
1563
|
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1865
|
-
end
|
1866
|
-
test 'By simple query, charset of retrieved data is connection charset' do
|
1867
|
-
assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
|
1868
|
-
end
|
1869
|
-
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1870
|
-
assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
|
1871
|
-
end
|
1564
|
+
sub_test_case 'The encoding of data are correspond to charset of column:' do
|
1565
|
+
setup do
|
1566
|
+
@m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
|
1567
|
+
end
|
1568
|
+
test 'data is stored as is' do
|
1569
|
+
assert{ @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF'] }
|
1872
1570
|
end
|
1571
|
+
test 'By simple query, charset of retrieved data is connection charset' do
|
1572
|
+
assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
|
1573
|
+
end
|
1574
|
+
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1575
|
+
assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
|
1576
|
+
end
|
1577
|
+
end
|
1873
1578
|
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
end
|
1878
|
-
test 'stored data is converted' do
|
1879
|
-
assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
|
1880
|
-
end
|
1881
|
-
test 'By simple query, charset of retrieved data is connection charset' do
|
1882
|
-
assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
|
1883
|
-
end
|
1884
|
-
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1885
|
-
assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
|
1886
|
-
end
|
1579
|
+
sub_test_case 'The encoding of data are different from charset of column:' do
|
1580
|
+
setup do
|
1581
|
+
@m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @utf8, @utf8, @utf8
|
1887
1582
|
end
|
1583
|
+
test 'stored data is converted' do
|
1584
|
+
assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
|
1585
|
+
end
|
1586
|
+
test 'By simple query, charset of retrieved data is connection charset' do
|
1587
|
+
assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
|
1588
|
+
end
|
1589
|
+
test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
|
1590
|
+
assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
|
1591
|
+
end
|
1592
|
+
end
|
1888
1593
|
|
1889
|
-
|
1890
|
-
|
1891
|
-
|
1892
|
-
|
1893
|
-
|
1894
|
-
end
|
1594
|
+
sub_test_case 'The data include invalid byte code:' do
|
1595
|
+
test 'raises Encoding::InvalidByteSequenceError' do
|
1596
|
+
cp932 = "\x01\xFF\x80".force_encoding("CP932")
|
1597
|
+
assert_raise Encoding::InvalidByteSequenceError do
|
1598
|
+
@m.prepare("insert into t (cp932) values (?)").execute cp932
|
1895
1599
|
end
|
1896
1600
|
end
|
1897
1601
|
end
|