Search in sources :

Example 16 with DalException

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

the class BatchUpdateTask method createTaskContext.

@Override
public BulkTaskContext<T> createTaskContext(DalHints hints, List<Map<String, ?>> daoPojos, List<T> rawPojos) throws DalException {
    BulkTaskContext<T> taskContext = new BulkTaskContext<T>(rawPojos);
    Map<String, Boolean> pojoFieldStatus = taskContext.isUpdatableEntity() ? filterUpdatableEntity(hints, rawPojos) : filterNullColumns(hints, daoPojos);
    if (pojoFieldStatus.size() == 0)
        throw new DalException(ErrorCode.ValidateFieldCount);
    taskContext.setPojoFieldStatus(pojoFieldStatus);
    return taskContext;
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 17 with DalException

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

the class DalBulkTaskRequest method validate.

@Override
public void validate() throws SQLException {
    if (null == rawPojos)
        throw new DalException(ErrorCode.ValidatePojoList);
    if (task == null)
        throw new DalException(ErrorCode.ValidateTask);
    dbShardMerger = task.createMerger();
    daoPojos = task.getPojosFields(rawPojos);
    taskContext = task.createTaskContext(hints, daoPojos, rawPojos);
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 18 with DalException

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

the class DalSingleTaskRequest method validate.

@Override
public void validate() throws SQLException {
    if (isList && null == rawPojos)
        throw new DalException(ErrorCode.ValidatePojoList);
    if (isList == false && null == rawPojo)
        throw new DalException(ErrorCode.ValidatePojo);
    if (task == null)
        throw new DalException(ErrorCode.ValidateTask);
    if (isList == false) {
        rawPojos = new ArrayList<>(1);
        rawPojos.add(rawPojo);
    }
    daoPojos = task.getPojosFields(rawPojos);
    detectDistributedTransaction(logicDbName, hints, daoPojos);
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 19 with DalException

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

the class SingleUpdateTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo) throws SQLException {
    if (fields.size() == 0)
        throw new DalException(ErrorCode.ValidateFieldCount);
    Map<String, ?> pks = getPrimaryKeys(fields);
    Object version = getVersion(fields);
    Map<String, ?> filted = null;
    if (rawPojo instanceof UpdatableEntity)
        filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
    else
        filted = filterNullColumns(hints, fields);
    // If there is no columns changed, we will not perform update
    if (filted.size() == 0)
        return 0;
    String updateSql = buildUpdateSql(getTableName(hints, fields), filted, hints);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, filted);
    addParameters(parameters, pks);
    addVersion(parameters, version);
    return client.update(updateSql, parameters, hints.setFields(fields));
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 20 with DalException

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

the class DalTableDao method queryByPk.

/**
 * Query by Primary key. The key column type should be Integer, Long, etc.
 * For table that the primary key is not of Integer type, this method will
 * fail.
 *
 * @param id The primary key in number format
 * @param hints Additional parameters that instruct how DAL Client perform database operation.
 * @return entity of this table. Null if no result found.
 * @throws SQLException
 */
public T queryByPk(Number id, DalHints hints) throws SQLException {
    if (parser.getPrimaryKeyNames().length != 1)
        throw new DalException(ErrorCode.ValidatePrimaryKeyCount);
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, parser.getPrimaryKeyNames()[0], getColumnType(parser.getPrimaryKeyNames()[0]), id);
    return queryObject(new SelectSqlBuilder().where(pkSql).with(parameters).requireSingle().nullable(), hints);
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Aggregations

DalException (com.ctrip.platform.dal.exceptions.DalException)21 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 DalHints (com.ctrip.platform.dal.dao.DalHints)4 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)3 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 StatementParameter (com.ctrip.platform.dal.dao.StatementParameter)1 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 DalConnection (com.ctrip.platform.dal.dao.client.DalConnection)1 DalHA (com.ctrip.platform.dal.dao.client.DalHA)1 LogContext (com.ctrip.platform.dal.dao.client.LogContext)1 DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)1 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)1 Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1