Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Meiracle
spring-framework
提交
e1a06590
S
spring-framework
项目概览
Meiracle
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e1a06590
编写于
11月 01, 2013
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid unnecessary char[] allocation in NamedParameterUtils
Issue: SPR-11042 (cherry picked from commit
1dc7ff86
)
上级
3bfddc50
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
40 addition
and
35 deletion
+40
-35
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java
...ngframework/jdbc/core/namedparam/NamedParameterUtils.java
+40
-35
未找到文件。
spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java
浏览文件 @
e1a06590
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
3
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -32,7 +32,8 @@ import org.springframework.util.Assert;
/**
* Helper methods for named parameter parsing.
* Only intended for internal use within Spring's JDBC framework.
*
* <p>Only intended for internal use within Spring's JDBC framework.
*
* @author Thomas Risberg
* @author Juergen Hoeller
...
...
@@ -112,11 +113,13 @@ public abstract class NamedParameterUtils {
while
(
j
<
statement
.
length
&&
!(
'}'
==
statement
[
j
]))
{
j
++;
if
(
':'
==
statement
[
j
]
||
'{'
==
statement
[
j
])
{
throw
new
InvalidDataAccessApiUsageException
(
"Parameter name contains invalid character '"
+
statement
[
j
]
+
"' at position "
+
i
+
" in statement "
+
sql
);
throw
new
InvalidDataAccessApiUsageException
(
"Parameter name contains invalid character '"
+
statement
[
j
]
+
"' at position "
+
i
+
" in statement: "
+
sql
);
}
}
if
(
j
>=
statement
.
length
)
{
throw
new
InvalidDataAccessApiUsageException
(
"Non-terminated named parameter declaration at position "
+
i
+
" in statement "
+
sql
);
throw
new
InvalidDataAccessApiUsageException
(
"Non-terminated named parameter declaration at position "
+
i
+
" in statement: "
+
sql
);
}
if
(
j
-
i
>
3
)
{
parameter
=
sql
.
substring
(
i
+
2
,
j
);
...
...
@@ -165,8 +168,9 @@ public abstract class NamedParameterUtils {
return
parsedSql
;
}
private
static
int
addNamedParameter
(
List
<
ParameterHolder
>
parameterList
,
int
totalParameterCount
,
int
escapes
,
int
i
,
int
j
,
String
parameter
)
{
private
static
int
addNamedParameter
(
List
<
ParameterHolder
>
parameterList
,
int
totalParameterCount
,
int
escapes
,
int
i
,
int
j
,
String
parameter
)
{
parameterList
.
add
(
new
ParameterHolder
(
parameter
,
i
-
escapes
,
j
-
escapes
));
totalParameterCount
++;
return
totalParameterCount
;
...
...
@@ -229,18 +233,18 @@ public abstract class NamedParameterUtils {
}
/**
* Parse the SQL statement and locate any placeholders or named parameters.
*
Named parameters are substituted for a JDBC placeholder and any select list
*
is expanded to the required number of placeholders. Select lists may contain
*
an array of objects and in that case the placeholders will be grouped and
*
enclosed with parantheses. This allows for the use of "expression lists" in
*
the SQL statement like:<br
/>
*
select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))
* <p>The parameter values passed in are used to determine the number of
*
placeholder to be used for a select list. Select lists should be limited
*
to 100 or fewer elements. A larger number of elements is not guaramteed to
*
be supported by the database and
is strictly vendor-dependent.
* @param parsedSql the parsed represenation of the SQL statement
* Parse the SQL statement and locate any placeholders or named parameters.
Named
*
parameters are substituted for a JDBC placeholder, and any select list is expanded
*
to the required number of placeholders. Select lists may contain an array of
*
objects, and in that case the placeholders will be grouped and enclosed with
*
parentheses. This allows for the use of "expression lists" in the SQL statement
*
like: <br /><br
/>
*
{@code select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))}
* <p>The parameter values passed in are used to determine the number of
placeholders to
*
be used for a select list. Select lists should be limited to 100 or fewer elements.
*
A larger number of elements is not guaranteed to be supported by the database and
* is strictly vendor-dependent.
* @param parsedSql the parsed represen
t
ation of the SQL statement
* @param paramSource the source for named parameters
* @return the SQL statement with substituted parameters
* @see #parseSqlStatement
...
...
@@ -255,7 +259,7 @@ public abstract class NamedParameterUtils {
int
[]
indexes
=
parsedSql
.
getParameterIndexes
(
i
);
int
startIndex
=
indexes
[
0
];
int
endIndex
=
indexes
[
1
];
actualSql
.
append
(
originalSql
.
substring
(
lastIndex
,
startIndex
)
);
actualSql
.
append
(
originalSql
,
lastIndex
,
startIndex
);
if
(
paramSource
!=
null
&&
paramSource
.
hasValue
(
paramName
))
{
Object
value
=
paramSource
.
getValue
(
paramName
);
if
(
value
instanceof
SqlParameterValue
)
{
...
...
@@ -295,7 +299,7 @@ public abstract class NamedParameterUtils {
}
lastIndex
=
endIndex
;
}
actualSql
.
append
(
originalSql
.
substring
(
lastIndex
,
originalSql
.
length
()
));
actualSql
.
append
(
originalSql
,
lastIndex
,
originalSql
.
length
(
));
return
actualSql
.
toString
();
}
...
...
@@ -314,10 +318,10 @@ public abstract class NamedParameterUtils {
Object
[]
paramArray
=
new
Object
[
parsedSql
.
getTotalParameterCount
()];
if
(
parsedSql
.
getNamedParameterCount
()
>
0
&&
parsedSql
.
getUnnamedParameterCount
()
>
0
)
{
throw
new
InvalidDataAccessApiUsageException
(
"
You can't
mix named and traditional ? placeholders. You have "
+
"
Not allowed to
mix named and traditional ? placeholders. You have "
+
parsedSql
.
getNamedParameterCount
()
+
" named parameter(s) and "
+
parsedSql
.
getUnnamedParameterCount
()
+
" tradit
onal placeholder(s) in [
"
+
parsedSql
.
getOriginalSql
()
+
"]"
);
parsedSql
.
getUnnamedParameterCount
()
+
" tradit
ional placeholder(s) in statement:
"
+
parsedSql
.
getOriginalSql
());
}
List
<
String
>
paramNames
=
parsedSql
.
getParameterNames
();
for
(
int
i
=
0
;
i
<
paramNames
.
size
();
i
++)
{
...
...
@@ -408,15 +412,12 @@ public abstract class NamedParameterUtils {
List
<
String
>
paramNames
=
parsedSql
.
getParameterNames
();
List
<
SqlParameter
>
params
=
new
LinkedList
<
SqlParameter
>();
for
(
String
paramName
:
paramNames
)
{
SqlParameter
param
=
new
SqlParameter
(
paramName
,
paramSource
.
getSqlType
(
paramName
),
paramSource
.
getTypeName
(
paramName
));
params
.
add
(
param
);
params
.
add
(
new
SqlParameter
(
paramName
,
paramSource
.
getSqlType
(
paramName
),
paramSource
.
getTypeName
(
paramName
)));
}
return
params
;
}
//-------------------------------------------------------------------------
// Convenience methods operating on a plain SQL String
//-------------------------------------------------------------------------
...
...
@@ -463,28 +464,32 @@ public abstract class NamedParameterUtils {
return
buildValueArray
(
parsedSql
,
new
MapSqlParameterSource
(
paramMap
),
null
);
}
private
static
class
ParameterHolder
{
private
String
parameterName
;
private
int
startIndex
;
private
int
endIndex
;
private
final
String
parameterName
;
private
final
int
startIndex
;
private
final
int
endIndex
;
public
ParameterHolder
(
String
parameterName
,
int
startIndex
,
int
endIndex
)
{
super
();
this
.
parameterName
=
parameterName
;
this
.
startIndex
=
startIndex
;
this
.
endIndex
=
endIndex
;
}
public
String
getParameterName
()
{
return
parameterName
;
return
this
.
parameterName
;
}
public
int
getStartIndex
()
{
return
startIndex
;
return
this
.
startIndex
;
}
public
int
getEndIndex
()
{
return
endIndex
;
return
this
.
endIndex
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录