forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove ShardingSphereSchema.dataSourceParameters (apache#6569)
* Decouple ShardingSphereSchema.dataSourceParameters * Remove ShardingSphereSchema.dataSourceParameters * Add ProxyDataSourceContext * Refactor SchemaContextsBuilder
- Loading branch information
Showing
7 changed files
with
127 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.proxy.backend.schema; | ||
|
||
import lombok.Getter; | ||
import org.apache.shardingsphere.infra.database.type.DatabaseType; | ||
import org.apache.shardingsphere.infra.database.type.DatabaseTypes; | ||
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; | ||
import org.apache.shardingsphere.infra.exception.ShardingSphereException; | ||
import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter; | ||
import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.JDBCRawBackendDataSourceFactory; | ||
import org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Proxy data source context. | ||
*/ | ||
@Getter | ||
public final class ProxyDataSourceContext { | ||
|
||
private final DatabaseType databaseType; | ||
|
||
private final Map<String, Map<String, DataSource>> dataSourcesMap; | ||
|
||
public ProxyDataSourceContext(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) { | ||
databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType() : DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources)); | ||
dataSourcesMap = createDataSourcesMap(schemaDataSources); | ||
} | ||
|
||
private static String getDatabaseTypeName(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) { | ||
return JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType(); | ||
} | ||
|
||
private static Map<String, Map<String, DataSource>> createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) { | ||
return schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); | ||
} | ||
|
||
private static Map<String, DataSource> createDataSources(final Map<String, DataSourceParameter> dataSourceParameters) { | ||
Map<String, DataSource> result = new LinkedHashMap<>(dataSourceParameters.size(), 1); | ||
for (Entry<String, DataSourceParameter> entry : dataSourceParameters.entrySet()) { | ||
try { | ||
result.put(entry.getKey(), JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), entry.getValue())); | ||
// CHECKSTYLE:OFF | ||
} catch (final Exception ex) { | ||
// CHECKSTYLE:ON | ||
throw new ShardingSphereException(String.format("Can not build data source, name is `%s`.", entry.getKey()), ex); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.