use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.
the class OrchestrationSpringBootConfiguration method setDataSourceMap.
private void setDataSourceMap(final Environment environment) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "sharding.jdbc.datasource.");
String dataSources = propertyResolver.getProperty("names");
if (StringUtils.isEmpty(dataSources)) {
return;
}
dataSources = dataSources.trim();
for (String each : dataSources.split(",")) {
try {
Map<String, Object> dataSourceProps = propertyResolver.getSubProperties(each + ".");
Preconditions.checkState(!dataSourceProps.isEmpty(), String.format("Wrong datasource [%s] properties!", each));
DataSource dataSource = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
dataSourceMap.put(each, dataSource);
} catch (final ReflectiveOperationException ex) {
throw new ShardingJdbcException("Can't find datasource type!", ex);
}
}
}
use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.
the class DataSourceGsonTypeAdapter method read.
@SuppressWarnings("unchecked")
@Override
public NamedDataSource read(final JsonReader in) throws IOException {
String name = "";
String clazz = "";
Map<String, Object> properties = new TreeMap<>();
in.beginObject();
while (in.hasNext()) {
String jsonName = in.nextName();
switch(jsonName) {
case DataSourceGsonTypeConstants.DATASOURCE_NAME:
name = in.nextString();
break;
case DataSourceGsonTypeConstants.CLAZZ_NAME:
clazz = in.nextString();
break;
default:
properties.put(jsonName, in.nextString());
break;
}
}
in.endObject();
try {
return new NamedDataSource(name, DataSourceUtil.getDataSource(clazz, properties));
} catch (final ReflectiveOperationException ex) {
throw new ShardingJdbcException(ex);
}
}
use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.
the class SQLBuilder method toSQL.
/**
* Convert to SQL string.
*
* @param logicAndActualTableMap logic and actual map
* @param shardingRule sharding rule
* @return SQL string
*/
public String toSQL(final Map<String, String> logicAndActualTableMap, final ShardingRule shardingRule) {
StringBuilder result = new StringBuilder();
for (Object each : segments) {
if (!(each instanceof ShardingPlaceholder)) {
result.append(each);
continue;
}
String logicTableName = ((ShardingPlaceholder) each).getLogicTableName();
String actualTableName = logicAndActualTableMap.get(logicTableName);
if (each instanceof TablePlaceholder) {
result.append(null == actualTableName ? logicTableName : actualTableName);
} else if (each instanceof SchemaPlaceholder) {
SchemaPlaceholder schemaPlaceholder = (SchemaPlaceholder) each;
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName);
if (!tableRule.isPresent() && Strings.isNullOrEmpty(shardingRule.getDefaultDataSourceName())) {
throw new ShardingJdbcException("Cannot found schema name '%s' in sharding rule.", schemaPlaceholder.getLogicSchemaName());
}
// TODO 目前只能找到真实数据源名称. 未来需要在初始化sharding rule时创建connection,并验证连接是否正确,并获取出真实的schema的名字, 然后在这里替换actualDataSourceName为actualSchemaName
// TODO 目前actualDataSourceName必须actualSchemaName一样,才能保证替换schema的场景不出错, 如: show columns xxx
result.append(tableRule.get().getActualDatasourceNames().iterator().next());
} else if (each instanceof IndexPlaceholder) {
IndexPlaceholder indexPlaceholder = (IndexPlaceholder) each;
result.append(indexPlaceholder.getLogicIndexName());
if (!Strings.isNullOrEmpty(actualTableName)) {
result.append("_");
result.append(actualTableName);
}
} else {
result.append(each);
}
}
return result.toString();
}
use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.
the class ShardingJdbcDatabaseTester method getConnection.
@Override
public IDatabaseConnection getConnection() throws Exception {
IDatabaseConnection result = super.getConnection();
DatabaseConfig dbConfig = result.getConfig();
dbConfig.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, false);
dbConfig.setProperty(DatabaseConfig.FEATURE_DATATYPE_WARNING, false);
switch(driverClass) {
case "org.h2.Driver":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
break;
case "com.mysql.jdbc.Driver":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
break;
case "org.postgresql.Driver":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
break;
case "oracle.jdbc.driver.OracleDriver":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory());
break;
case "com.microsoft.sqlserver.jdbc.SQLServerDriver":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MsSqlDataTypeFactory());
break;
default:
throw new ShardingJdbcException("Unsupported JDBC driver '%s'", driverClass);
}
return result;
}
use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.
the class SpringBootConfiguration method setDataSourceMap.
@SuppressWarnings("unchecked")
private void setDataSourceMap(final Environment environment) {
String prefix = "sharding.jdbc.datasource.";
String dataSources = environment.getProperty(prefix + "names");
for (String each : dataSources.split(",")) {
try {
Map<String, Object> dataSourceProps = PropertyUtil.handle(environment, prefix + each, Map.class);
Preconditions.checkState(!dataSourceProps.isEmpty(), "Wrong datasource properties!");
DataSource dataSource = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
dataSourceMap.put(each, dataSource);
} catch (final ReflectiveOperationException ex) {
throw new ShardingJdbcException("Can't find datasource type!", ex);
}
}
}
Aggregations