1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# encoding: utf-8
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class MysqlAdapterTest < ActiveRecord::TestCase
def setup
@conn = ActiveRecord::Base.connection
@conn.exec_query('drop table if exists ex')
@conn.exec_query(<<-eosql)
CREATE TABLE `ex` (
`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
`number` integer,
`data` varchar(255))
eosql
end
def test_client_encoding
assert_equal Encoding::UTF_8, @conn.client_encoding
end
def test_exec_insert_number
insert(@conn, 'number' => 10)
result = @conn.exec_query('SELECT number FROM ex WHERE number = 10')
assert_equal 1, result.rows.length
assert_equal 10, result.rows.last.last
end
def test_exec_insert_string
str = 'いただきます!'
insert(@conn, 'number' => 10, 'data' => str)
result = @conn.exec_query('SELECT number, data FROM ex WHERE number = 10')
value = result.rows.last.last
# FIXME: this should probably be inside the mysql AR adapter?
value.force_encoding(@conn.client_encoding)
# The strings in this file are utf-8, so transcode to utf-8
value.encode!(Encoding::UTF_8)
assert_equal str, value
end
def test_tables_quoting
begin
@conn.tables(nil, "foo-bar", nil)
flunk
rescue => e
# assertion for *quoted* database properly
assert_match(/Unknown database 'foo-bar': SHOW TABLES IN `foo-bar`/, e.inspect)
end
end
private
def insert(ctx, data)
binds = data.map { |name, value|
[ctx.columns('ex').find { |x| x.name == name }, value]
}
columns = binds.map(&:first).map(&:name)
sql = "INSERT INTO ex (#{columns.join(", ")})
VALUES (#{(['?'] * columns.length).join(', ')})"
ctx.exec_insert(sql, 'SQL', binds)
end
end
end
end
|