Skip to content
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 @@ -449,7 +449,7 @@ public void assertIntegerInJDBCType() {
new RangeShardingValue<>("t_order", "create_time", DATA_NODE_INFO, Range.closed("04", "10")));
assertThat(actualAsMonthString.size(), is(4));
}

@Test
public void assertDateInSqlDate() {
Collection<String> actualAsLocalDate = shardingAlgorithmByJDBCDate.doSharding(availableTablesForJDBCDateDataSources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.shardingsphere.infra.binder.type.CursorAvailable;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.context.refresher.MetaDataRefreshEngine;
import org.apache.shardingsphere.infra.distsql.exception.resource.EmptyResourceException;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
Expand All @@ -44,6 +43,7 @@
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.StorageUnitNotExistedException;
import org.apache.shardingsphere.proxy.backend.handler.data.DatabaseBackendHandler;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
Expand Down Expand Up @@ -98,9 +98,9 @@ public DatabaseCommunicationEngine(final String driverType, final ShardingSphere
private void failedIfBackendNotReady(final ConnectionSession connectionSession, final SQLStatementContext<?> sqlStatementContext) {
ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(connectionSession.getDatabaseName());
boolean isSystemSchema = SystemSchemaUtil.containsSystemSchema(sqlStatementContext.getDatabaseType(), sqlStatementContext.getTablesContext().getSchemaNames(), database);
ShardingSpherePreconditions.checkState(isSystemSchema || database.containsDataSource(), () -> new EmptyResourceException(connectionSession.getDatabaseName()));
ShardingSpherePreconditions.checkState(isSystemSchema || database.containsDataSource(), () -> new StorageUnitNotExistedException(connectionSession.getDatabaseName()));
if (!isSystemSchema && !database.isComplete()) {
throw new RuleNotExistedException();
throw new RuleNotExistedException(connectionSession.getDatabaseName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class RuleNotExistedException extends MetaDataSQLException {

private static final long serialVersionUID = -4150905802300104824L;

public RuleNotExistedException() {
super(XOpenSQLState.SYNTAX_ERROR, 10, "Rule does not exist.");
public RuleNotExistedException(final String databaseName) {
super(XOpenSQLState.SYNTAX_ERROR, 10, "There is no rule in database `%s`.", databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Resource does not exist exception.
* Storage unit not existed exception.
*/
public final class ResourceNotExistedException extends MetaDataSQLException {
public final class StorageUnitNotExistedException extends MetaDataSQLException {

private static final long serialVersionUID = 4146100333670404924L;

public StorageUnitNotExistedException() {
super(XOpenSQLState.SYNTAX_ERROR, 0, "There is no storage unit in any database.");
}

public ResourceNotExistedException() {
super(XOpenSQLState.SYNTAX_ERROR, 0, "Resource does not exist.");
public StorageUnitNotExistedException(final String databaseName) {
super(XOpenSQLState.SYNTAX_ERROR, 0, "There is no storage unit in database `%s`.", databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.StorageUnitNotExistedException;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.handler.admin.FunctionWithException;

Expand Down Expand Up @@ -209,7 +209,7 @@ protected List<String> getDatabaseNames(final ConnectionSession connectionSessio
protected void getSourceData(final String databaseName, final FunctionWithException<ResultSet, SQLException> callback) throws SQLException {
ShardingSphereResourceMetaData resourceMetaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData();
Optional<Entry<String, DataSource>> dataSourceEntry = resourceMetaData.getDataSources().entrySet().stream().findFirst();
log.info("Actual SQL: {} ::: {}", dataSourceEntry.orElseThrow(ResourceNotExistedException::new).getKey(), sql);
log.info("Actual SQL: {} ::: {}", dataSourceEntry.orElseThrow(() -> new StorageUnitNotExistedException(databaseName)).getKey(), sql);
try (
Connection connection = dataSourceEntry.get().getValue().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.JDBCDatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.StorageUnitNotExistedException;
import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;
Expand Down Expand Up @@ -74,9 +75,7 @@ public final class UnicastResourceShowExecutor implements DatabaseAdminQueryExec
public void execute(final ConnectionSession connectionSession) throws SQLException {
String originDatabase = connectionSession.getDatabaseName();
String databaseName = null == originDatabase ? getFirstDatabaseName() : originDatabase;
if (!ProxyContext.getInstance().getDatabase(databaseName).containsDataSource()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getDatabase(databaseName).containsDataSource(), () -> new StorageUnitNotExistedException(databaseName));
try {
connectionSession.setCurrentDatabase(databaseName);
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData(),
Expand All @@ -97,9 +96,7 @@ private String getFirstDatabaseName() {
throw new NoDatabaseSelectedException();
}
Optional<String> result = databaseNames.stream().filter(each -> ProxyContext.getInstance().getDatabase(each).containsDataSource()).findFirst();
if (!result.isPresent()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(result.isPresent(), StorageUnitNotExistedException::new);
return result.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@
package org.apache.shardingsphere.proxy.backend.handler.data.impl;

import io.vertx.core.Future;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.dialect.exception.syntax.database.NoDatabaseSelectedException;
import org.apache.shardingsphere.infra.binder.QueryContext;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.dialect.exception.syntax.database.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.StorageUnitNotExistedException;
import org.apache.shardingsphere.proxy.backend.handler.data.DatabaseBackendHandler;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.handler.data.DatabaseBackendHandler;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

/**
* Database backend handler with unicast schema.
Expand All @@ -55,9 +56,7 @@ public final class UnicastDatabaseBackendHandler implements DatabaseBackendHandl
public Future<ResponseHeader> executeFuture() {
String originDatabase = connectionSession.getDatabaseName();
String databaseName = null == originDatabase ? getFirstDatabaseName() : originDatabase;
if (!ProxyContext.getInstance().getDatabase(databaseName).containsDataSource()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getDatabase(databaseName).containsDataSource(), () -> new StorageUnitNotExistedException(databaseName));
connectionSession.setCurrentDatabase(databaseName);
databaseCommunicationEngine = databaseCommunicationEngineFactory.newDatabaseCommunicationEngine(queryContext, connectionSession.getBackendConnection(), false);
return databaseCommunicationEngine.executeFuture().eventually(unused -> {
Expand All @@ -70,9 +69,7 @@ public Future<ResponseHeader> executeFuture() {
public ResponseHeader execute() throws SQLException {
String originDatabase = connectionSession.getDefaultDatabaseName();
String databaseName = null == originDatabase ? getFirstDatabaseName() : originDatabase;
if (!ProxyContext.getInstance().getDatabase(databaseName).containsDataSource()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getDatabase(databaseName).containsDataSource(), () -> new StorageUnitNotExistedException(databaseName));
try {
connectionSession.setCurrentDatabase(databaseName);
databaseCommunicationEngine = databaseCommunicationEngineFactory.newDatabaseCommunicationEngine(queryContext, connectionSession.getBackendConnection(), false);
Expand All @@ -87,15 +84,11 @@ private String getFirstDatabaseName() {
if (databaseNames.isEmpty()) {
throw new NoDatabaseSelectedException();
}
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(
AuthorityRule.class);
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
Optional<ShardingSpherePrivileges> privileges = authorityRule.findPrivileges(connectionSession.getGrantee());
Stream<String> databaseStream = databaseNames.stream().filter(each -> ProxyContext.getInstance().getDatabase(each).containsDataSource());
Optional<String> result = privileges.isPresent() ? databaseStream.filter(each -> privileges.get().hasPrivileges(each)).findFirst()
: databaseStream.findFirst();
if (!result.isPresent()) {
throw new RuleNotExistedException();
}
Optional<String> result = privileges.map(optional -> databaseStream.filter(optional::hasPrivileges).findFirst()).orElseGet(databaseStream::findFirst);
ShardingSpherePreconditions.checkState(result.isPresent(), StorageUnitNotExistedException::new);
return result.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.hint.enums.HintShardingType;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.hint.result.ShowShardingHintStatusResult;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.sharding.distsql.parser.statement.hint.ShowShardingHintStatusStatement;

import java.sql.Types;
Expand Down Expand Up @@ -63,9 +64,7 @@ protected List<QueryHeader> createQueryHeaders() {
protected MergedResult createMergedResult() {
Map<String, ShowShardingHintStatusResult> results = new HashMap<>();
ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(connectionSession.getDatabaseName());
if (!database.isComplete()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(database.isComplete(), () -> new RuleNotExistedException(connectionSession.getDatabaseName()));
String schemaName = DatabaseTypeEngine.getDefaultSchemaName(connectionSession.getProtocolType(), connectionSession.getDatabaseName());
Collection<String> tableNames = database.getSchema(schemaName).getAllTableNames();
for (String each : tableNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
Expand Down Expand Up @@ -106,9 +107,7 @@ protected Collection<LocalDataQueryResultRow> getRows(final ContextManager conte
setUpCursorDefinition(sqlStatementContext);
}
ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(getConnectionSession().getDatabaseName());
if (!database.isComplete()) {
throw new RuleNotExistedException();
}
ShardingSpherePreconditions.checkState(database.isComplete(), () -> new RuleNotExistedException(getConnectionSession().getDatabaseName()));
ConfigurationProperties props = metaDataContexts.getMetaData().getProps();
SQLFederationDeciderContext deciderContext = decide(queryContext, props, metaDataContexts.getMetaData().getDatabase(getConnectionSession().getDatabaseName()));
Collection<ExecutionUnit> executionUnits = deciderContext.isUseSQLFederation() ? getFederationExecutionUnits(queryContext, databaseName, metaDataContexts)
Expand Down