Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
0373b490
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
0373b490
编写于
6月 04, 2020
作者:
X
xiaoyu
提交者:
GitHub
6月 04, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix oracle connection getSchema() not exist. (#5914)
* fix oracle getSchema. * import format.
上级
1c1741ed
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
333 addition
and
14 deletion
+333
-14
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
...l/parser/binder/metadata/column/ColumnMetaDataLoader.java
+5
-4
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/index/IndexMetaDataLoader.java
...sql/parser/binder/metadata/index/IndexMetaDataLoader.java
+4
-2
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/schema/SchemaMetaDataLoader.java
...l/parser/binder/metadata/schema/SchemaMetaDataLoader.java
+5
-4
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
...sql/parser/binder/metadata/table/TableMetaDataLoader.java
+5
-4
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
...rdingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
+46
-0
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/index/IndexMetaDataLoaderTest.java
...parser/binder/metadata/index/IndexMetaDataLoaderTest.java
+80
-0
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
...parser/binder/metadata/table/TableMetaDataLoaderTest.java
+137
-0
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtilTest.java
...gsphere/sql/parser/binder/metadata/util/JdbcUtilTest.java
+51
-0
未找到文件。
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
浏览文件 @
0373b490
...
...
@@ -28,6 +28,7 @@ import java.util.Collection;
import
java.util.HashSet
;
import
java.util.LinkedList
;
import
java.util.List
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil
;
/**
* Column meta data loader.
...
...
@@ -52,13 +53,13 @@ public final class ColumnMetaDataLoader {
*/
public
static
Collection
<
ColumnMetaData
>
load
(
final
Connection
connection
,
final
String
table
,
final
String
databaseType
)
throws
SQLException
{
Collection
<
ColumnMetaData
>
result
=
new
LinkedList
<>();
Collection
<
String
>
primaryKeys
=
loadPrimaryKeys
(
connection
,
table
);
Collection
<
String
>
primaryKeys
=
loadPrimaryKeys
(
connection
,
table
,
databaseType
);
List
<
String
>
columnNames
=
new
ArrayList
<>();
List
<
Integer
>
columnTypes
=
new
ArrayList
<>();
List
<
String
>
columnTypeNames
=
new
ArrayList
<>();
List
<
Boolean
>
isPrimaryKeys
=
new
ArrayList
<>();
List
<
Boolean
>
isCaseSensitives
=
new
ArrayList
<>();
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getColumns
(
connection
.
getCatalog
(),
connection
.
getSchema
(
),
table
,
"%"
))
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getColumns
(
connection
.
getCatalog
(),
JdbcUtil
.
getSchema
(
connection
,
databaseType
),
table
,
"%"
))
{
while
(
resultSet
.
next
())
{
String
columnName
=
resultSet
.
getString
(
COLUMN_NAME
);
columnTypes
.
add
(
resultSet
.
getInt
(
DATA_TYPE
));
...
...
@@ -99,9 +100,9 @@ public final class ColumnMetaDataLoader {
return
"SELECT * FROM "
+
delimiterLeft
+
table
+
delimiterRight
+
" WHERE 1 != 1"
;
}
private
static
Collection
<
String
>
loadPrimaryKeys
(
final
Connection
connection
,
final
String
table
)
throws
SQLException
{
private
static
Collection
<
String
>
loadPrimaryKeys
(
final
Connection
connection
,
final
String
table
,
final
String
databaseType
)
throws
SQLException
{
Collection
<
String
>
result
=
new
HashSet
<>();
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getPrimaryKeys
(
connection
.
getCatalog
(),
connection
.
getSchema
(
),
table
))
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getPrimaryKeys
(
connection
.
getCatalog
(),
JdbcUtil
.
getSchema
(
connection
,
databaseType
),
table
))
{
while
(
resultSet
.
next
())
{
result
.
add
(
resultSet
.
getString
(
COLUMN_NAME
));
}
...
...
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/index/IndexMetaDataLoader.java
浏览文件 @
0373b490
...
...
@@ -24,6 +24,7 @@ import java.util.Collection;
import
java.util.HashSet
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil
;
/**
* Index meta data loader.
...
...
@@ -39,12 +40,13 @@ public final class IndexMetaDataLoader {
*
* @param connection connection
* @param table table name
* @param databaseType databaseType
* @return index meta data list
* @throws SQLException SQL exception
*/
public
static
Collection
<
IndexMetaData
>
load
(
final
Connection
connection
,
final
String
table
)
throws
SQLException
{
public
static
Collection
<
IndexMetaData
>
load
(
final
Connection
connection
,
final
String
table
,
final
String
databaseType
)
throws
SQLException
{
Collection
<
IndexMetaData
>
result
=
new
HashSet
<>();
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getIndexInfo
(
connection
.
getCatalog
(),
connection
.
getSchema
(
),
table
,
false
,
false
))
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getIndexInfo
(
connection
.
getCatalog
(),
JdbcUtil
.
getSchema
(
connection
,
databaseType
),
table
,
false
,
false
))
{
while
(
resultSet
.
next
())
{
String
indexName
=
resultSet
.
getString
(
INDEX_NAME
);
if
(
null
!=
indexName
)
{
...
...
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/schema/SchemaMetaDataLoader.java
浏览文件 @
0373b490
...
...
@@ -41,6 +41,7 @@ import java.util.concurrent.ExecutionException;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil
;
/**
* Schema meta data loader.
...
...
@@ -79,7 +80,7 @@ public final class SchemaMetaDataLoader {
public
static
SchemaMetaData
load
(
final
DataSource
dataSource
,
final
int
maxConnectionCount
,
final
String
databaseType
,
final
Collection
<
String
>
excludedTableNames
)
throws
SQLException
{
List
<
String
>
tableNames
;
try
(
MetaDataConnection
connection
=
new
MetaDataConnection
(
dataSource
.
getConnection
()))
{
tableNames
=
loadAllTableNames
(
connection
);
tableNames
=
loadAllTableNames
(
connection
,
databaseType
);
tableNames
.
removeAll
(
excludedTableNames
);
}
log
.
info
(
"Loading {} tables' meta data."
,
tableNames
.
size
());
...
...
@@ -96,15 +97,15 @@ public final class SchemaMetaDataLoader {
try
(
MetaDataConnection
connection
=
new
MetaDataConnection
(
con
))
{
Map
<
String
,
TableMetaData
>
result
=
new
LinkedHashMap
<>();
for
(
String
each
:
tables
)
{
result
.
put
(
each
,
new
TableMetaData
(
ColumnMetaDataLoader
.
load
(
connection
,
each
,
databaseType
),
IndexMetaDataLoader
.
load
(
connection
,
each
)));
result
.
put
(
each
,
new
TableMetaData
(
ColumnMetaDataLoader
.
load
(
connection
,
each
,
databaseType
),
IndexMetaDataLoader
.
load
(
connection
,
each
,
databaseType
)));
}
return
result
;
}
}
private
static
List
<
String
>
loadAllTableNames
(
final
Connection
connection
)
throws
SQLException
{
private
static
List
<
String
>
loadAllTableNames
(
final
Connection
connection
,
final
String
databaseType
)
throws
SQLException
{
List
<
String
>
result
=
new
LinkedList
<>();
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getTables
(
connection
.
getCatalog
(),
connection
.
getSchema
(
),
null
,
new
String
[]{
TABLE_TYPE
}))
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getTables
(
connection
.
getCatalog
(),
JdbcUtil
.
getSchema
(
connection
,
databaseType
),
null
,
new
String
[]{
TABLE_TYPE
}))
{
while
(
resultSet
.
next
())
{
String
table
=
resultSet
.
getString
(
TABLE_NAME
);
if
(!
isSystemTable
(
table
))
{
...
...
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
浏览文件 @
0373b490
...
...
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaDataL
import
javax.sql.DataSource
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil
;
/**
* Table meta data loader.
...
...
@@ -46,15 +47,15 @@ public final class TableMetaDataLoader {
*/
public
static
Optional
<
TableMetaData
>
load
(
final
DataSource
dataSource
,
final
String
table
,
final
String
databaseType
)
throws
SQLException
{
try
(
MetaDataConnection
connection
=
new
MetaDataConnection
(
dataSource
.
getConnection
()))
{
if
(!
isTableExist
(
connection
,
table
))
{
if
(!
isTableExist
(
connection
,
table
,
databaseType
))
{
return
Optional
.
empty
();
}
return
Optional
.
of
(
new
TableMetaData
(
ColumnMetaDataLoader
.
load
(
connection
,
table
,
databaseType
),
IndexMetaDataLoader
.
load
(
connection
,
table
)));
return
Optional
.
of
(
new
TableMetaData
(
ColumnMetaDataLoader
.
load
(
connection
,
table
,
databaseType
),
IndexMetaDataLoader
.
load
(
connection
,
table
,
databaseType
)));
}
}
private
static
boolean
isTableExist
(
final
Connection
connection
,
final
String
table
)
throws
SQLException
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getTables
(
connection
.
getCatalog
(),
connection
.
getSchema
(
),
table
,
null
))
{
private
static
boolean
isTableExist
(
final
Connection
connection
,
final
String
table
,
final
String
databaseType
)
throws
SQLException
{
try
(
ResultSet
resultSet
=
connection
.
getMetaData
().
getTables
(
connection
.
getCatalog
(),
JdbcUtil
.
getSchema
(
connection
,
databaseType
),
table
,
null
))
{
return
resultSet
.
next
();
}
}
...
...
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtil.java
0 → 100644
浏览文件 @
0373b490
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.sql.parser.binder.metadata.util
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
/**
* JDBC util.
*/
public
class
JdbcUtil
{
/**
* Get schema.
*
* @param connection connection
* @param databaseType database type
* @return schema
*/
public
static
String
getSchema
(
final
Connection
connection
,
final
String
databaseType
)
{
String
result
=
null
;
try
{
if
(
"Oracle"
.
equals
(
databaseType
))
{
return
null
;
}
result
=
connection
.
getSchema
();
}
catch
(
final
SQLException
ignore
)
{
}
return
result
;
}
}
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/index/IndexMetaDataLoaderTest.java
0 → 100644
浏览文件 @
0373b490
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.sql.parser.binder.metadata.index
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Collection
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
final
class
IndexMetaDataLoaderTest
{
private
static
final
String
TEST_CATALOG
=
"catalog"
;
private
static
final
String
TEST_TABLE
=
"table"
;
@Mock
private
Connection
connection
;
@Mock
private
DatabaseMetaData
databaseMetaData
;
@Mock
private
ResultSet
indexResultSet
;
@Before
public
void
setUp
()
throws
SQLException
{
when
(
connection
.
getCatalog
()).
thenReturn
(
TEST_CATALOG
);
when
(
connection
.
getMetaData
()).
thenReturn
(
databaseMetaData
);
}
@Test
public
void
assertLoad
()
throws
SQLException
{
when
(
databaseMetaData
.
getIndexInfo
(
TEST_CATALOG
,
JdbcUtil
.
getSchema
(
connection
,
"Mysql"
),
TEST_TABLE
,
false
,
false
)).
thenReturn
(
indexResultSet
);
when
(
indexResultSet
.
next
()).
thenReturn
(
true
,
true
,
false
);
when
(
indexResultSet
.
getString
(
"INDEX_NAME"
)).
thenReturn
(
"my_index"
);
Collection
<
IndexMetaData
>
actual
=
IndexMetaDataLoader
.
load
(
connection
,
TEST_TABLE
,
"Mysql"
);
assertThat
(
actual
.
size
(),
is
(
1
));
IndexMetaData
indexMetaData
=
actual
.
iterator
().
next
();
assertThat
(
indexMetaData
.
getName
(),
is
(
"my_index"
));
}
@Test
public
void
assertOracleLoad
()
throws
SQLException
{
when
(
databaseMetaData
.
getIndexInfo
(
TEST_CATALOG
,
JdbcUtil
.
getSchema
(
connection
,
"Oracle"
),
TEST_TABLE
,
false
,
false
)).
thenReturn
(
indexResultSet
);
when
(
indexResultSet
.
next
()).
thenReturn
(
true
,
true
,
false
);
when
(
indexResultSet
.
getString
(
"INDEX_NAME"
)).
thenReturn
(
"my_index"
);
Collection
<
IndexMetaData
>
actual
=
IndexMetaDataLoader
.
load
(
connection
,
TEST_TABLE
,
"Oracle"
);
assertThat
(
actual
.
size
(),
is
(
1
));
IndexMetaData
indexMetaData
=
actual
.
iterator
().
next
();
assertThat
(
indexMetaData
.
getName
(),
is
(
"my_index"
));
}
}
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
0 → 100644
浏览文件 @
0373b490
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.sql.parser.binder.metadata.table
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.ResultSet
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.Types
;
import
java.util.Map
;
import
java.util.Optional
;
import
javax.sql.DataSource
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaData
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
final
class
TableMetaDataLoaderTest
{
private
static
final
String
TEST_CATALOG
=
"catalog"
;
private
static
final
String
TEST_TABLE
=
"table"
;
@Mock
private
DataSource
dataSource
;
@Mock
private
Connection
connection
;
@Mock
private
DatabaseMetaData
databaseMetaData
;
@Mock
private
ResultSet
primaryResultSet
;
@Mock
private
ResultSet
tableExistResultSet
;
@Mock
private
ResultSet
tableNotExistResultSet
;
@Mock
private
ResultSet
columnResultSet
;
@Mock
private
Statement
statement
;
@Mock
private
ResultSet
caseSensitivesResultSet
;
@Mock
private
ResultSetMetaData
resultSetMetaData
;
@Mock
private
ResultSet
indexResultSet
;
@Before
public
void
setUp
()
throws
SQLException
{
when
(
dataSource
.
getConnection
()).
thenReturn
(
connection
);
when
(
connection
.
getCatalog
()).
thenReturn
(
TEST_CATALOG
);
when
(
connection
.
getMetaData
()).
thenReturn
(
databaseMetaData
);
when
(
databaseMetaData
.
getTables
(
TEST_CATALOG
,
null
,
TEST_TABLE
,
null
)).
thenReturn
(
tableExistResultSet
);
when
(
tableExistResultSet
.
next
()).
thenReturn
(
true
);
when
(
databaseMetaData
.
getColumns
(
TEST_CATALOG
,
null
,
TEST_TABLE
,
"%"
)).
thenReturn
(
columnResultSet
);
when
(
columnResultSet
.
next
()).
thenReturn
(
true
,
true
,
false
);
when
(
columnResultSet
.
getString
(
"COLUMN_NAME"
)).
thenReturn
(
"pk_col"
,
"col"
);
when
(
columnResultSet
.
getInt
(
"DATA_TYPE"
)).
thenReturn
(
Types
.
INTEGER
,
Types
.
VARCHAR
);
when
(
columnResultSet
.
getString
(
"TYPE_NAME"
)).
thenReturn
(
"INT"
,
"VARCHAR"
);
when
(
databaseMetaData
.
getPrimaryKeys
(
TEST_CATALOG
,
null
,
TEST_TABLE
)).
thenReturn
(
primaryResultSet
);
when
(
primaryResultSet
.
next
()).
thenReturn
(
true
,
false
);
when
(
primaryResultSet
.
getString
(
"COLUMN_NAME"
)).
thenReturn
(
"pk_col"
);
when
(
connection
.
createStatement
()).
thenReturn
(
statement
);
when
(
statement
.
executeQuery
(
anyString
())).
thenReturn
(
caseSensitivesResultSet
);
when
(
caseSensitivesResultSet
.
findColumn
(
"pk_col"
)).
thenReturn
(
1
);
when
(
caseSensitivesResultSet
.
findColumn
(
"col"
)).
thenReturn
(
2
);
when
(
caseSensitivesResultSet
.
getMetaData
()).
thenReturn
(
resultSetMetaData
);
when
(
resultSetMetaData
.
isCaseSensitive
(
1
)).
thenReturn
(
true
);
when
(
databaseMetaData
.
getIndexInfo
(
TEST_CATALOG
,
null
,
TEST_TABLE
,
false
,
false
)).
thenReturn
(
indexResultSet
);
when
(
indexResultSet
.
next
()).
thenReturn
(
true
,
false
);
when
(
indexResultSet
.
getString
(
"INDEX_NAME"
)).
thenReturn
(
"my_index"
);
}
@Test
public
void
assertLoad
()
throws
SQLException
{
Optional
<
TableMetaData
>
actual
=
TableMetaDataLoader
.
load
(
dataSource
,
TEST_TABLE
,
""
);
assertThat
(
actual
.
isPresent
(),
is
(
true
));
Map
<
String
,
ColumnMetaData
>
columnMetaDataMap
=
actual
.
get
().
getColumns
();
assertThat
(
columnMetaDataMap
.
size
(),
is
(
2
));
assertColumnMetaData
(
columnMetaDataMap
.
get
(
"pk_col"
),
"pk_col"
,
Types
.
INTEGER
,
"INT"
,
true
,
true
);
assertColumnMetaData
(
columnMetaDataMap
.
get
(
"col"
),
"col"
,
Types
.
VARCHAR
,
"VARCHAR"
,
false
,
false
);
Map
<
String
,
IndexMetaData
>
indexMetaDataMap
=
actual
.
get
().
getIndexes
();
assertThat
(
indexMetaDataMap
.
size
(),
is
(
1
));
assertThat
(
indexMetaDataMap
.
containsKey
(
"my_index"
),
is
(
true
));
}
@Test
public
void
assertTableNotExist
()
throws
SQLException
{
when
(
databaseMetaData
.
getTables
(
TEST_CATALOG
,
null
,
TEST_TABLE
,
null
)).
thenReturn
(
tableNotExistResultSet
);
when
(
tableNotExistResultSet
.
next
()).
thenReturn
(
false
);
Optional
<
TableMetaData
>
actual
=
TableMetaDataLoader
.
load
(
dataSource
,
TEST_TABLE
,
""
);
assertThat
(
actual
.
isPresent
(),
is
(
false
));
}
private
void
assertColumnMetaData
(
final
ColumnMetaData
actual
,
final
String
name
,
final
int
dataType
,
final
String
typeName
,
final
boolean
primaryKey
,
final
boolean
caseSensitive
)
{
assertThat
(
actual
.
getName
(),
is
(
name
));
assertThat
(
actual
.
getDataType
(),
is
(
dataType
));
assertThat
(
actual
.
getDataTypeName
(),
is
(
typeName
));
assertThat
(
actual
.
isPrimaryKey
(),
is
(
primaryKey
));
assertThat
(
actual
.
isCaseSensitive
(),
is
(
caseSensitive
));
}
}
shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/util/JdbcUtilTest.java
0 → 100644
浏览文件 @
0373b490
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.sql.parser.binder.metadata.util
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
CoreMatchers
.
nullValue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
final
class
JdbcUtilTest
{
@Mock
private
Connection
connection
;
@Test
public
void
assertMysqlGetSchema
()
throws
SQLException
{
when
(
connection
.
getSchema
()).
thenReturn
(
"demo_ds"
);
String
schema
=
JdbcUtil
.
getSchema
(
connection
,
""
);
assertThat
(
schema
,
is
(
"demo_ds"
));
}
@Test
public
void
assertOracleGetSchema
()
{
String
schema
=
JdbcUtil
.
getSchema
(
connection
,
"Oracle"
);
assertThat
(
schema
,
is
(
nullValue
()));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录