Search in sources :

Example 1 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class AbstractSingleInsertTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    List<Map<String, ?>> pojoList = new ArrayList<Map<String, ?>>();
    List<T> rawPojos = new ArrayList<>();
    pojoList.add(fields);
    rawPojos.add(rawPojo);
    Set<String> unqualifiedColumns = filterUnqualifiedColumns(hints, pojoList, rawPojos);
    removeUnqualifiedColumns(fields, unqualifiedColumns);
    // Put identityFields into context
    List<Map<String, Object>> identityFields = new ArrayList<>();
    Map<String, Object> identityField = getIdentityField(fields);
    if (identityField != null) {
        identityFields.add(identityField);
    }
    if (taskContext instanceof DefaultTaskContext) {
        ((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
        ((DefaultTaskContext) taskContext).setPojosCount(1);
    }
    String tableName = getRawTableName(hints, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    /*
         * In case fields is empty, the final sql will be like "insert into tableName () values()".
         * We do not report error or simply return 0, but just let DB decide what to do.
         * For MS Sql server, sql like this is illegal, but for mysql, this works however
         */
    String insertSql = buildInsertSql(hints, fields, tableName);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, fields);
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(insertSql, parameters, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Map(java.util.Map)

Example 2 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class BatchUpdateTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    List<T> rawPojos = taskContext.getRawPojos();
    boolean isUpdatableEntity = taskContext.isUpdatableEntity();
    Map<String, Boolean> pojoFieldStatus = taskContext.getPojoFieldStatus();
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    int i = 0;
    String[] updateColumnNames = pojoFieldStatus.keySet().toArray(new String[pojoFieldStatus.size()]);
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        StatementParameters parameters = new StatementParameters();
        if (isUpdatableEntity && !hints.isUpdateUnchangedField())
            addParameters(parameters, pojo, updateColumnNames, ((UpdatableEntity) rawPojos.get(index)).getUpdatedColumns());
        else
            addParameters(parameters, pojo, updateColumnNames);
        addParameters(parameters, pojo, parser.getPrimaryKeyNames());
        addVersion(parameters, pojo);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String batchUpdateSql = buildBatchUpdateSql(quote(tableName), pojoFieldStatus);
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(batchUpdateSql, parametersList, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
    return result;
}
Also used : DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException)

Example 3 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class CombinedDeleteTask method execute.

@Override
public Integer execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    StringBuilder values = new StringBuilder();
    List<String> pkColumns = clonePkColumns();
    int startIndex = 1;
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> daoPojo = daoPojos.get(index);
        int paramCount = addParameters(startIndex, parameters, daoPojo, pkColumns);
        startIndex += paramCount;
        values.append(String.format(IN_TEMPLATE, combine("?", paramCount, ",")));
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String sql = String.format(TMPL_COMBINED_SQL_DELETE, quote(tableName), combineColumns(pkColumns, COLUMN_SEPARATOR), values.substring(0, values.length() - 2) + ")");
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(sql, parameters, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 4 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class BatchDeleteTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    List<String> pkNames = Arrays.asList(parser.getPrimaryKeyNames());
    int i = 0;
    for (Integer index : daoPojos.keySet()) {
        StatementParameters parameters = new StatementParameters();
        addParameters(1, parameters, daoPojos.get(index), pkNames);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String deleteSql = buildDeleteSql(quote(tableName));
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(deleteSql, parametersList, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
    return result;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 5 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class IdGeneratorFactoryManager method createFactory.

private IIdGeneratorFactory createFactory(String className) {
    IIdGeneratorFactory factory = null;
    Class clazz = null;
    try {
        clazz = Class.forName(className);
    } catch (Throwable t) {
        throw new DalRuntimeException("Failed to load class: " + className, t);
    }
    try {
        factory = (IIdGeneratorFactory) clazz.newInstance();
    } catch (Throwable t1) {
        try {
            factory = (IIdGeneratorFactory) clazz.getMethod("getInstance").invoke(null);
        } catch (Throwable t2) {
            String msg = String.format("Failed to create factory: %s. Cause 1: %s; cause 2: %s", className, t1.getMessage(), t2.getMessage());
            throw new DalRuntimeException(msg);
        }
    }
    if (null == factory) {
        throw new DalRuntimeException(String.format("The created factory '%s' is null", className));
    }
    return factory;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException)

Aggregations

DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)29 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 Callback (com.ctrip.platform.dal.dao.log.Callback)3 Map (java.util.Map)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Node (org.w3c.dom.Node)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 Listener (com.ctrip.framework.dal.cluster.client.base.Listener)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 ClusterConfigException (com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 ClusterIdGeneratorConfig (com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig)1