Search in sources :

Example 6 with ShardingJdbcException

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);
        }
    }
}
Also used : RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) DataSource(javax.sql.DataSource)

Example 7 with ShardingJdbcException

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);
    }
}
Also used : ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) TreeMap(java.util.TreeMap) NamedDataSource(io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource)

Example 8 with ShardingJdbcException

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();
}
Also used : TablePlaceholder(io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder) Optional(com.google.common.base.Optional) ShardingPlaceholder(io.shardingjdbc.core.rewrite.placeholder.ShardingPlaceholder) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) SchemaPlaceholder(io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder) IndexPlaceholder(io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder)

Example 9 with ShardingJdbcException

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;
}
Also used : PostgresqlDataTypeFactory(org.dbunit.ext.postgresql.PostgresqlDataTypeFactory) MySqlDataTypeFactory(org.dbunit.ext.mysql.MySqlDataTypeFactory) Oracle10DataTypeFactory(org.dbunit.ext.oracle.Oracle10DataTypeFactory) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) MsSqlDataTypeFactory(org.dbunit.ext.mssql.MsSqlDataTypeFactory) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) H2DataTypeFactory(org.dbunit.ext.h2.H2DataTypeFactory) DatabaseConfig(org.dbunit.database.DatabaseConfig)

Example 10 with ShardingJdbcException

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);
        }
    }
}
Also used : ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) DataSource(javax.sql.DataSource)

Aggregations

ShardingJdbcException (io.shardingjdbc.core.exception.ShardingJdbcException)11 ActiveSpan (io.opentracing.ActiveSpan)2 Tracer (io.opentracing.Tracer)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 DataSource (javax.sql.DataSource)2 Optional (com.google.common.base.Optional)1 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 Span (io.opentracing.Span)1 NamedDataSource (io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource)1 IndexPlaceholder (io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder)1 SchemaPlaceholder (io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder)1 ShardingPlaceholder (io.shardingjdbc.core.rewrite.placeholder.ShardingPlaceholder)1 TablePlaceholder (io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1 ToString (lombok.ToString)1 DatabaseConfig (org.dbunit.database.DatabaseConfig)1 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)1