Search in sources :

Example 31 with DalException

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

the class DalTransaction method validate.

public void validate(String desiganateLogicDbName, String desiganateShard) throws SQLException {
    if (desiganateLogicDbName == null || desiganateLogicDbName.length() == 0)
        throw new DalException(ErrorCode.LogicDbEmpty);
    if (!desiganateLogicDbName.equals(this.logicDbName))
        throw new DalException(ErrorCode.TransactionDistributed, this.logicDbName, desiganateLogicDbName);
    String curShard = connHolder.getShardId();
    if (curShard == null)
        return;
    if (desiganateShard == null)
        return;
    try {
        DalConfigure dalConfigure = DalClientFactory.getDalConfigure();
        DatabaseSet databaseSet = dalConfigure.getDatabaseSet(desiganateLogicDbName);
        if (databaseSet instanceof ClusterDatabaseSet && Integer.valueOf(curShard).equals(Integer.valueOf(desiganateShard)))
            return;
    } catch (Exception e) {
    // needn't handle
    }
    if (!curShard.equals(desiganateShard))
        throw new DalException(ErrorCode.TransactionDistributedShard, curShard, desiganateShard);
}
Also used : ClusterDatabaseSet(com.ctrip.platform.dal.dao.configure.ClusterDatabaseSet) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalException(com.ctrip.platform.dal.exceptions.DalException) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) ClusterDatabaseSet(com.ctrip.platform.dal.dao.configure.ClusterDatabaseSet) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) DalTransactionConflictException(com.ctrip.platform.dal.exceptions.DalTransactionConflictException) TransactionSystemException(com.ctrip.platform.dal.exceptions.TransactionSystemException)

Example 32 with DalException

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

the class PartialQueryTableDaoUnitTest method testDetectFieldNotExist.

@Test
public void testDetectFieldNotExist() throws Exception {
    DalTableDao<PersonWithoutName> client = new DalTableDao<>(new DalDefaultJpaParser<>(PersonWithoutName.class));
    List<Integer> peopleIds = new ArrayList<>();
    peopleIds.add(1);
    peopleIds.add(2);
    peopleIds.add(3);
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID", "PeopleID", "CountryID");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.fail();
    } catch (DalException e) {
        e.printStackTrace();
        assertEquals(ErrorCode.FieldNotExists.getCode(), e.getErrorCode());
    }
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Example 33 with DalException

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

the class PartialQueryTableDaoUnitTest method testFindBySelectedField.

@Test
public void testFindBySelectedField() throws Exception {
    DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.class));
    List<Integer> peopleIds = new ArrayList<>();
    peopleIds.add(1);
    peopleIds.add(2);
    peopleIds.add(3);
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        List<Person> ret = client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.assertNull(ret.get(0).getCountryID());
        Assert.assertNull(ret.get(0).getPeopleID());
    } catch (DalException e) {
        Assert.fail();
    }
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Example 34 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)

Example 35 with DalException

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

the class KeyHolder method getKeyList.

/**
 * Get all the generated keys for multiple insert.
 *
 * @return all the generated keys
 * @throws DalException
 */
public List<Map<String, Object>> getKeyList() throws DalException {
    if (requireMerge && merged.get() == false)
        throw new DalException(ErrorCode.KeyGenerationFailOrNotCompleted);
    List<Map<String, Object>> keyList = new ArrayList<>();
    int size = size();
    for (int i = 0; i < size; i++) keyList.add(allKeys.get(i));
    return keyList;
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DalException (com.ctrip.platform.dal.exceptions.DalException)37 ArrayList (java.util.ArrayList)8 SQLException (java.sql.SQLException)5 Test (org.junit.Test)5 DalHints (com.ctrip.platform.dal.dao.DalHints)4 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)4 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)3 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)2 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2 LogContext (com.ctrip.platform.dal.dao.client.LogContext)2 DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)2 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)2 Field (java.lang.reflect.Field)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)2 DatabaseCategory (com.ctrip.platform.dal.common.enums.DatabaseCategory)1 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)1 StatementParameter (com.ctrip.platform.dal.dao.StatementParameter)1 Type (com.ctrip.platform.dal.dao.annotation.Type)1