mysql2 0.2.24 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1 -0
- data/LICENSE +21 -0
- data/README.md +237 -85
- data/ext/mysql2/client.c +582 -249
- data/ext/mysql2/client.h +10 -38
- data/ext/mysql2/extconf.rb +217 -66
- data/ext/mysql2/infile.c +2 -2
- data/ext/mysql2/mysql2_ext.c +9 -2
- data/ext/mysql2/mysql2_ext.h +13 -14
- data/ext/mysql2/mysql_enc_name_to_ruby.h +62 -58
- data/ext/mysql2/mysql_enc_to_ruby.h +82 -18
- data/ext/mysql2/result.c +736 -200
- data/ext/mysql2/result.h +13 -6
- data/ext/mysql2/statement.c +612 -0
- data/ext/mysql2/statement.h +17 -0
- data/ext/mysql2/wait_for_single_fd.h +2 -1
- data/lib/mysql2/client.rb +110 -28
- data/lib/mysql2/console.rb +1 -1
- data/lib/mysql2/em.rb +15 -9
- data/lib/mysql2/error.rb +57 -36
- data/lib/mysql2/field.rb +3 -0
- data/lib/mysql2/result.rb +2 -0
- data/lib/mysql2/statement.rb +9 -0
- data/lib/mysql2/version.rb +1 -1
- data/lib/mysql2.rb +66 -15
- data/support/3A79BD29.asc +49 -0
- data/support/5072E1F5.asc +432 -0
- data/support/libmysql.def +219 -0
- data/support/mysql_enc_to_ruby.rb +15 -11
- data/support/ruby_enc_to_mysql.rb +8 -6
- metadata +30 -94
- data/MIT-LICENSE +0 -20
- data/examples/eventmachine.rb +0 -21
- data/examples/threaded.rb +0 -20
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -635
- data/lib/arel/engines/sql/compilers/mysql2_compiler.rb +0 -11
- data/spec/configuration.yml.example +0 -17
- data/spec/em/em_spec.rb +0 -114
- data/spec/my.cnf.example +0 -9
- data/spec/mysql2/client_spec.rb +0 -897
- data/spec/mysql2/error_spec.rb +0 -83
- data/spec/mysql2/result_spec.rb +0 -505
- data/spec/rcov.opts +0 -3
- data/spec/spec_helper.rb +0 -87
- data/spec/test_data +0 -1
data/spec/mysql2/error_spec.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Mysql2::Error do
|
6
|
-
let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
|
7
|
-
|
8
|
-
let :error do
|
9
|
-
begin
|
10
|
-
client.query("HAHAHA")
|
11
|
-
rescue Mysql2::Error => e
|
12
|
-
error = e
|
13
|
-
ensure
|
14
|
-
client.close
|
15
|
-
end
|
16
|
-
|
17
|
-
error
|
18
|
-
end
|
19
|
-
|
20
|
-
it "responds to error_number and sql_state, with aliases" do
|
21
|
-
error.should respond_to(:error_number)
|
22
|
-
error.should respond_to(:sql_state)
|
23
|
-
|
24
|
-
# Mysql gem compatibility
|
25
|
-
error.should respond_to(:errno)
|
26
|
-
error.should respond_to(:error)
|
27
|
-
end
|
28
|
-
|
29
|
-
if "".respond_to? :encoding
|
30
|
-
let :error do
|
31
|
-
client = Mysql2::Client.new(DatabaseCredentials['root'])
|
32
|
-
begin
|
33
|
-
client.query("\xE9\x80\xA0\xE5\xAD\x97")
|
34
|
-
rescue Mysql2::Error => e
|
35
|
-
error = e
|
36
|
-
ensure
|
37
|
-
client.close
|
38
|
-
end
|
39
|
-
|
40
|
-
error
|
41
|
-
end
|
42
|
-
|
43
|
-
let :bad_err do
|
44
|
-
client = Mysql2::Client.new(DatabaseCredentials['root'])
|
45
|
-
begin
|
46
|
-
client.query("\xE5\xC6\x7D\x1F")
|
47
|
-
rescue Mysql2::Error => e
|
48
|
-
error = e
|
49
|
-
ensure
|
50
|
-
client.close
|
51
|
-
end
|
52
|
-
|
53
|
-
error
|
54
|
-
end
|
55
|
-
|
56
|
-
it "returns error messages as UTF-8 by default" do
|
57
|
-
with_internal_encoding nil do
|
58
|
-
error.message.encoding.should eql(Encoding::UTF_8)
|
59
|
-
error.message.valid_encoding?
|
60
|
-
|
61
|
-
bad_err.message.encoding.should eql(Encoding::UTF_8)
|
62
|
-
bad_err.message.valid_encoding?
|
63
|
-
|
64
|
-
bad_err.message.should include("??}\u001F")
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
it "returns sql state as ASCII" do
|
69
|
-
error.sql_state.encoding.should eql(Encoding::US_ASCII)
|
70
|
-
error.sql_state.valid_encoding?
|
71
|
-
end
|
72
|
-
|
73
|
-
it "returns error messages and sql state in Encoding.default_internal if set" do
|
74
|
-
with_internal_encoding 'UTF-16LE' do
|
75
|
-
error.message.encoding.should eql(Encoding.default_internal)
|
76
|
-
error.message.valid_encoding?
|
77
|
-
|
78
|
-
bad_err.message.encoding.should eql(Encoding.default_internal)
|
79
|
-
bad_err.message.valid_encoding?
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
data/spec/mysql2/result_spec.rb
DELETED
@@ -1,505 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe Mysql2::Result do
|
5
|
-
before(:each) do
|
6
|
-
@result = @client.query "SELECT 1"
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should have included Enumerable" do
|
10
|
-
Mysql2::Result.ancestors.include?(Enumerable).should be_true
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should respond to #each" do
|
14
|
-
@result.should respond_to(:each)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should raise a Mysql2::Error exception upon a bad query" do
|
18
|
-
lambda {
|
19
|
-
@client.query "bad sql"
|
20
|
-
}.should raise_error(Mysql2::Error)
|
21
|
-
|
22
|
-
lambda {
|
23
|
-
@client.query "SELECT 1"
|
24
|
-
}.should_not raise_error(Mysql2::Error)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should respond to #count, which is aliased as #size" do
|
28
|
-
r = @client.query "SELECT 1"
|
29
|
-
r.should respond_to :count
|
30
|
-
r.should respond_to :size
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should be able to return the number of rows in the result set" do
|
34
|
-
r = @client.query "SELECT 1"
|
35
|
-
r.count.should eql(1)
|
36
|
-
r.size.should eql(1)
|
37
|
-
end
|
38
|
-
|
39
|
-
context "metadata queries" do
|
40
|
-
it "should show tables" do
|
41
|
-
@result = @client.query "SHOW TABLES"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "#each" do
|
46
|
-
it "should yield rows as hash's" do
|
47
|
-
@result.each do |row|
|
48
|
-
row.class.should eql(Hash)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
|
53
|
-
@result.each(:symbolize_keys => true) do |row|
|
54
|
-
row.keys.first.class.should eql(Symbol)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should be able to return results as an array" do
|
59
|
-
@result.each(:as => :array) do |row|
|
60
|
-
row.class.should eql(Array)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should cache previously yielded results by default" do
|
65
|
-
@result.first.object_id.should eql(@result.first.object_id)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should not cache previously yielded results if cache_rows is disabled" do
|
69
|
-
result = @client.query "SELECT 1", :cache_rows => false
|
70
|
-
result.first.object_id.should_not eql(result.first.object_id)
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should yield different value for #first if streaming" do
|
74
|
-
result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
|
75
|
-
result.first.should_not eql(result.first)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should yield the same value for #first if streaming is disabled" do
|
79
|
-
result = @client.query "SELECT 1 UNION SELECT 2", :stream => false
|
80
|
-
result.first.should eql(result.first)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should throw an exception if we try to iterate twice when streaming is enabled" do
|
84
|
-
result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
|
85
|
-
|
86
|
-
expect {
|
87
|
-
result.each.to_a
|
88
|
-
result.each.to_a
|
89
|
-
}.to raise_exception(Mysql2::Error)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context "#fields" do
|
94
|
-
before(:each) do
|
95
|
-
@test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
|
96
|
-
end
|
97
|
-
|
98
|
-
it "method should exist" do
|
99
|
-
@test_result.should respond_to(:fields)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should return an array of field names in proper order" do
|
103
|
-
result = @client.query "SELECT 'a', 'b', 'c'"
|
104
|
-
result.fields.should eql(['a', 'b', 'c'])
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
context "streaming" do
|
109
|
-
it "should maintain a count while streaming" do
|
110
|
-
result = @client.query('SELECT 1')
|
111
|
-
|
112
|
-
result.count.should eql(1)
|
113
|
-
result.each.to_a
|
114
|
-
result.count.should eql(1)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should set the actual count of rows after streaming" do
|
118
|
-
result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
|
119
|
-
result.count.should eql(0)
|
120
|
-
result.each {|r| }
|
121
|
-
result.count.should eql(1)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "should not yield nil at the end of streaming" do
|
125
|
-
result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
|
126
|
-
result.each { |r| r.should_not be_nil}
|
127
|
-
end
|
128
|
-
|
129
|
-
it "#count should be zero for rows after streaming when there were no results" do
|
130
|
-
result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
|
131
|
-
result.count.should eql(0)
|
132
|
-
result.each.to_a
|
133
|
-
result.count.should eql(0)
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should raise an exception if streaming ended due to a timeout" do
|
137
|
-
# Create an extra client instance, since we're going to time it out
|
138
|
-
client = Mysql2::Client.new DatabaseCredentials['root']
|
139
|
-
client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255))"
|
140
|
-
|
141
|
-
# Insert enough records to force the result set into multiple reads
|
142
|
-
# (the BINARY type is used simply because it forces full width results)
|
143
|
-
10000.times do |i|
|
144
|
-
client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
|
145
|
-
end
|
146
|
-
|
147
|
-
client.query "SET net_write_timeout = 1"
|
148
|
-
res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
|
149
|
-
|
150
|
-
lambda {
|
151
|
-
res.each_with_index do |row, i|
|
152
|
-
# Exhaust the first result packet then trigger a timeout
|
153
|
-
sleep 2 if i > 0 && i % 1000 == 0
|
154
|
-
end
|
155
|
-
}.should raise_error(Mysql2::Error, /Lost connection/)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
context "row data type mapping" do
|
160
|
-
before(:each) do
|
161
|
-
@test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
162
|
-
end
|
163
|
-
|
164
|
-
it "should return nil values for NULL and strings for everything else when :cast is false" do
|
165
|
-
result = @client.query('SELECT null_test, tiny_int_test, bool_cast_test, int_test, date_test, enum_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast => false).first
|
166
|
-
result["null_test"].should be_nil
|
167
|
-
result["tiny_int_test"].should eql("1")
|
168
|
-
result["bool_cast_test"].should eql("1")
|
169
|
-
result["int_test"].should eql("10")
|
170
|
-
result["date_test"].should eql("2010-04-04")
|
171
|
-
result["enum_test"].should eql("val1")
|
172
|
-
end
|
173
|
-
|
174
|
-
it "should return nil for a NULL value" do
|
175
|
-
@test_result['null_test'].class.should eql(NilClass)
|
176
|
-
@test_result['null_test'].should eql(nil)
|
177
|
-
end
|
178
|
-
|
179
|
-
it "should return String for a BIT(64) value" do
|
180
|
-
@test_result['bit_test'].class.should eql(String)
|
181
|
-
@test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should return String for a BIT(1) value" do
|
185
|
-
@test_result['single_bit_test'].class.should eql(String)
|
186
|
-
@test_result['single_bit_test'].should eql("\001")
|
187
|
-
end
|
188
|
-
|
189
|
-
it "should return Fixnum for a TINYINT value" do
|
190
|
-
[Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
|
191
|
-
@test_result['tiny_int_test'].should eql(1)
|
192
|
-
end
|
193
|
-
|
194
|
-
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
|
195
|
-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
|
196
|
-
id1 = @client.last_id
|
197
|
-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
|
198
|
-
id2 = @client.last_id
|
199
|
-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
|
200
|
-
id3 = @client.last_id
|
201
|
-
|
202
|
-
result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
|
203
|
-
result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
|
204
|
-
result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
|
205
|
-
result1.first['bool_cast_test'].should be_true
|
206
|
-
result2.first['bool_cast_test'].should be_false
|
207
|
-
result3.first['bool_cast_test'].should be_true
|
208
|
-
|
209
|
-
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
210
|
-
end
|
211
|
-
|
212
|
-
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
|
213
|
-
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
|
214
|
-
id1 = @client.last_id
|
215
|
-
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
|
216
|
-
id2 = @client.last_id
|
217
|
-
|
218
|
-
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
|
219
|
-
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
|
220
|
-
result1.first['single_bit_test'].should be_true
|
221
|
-
result2.first['single_bit_test'].should be_false
|
222
|
-
|
223
|
-
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
|
224
|
-
end
|
225
|
-
|
226
|
-
it "should return Fixnum for a SMALLINT value" do
|
227
|
-
[Fixnum, Bignum].should include(@test_result['small_int_test'].class)
|
228
|
-
@test_result['small_int_test'].should eql(10)
|
229
|
-
end
|
230
|
-
|
231
|
-
it "should return Fixnum for a MEDIUMINT value" do
|
232
|
-
[Fixnum, Bignum].should include(@test_result['medium_int_test'].class)
|
233
|
-
@test_result['medium_int_test'].should eql(10)
|
234
|
-
end
|
235
|
-
|
236
|
-
it "should return Fixnum for an INT value" do
|
237
|
-
[Fixnum, Bignum].should include(@test_result['int_test'].class)
|
238
|
-
@test_result['int_test'].should eql(10)
|
239
|
-
end
|
240
|
-
|
241
|
-
it "should return Fixnum for a BIGINT value" do
|
242
|
-
[Fixnum, Bignum].should include(@test_result['big_int_test'].class)
|
243
|
-
@test_result['big_int_test'].should eql(10)
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should return Fixnum for a YEAR value" do
|
247
|
-
[Fixnum, Bignum].should include(@test_result['year_test'].class)
|
248
|
-
@test_result['year_test'].should eql(2009)
|
249
|
-
end
|
250
|
-
|
251
|
-
it "should return BigDecimal for a DECIMAL value" do
|
252
|
-
@test_result['decimal_test'].class.should eql(BigDecimal)
|
253
|
-
@test_result['decimal_test'].should eql(10.3)
|
254
|
-
end
|
255
|
-
|
256
|
-
it "should return Float for a FLOAT value" do
|
257
|
-
@test_result['float_test'].class.should eql(Float)
|
258
|
-
@test_result['float_test'].should eql(10.3)
|
259
|
-
end
|
260
|
-
|
261
|
-
it "should return Float for a DOUBLE value" do
|
262
|
-
@test_result['double_test'].class.should eql(Float)
|
263
|
-
@test_result['double_test'].should eql(10.3)
|
264
|
-
end
|
265
|
-
|
266
|
-
it "should return Time for a DATETIME value when within the supported range" do
|
267
|
-
@test_result['date_time_test'].class.should eql(Time)
|
268
|
-
@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
|
269
|
-
end
|
270
|
-
|
271
|
-
if 1.size == 4 # 32bit
|
272
|
-
unless RUBY_VERSION =~ /1.8/
|
273
|
-
klass = Time
|
274
|
-
else
|
275
|
-
klass = DateTime
|
276
|
-
end
|
277
|
-
|
278
|
-
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
|
279
|
-
# 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
|
280
|
-
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
281
|
-
r.first['test'].class.should eql(klass)
|
282
|
-
end
|
283
|
-
|
284
|
-
it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
|
285
|
-
# 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
|
286
|
-
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
287
|
-
r.first['test'].class.should eql(klass)
|
288
|
-
end
|
289
|
-
elsif 1.size == 8 # 64bit
|
290
|
-
unless RUBY_VERSION =~ /1.8/
|
291
|
-
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
|
292
|
-
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
293
|
-
r.first['test'].class.should eql(Time)
|
294
|
-
end
|
295
|
-
|
296
|
-
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
297
|
-
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
298
|
-
r.first['test'].class.should eql(Time)
|
299
|
-
end
|
300
|
-
else
|
301
|
-
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
|
302
|
-
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
|
303
|
-
r.first['test'].class.should eql(Time)
|
304
|
-
end
|
305
|
-
|
306
|
-
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
|
307
|
-
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
|
308
|
-
r.first['test'].class.should eql(DateTime)
|
309
|
-
end
|
310
|
-
|
311
|
-
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
312
|
-
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
313
|
-
r.first['test'].class.should eql(Time)
|
314
|
-
end
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
it "should return Time for a TIMESTAMP value when within the supported range" do
|
319
|
-
@test_result['timestamp_test'].class.should eql(Time)
|
320
|
-
@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
|
321
|
-
end
|
322
|
-
|
323
|
-
it "should return Time for a TIME value" do
|
324
|
-
@test_result['time_test'].class.should eql(Time)
|
325
|
-
@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2000-01-01 11:44:00')
|
326
|
-
end
|
327
|
-
|
328
|
-
it "should return Date for a DATE value" do
|
329
|
-
@test_result['date_test'].class.should eql(Date)
|
330
|
-
@test_result['date_test'].strftime("%Y-%m-%d").should eql('2010-04-04')
|
331
|
-
end
|
332
|
-
|
333
|
-
it "should return String for an ENUM value" do
|
334
|
-
@test_result['enum_test'].class.should eql(String)
|
335
|
-
@test_result['enum_test'].should eql('val1')
|
336
|
-
end
|
337
|
-
|
338
|
-
it "should raise an error given an invalid DATETIME" do
|
339
|
-
begin
|
340
|
-
@client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each
|
341
|
-
rescue Mysql2::Error => e
|
342
|
-
error = e
|
343
|
-
end
|
344
|
-
|
345
|
-
error.message.should eql("Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
|
346
|
-
end
|
347
|
-
|
348
|
-
if defined? Encoding
|
349
|
-
context "string encoding for ENUM values" do
|
350
|
-
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
351
|
-
with_internal_encoding nil do
|
352
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
353
|
-
result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
|
354
|
-
|
355
|
-
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
356
|
-
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
357
|
-
result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
|
358
|
-
client2.close
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
it "should use Encoding.default_internal" do
|
363
|
-
with_internal_encoding 'utf-8' do
|
364
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
365
|
-
result['enum_test'].encoding.should eql(Encoding.default_internal)
|
366
|
-
end
|
367
|
-
|
368
|
-
with_internal_encoding 'us-ascii' do
|
369
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
370
|
-
result['enum_test'].encoding.should eql(Encoding.default_internal)
|
371
|
-
end
|
372
|
-
end
|
373
|
-
end
|
374
|
-
end
|
375
|
-
|
376
|
-
it "should return String for a SET value" do
|
377
|
-
@test_result['set_test'].class.should eql(String)
|
378
|
-
@test_result['set_test'].should eql('val1,val2')
|
379
|
-
end
|
380
|
-
|
381
|
-
if defined? Encoding
|
382
|
-
context "string encoding for SET values" do
|
383
|
-
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
384
|
-
with_internal_encoding nil do
|
385
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
386
|
-
result['set_test'].encoding.should eql(Encoding.find('utf-8'))
|
387
|
-
|
388
|
-
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
389
|
-
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
390
|
-
result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
|
391
|
-
client2.close
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
it "should use Encoding.default_internal" do
|
396
|
-
with_internal_encoding 'utf-8' do
|
397
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
398
|
-
result['set_test'].encoding.should eql(Encoding.default_internal)
|
399
|
-
end
|
400
|
-
|
401
|
-
with_internal_encoding 'us-ascii' do
|
402
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
403
|
-
result['set_test'].encoding.should eql(Encoding.default_internal)
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
407
|
-
end
|
408
|
-
|
409
|
-
it "should return String for a BINARY value" do
|
410
|
-
@test_result['binary_test'].class.should eql(String)
|
411
|
-
@test_result['binary_test'].should eql("test#{"\000"*6}")
|
412
|
-
end
|
413
|
-
|
414
|
-
if defined? Encoding
|
415
|
-
context "string encoding for BINARY values" do
|
416
|
-
it "should default to binary if Encoding.default_internal is nil" do
|
417
|
-
with_internal_encoding nil do
|
418
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
419
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
420
|
-
end
|
421
|
-
end
|
422
|
-
|
423
|
-
it "should not use Encoding.default_internal" do
|
424
|
-
with_internal_encoding 'utf-8' do
|
425
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
426
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
427
|
-
end
|
428
|
-
|
429
|
-
with_internal_encoding 'us-ascii' do
|
430
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
431
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
432
|
-
end
|
433
|
-
end
|
434
|
-
end
|
435
|
-
end
|
436
|
-
|
437
|
-
{
|
438
|
-
'char_test' => 'CHAR',
|
439
|
-
'varchar_test' => 'VARCHAR',
|
440
|
-
'varbinary_test' => 'VARBINARY',
|
441
|
-
'tiny_blob_test' => 'TINYBLOB',
|
442
|
-
'tiny_text_test' => 'TINYTEXT',
|
443
|
-
'blob_test' => 'BLOB',
|
444
|
-
'text_test' => 'TEXT',
|
445
|
-
'medium_blob_test' => 'MEDIUMBLOB',
|
446
|
-
'medium_text_test' => 'MEDIUMTEXT',
|
447
|
-
'long_blob_test' => 'LONGBLOB',
|
448
|
-
'long_text_test' => 'LONGTEXT'
|
449
|
-
}.each do |field, type|
|
450
|
-
it "should return a String for #{type}" do
|
451
|
-
@test_result[field].class.should eql(String)
|
452
|
-
@test_result[field].should eql("test")
|
453
|
-
end
|
454
|
-
|
455
|
-
if defined? Encoding
|
456
|
-
context "string encoding for #{type} values" do
|
457
|
-
if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
|
458
|
-
it "should default to binary if Encoding.default_internal is nil" do
|
459
|
-
with_internal_encoding nil do
|
460
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
461
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
it "should not use Encoding.default_internal" do
|
466
|
-
with_internal_encoding 'utf-8' do
|
467
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
468
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
469
|
-
end
|
470
|
-
|
471
|
-
with_internal_encoding 'us-ascii' do
|
472
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
473
|
-
result['binary_test'].encoding.should eql(Encoding.find('binary'))
|
474
|
-
end
|
475
|
-
end
|
476
|
-
else
|
477
|
-
it "should default to utf-8 if Encoding.default_internal is nil" do
|
478
|
-
with_internal_encoding nil do
|
479
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
480
|
-
result[field].encoding.should eql(Encoding.find('utf-8'))
|
481
|
-
|
482
|
-
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
|
483
|
-
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
484
|
-
result[field].encoding.should eql(Encoding.find('us-ascii'))
|
485
|
-
client2.close
|
486
|
-
end
|
487
|
-
end
|
488
|
-
|
489
|
-
it "should use Encoding.default_internal" do
|
490
|
-
with_internal_encoding 'utf-8' do
|
491
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
492
|
-
result[field].encoding.should eql(Encoding.default_internal)
|
493
|
-
end
|
494
|
-
|
495
|
-
with_internal_encoding 'us-ascii' do
|
496
|
-
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
497
|
-
result[field].encoding.should eql(Encoding.default_internal)
|
498
|
-
end
|
499
|
-
end
|
500
|
-
end
|
501
|
-
end
|
502
|
-
end
|
503
|
-
end
|
504
|
-
end
|
505
|
-
end
|
data/spec/rcov.opts
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'rspec'
|
4
|
-
require 'mysql2'
|
5
|
-
require 'timeout'
|
6
|
-
require 'yaml'
|
7
|
-
DatabaseCredentials = YAML.load_file('spec/configuration.yml')
|
8
|
-
|
9
|
-
RSpec.configure do |config|
|
10
|
-
def with_internal_encoding(encoding)
|
11
|
-
old_enc = Encoding.default_internal
|
12
|
-
Encoding.default_internal = encoding
|
13
|
-
|
14
|
-
yield
|
15
|
-
ensure
|
16
|
-
Encoding.default_internal = old_enc
|
17
|
-
end
|
18
|
-
|
19
|
-
config.before :each do
|
20
|
-
@client = Mysql2::Client.new DatabaseCredentials['root']
|
21
|
-
end
|
22
|
-
|
23
|
-
config.after :each do
|
24
|
-
@client.close
|
25
|
-
end
|
26
|
-
|
27
|
-
config.before(:all) do
|
28
|
-
client = Mysql2::Client.new DatabaseCredentials['root']
|
29
|
-
client.query %[
|
30
|
-
CREATE TABLE IF NOT EXISTS mysql2_test (
|
31
|
-
id MEDIUMINT NOT NULL AUTO_INCREMENT,
|
32
|
-
null_test VARCHAR(10),
|
33
|
-
bit_test BIT(64),
|
34
|
-
single_bit_test BIT(1),
|
35
|
-
tiny_int_test TINYINT,
|
36
|
-
bool_cast_test TINYINT(1),
|
37
|
-
small_int_test SMALLINT,
|
38
|
-
medium_int_test MEDIUMINT,
|
39
|
-
int_test INT,
|
40
|
-
big_int_test BIGINT,
|
41
|
-
float_test FLOAT(10,3),
|
42
|
-
float_zero_test FLOAT(10,3),
|
43
|
-
double_test DOUBLE(10,3),
|
44
|
-
decimal_test DECIMAL(10,3),
|
45
|
-
decimal_zero_test DECIMAL(10,3),
|
46
|
-
date_test DATE,
|
47
|
-
date_time_test DATETIME,
|
48
|
-
timestamp_test TIMESTAMP,
|
49
|
-
time_test TIME,
|
50
|
-
year_test YEAR(4),
|
51
|
-
char_test CHAR(10),
|
52
|
-
varchar_test VARCHAR(10),
|
53
|
-
binary_test BINARY(10),
|
54
|
-
varbinary_test VARBINARY(10),
|
55
|
-
tiny_blob_test TINYBLOB,
|
56
|
-
tiny_text_test TINYTEXT,
|
57
|
-
blob_test BLOB,
|
58
|
-
text_test TEXT,
|
59
|
-
medium_blob_test MEDIUMBLOB,
|
60
|
-
medium_text_test MEDIUMTEXT,
|
61
|
-
long_blob_test LONGBLOB,
|
62
|
-
long_text_test LONGTEXT,
|
63
|
-
enum_test ENUM('val1', 'val2'),
|
64
|
-
set_test SET('val1', 'val2'),
|
65
|
-
PRIMARY KEY (id)
|
66
|
-
)
|
67
|
-
]
|
68
|
-
client.query "DELETE FROM mysql2_test;"
|
69
|
-
client.query %[
|
70
|
-
INSERT INTO mysql2_test (
|
71
|
-
null_test, bit_test, single_bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
|
72
|
-
float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
|
73
|
-
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
|
74
|
-
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
|
75
|
-
long_blob_test, long_text_test, enum_test, set_test
|
76
|
-
)
|
77
|
-
|
78
|
-
VALUES (
|
79
|
-
NULL, b'101', b'1', 1, 1, 10, 10, 10, 10,
|
80
|
-
10.3, 0, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
|
81
|
-
2009, "test", "test", "test", "test", "test",
|
82
|
-
"test", "test", "test", "test", "test",
|
83
|
-
"test", "test", 'val1', 'val1,val2'
|
84
|
-
)
|
85
|
-
]
|
86
|
-
end
|
87
|
-
end
|
data/spec/test_data
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
\N Hello World
|