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

Rename schema name to database name in shardingsphere-proxy module #18187

Merged
merged 1 commit into from
Jun 6, 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 @@ -93,13 +93,13 @@ private static void logWarningBanner() {
/**
* Get Vert.x sql connection future.
*
* @param schemaName schema name
* @param databaseName database name
* @param dataSourceName data source name
* @param connectionSize connection size
* @return futures of sql connections
*/
public List<Future<SqlConnection>> getConnections(final String schemaName, final String dataSourceName, final int connectionSize) {
Pool pool = getPool(schemaName, dataSourceName);
public List<Future<SqlConnection>> getConnections(final String databaseName, final String dataSourceName, final int connectionSize) {
Pool pool = getPool(databaseName, dataSourceName);
List<Future<SqlConnection>> result = new ArrayList<>(connectionSize);
for (int i = 0; i < connectionSize; i++) {
result.add(pool.getConnection());
Expand All @@ -110,24 +110,24 @@ public List<Future<SqlConnection>> getConnections(final String schemaName, final
/**
* Get Vert.x pool.
*
* @param schemaName schema name
* @param databaseName database name
* @param dataSourceName data source name
* @return Vert.x pool
*/
public Pool getPool(final String schemaName, final String dataSourceName) {
Map<String, Pool> vertxPools = schemaVertxPools.get(schemaName);
public Pool getPool(final String databaseName, final String dataSourceName) {
Map<String, Pool> vertxPools = schemaVertxPools.get(databaseName);
if (null == vertxPools) {
vertxPools = schemaVertxPools.computeIfAbsent(schemaName, unused -> new ConcurrentHashMap<>());
vertxPools = schemaVertxPools.computeIfAbsent(databaseName, unused -> new ConcurrentHashMap<>());
}
Pool result = vertxPools.get(dataSourceName);
if (null == result) {
result = vertxPools.computeIfAbsent(dataSourceName, unused -> createPoolFromSchemaDataSource(schemaName, dataSourceName));
result = vertxPools.computeIfAbsent(dataSourceName, unused -> createPoolFromSchemaDataSource(databaseName, dataSourceName));
}
return result;
}

private Pool createPoolFromSchemaDataSource(final String schemaName, final String dataSourceName) {
DataSource dataSource = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(schemaName).getResource().getDataSources().get(dataSourceName);
private Pool createPoolFromSchemaDataSource(final String databaseName, final String dataSourceName) {
DataSource dataSource = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(databaseName).getResource().getDataSources().get(dataSourceName);
Preconditions.checkNotNull(dataSource, "Can not get connection from datasource %s.", dataSourceName);
HikariDataSource value = (HikariDataSource) dataSource;
URI uri = URI.create(value.getJdbcUrl().replace("jdbc:", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public interface DatabaseAdminExecutorCreator extends TypedSPI {
*
* @param sqlStatementContext SQL statement context
* @param sql SQL
* @param schemaName schema name
* @param databaseName database name
* @return created instance
*/
Optional<DatabaseAdminExecutor> create(SQLStatementContext<?> sqlStatementContext, String sql, String schemaName);
Optional<DatabaseAdminExecutor> create(SQLStatementContext<?> sqlStatementContext, String sql, String databaseName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlSt
}

@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String schemaName) {
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String databaseName) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof UseStatement) {
return Optional.of(new UseDatabaseExecutor((UseStatement) sqlStatement));
Expand Down Expand Up @@ -131,7 +131,7 @@ public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlSt
// TODO
return Optional.empty();
}
return Optional.ofNullable(mockExecutor(schemaName, (SelectStatement) sqlStatement, sql));
return Optional.ofNullable(mockExecutor(databaseName, (SelectStatement) sqlStatement, sql));
}
return Optional.empty();
}
Expand All @@ -157,8 +157,8 @@ private boolean isQuerySpecialSchema(final SelectStatement sqlStatement, final S
return ((SimpleTableSegment) tableSegment).getOwner().isPresent() && specialSchemaName.equalsIgnoreCase(((SimpleTableSegment) tableSegment).getOwner().get().getIdentifier().getValue());
}

private DatabaseAdminExecutor mockExecutor(final String schemaName, final SelectStatement sqlStatement, final String sql) {
boolean isNotUseSchema = !Optional.ofNullable(schemaName).isPresent() && sqlStatement.getFrom() == null;
private DatabaseAdminExecutor mockExecutor(final String databaseName, final SelectStatement sqlStatement, final String sql) {
boolean isNotUseSchema = !Optional.ofNullable(databaseName).isPresent() && sqlStatement.getFrom() == null;
if (!hasDatabases() || !hasResources()) {
return new NoResourceShowExecutor(sqlStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public final class ShowDatabasesExecutor implements DatabaseAdminQueryExecutor {

@Override
public void execute(final ConnectionSession connectionSession) {
mergedResult = new SingleLocalDataMergedResult(getSchemaNames(connectionSession));
mergedResult = new SingleLocalDataMergedResult(getDatabaseNames(connectionSession));
}

private Collection<Object> getSchemaNames(final ConnectionSession connectionSession) {
private Collection<Object> getDatabaseNames(final ConnectionSession connectionSession) {
Collection<Object> result = new LinkedList<>();
for (String each : ProxyContext.getInstance().getAllDatabaseNames()) {
if (checkLikePattern(each) && SQLCheckEngine.check(each, getRules(each), connectionSession.getGrantee())) {
Expand All @@ -64,17 +64,17 @@ private Collection<Object> getSchemaNames(final ConnectionSession connectionSess
return result;
}

private boolean checkLikePattern(final String schemaName) {
private boolean checkLikePattern(final String databaseName) {
if (showDatabasesStatement.getFilter().isPresent()) {
Optional<String> pattern = showDatabasesStatement.getFilter().get().getLike().map(optional -> SQLUtil.convertLikePatternToRegex(optional.getPattern()));
return !pattern.isPresent() || schemaName.matches(pattern.get());
return !pattern.isPresent() || databaseName.matches(pattern.get());
}
return true;
}

private Collection<ShardingSphereRule> getRules(final String schemaName) {
private Collection<ShardingSphereRule> getRules(final String databaseName) {
Collection<ShardingSphereRule> result;
result = new LinkedList<>(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(schemaName).getRuleMetaData().getRules());
result = new LinkedList<>(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(databaseName).getRuleMetaData().getRules());
result.addAll(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void execute(final ConnectionSession connectionSession) {
throw new UnknownDatabaseException(databaseName);
}

private Collection<ShardingSphereRule> getRules(final String schemaName) {
private Collection<ShardingSphereRule> getRules(final String databaseName) {
Collection<ShardingSphereRule> result = new LinkedList<>();
Optional.ofNullable(ProxyContext.getInstance()
.getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(schemaName)).ifPresent(optional -> result.addAll(optional.getRuleMetaData().getRules()));
.getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(databaseName)).ifPresent(optional -> result.addAll(optional.getRuleMetaData().getRules()));
result.addAll(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ private void removeDuplicatedRow() {

@Override
protected List<String> getDatabaseNames(final ConnectionSession connectionSession) {
Collection<String> schemaNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
SCHEMA_WITHOUT_DATA_SOURCE.addAll(schemaNames.stream().filter(each -> !AbstractDatabaseMetadataExecutor.hasDatasource(each)).collect(Collectors.toSet()));
List<String> result = schemaNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
Collection<String> databaseNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
SCHEMA_WITHOUT_DATA_SOURCE.addAll(databaseNames.stream().filter(each -> !AbstractDatabaseMetadataExecutor.hasDatasource(each)).collect(Collectors.toSet()));
List<String> result = databaseNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
if (!SCHEMA_WITHOUT_DATA_SOURCE.isEmpty()) {
fillSchemasWithoutDatasource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlSt
}

@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String schemaName) {
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String databaseName) {
if (sqlStatementContext.getTablesContext().getTableNames().contains(OG_DATABASE)) {
return Optional.of(new OpenGaussSelectDatabaseExecutor(sql));
}
return delegated.create(sqlStatementContext, sql, schemaName);
return delegated.create(sqlStatementContext, sql, databaseName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlSt
}

@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String schemaName) {
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String databaseName) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof SelectStatement) {
Collection<String> selectedTableNames = getSelectedTableNames((SelectStatement) sqlStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private void removeDuplicatedRow() {

@Override
protected List<String> getDatabaseNames(final ConnectionSession connectionSession) {
Collection<String> schemaNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
return schemaNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
Collection<String> databaseNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
return databaseNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ protected void initDatabaseData(final String databaseName) {

@Override
protected List<String> getDatabaseNames(final ConnectionSession connectionSession) {
Collection<String> schemaNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
return schemaNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
Collection<String> databaseNames = ProxyContext.getInstance().getAllDatabaseNames().stream().filter(each -> hasAuthority(each, connectionSession.getGrantee())).collect(Collectors.toList());
return databaseNames.stream().filter(AbstractDatabaseMetadataExecutor::hasDatasource).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlSt
}

@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String schemaName) {
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> sqlStatementContext, final String sql, final String databaseName) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ private ShardingSphereDatabase createDatabase(final Map<String, String> expected
Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection(expectedResultSetMap)))), mock(ShardingSphereRuleMetaData.class), Collections.emptyMap());
}

private ShardingSphereDatabase createEmptyDatabase(final String schemaName) {
private ShardingSphereDatabase createEmptyDatabase(final String databaseName) {
ShardingSphereRuleMetaData ruleMetaData = mock(ShardingSphereRuleMetaData.class);
when(ruleMetaData.getRules()).thenReturn(Collections.singleton(mock(AuthorityRule.class, RETURNS_DEEP_STUBS)));
return new ShardingSphereDatabase(schemaName, new MySQLDatabaseType(), new ShardingSphereResource(Collections.emptyMap()), ruleMetaData, Collections.emptyMap());
return new ShardingSphereDatabase(databaseName, new MySQLDatabaseType(), new ShardingSphereResource(Collections.emptyMap()), ruleMetaData, Collections.emptyMap());
}

private Connection mockConnection(final Map<String, String> expectedResultSetMap) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public MySQLFrontendEngine() {
}

@Override
public void setDatabaseVersion(final String schemaName, final String databaseVersion) {
MySQLServerInfo.setServerVersion(schemaName, databaseVersion);
public void setDatabaseVersion(final String databaseName, final String databaseVersion) {
MySQLServerInfo.setServerVersion(databaseName, databaseVersion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public FrontendContext getFrontendContext() {
}

@Override
public void setDatabaseVersion(final String schemaName, final String databaseVersion) {
public void setDatabaseVersion(final String databaseName, final String databaseVersion) {
PostgreSQLServerInfo.setServerVersion(databaseVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class PostgreSQLFrontendEngine implements DatabaseProtocolFrontendE
private final DatabasePacketCodecEngine<PostgreSQLPacket> codecEngine = new PostgreSQLPacketCodecEngine();

@Override
public void setDatabaseVersion(final String schemaName, final String databaseVersion) {
public void setDatabaseVersion(final String databaseName, final String databaseVersion) {
PostgreSQLServerInfo.setServerVersion(databaseVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public Collection<DatabasePacket<?>> execute() {
return Collections.singletonList(PostgreSQLParseCompletePacket.getInstance());
}

private ShardingSphereSQLParserEngine createShardingSphereSQLParserEngine(final String schemaName) {
private ShardingSphereSQLParserEngine createShardingSphereSQLParserEngine(final String databaseName) {
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
Optional<SQLParserRule> sqlParserRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().findSingleRule(SQLParserRule.class);
Preconditions.checkState(sqlParserRule.isPresent());
return sqlParserRule.get().getSQLParserEngine(DatabaseTypeEngine.getTrunkDatabaseTypeName(metaDataContexts.getMetaData().getDatabases().get(schemaName).getProtocolType()));
return sqlParserRule.get().getSQLParserEngine(DatabaseTypeEngine.getTrunkDatabaseTypeName(metaDataContexts.getMetaData().getDatabases().get(databaseName).getProtocolType()));
}

private String convertSQLToJDBCStyle(final SQLStatement sqlStatement, final String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public ReactiveMySQLComStmtExecuteExecutor(final MySQLComStmtExecutePacket packe
.newBinaryProtocolInstance(sqlStatementContext, packet.getSql(), packet.getParameters(), connectionSession.getBackendConnection());
}

private static Collection<ShardingSphereRule> getRules(final String schemaName) {
private static Collection<ShardingSphereRule> getRules(final String databaseName) {
Collection<ShardingSphereRule> result;
result = new LinkedList<>(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(schemaName).getRuleMetaData().getRules());
result = new LinkedList<>(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().get(databaseName).getRuleMetaData().getRules());
result.addAll(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public interface DatabaseProtocolFrontendEngine extends TypedSPI {
/**
* Set database version.
*
* @param schemaName schema name
* @param databaseName database name
* @param databaseVersion database version
*/
default void setDatabaseVersion(String schemaName, String databaseVersion) {
default void setDatabaseVersion(String databaseName, String databaseVersion) {
}

/**
Expand Down