Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move getSchemas method to ShardingSphereDatabase #17798

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public Optional<EncryptContext> findEncryptContext(final int columnIndex) {
}
TablesContext tablesContext = selectStatementContext.getTablesContext();
String schemaName = tablesContext.getSchemaName().orElse(selectStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName()));
Map<String, String> expressionTableNames = tablesContext.findTableNamesByColumnProjection(Collections.singletonList(columnProjection.get()), databaseMetaData.getSchema(schemaName));
Map<String, String> expressionTableNames = tablesContext.findTableNamesByColumnProjection(
Collections.singletonList(columnProjection.get()), databaseMetaData.getDatabase().getSchema(schemaName));
Optional<String> tableName = findTableName(columnProjection.get(), expressionTableNames);
return tableName.map(optional -> EncryptContextBuilder.build(databaseName, schemaName, optional, columnProjection.get().getName(), encryptRule));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void setUp() {
when(selectStatementContext.getTablesContext()).thenReturn(tablesContext);
when(selectStatementContext.getDatabaseType()).thenReturn(new MySQLDatabaseType());
when(databaseMetaData.getDatabase().getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(databaseMetaData.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
when(databaseMetaData.getDatabase().getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("MD5", new Properties()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public MergedResult merge(final List<QueryResult> queryResults, final SQLStateme
if (dalStatement instanceof MySQLShowDatabasesStatement) {
return new SingleLocalDataMergedResult(Collections.singletonList(databaseName));
}
ShardingSphereSchema schema = databaseMetaData.getSchema(schemaName);
ShardingSphereSchema schema = databaseMetaData.getDatabase().getSchema(schemaName);
if (dalStatement instanceof MySQLShowTablesStatement) {
return new LogicTablesMergedResult(shardingRule, sqlStatementContext, schema, queryResults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private Map<String, Integer> getColumnLabelIndexMap(final QueryResult queryResul
private MergedResult build(final List<QueryResult> queryResults, final SelectStatementContext selectStatementContext,
final Map<String, Integer> columnLabelIndexMap, final ShardingSphereDatabaseMetaData databaseMetaData) throws SQLException {
String defaultSchema = selectStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = selectStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = selectStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
if (isNeedProcessGroupBy(selectStatementContext)) {
return getGroupByMergedResult(queryResults, selectStatementContext, columnLabelIndexMap, schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public List<ShardingCondition> createShardingConditions(final SQLStatementContex
}
Collection<ColumnSegment> columnSegments = ((WhereAvailable) sqlStatementContext).getColumnSegments();
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
Map<String, String> columnExpressionTableNames = sqlStatementContext.getTablesContext().findTableNamesByColumnSegment(columnSegments, schema);
List<ShardingCondition> result = new ArrayList<>();
for (WhereSegment each : ((WhereAvailable) sqlStatementContext).getWhereSegments()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
final List<Object> parameters, final ShardingSphereDatabaseMetaData databaseMetaData) {
Optional<IndexSegment> index = sqlStatementContext.getSqlStatement().getIndex();
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = index.isPresent() ? index.get().getOwner().map(optional -> optional.getIdentifier().getValue())
.map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema)) : databaseMetaData.getSchema(defaultSchema);
ShardingSphereSchema schema = index.flatMap(optional -> optional.getOwner()
.map(owner -> databaseMetaData.getDatabase().getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
if (index.isPresent() && !isSchemaContainsIndex(schema, index.get())) {
throw new ShardingSphereException("Index '%s' does not exist.", index.get().getIndexName().getIdentifier().getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
validateShardingTable(shardingRule, existTables);
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getSqlStatement().getFunctionName().flatMap(optional -> optional.getOwner()
.map(owner -> databaseMetaData.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
.map(owner -> databaseMetaData.getDatabase().getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
validateTableExist(schema, existTables);
validateTableNotExist(schema, extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public final class ShardingCreateIndexStatementValidator extends ShardingDDLStat
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<CreateIndexStatement> sqlStatementContext,
final List<Object> parameters, final ShardingSphereDatabaseMetaData databaseMetaData) {
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
validateTableExist(schema, Collections.singletonList(sqlStatementContext.getSqlStatement().getTable()));
String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
String indexName = ((IndexAvailable) sqlStatementContext).getIndexes().stream().map(each -> each.getIndexName().getIdentifier().getValue()).findFirst().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
TableExtractor extractor = new TableExtractor();
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getSqlStatement().getProcedureName().flatMap(optional -> optional.getOwner()
.map(owner -> databaseMetaData.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
.map(owner -> databaseMetaData.getDatabase().getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
Collection<SimpleTableSegment> existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
validateShardingTable(shardingRule, existTables);
validateTableExist(schema, existTables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
final List<Object> parameters, final ShardingSphereDatabaseMetaData databaseMetaData) {
if (!CreateTableStatementHandler.containsNotExistClause(sqlStatementContext.getSqlStatement())) {
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
validateTableNotExist(schema, Collections.singletonList(sqlStatementContext.getSqlStatement().getTable()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
}
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
for (IndexSegment each : sqlStatementContext.getSqlStatement().getIndexes()) {
ShardingSphereSchema schema = each.getOwner()
.map(optional -> optional.getIdentifier().getValue()).map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = each.getOwner().map(optional -> optional.getIdentifier().getValue())
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
if (!isSchemaContainsIndex(schema, each)) {
throw new ShardingSphereException("Index '%s' does not exist.", each.getIndexName().getIdentifier().getValue());
}
Expand All @@ -66,7 +66,7 @@ public void postValidate(final ShardingRule shardingRule, final SQLStatementCont
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
for (IndexSegment each : indexSegments) {
ShardingSphereSchema schema = each.getOwner().map(optional -> optional.getIdentifier().getValue())
.map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
logicTableName = schema.getAllTableNames().stream().filter(tableName -> schema.get(tableName).getIndexes().containsKey(each.getIndexName().getIdentifier().getValue())).findFirst();
logicTableName.ifPresent(optional -> validateDropIndexRouteUnit(shardingRule, routeContext, indexSegments, optional));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void preValidate(final ShardingRule shardingRule,
final SQLStatementContext<DropTableStatement> sqlStatementContext, final List<Object> parameters, final ShardingSphereDatabaseMetaData databaseMetaData) {
if (!DropTableStatementHandler.containsExistClause(sqlStatementContext.getSqlStatement())) {
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
validateTableExist(schema, sqlStatementContext.getTablesContext().getTables());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ public boolean isAllBindingTables(final ShardingSphereDatabaseMetaData databaseM
return false;
}
String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseMetaData.getDatabase().getName());
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(databaseMetaData::getSchema).orElseGet(() -> databaseMetaData.getSchema(defaultSchema));
ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName()
.map(optional -> databaseMetaData.getDatabase().getSchema(optional)).orElseGet(() -> databaseMetaData.getDatabase().getSchema(defaultSchema));
SelectStatementContext select = (SelectStatementContext) sqlStatementContext;
Collection<WhereSegment> joinSegments = WhereExtractUtil.getJoinWhereSegments(select.getSqlStatement());
return isJoinConditionContainsShardingColumns(schema, select, logicTableNames, joinSegments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -83,8 +84,8 @@ public void assertNewInstanceWithSelectStatementForSQLServer() {

private void assertNewInstanceWithSelectStatement(final SelectStatement selectStatement) {
ConfigurationProperties props = new ConfigurationProperties(new Properties());
ShardingSphereDatabaseMetaData databaseMetaData = mock(ShardingSphereDatabaseMetaData.class);
when(databaseMetaData.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
ShardingSphereDatabaseMetaData databaseMetaData = mock(ShardingSphereDatabaseMetaData.class, RETURNS_DEEP_STUBS);
when(databaseMetaData.getDatabase().getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
selectStatement.setProjections(new ProjectionsSegment(0, 0));
SelectStatementContext sqlStatementContext = new SelectStatementContext(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, databaseMetaData),
Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME);
Expand Down Expand Up @@ -113,8 +114,8 @@ public void assertNewInstanceWithOtherStatement() {
}

private InsertStatementContext createInsertStatementContext(final InsertStatement insertStatement) {
ShardingSphereDatabaseMetaData databaseMetaData = mock(ShardingSphereDatabaseMetaData.class);
when(databaseMetaData.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
ShardingSphereDatabaseMetaData databaseMetaData = mock(ShardingSphereDatabaseMetaData.class, RETURNS_DEEP_STUBS);
when(databaseMetaData.getDatabase().getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
Map<String, ShardingSphereDatabaseMetaData> databaseMetaDataMap = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, databaseMetaData);
return new InsertStatementContext(databaseMetaDataMap, Collections.emptyList(), insertStatement, DefaultDatabase.LOGIC_NAME);
}
Expand Down
Loading