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);
}
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());
}
}
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();
}
}
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);
}
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;
}
Aggregations