Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e2763b48
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,发现更多精彩内容 >>
提交
e2763b48
编写于
7月 03, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use the type object for type casting HStore columns
上级
03c9c0e2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
39 addition
and
66 deletion
+39
-66
activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
.../lib/active_record/connection_adapters/postgresql/cast.rb
+0
-43
activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb
...ctive_record/connection_adapters/postgresql/oid/hstore.rb
+34
-2
activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
...b/active_record/connection_adapters/postgresql/quoting.rb
+0
-10
activerecord/test/cases/adapters/postgresql/hstore_test.rb
activerecord/test/cases/adapters/postgresql/hstore_test.rb
+5
-11
未找到文件。
activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
浏览文件 @
e2763b48
...
...
@@ -2,54 +2,11 @@ module ActiveRecord
module
ConnectionAdapters
module
PostgreSQL
module
Cast
# :nodoc:
def
hstore_to_string
(
object
)
# :nodoc:
if
Hash
===
object
string
=
object
.
map
{
|
k
,
v
|
"
#{
escape_hstore
(
k
)
}
=>
#{
escape_hstore
(
v
)
}
"
}.
join
(
', '
)
string
else
object
end
end
def
string_to_hstore
(
string
)
# :nodoc:
if
string
.
nil?
nil
elsif
String
===
string
Hash
[
string
.
scan
(
HstorePair
).
map
{
|
k
,
v
|
v
=
v
.
upcase
==
'NULL'
?
nil
:
v
.
gsub
(
/\A"(.*)"\Z/m
,
'\1'
).
gsub
(
/\\(.)/
,
'\1'
)
k
=
k
.
gsub
(
/\A"(.*)"\Z/m
,
'\1'
).
gsub
(
/\\(.)/
,
'\1'
)
[
k
,
v
]
}]
else
string
end
end
def
range_to_string
(
object
)
# :nodoc:
from
=
object
.
begin
.
respond_to?
(
:infinite?
)
&&
object
.
begin
.
infinite?
?
''
:
object
.
begin
to
=
object
.
end
.
respond_to?
(
:infinite?
)
&&
object
.
end
.
infinite?
?
''
:
object
.
end
"[
#{
from
}
,
#{
to
}#{
object
.
exclude_end?
?
')'
:
']'
}
"
end
private
HstorePair
=
begin
quoted_string
=
/"[^"\\]*(?:\\.[^"\\]*)*"/
unquoted_string
=
/(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
/(
#{
quoted_string
}
|
#{
unquoted_string
}
)\s*=>\s*(
#{
quoted_string
}
|
#{
unquoted_string
}
)/
end
def
escape_hstore
(
value
)
if
value
.
nil?
'NULL'
else
if
value
==
""
'""'
else
'"%s"'
%
value
.
to_s
.
gsub
(
/(["\\])/
,
'\\\\\1'
)
end
end
end
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb
浏览文件 @
e2763b48
...
...
@@ -10,16 +10,48 @@ def type
end
def
type_cast_from_database
(
value
)
ConnectionAdapters
::
PostgreSQLColumn
.
string_to_hstore
(
value
)
if
value
.
is_a?
(
::
String
)
::
Hash
[
value
.
scan
(
HstorePair
).
map
{
|
k
,
v
|
v
=
v
.
upcase
==
'NULL'
?
nil
:
v
.
gsub
(
/\A"(.*)"\Z/m
,
'\1'
).
gsub
(
/\\(.)/
,
'\1'
)
k
=
k
.
gsub
(
/\A"(.*)"\Z/m
,
'\1'
).
gsub
(
/\\(.)/
,
'\1'
)
[
k
,
v
]
}]
else
value
end
end
def
type_cast_for_database
(
value
)
ConnectionAdapters
::
PostgreSQLColumn
.
hstore_to_string
(
value
)
if
value
.
is_a?
(
::
Hash
)
value
.
map
{
|
k
,
v
|
"
#{
escape_hstore
(
k
)
}
=>
#{
escape_hstore
(
v
)
}
"
}.
join
(
', '
)
else
value
end
end
def
accessor
ActiveRecord
::
Store
::
StringKeyedHashAccessor
end
private
HstorePair
=
begin
quoted_string
=
/"[^"\\]*(?:\\.[^"\\]*)*"/
unquoted_string
=
/(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
/(
#{
quoted_string
}
|
#{
unquoted_string
}
)\s*=>\s*(
#{
quoted_string
}
|
#{
unquoted_string
}
)/
end
def
escape_hstore
(
value
)
if
value
.
nil?
'NULL'
else
if
value
==
""
'""'
else
'"%s"'
%
value
.
to_s
.
gsub
(
/(["\\])/
,
'\\\\\1'
)
end
end
end
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
浏览文件 @
e2763b48
...
...
@@ -28,11 +28,6 @@ def quote(value, column = nil) #:nodoc:
else
super
end
when
Hash
case
sql_type
when
'hstore'
then
super
(
PostgreSQLColumn
.
hstore_to_string
(
value
),
column
)
else
super
end
when
Float
if
value
.
infinite?
&&
column
.
type
==
:datetime
"'
#{
value
.
to_s
.
downcase
}
'"
...
...
@@ -80,11 +75,6 @@ def type_cast(value, column)
else
super
end
when
Hash
case
column
.
sql_type
when
'hstore'
then
PostgreSQLColumn
.
hstore_to_string
(
value
)
else
super
end
else
super
end
...
...
activerecord/test/cases/adapters/postgresql/hstore_test.rb
浏览文件 @
e2763b48
...
...
@@ -112,13 +112,7 @@ def test_cast_value_on_write
end
def
test_type_cast_hstore
assert
@column
data
=
"
\"
1
\"
=>
\"
2
\"
"
hash
=
@column
.
class
.
string_to_hstore
data
assert_equal
({
'1'
=>
'2'
},
hash
)
assert_equal
({
'1'
=>
'2'
},
@column
.
type_cast_from_database
(
data
))
assert_equal
({
'1'
=>
'2'
},
@column
.
type_cast_from_database
(
"
\"
1
\"
=>
\"
2
\"
"
))
assert_equal
({},
@column
.
type_cast_from_database
(
""
))
assert_equal
({
'key'
=>
nil
},
@column
.
type_cast_from_database
(
'key => NULL'
))
assert_equal
({
'c'
=>
'}'
,
'"a"'
=>
'b "a b'
},
@column
.
type_cast_from_database
(
%q(c=>"}", "\"a\""=>"b \"a b")
))
...
...
@@ -173,19 +167,19 @@ def test_changes_in_place
end
def
test_gen1
assert_equal
(
%q(" "=>"")
,
@column
.
c
lass
.
hstore_to_string
({
' '
=>
''
}))
assert_equal
(
%q(" "=>"")
,
@column
.
c
ast_type
.
type_cast_for_database
({
' '
=>
''
}))
end
def
test_gen2
assert_equal
(
%q(","=>"")
,
@column
.
c
lass
.
hstore_to_string
({
','
=>
''
}))
assert_equal
(
%q(","=>"")
,
@column
.
c
ast_type
.
type_cast_for_database
({
','
=>
''
}))
end
def
test_gen3
assert_equal
(
%q("="=>"")
,
@column
.
c
lass
.
hstore_to_string
({
'='
=>
''
}))
assert_equal
(
%q("="=>"")
,
@column
.
c
ast_type
.
type_cast_for_database
({
'='
=>
''
}))
end
def
test_gen4
assert_equal
(
%q(">"=>"")
,
@column
.
c
lass
.
hstore_to_string
({
'>'
=>
''
}))
assert_equal
(
%q(">"=>"")
,
@column
.
c
ast_type
.
type_cast_for_database
({
'>'
=>
''
}))
end
def
test_parse1
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录