Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
71248fc8
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,发现更多精彩内容 >>
提交
71248fc8
编写于
5月 22, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15248 from sgrif/sg-additional-type-map-args
Allow additional arguments to be used during type map lookups
上级
39473053
ecf4ad7c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
29 addition
and
12 deletion
+29
-12
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+1
-1
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+2
-2
activerecord/lib/active_record/connection_adapters/type/hash_lookup_type_map.rb
...e_record/connection_adapters/type/hash_lookup_type_map.rb
+5
-5
activerecord/lib/active_record/connection_adapters/type/type_map.rb
...rd/lib/active_record/connection_adapters/type/type_map.rb
+4
-4
activerecord/test/cases/connection_adapters/type/type_map_test.rb
...cord/test/cases/connection_adapters/type/type_map_test.rb
+17
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
71248fc8
...
...
@@ -178,7 +178,7 @@ def indexes(table_name, name = nil)
def
columns
(
table_name
)
# Limit, precision, and scale are all handled by the superclass.
column_definitions
(
table_name
).
map
do
|
column_name
,
type
,
default
,
notnull
,
oid
,
fmod
|
oid
=
get_oid_type
(
oid
.
to_i
,
fmod
.
to_i
,
column_name
)
oid
=
get_oid_type
(
oid
.
to_i
,
fmod
.
to_i
,
column_name
,
type
)
PostgreSQLColumn
.
new
(
column_name
,
default
,
oid
,
type
,
notnull
==
'f'
)
end
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
71248fc8
...
...
@@ -538,12 +538,12 @@ def translate_exception(exception, message)
private
def
get_oid_type
(
oid
,
fmod
,
column_name
)
def
get_oid_type
(
oid
,
fmod
,
column_name
,
sql_type
=
''
)
if
!
type_map
.
key?
(
oid
)
initialize_type_map
(
type_map
,
[
oid
])
end
type_map
.
fetch
(
normalize_oid_type
(
oid
,
fmod
))
{
type_map
.
fetch
(
normalize_oid_type
(
oid
,
fmod
)
,
sql_type
)
{
warn
"unknown OID
#{
oid
}
: failed to recognize type of '
#{
column_name
}
'. It will be treated as String."
Type
::
Value
.
new
.
tap
do
|
cast_type
|
type_map
.
register_type
(
oid
,
cast_type
)
...
...
activerecord/lib/active_record/connection_adapters/type/hash_lookup_type_map.rb
浏览文件 @
71248fc8
...
...
@@ -4,16 +4,16 @@ module Type
class
HashLookupTypeMap
<
TypeMap
# :nodoc:
delegate
:key?
,
to: :@mapping
def
lookup
(
type
)
@mapping
.
fetch
(
type
,
proc
{
default_value
}).
call
(
type
)
def
lookup
(
type
,
*
args
)
@mapping
.
fetch
(
type
,
proc
{
default_value
}).
call
(
type
,
*
args
)
end
def
fetch
(
type
,
&
block
)
@mapping
.
fetch
(
type
,
block
).
call
(
type
)
def
fetch
(
type
,
*
args
,
&
block
)
@mapping
.
fetch
(
type
,
block
).
call
(
type
,
*
args
)
end
def
alias_type
(
type
,
alias_type
)
register_type
(
type
)
{
lookup
(
alias_type
)
}
register_type
(
type
)
{
|
_
,
*
args
|
lookup
(
alias_type
,
*
args
)
}
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/type/type_map.rb
浏览文件 @
71248fc8
...
...
@@ -6,13 +6,13 @@ def initialize
@mapping
=
{}
end
def
lookup
(
lookup_key
)
def
lookup
(
lookup_key
,
*
args
)
matching_pair
=
@mapping
.
reverse_each
.
detect
do
|
key
,
_
|
key
===
lookup_key
end
if
matching_pair
matching_pair
.
last
.
call
(
lookup_key
)
matching_pair
.
last
.
call
(
lookup_key
,
*
args
)
else
default_value
end
...
...
@@ -29,9 +29,9 @@ def register_type(key, value = nil, &block)
end
def
alias_type
(
key
,
target_key
)
register_type
(
key
)
do
|
sql_type
|
register_type
(
key
)
do
|
sql_type
,
*
args
|
metadata
=
sql_type
[
/\(.*\)/
,
0
]
lookup
(
"
#{
target_key
}#{
metadata
}
"
)
lookup
(
"
#{
target_key
}#{
metadata
}
"
,
*
args
)
end
end
...
...
activerecord/test/cases/connection_adapters/type/type_map_test.rb
浏览文件 @
71248fc8
...
...
@@ -88,6 +88,23 @@ def test_register_proc
assert_equal
mapping
.
lookup
(
'varchar'
),
binary
end
def
test_additional_lookup_args
mapping
=
TypeMap
.
new
mapping
.
register_type
(
/varchar/i
)
do
|
type
,
limit
|
if
limit
>
255
'text'
else
'string'
end
end
mapping
.
alias_type
(
/string/i
,
'varchar'
)
assert_equal
mapping
.
lookup
(
'varchar'
,
200
),
'string'
assert_equal
mapping
.
lookup
(
'varchar'
,
400
),
'text'
assert_equal
mapping
.
lookup
(
'string'
,
400
),
'text'
end
def
test_requires_value_or_block
mapping
=
TypeMap
.
new
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录