Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
ccb22821
提交
ccb22821
authored
3月 25, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Check whether synonyms are available in JdbcDatabaseMetaData
上级
a5e72ddc
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
111 行增加
和
64 行删除
+111
-64
changelog.html
h2/src/docsrc/html/changelog.html
+2
-0
JdbcDatabaseMetaData.java
h2/src/main/org/h2/jdbc/JdbcDatabaseMetaData.java
+109
-64
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
ccb22821
...
@@ -21,6 +21,8 @@ Change Log
...
@@ -21,6 +21,8 @@ Change Log
<h2>
Next Version (unreleased)
</h2>
<h2>
Next Version (unreleased)
</h2>
<ul>
<ul>
<li>
Issue #992: 1.4.196+ client cannot use DatabaseMetaData with 1.4.195- server
</li>
<li>
Issue #1016: ResultSet.getObject() should return enum value, not ordinal
<li>
Issue #1016: ResultSet.getObject() should return enum value, not ordinal
</li>
</li>
<li>
Issue #1012: NPE when querying INFORMATION_SCHEMA.COLUMNS on a view that references an ENUM column
<li>
Issue #1012: NPE when querying INFORMATION_SCHEMA.COLUMNS on a view that references an ENUM column
...
...
h2/src/main/org/h2/jdbc/JdbcDatabaseMetaData.java
浏览文件 @
ccb22821
...
@@ -14,11 +14,16 @@ import java.sql.SQLException;
...
@@ -14,11 +14,16 @@ import java.sql.SQLException;
import
java.sql.Types
;
import
java.sql.Types
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Properties
;
import
java.util.Properties
;
import
org.h2.command.CommandInterface
;
import
org.h2.engine.Constants
;
import
org.h2.engine.Constants
;
import
org.h2.engine.SessionInterface
;
import
org.h2.engine.SessionRemote
;
import
org.h2.engine.SysProperties
;
import
org.h2.engine.SysProperties
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.message.Trace
;
import
org.h2.message.Trace
;
import
org.h2.message.TraceObject
;
import
org.h2.message.TraceObject
;
import
org.h2.result.ResultInterface
;
import
org.h2.tools.SimpleResultSet
;
import
org.h2.tools.SimpleResultSet
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
import
org.h2.util.StringUtils
;
...
@@ -31,6 +36,11 @@ public class JdbcDatabaseMetaData extends TraceObject implements
...
@@ -31,6 +36,11 @@ public class JdbcDatabaseMetaData extends TraceObject implements
private
final
JdbcConnection
conn
;
private
final
JdbcConnection
conn
;
/**
* Whether database has support for synonyms ({@code null} if not yet known).
*/
private
Boolean
hasSynonyms
;
JdbcDatabaseMetaData
(
JdbcConnection
conn
,
Trace
trace
,
int
id
)
{
JdbcDatabaseMetaData
(
JdbcConnection
conn
,
Trace
trace
,
int
id
)
{
setTrace
(
trace
,
TraceObject
.
DATABASE_META_DATA
,
id
);
setTrace
(
trace
,
TraceObject
.
DATABASE_META_DATA
,
id
);
this
.
conn
=
conn
;
this
.
conn
=
conn
;
...
@@ -105,6 +115,32 @@ public class JdbcDatabaseMetaData extends TraceObject implements
...
@@ -105,6 +115,32 @@ public class JdbcDatabaseMetaData extends TraceObject implements
return
Constants
.
getFullVersion
();
return
Constants
.
getFullVersion
();
}
}
private
boolean
hasSynonyms
()
{
Boolean
hasSynonyms
=
this
.
hasSynonyms
;
if
(
hasSynonyms
==
null
)
{
SessionInterface
si
=
conn
.
getSession
();
if
(
si
instanceof
SessionRemote
)
{
SessionRemote
sr
=
(
SessionRemote
)
si
;
int
clientVersion
=
sr
.
getClientVersion
();
if
(
clientVersion
>=
Constants
.
TCP_PROTOCOL_VERSION_17
)
{
hasSynonyms
=
true
;
}
else
if
(
clientVersion
<=
Constants
.
TCP_PROTOCOL_VERSION_15
)
{
hasSynonyms
=
false
;
}
else
{
// 1.4.194-1.4.196
CommandInterface
c
=
sr
.
prepareCommand
(
"CALL H2VERSION()"
,
Integer
.
MAX_VALUE
);
ResultInterface
result
=
c
.
executeQuery
(
0
,
false
);
result
.
next
();
String
s
=
result
.
currentRow
()[
0
].
getString
();
result
.
close
();
hasSynonyms
=
"1.4.196"
.
equals
(
s
);
}
}
else
{
hasSynonyms
=
true
;
}
}
return
hasSynonyms
;
}
/**
/**
* Gets the list of tables in the database. The result set is sorted by
* Gets the list of tables in the database. The result set is sorted by
* TABLE_TYPE, TABLE_SCHEM, and TABLE_NAME.
* TABLE_TYPE, TABLE_SCHEM, and TABLE_NAME.
...
@@ -144,7 +180,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
...
@@ -144,7 +180,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
}
}
checkClosed
();
checkClosed
();
int
typesLength
=
types
!=
null
?
types
.
length
:
0
;
int
typesLength
=
types
!=
null
?
types
.
length
:
0
;
boolean
includeSynonyms
=
types
==
null
||
Arrays
.
asList
(
types
).
contains
(
"SYNONYM"
);
boolean
includeSynonyms
=
hasSynonyms
()
&&
(
types
==
null
||
Arrays
.
asList
(
types
).
contains
(
"SYNONYM"
)
);
// (1024 - 16) is enough for the most cases
// (1024 - 16) is enough for the most cases
StringBuilder
select
=
new
StringBuilder
(
1008
);
StringBuilder
select
=
new
StringBuilder
(
1008
);
...
@@ -279,39 +315,37 @@ public class JdbcDatabaseMetaData extends TraceObject implements
...
@@ -279,39 +315,37 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+
quote
(
columnNamePattern
)+
");"
);
+
quote
(
columnNamePattern
)+
");"
);
}
}
checkClosed
();
checkClosed
();
String
tableSql
=
"SELECT "
boolean
includeSynonyms
=
hasSynonyms
();
+
"TABLE_CATALOG TABLE_CAT, "
+
"TABLE_SCHEMA TABLE_SCHEM, "
StringBuilder
select
=
new
StringBuilder
(
2432
);
if
(
includeSynonyms
)
{
select
.
append
(
"SELECT "
+
"TABLE_CAT, "
+
"TABLE_SCHEM, "
+
"TABLE_NAME, "
+
"TABLE_NAME, "
+
"COLUMN_NAME, "
+
"COLUMN_NAME, "
+
"DATA_TYPE, "
+
"DATA_TYPE, "
+
"TYPE_NAME, "
+
"TYPE_NAME, "
+
"CHARACTER_MAXIMUM_LENGTH
COLUMN_SIZE, "
+
"
COLUMN_SIZE, "
+
"CHARACTER_MAXIMUM_LENGTH
BUFFER_LENGTH, "
+
"
BUFFER_LENGTH, "
+
"NUMERIC_SCALE
DECIMAL_DIGITS, "
+
"
DECIMAL_DIGITS, "
+
"NUMERIC_PRECISION_RADIX
NUM_PREC_RADIX, "
+
"
NUM_PREC_RADIX, "
+
"NULLABLE, "
+
"NULLABLE, "
+
"REMARKS, "
+
"REMARKS, "
+
"COLUMN_DEFAULT
COLUMN_DEF, "
+
"
COLUMN_DEF, "
+
"DATA_TYPE
SQL_DATA_TYPE, "
+
"
SQL_DATA_TYPE, "
+
"ZERO()
SQL_DATETIME_SUB, "
+
"
SQL_DATETIME_SUB, "
+
"CHARACTER_OCTET_LENGTH
CHAR_OCTET_LENGTH, "
+
"
CHAR_OCTET_LENGTH, "
+
"ORDINAL_POSITION, "
+
"ORDINAL_POSITION, "
+
"IS_NULLABLE
IS_NULLABLE, "
+
"
IS_NULLABLE, "
+
"CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_CATALOG, "
+
"
SCOPE_CATALOG, "
+
"CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_SCHEMA, "
+
"
SCOPE_SCHEMA, "
+
"CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_TABLE, "
+
"
SCOPE_TABLE, "
+
"SOURCE_DATA_TYPE, "
+
"SOURCE_DATA_TYPE, "
+
"CASE WHEN SEQUENCE_NAME IS NULL THEN "
+
"IS_AUTOINCREMENT, "
+
"CAST(?1 AS VARCHAR) ELSE CAST(?2 AS VARCHAR) END IS_AUTOINCREMENT, "
+
"SCOPE_CATLOG "
+
"CAST(SOURCE_DATA_TYPE AS VARCHAR) SCOPE_CATLOG "
+
"FROM ("
+
"FROM INFORMATION_SCHEMA.COLUMNS "
+
"SELECT "
+
"WHERE TABLE_CATALOG LIKE ?3 ESCAPE ?7 "
+
"AND TABLE_SCHEMA LIKE ?4 ESCAPE ?7 "
+
"AND TABLE_NAME LIKE ?5 ESCAPE ?7 "
+
"AND COLUMN_NAME LIKE ?6 ESCAPE ?7 "
+
"ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION"
;
String
synonymSql
=
"SELECT "
+
"s.SYNONYM_CATALOG TABLE_CAT, "
+
"s.SYNONYM_CATALOG TABLE_CAT, "
+
"s.SYNONYM_SCHEMA TABLE_SCHEM, "
+
"s.SYNONYM_SCHEMA TABLE_SCHEM, "
+
"s.SYNONYM_NAME TABLE_NAME, "
+
"s.SYNONYM_NAME TABLE_NAME, "
...
@@ -343,34 +377,45 @@ public class JdbcDatabaseMetaData extends TraceObject implements
...
@@ -343,34 +377,45 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+
"WHERE s.SYNONYM_CATALOG LIKE ?3 ESCAPE ?7 "
+
"WHERE s.SYNONYM_CATALOG LIKE ?3 ESCAPE ?7 "
+
"AND s.SYNONYM_SCHEMA LIKE ?4 ESCAPE ?7 "
+
"AND s.SYNONYM_SCHEMA LIKE ?4 ESCAPE ?7 "
+
"AND s.SYNONYM_NAME LIKE ?5 ESCAPE ?7 "
+
"AND s.SYNONYM_NAME LIKE ?5 ESCAPE ?7 "
+
"AND c.COLUMN_NAME LIKE ?6 ESCAPE ?7 "
;
+
"AND c.COLUMN_NAME LIKE ?6 ESCAPE ?7 "
PreparedStatement
prep
=
conn
.
prepareAutoCloseStatement
(
"SELECT "
+
"UNION "
);
+
"TABLE_CAT, "
}
+
"TABLE_SCHEM, "
select
.
append
(
"SELECT "
+
"TABLE_CATALOG TABLE_CAT, "
+
"TABLE_SCHEMA TABLE_SCHEM, "
+
"TABLE_NAME, "
+
"TABLE_NAME, "
+
"COLUMN_NAME, "
+
"COLUMN_NAME, "
+
"DATA_TYPE, "
+
"DATA_TYPE, "
+
"TYPE_NAME, "
+
"TYPE_NAME, "
+
"COLUMN_SIZE, "
+
"C
HARACTER_MAXIMUM_LENGTH C
OLUMN_SIZE, "
+
"BUFFER_LENGTH, "
+
"
CHARACTER_MAXIMUM_LENGTH
BUFFER_LENGTH, "
+
"DECIMAL_DIGITS, "
+
"
NUMERIC_SCALE
DECIMAL_DIGITS, "
+
"NUM_PREC_RADIX, "
+
"NUM
ERIC_PRECISION_RADIX NUM
_PREC_RADIX, "
+
"NULLABLE, "
+
"NULLABLE, "
+
"REMARKS, "
+
"REMARKS, "
+
"COLUMN_DEF, "
+
"COLUMN_DEF
AULT COLUMN_DEF
, "
+
"SQL_DATA_TYPE, "
+
"
DATA_TYPE
SQL_DATA_TYPE, "
+
"SQL_DATETIME_SUB, "
+
"
ZERO()
SQL_DATETIME_SUB, "
+
"CHAR_OCTET_LENGTH, "
+
"CHAR
ACTER_OCTET_LENGTH CHAR
_OCTET_LENGTH, "
+
"ORDINAL_POSITION, "
+
"ORDINAL_POSITION, "
+
"IS_NULLABLE, "
+
"IS_NULLABLE
IS_NULLABLE
, "
+
"SCOPE_CATALOG, "
+
"
CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_CATALOG, "
+
"SCOPE_SCHEMA, "
+
"
CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_SCHEMA, "
+
"SCOPE_TABLE, "
+
"
CAST(SOURCE_DATA_TYPE AS VARCHAR)
SCOPE_TABLE, "
+
"SOURCE_DATA_TYPE, "
+
"SOURCE_DATA_TYPE, "
+
"IS_AUTOINCREMENT, "
+
"CASE WHEN SEQUENCE_NAME IS NULL THEN "
+
"SCOPE_CATLOG "
+
"CAST(?1 AS VARCHAR) ELSE CAST(?2 AS VARCHAR) END IS_AUTOINCREMENT, "
+
"FROM (("
+
tableSql
+
") UNION ("
+
synonymSql
+
"CAST(SOURCE_DATA_TYPE AS VARCHAR) SCOPE_CATLOG "
+
")) ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION"
);
+
"FROM INFORMATION_SCHEMA.COLUMNS "
+
"WHERE TABLE_CATALOG LIKE ?3 ESCAPE ?7 "
+
"AND TABLE_SCHEMA LIKE ?4 ESCAPE ?7 "
+
"AND TABLE_NAME LIKE ?5 ESCAPE ?7 "
+
"AND COLUMN_NAME LIKE ?6 ESCAPE ?7"
);
if
(
includeSynonyms
)
{
select
.
append
(
')'
);
}
PreparedStatement
prep
=
conn
.
prepareAutoCloseStatement
(
select
.
append
(
" ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION"
).
toString
());
prep
.
setString
(
1
,
"NO"
);
prep
.
setString
(
1
,
"NO"
);
prep
.
setString
(
2
,
"YES"
);
prep
.
setString
(
2
,
"YES"
);
prep
.
setString
(
3
,
getCatalogPattern
(
catalogPattern
));
prep
.
setString
(
3
,
getCatalogPattern
(
catalogPattern
));
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论