Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
53008447
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
53008447
编写于
5月 20, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15200 from sgrif/sg-type-map-mysql
Use the generic type map object for mysql field lookups
上级
f3407d13
ac4c1c3b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
39 addition
and
23 deletion
+39
-23
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
...rd/lib/active_record/connection_adapters/mysql_adapter.rb
+9
-23
activerecord/lib/active_record/connection_adapters/type.rb
activerecord/lib/active_record/connection_adapters/type.rb
+2
-0
activerecord/lib/active_record/connection_adapters/type/hash_lookup_type_map.rb
...e_record/connection_adapters/type/hash_lookup_type_map.rb
+15
-0
activerecord/test/cases/connection_adapters/type/type_map_test.rb
...cord/test/cases/connection_adapters/type/type_map_test.rb
+13
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
浏览文件 @
53008447
...
@@ -297,23 +297,17 @@ def cast_value(value)
...
@@ -297,23 +297,17 @@ def cast_value(value)
end
end
end
end
TYPES
=
{}
class
<<
self
TYPES
=
ConnectionAdapters
::
Type
::
HashLookupTypeMap
.
new
# :nodoc:
# Register an MySQL +type_id+ with a typecasting object in
delegate
:register_type
,
:alias_type
,
to: :TYPES
# +type+.
def
self
.
register_type
(
type_id
,
type
)
TYPES
[
type_id
]
=
type
end
def
self
.
alias_type
(
new
,
old
)
TYPES
[
new
]
=
TYPES
[
old
]
end
def
self
.
find_type
(
field
)
def
find_type
(
field
)
if
field
.
type
==
Mysql
::
Field
::
TYPE_TINY
&&
field
.
length
>
1
if
field
.
type
==
Mysql
::
Field
::
TYPE_TINY
&&
field
.
length
>
1
TYPES
[
Mysql
::
Field
::
TYPE_LONG
]
TYPES
.
lookup
(
Mysql
::
Field
::
TYPE_LONG
)
else
else
TYPES
.
fetch
(
field
.
type
)
{
Type
::
Value
.
new
}
TYPES
.
lookup
(
field
.
type
)
end
end
end
end
end
...
@@ -322,18 +316,10 @@ def self.find_type(field)
...
@@ -322,18 +316,10 @@ def self.find_type(field)
alias_type
Mysql
::
Field
::
TYPE_LONGLONG
,
Mysql
::
Field
::
TYPE_LONG
alias_type
Mysql
::
Field
::
TYPE_LONGLONG
,
Mysql
::
Field
::
TYPE_LONG
alias_type
Mysql
::
Field
::
TYPE_NEWDECIMAL
,
Mysql
::
Field
::
TYPE_LONG
alias_type
Mysql
::
Field
::
TYPE_NEWDECIMAL
,
Mysql
::
Field
::
TYPE_LONG
register_type
Mysql
::
Field
::
TYPE_VAR_STRING
,
Type
::
Value
.
new
register_type
Mysql
::
Field
::
TYPE_BLOB
,
Type
::
Value
.
new
register_type
Mysql
::
Field
::
TYPE_DATE
,
Type
::
Date
.
new
register_type
Mysql
::
Field
::
TYPE_DATE
,
Type
::
Date
.
new
register_type
Mysql
::
Field
::
TYPE_DATETIME
,
Fields
::
DateTime
.
new
register_type
Mysql
::
Field
::
TYPE_DATETIME
,
Fields
::
DateTime
.
new
register_type
Mysql
::
Field
::
TYPE_TIME
,
Fields
::
Time
.
new
register_type
Mysql
::
Field
::
TYPE_TIME
,
Fields
::
Time
.
new
register_type
Mysql
::
Field
::
TYPE_FLOAT
,
Type
::
Float
.
new
register_type
Mysql
::
Field
::
TYPE_FLOAT
,
Type
::
Float
.
new
Mysql
::
Field
.
constants
.
grep
(
/TYPE/
).
map
{
|
class_name
|
Mysql
::
Field
.
const_get
class_name
}.
reject
{
|
const
|
TYPES
.
key?
const
}.
each
do
|
const
|
register_type
const
,
Type
::
Value
.
new
end
end
end
def
initialize_type_map
(
m
)
# :nodoc:
def
initialize_type_map
(
m
)
# :nodoc:
...
...
activerecord/lib/active_record/connection_adapters/type.rb
浏览文件 @
53008447
...
@@ -11,7 +11,9 @@
...
@@ -11,7 +11,9 @@
require
'active_record/connection_adapters/type/string'
require
'active_record/connection_adapters/type/string'
require
'active_record/connection_adapters/type/text'
require
'active_record/connection_adapters/type/text'
require
'active_record/connection_adapters/type/time'
require
'active_record/connection_adapters/type/time'
require
'active_record/connection_adapters/type/type_map'
require
'active_record/connection_adapters/type/type_map'
require
'active_record/connection_adapters/type/hash_lookup_type_map'
module
ActiveRecord
module
ActiveRecord
module
ConnectionAdapters
module
ConnectionAdapters
...
...
activerecord/lib/active_record/connection_adapters/type/hash_lookup_type_map.rb
0 → 100644
浏览文件 @
53008447
module
ActiveRecord
module
ConnectionAdapters
module
Type
class
HashLookupTypeMap
<
TypeMap
# :nodoc:
def
lookup
(
type
)
@mapping
.
fetch
(
type
,
proc
{
default_value
}).
call
(
type
)
end
def
alias_type
(
type
,
alias_type
)
register_type
(
type
)
{
lookup
(
alias_type
)
}
end
end
end
end
end
activerecord/test/cases/connection_adapters/type/type_map_test.rb
浏览文件 @
53008447
...
@@ -95,6 +95,19 @@ def test_requires_value_or_block
...
@@ -95,6 +95,19 @@ def test_requires_value_or_block
mapping
.
register_type
(
/only key/i
)
mapping
.
register_type
(
/only key/i
)
end
end
end
end
def
test_lookup_non_strings
mapping
=
HashLookupTypeMap
.
new
mapping
.
register_type
(
1
,
'string'
)
mapping
.
register_type
(
2
,
'int'
)
mapping
.
alias_type
(
3
,
1
)
assert_equal
mapping
.
lookup
(
1
),
'string'
assert_equal
mapping
.
lookup
(
2
),
'int'
assert_equal
mapping
.
lookup
(
3
),
'string'
assert_kind_of
Type
::
Value
,
mapping
.
lookup
(
4
)
end
end
end
end
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录