Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
c38da2df
Unverified
提交
c38da2df
authored
3月 01, 2019
作者:
Evgenij Ryazanov
提交者:
GitHub
3月 01, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1779 from katzyn/identifier
Improve isSimpleIdentifier() and enquoteIdentifier()
上级
6178c2ed
92fae177
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
151 行增加
和
31 行删除
+151
-31
Parser.java
h2/src/main/org/h2/command/Parser.java
+2
-2
JdbcStatement.java
h2/src/main/org/h2/jdbc/JdbcStatement.java
+25
-6
ParserUtil.java
h2/src/main/org/h2/util/ParserUtil.java
+43
-6
TestStatement.java
h2/src/test/org/h2/test/jdbc/TestStatement.java
+81
-17
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
c38da2df
...
...
@@ -8106,7 +8106,7 @@ public class Parser {
if
(
s
==
null
)
{
return
"\"\""
;
}
if
(
ParserUtil
.
isSimpleIdentifier
(
s
))
{
if
(
ParserUtil
.
isSimpleIdentifier
(
s
,
true
,
false
))
{
return
s
;
}
return
StringUtils
.
quoteIdentifier
(
s
);
...
...
@@ -8124,7 +8124,7 @@ public class Parser {
if
(
s
==
null
)
{
return
builder
.
append
(
"\"\""
);
}
if
(
ParserUtil
.
isSimpleIdentifier
(
s
))
{
if
(
ParserUtil
.
isSimpleIdentifier
(
s
,
true
,
false
))
{
return
builder
.
append
(
s
);
}
return
StringUtils
.
quoteIdentifier
(
builder
,
s
);
...
...
h2/src/main/org/h2/jdbc/JdbcStatement.java
浏览文件 @
c38da2df
...
...
@@ -1383,17 +1383,35 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme
/**
* @param identifier
* identifier to quote if required
* identifier to quote if required
, may be quoted or unquoted
* @param alwaysQuote
* if {@code true} identifier will be quoted unconditionally
* @return specified identifier quoted if required or explicitly requested
* @return specified identifier quoted if required, explicitly requested, or
* if it was already quoted
* @throws SQLException
* if identifier is not a valid identifier
*/
@Override
public
String
enquoteIdentifier
(
String
identifier
,
boolean
alwaysQuote
)
throws
SQLException
{
if
(
alwaysQuote
||
!
isSimpleIdentifier
(
identifier
))
{
return
StringUtils
.
quoteIdentifier
(
identifier
)
;
if
(
isSimpleIdentifier
(
identifier
))
{
return
alwaysQuote
?
'"'
+
identifier
+
'"'
:
identifier
;
}
return
identifier
;
int
length
=
identifier
.
length
();
if
(
length
>
0
&&
identifier
.
charAt
(
0
)
==
'"'
)
{
boolean
quoted
=
true
;
for
(
int
i
=
1
;
i
<
length
;
i
++)
{
if
(
identifier
.
charAt
(
i
)
==
'"'
)
{
quoted
=
!
quoted
;
}
else
if
(!
quoted
)
{
throw
new
SQLException
();
}
}
if
(
quoted
)
{
throw
new
SQLException
();
}
return
identifier
;
}
return
StringUtils
.
quoteIdentifier
(
identifier
);
}
/**
...
...
@@ -1403,7 +1421,8 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme
*/
@Override
public
boolean
isSimpleIdentifier
(
String
identifier
)
throws
SQLException
{
return
ParserUtil
.
isSimpleIdentifier
(
identifier
);
JdbcConnection
.
Settings
settings
=
conn
.
getSettings
();
return
ParserUtil
.
isSimpleIdentifier
(
identifier
,
settings
.
databaseToUpper
,
settings
.
databaseToLower
);
}
/**
...
...
h2/src/main/org/h2/util/ParserUtil.java
浏览文件 @
c38da2df
...
...
@@ -284,7 +284,6 @@ public class ParserUtil {
private
static
final
int
UPPER_OR_OTHER_LETTER
=
1
<<
Character
.
UPPERCASE_LETTER
|
1
<<
Character
.
TITLECASE_LETTER
|
1
<<
Character
.
MODIFIER_LETTER
|
1
<<
Character
.
OTHER_LETTER
;
...
...
@@ -292,6 +291,26 @@ public class ParserUtil {
UPPER_OR_OTHER_LETTER
|
1
<<
Character
.
DECIMAL_DIGIT_NUMBER
;
private
static
final
int
LOWER_OR_OTHER_LETTER
=
1
<<
Character
.
LOWERCASE_LETTER
|
1
<<
Character
.
MODIFIER_LETTER
|
1
<<
Character
.
OTHER_LETTER
;
private
static
final
int
LOWER_OR_OTHER_LETTER_OR_DIGIT
=
LOWER_OR_OTHER_LETTER
|
1
<<
Character
.
DECIMAL_DIGIT_NUMBER
;
private
static
final
int
LETTER
=
1
<<
Character
.
UPPERCASE_LETTER
|
1
<<
Character
.
LOWERCASE_LETTER
|
1
<<
Character
.
TITLECASE_LETTER
|
1
<<
Character
.
MODIFIER_LETTER
|
1
<<
Character
.
OTHER_LETTER
;
private
static
final
int
LETTER_OR_DIGIT
=
LETTER
|
1
<<
Character
.
DECIMAL_DIGIT_NUMBER
;
private
ParserUtil
()
{
// utility class
}
...
...
@@ -316,26 +335,44 @@ public class ParserUtil {
* Is this a simple identifier (in the JDBC specification sense).
*
* @param s identifier to check
* @param databaseToUpper whether unquoted identifiers are converted to upper case
* @param databaseToLower whether unquoted identifiers are converted to lower case
* @return is specified identifier may be used without quotes
* @throws NullPointerException if s is {@code null}
*/
public
static
boolean
isSimpleIdentifier
(
String
s
)
{
public
static
boolean
isSimpleIdentifier
(
String
s
,
boolean
databaseToUpper
,
boolean
databaseToLower
)
{
int
length
=
s
.
length
();
if
(
length
==
0
)
{
return
false
;
}
int
startFlags
,
partFlags
;
if
(
databaseToUpper
)
{
if
(
databaseToLower
)
{
throw
new
IllegalArgumentException
(
"databaseToUpper && databaseToLower"
);
}
else
{
startFlags
=
UPPER_OR_OTHER_LETTER
;
partFlags
=
UPPER_OR_OTHER_LETTER_OR_DIGIT
;
}
}
else
{
if
(
databaseToLower
)
{
startFlags
=
LOWER_OR_OTHER_LETTER
;
partFlags
=
LOWER_OR_OTHER_LETTER_OR_DIGIT
;
}
else
{
startFlags
=
LETTER
;
partFlags
=
LETTER_OR_DIGIT
;
}
}
char
c
=
s
.
charAt
(
0
);
// lowercase a-z is quoted as well
if
((
UPPER_OR_OTHER_LETTER
>>>
Character
.
getType
(
c
)
&
1
)
==
0
&&
c
!=
'_'
)
{
if
((
startFlags
>>>
Character
.
getType
(
c
)
&
1
)
==
0
&&
c
!=
'_'
)
{
return
false
;
}
for
(
int
i
=
1
;
i
<
length
;
i
++)
{
c
=
s
.
charAt
(
i
);
if
((
UPPER_OR_OTHER_LETTER_OR_DIGIT
>>>
Character
.
getType
(
c
)
&
1
)
==
0
&&
c
!=
'_'
)
{
if
((
partFlags
>>>
Character
.
getType
(
c
)
&
1
)
==
0
&&
c
!=
'_'
)
{
return
false
;
}
}
return
getSaveTokenType
(
s
,
false
,
0
,
length
,
true
)
==
IDENTIFIER
;
return
getSaveTokenType
(
s
,
!
databaseToUpper
,
0
,
length
,
true
)
==
IDENTIFIER
;
}
/**
...
...
h2/src/test/org/h2/test/jdbc/TestStatement.java
浏览文件 @
c38da2df
...
...
@@ -53,6 +53,7 @@ public class TestStatement extends TestDb {
testIdentityMerge
();
testIdentity
();
conn
.
close
();
testIdentifiers
();
deleteDb
(
"statement"
);
}
...
...
@@ -330,23 +331,6 @@ public class TestStatement extends TestDb {
assertNull
(
stat
.
getWarnings
());
assertTrue
(
conn
==
stat
.
getConnection
());
assertEquals
(
"SOME_ID"
,
statBC
.
enquoteIdentifier
(
"SOME_ID"
,
false
));
assertEquals
(
"\"SOME ID\""
,
statBC
.
enquoteIdentifier
(
"SOME ID"
,
false
));
assertEquals
(
"\"SOME_ID\""
,
statBC
.
enquoteIdentifier
(
"SOME_ID"
,
true
));
assertEquals
(
"\"FROM\""
,
statBC
.
enquoteIdentifier
(
"FROM"
,
false
));
assertEquals
(
"\"Test\""
,
statBC
.
enquoteIdentifier
(
"Test"
,
false
));
assertEquals
(
"\"TODAY\""
,
statBC
.
enquoteIdentifier
(
"TODAY"
,
false
));
// Other lower case characters don't have upper case mappings
assertEquals
(
"\u02B0"
,
statBC
.
enquoteIdentifier
(
"\u02B0"
,
false
));
assertTrue
(
statBC
.
isSimpleIdentifier
(
"SOME_ID"
));
assertFalse
(
statBC
.
isSimpleIdentifier
(
"SOME ID"
));
assertFalse
(
statBC
.
isSimpleIdentifier
(
"FROM"
));
assertFalse
(
statBC
.
isSimpleIdentifier
(
"Test"
));
assertFalse
(
statBC
.
isSimpleIdentifier
(
"TODAY"
));
// Other lower case characters don't have upper case mappings
assertTrue
(
statBC
.
isSimpleIdentifier
(
"\u02B0"
));
stat
.
close
();
}
...
...
@@ -489,4 +473,84 @@ public class TestStatement extends TestDb {
stat
.
execute
(
"drop table test"
);
}
private
void
testIdentifiers
()
throws
SQLException
{
Connection
conn
=
getConnection
(
"statement"
);
JdbcStatement
stat
=
(
JdbcStatement
)
conn
.
createStatement
();
assertEquals
(
"SOME_ID"
,
stat
.
enquoteIdentifier
(
"SOME_ID"
,
false
));
assertEquals
(
"\"SOME ID\""
,
stat
.
enquoteIdentifier
(
"SOME ID"
,
false
));
assertEquals
(
"\"SOME_ID\""
,
stat
.
enquoteIdentifier
(
"SOME_ID"
,
true
));
assertEquals
(
"\"FROM\""
,
stat
.
enquoteIdentifier
(
"FROM"
,
false
));
assertEquals
(
"\"Test\""
,
stat
.
enquoteIdentifier
(
"Test"
,
false
));
assertEquals
(
"\"test\""
,
stat
.
enquoteIdentifier
(
"test"
,
false
));
assertEquals
(
"\"TODAY\""
,
stat
.
enquoteIdentifier
(
"TODAY"
,
false
));
assertEquals
(
"\"Test\""
,
stat
.
enquoteIdentifier
(
"\"Test\""
,
false
));
assertEquals
(
"\"Test\""
,
stat
.
enquoteIdentifier
(
"\"Test\""
,
true
));
assertEquals
(
"\"\"\"Test\""
,
stat
.
enquoteIdentifier
(
"\"\"\"Test\""
,
true
));
try
{
stat
.
enquoteIdentifier
(
"\"Test"
,
true
);
fail
();
}
catch
(
SQLException
ex
)
{
// OK
}
// Other lower case characters don't have upper case mappings
assertEquals
(
"\u02B0"
,
stat
.
enquoteIdentifier
(
"\u02B0"
,
false
));
assertTrue
(
stat
.
isSimpleIdentifier
(
"SOME_ID"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"SOME ID"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"FROM"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"Test"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"test"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"TODAY"
));
// Other lower case characters don't have upper case mappings
assertTrue
(
stat
.
isSimpleIdentifier
(
"\u02B0"
));
conn
.
close
();
conn
=
getConnection
(
"statement;DATABASE_TO_LOWER=TRUE"
);
stat
=
(
JdbcStatement
)
conn
.
createStatement
();
assertEquals
(
"some_id"
,
stat
.
enquoteIdentifier
(
"some_id"
,
false
));
assertEquals
(
"\"some id\""
,
stat
.
enquoteIdentifier
(
"some id"
,
false
));
assertEquals
(
"\"some_id\""
,
stat
.
enquoteIdentifier
(
"some_id"
,
true
));
assertEquals
(
"\"from\""
,
stat
.
enquoteIdentifier
(
"from"
,
false
));
assertEquals
(
"\"Test\""
,
stat
.
enquoteIdentifier
(
"Test"
,
false
));
assertEquals
(
"\"TEST\""
,
stat
.
enquoteIdentifier
(
"TEST"
,
false
));
assertEquals
(
"\"today\""
,
stat
.
enquoteIdentifier
(
"today"
,
false
));
assertTrue
(
stat
.
isSimpleIdentifier
(
"some_id"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"some id"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"from"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"Test"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"TEST"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"today"
));
conn
.
close
();
conn
=
getConnection
(
"statement;DATABASE_TO_UPPER=FALSE"
);
stat
=
(
JdbcStatement
)
conn
.
createStatement
();
assertEquals
(
"SOME_ID"
,
stat
.
enquoteIdentifier
(
"SOME_ID"
,
false
));
assertEquals
(
"some_id"
,
stat
.
enquoteIdentifier
(
"some_id"
,
false
));
assertEquals
(
"\"SOME ID\""
,
stat
.
enquoteIdentifier
(
"SOME ID"
,
false
));
assertEquals
(
"\"some id\""
,
stat
.
enquoteIdentifier
(
"some id"
,
false
));
assertEquals
(
"\"SOME_ID\""
,
stat
.
enquoteIdentifier
(
"SOME_ID"
,
true
));
assertEquals
(
"\"some_id\""
,
stat
.
enquoteIdentifier
(
"some_id"
,
true
));
assertEquals
(
"\"FROM\""
,
stat
.
enquoteIdentifier
(
"FROM"
,
false
));
assertEquals
(
"\"from\""
,
stat
.
enquoteIdentifier
(
"from"
,
false
));
assertEquals
(
"Test"
,
stat
.
enquoteIdentifier
(
"Test"
,
false
));
assertEquals
(
"\"TODAY\""
,
stat
.
enquoteIdentifier
(
"TODAY"
,
false
));
assertEquals
(
"\"today\""
,
stat
.
enquoteIdentifier
(
"today"
,
false
));
assertTrue
(
stat
.
isSimpleIdentifier
(
"SOME_ID"
));
assertTrue
(
stat
.
isSimpleIdentifier
(
"some_id"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"SOME ID"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"some id"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"FROM"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"from"
));
assertTrue
(
stat
.
isSimpleIdentifier
(
"Test"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"TODAY"
));
assertFalse
(
stat
.
isSimpleIdentifier
(
"today"
));
conn
.
close
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论