Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
408227d9
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,发现更多精彩内容 >>
提交
408227d9
编写于
2月 07, 2013
作者:
G
Guillermo Iguaran
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9207 from dylanahsmith/mysql-quote-numeric
active_record: Quote numeric values compared to string columns.
上级
dca0b57d
a712e08e
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
60 addition
and
14 deletion
+60
-14
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+10
-0
activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
...lib/active_record/connection_adapters/abstract/quoting.rb
+8
-2
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+0
-2
activerecord/lib/active_record/relation/predicate_builder.rb
activerecord/lib/active_record/relation/predicate_builder.rb
+5
-0
activerecord/test/cases/quoting_test.rb
activerecord/test/cases/quoting_test.rb
+7
-7
activerecord/test/cases/relation/where_test.rb
activerecord/test/cases/relation/where_test.rb
+25
-0
activerecord/test/cases/relation_scoping_test.rb
activerecord/test/cases/relation_scoping_test.rb
+3
-3
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+2
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
408227d9
## Rails 4.0.0 (unreleased) ##
## Rails 4.0.0 (unreleased) ##
*
Quote numeric values being compared to non-numeric columns. Otherwise,
in some database, the string column values will be coerced to a numeric
allowing 0, 0.0 or false to match any string starting with a non-digit.
Example:
App.where(apikey: 0) # => SELECT * FROM users WHERE apikey = '0'
*Dylan Smith*
*
Schema dumper supports dumping the enabled database extensions to
`schema.rb`
*
Schema dumper supports dumping the enabled database extensions to
`schema.rb`
(currently only supported by postgresql).
(currently only supported by postgresql).
...
...
activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
浏览文件 @
408227d9
...
@@ -25,13 +25,19 @@ def quote(value, column = nil)
...
@@ -25,13 +25,19 @@ def quote(value, column = nil)
when
true
,
false
when
true
,
false
if
column
&&
column
.
type
==
:integer
if
column
&&
column
.
type
==
:integer
value
?
'1'
:
'0'
value
?
'1'
:
'0'
elsif
column
&&
[
:text
,
:string
,
:binary
].
include?
(
column
.
type
)
value
?
"'1'"
:
"'0'"
else
else
value
?
quoted_true
:
quoted_false
value
?
quoted_true
:
quoted_false
end
end
# BigDecimals need to be put in a non-normalized form and quoted.
# BigDecimals need to be put in a non-normalized form and quoted.
when
nil
then
"NULL"
when
nil
then
"NULL"
when
BigDecimal
then
value
.
to_s
(
'F'
)
when
Numeric
,
ActiveSupport
::
Duration
when
Numeric
,
ActiveSupport
::
Duration
then
value
.
to_s
value
=
BigDecimal
===
value
?
value
.
to_s
(
'F'
)
:
value
.
to_s
if
column
&&
!
[
:integer
,
:float
,
:decimal
].
include?
(
column
.
type
)
value
=
"'
#{
value
}
'"
end
value
when
Date
,
Time
then
"'
#{
quoted_date
(
value
)
}
'"
when
Date
,
Time
then
"'
#{
quoted_date
(
value
)
}
'"
when
Symbol
then
"'
#{
quote_string
(
value
.
to_s
)
}
'"
when
Symbol
then
"'
#{
quote_string
(
value
.
to_s
)
}
'"
when
Class
then
"'
#{
value
.
to_s
}
'"
when
Class
then
"'
#{
value
.
to_s
}
'"
...
...
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
408227d9
...
@@ -212,8 +212,6 @@ def quote(value, column = nil)
...
@@ -212,8 +212,6 @@ def quote(value, column = nil)
if
value
.
kind_of?
(
String
)
&&
column
&&
column
.
type
==
:binary
&&
column
.
class
.
respond_to?
(
:string_to_binary
)
if
value
.
kind_of?
(
String
)
&&
column
&&
column
.
type
==
:binary
&&
column
.
class
.
respond_to?
(
:string_to_binary
)
s
=
column
.
class
.
string_to_binary
(
value
).
unpack
(
"H*"
)[
0
]
s
=
column
.
class
.
string_to_binary
(
value
).
unpack
(
"H*"
)[
0
]
"x'
#{
s
}
'"
"x'
#{
s
}
'"
elsif
value
.
kind_of?
(
BigDecimal
)
value
.
to_s
(
"F"
)
else
else
super
super
end
end
...
...
activerecord/lib/active_record/relation/predicate_builder.rb
浏览文件 @
408227d9
...
@@ -98,6 +98,11 @@ def self.build(attribute, value)
...
@@ -98,6 +98,11 @@ def self.build(attribute, value)
when
Class
when
Class
# FIXME: I think we need to deprecate this behavior
# FIXME: I think we need to deprecate this behavior
attribute
.
eq
(
value
.
name
)
attribute
.
eq
(
value
.
name
)
when
Integer
,
ActiveSupport
::
Duration
# Arel treats integers as literals, but they should be quoted when compared with strings
table
=
attribute
.
relation
column
=
table
.
engine
.
connection
.
schema_cache
.
columns_hash
(
table
.
name
)[
attribute
.
name
.
to_s
]
attribute
.
eq
(
Arel
::
Nodes
::
SqlLiteral
.
new
(
table
.
engine
.
connection
.
quote
(
value
,
column
)))
else
else
attribute
.
eq
(
value
)
attribute
.
eq
(
value
)
end
end
...
...
activerecord/test/cases/quoting_test.rb
浏览文件 @
408227d9
...
@@ -122,35 +122,35 @@ def test_quote_false
...
@@ -122,35 +122,35 @@ def test_quote_false
def
test_quote_float
def
test_quote_float
float
=
1.2
float
=
1.2
assert_equal
float
.
to_s
,
@quoter
.
quote
(
float
,
nil
)
assert_equal
float
.
to_s
,
@quoter
.
quote
(
float
,
nil
)
assert_equal
float
.
to_s
,
@quoter
.
quote
(
float
,
Object
.
new
)
assert_equal
float
.
to_s
,
@quoter
.
quote
(
float
,
FakeColumn
.
new
(
:float
)
)
end
end
def
test_quote_fixnum
def
test_quote_fixnum
fixnum
=
1
fixnum
=
1
assert_equal
fixnum
.
to_s
,
@quoter
.
quote
(
fixnum
,
nil
)
assert_equal
fixnum
.
to_s
,
@quoter
.
quote
(
fixnum
,
nil
)
assert_equal
fixnum
.
to_s
,
@quoter
.
quote
(
fixnum
,
Object
.
new
)
assert_equal
fixnum
.
to_s
,
@quoter
.
quote
(
fixnum
,
FakeColumn
.
new
(
:integer
)
)
end
end
def
test_quote_bignum
def
test_quote_bignum
bignum
=
1
<<
100
bignum
=
1
<<
100
assert_equal
bignum
.
to_s
,
@quoter
.
quote
(
bignum
,
nil
)
assert_equal
bignum
.
to_s
,
@quoter
.
quote
(
bignum
,
nil
)
assert_equal
bignum
.
to_s
,
@quoter
.
quote
(
bignum
,
Object
.
new
)
assert_equal
bignum
.
to_s
,
@quoter
.
quote
(
bignum
,
FakeColumn
.
new
(
:integer
)
)
end
end
def
test_quote_bigdecimal
def
test_quote_bigdecimal
bigdec
=
BigDecimal
.
new
((
1
<<
100
).
to_s
)
bigdec
=
BigDecimal
.
new
((
1
<<
100
).
to_s
)
assert_equal
bigdec
.
to_s
(
'F'
),
@quoter
.
quote
(
bigdec
,
nil
)
assert_equal
bigdec
.
to_s
(
'F'
),
@quoter
.
quote
(
bigdec
,
nil
)
assert_equal
bigdec
.
to_s
(
'F'
),
@quoter
.
quote
(
bigdec
,
Object
.
new
)
assert_equal
bigdec
.
to_s
(
'F'
),
@quoter
.
quote
(
bigdec
,
FakeColumn
.
new
(
:decimal
)
)
end
end
def
test_dates_and_times
def
test_dates_and_times
@quoter
.
extend
(
Module
.
new
{
def
quoted_date
(
value
)
'lol'
end
})
@quoter
.
extend
(
Module
.
new
{
def
quoted_date
(
value
)
'lol'
end
})
assert_equal
"'lol'"
,
@quoter
.
quote
(
Date
.
today
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Date
.
today
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Date
.
today
,
Object
.
new
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Date
.
today
,
FakeColumn
.
new
(
:date
)
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Time
.
now
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Time
.
now
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Time
.
now
,
Object
.
new
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
Time
.
now
,
FakeColumn
.
new
(
:time
)
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
DateTime
.
now
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
DateTime
.
now
,
nil
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
DateTime
.
now
,
Object
.
new
)
assert_equal
"'lol'"
,
@quoter
.
quote
(
DateTime
.
now
,
FakeColumn
.
new
(
:datetime
)
)
end
end
def
test_crazy_object
def
test_crazy_object
...
...
activerecord/test/cases/relation/where_test.rb
浏览文件 @
408227d9
...
@@ -108,5 +108,30 @@ def test_where_with_blank_conditions
...
@@ -108,5 +108,30 @@ def test_where_with_blank_conditions
assert_equal
4
,
Edge
.
where
(
blank
).
order
(
"sink_id"
).
to_a
.
size
assert_equal
4
,
Edge
.
where
(
blank
).
order
(
"sink_id"
).
to_a
.
size
end
end
end
end
def
test_where_with_integer_for_string_column
count
=
Post
.
where
(
:title
=>
0
).
count
assert_equal
0
,
count
end
def
test_where_with_float_for_string_column
count
=
Post
.
where
(
:title
=>
0.0
).
count
assert_equal
0
,
count
end
def
test_where_with_boolean_for_string_column
count
=
Post
.
where
(
:title
=>
false
).
count
assert_equal
0
,
count
end
def
test_where_with_decimal_for_string_column
count
=
Post
.
where
(
:title
=>
BigDecimal
.
new
(
0
)).
count
assert_equal
0
,
count
end
def
test_where_with_duration_for_string_column
count
=
Post
.
where
(
:title
=>
0
.
seconds
).
count
assert_equal
0
,
count
end
end
end
end
end
activerecord/test/cases/relation_scoping_test.rb
浏览文件 @
408227d9
...
@@ -391,19 +391,19 @@ def test_default_scoping_with_threads
...
@@ -391,19 +391,19 @@ def test_default_scoping_with_threads
def
test_default_scope_with_inheritance
def
test_default_scope_with_inheritance
wheres
=
InheritedPoorDeveloperCalledJamis
.
all
.
where_values_hash
wheres
=
InheritedPoorDeveloperCalledJamis
.
all
.
where_values_hash
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
50000
,
wheres
[
:salary
]
assert_equal
Arel
.
sql
(
"50000"
),
wheres
[
:salary
]
end
end
def
test_default_scope_with_module_includes
def
test_default_scope_with_module_includes
wheres
=
ModuleIncludedPoorDeveloperCalledJamis
.
all
.
where_values_hash
wheres
=
ModuleIncludedPoorDeveloperCalledJamis
.
all
.
where_values_hash
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
50000
,
wheres
[
:salary
]
assert_equal
Arel
.
sql
(
"50000"
),
wheres
[
:salary
]
end
end
def
test_default_scope_with_multiple_calls
def
test_default_scope_with_multiple_calls
wheres
=
MultiplePoorDeveloperCalledJamis
.
all
.
where_values_hash
wheres
=
MultiplePoorDeveloperCalledJamis
.
all
.
where_values_hash
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
"Jamis"
,
wheres
[
:name
]
assert_equal
50000
,
wheres
[
:salary
]
assert_equal
Arel
.
sql
(
"50000"
),
wheres
[
:salary
]
end
end
def
test_scope_overwrites_default
def
test_scope_overwrites_default
...
...
activerecord/test/schema/schema.rb
浏览文件 @
408227d9
...
@@ -540,6 +540,8 @@ def create_table(*args, &block)
...
@@ -540,6 +540,8 @@ def create_table(*args, &block)
create_table
:price_estimates
,
:force
=>
true
do
|
t
|
create_table
:price_estimates
,
:force
=>
true
do
|
t
|
t
.
string
:estimate_of_type
t
.
string
:estimate_of_type
t
.
integer
:estimate_of_id
t
.
integer
:estimate_of_id
t
.
string
:thing_type
t
.
integer
:thing_id
t
.
integer
:price
t
.
integer
:price
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录