未验证 提交 0373b490 编写于 作者: X xiaoyu 提交者: GitHub

fix oracle connection getSchema() not exist. (#5914)

* fix oracle getSchema.

* import format.
上级 1c1741ed
......@@ -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));
}
......
......@@ -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) {
......
......@@ -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)) {
......
......@@ -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();
}
}
......
/*
* 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;
}
}
/*
* 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"));
}
}
/*
* 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));
}
}
/*
* 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.
先完成此消息的编辑!
想要评论请 注册