use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryObjectBySimpleTypeInTableShards.
@Test
public void testQueryObjectBySimpleTypeInTableShards() throws SQLException {
Set<String> tableShards = new HashSet<>();
tableShards.add("0");
tableShards.add("1");
SelectSqlBuilder ssb1 = new SelectSqlBuilder();
ssb1.select("id").from(TABLE_NAME).where(" id = 1 ").requireSingle();
try {
Integer id1 = dao.queryObject(ssb1, new DalHints().inTableShards(tableShards), Integer.class);
} catch (Exception e) {
assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
}
SelectSqlBuilder ssb2 = new SelectSqlBuilder();
ssb2.select("id").from(TABLE_NAME).where(" id = 1 ").requireFirst();
try {
Integer id2 = dao.queryObject(ssb2, new DalHints().inTableShards(tableShards), Integer.class);
assertTrue(id2 != null);
} catch (Exception e) {
Assert.fail();
}
}
use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCountTableShardBy.
@Test
public void testCountTableShardBy() throws SQLException {
int index = 1;
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
StatementParameters parameters = new StatementParameters();
parameters.setInParameter(index, "tableIndex", Types.BIGINT, list);
SelectSqlBuilder ssb1 = new SelectSqlBuilder();
ssb1.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).selectCount();
Number count1 = dao.count(ssb1, new DalHints().tableShardBy("tableIndex"));
assertEquals(3L, count1);
}
use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder 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.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryObjectInAllTableShards.
@Test
public void testQueryObjectInAllTableShards() throws SQLException {
// requireSingle result in exception
SelectSqlBuilder ssb1 = new SelectSqlBuilder();
ssb1.selectAll().from(TABLE_NAME).where(" id = 1 ").requireSingle();
try {
ClientTestModel model = dao.queryObject(ssb1, new DalHints().inAllTableShards());
Assert.fail();
} catch (Exception e) {
assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
}
// requireFirst returns first result,which is not recommended when used in inAllTableShards
SelectSqlBuilder ssb2 = new SelectSqlBuilder();
ssb2.selectAll().from(TABLE_NAME).where(" id = 1 ").requireFirst();
try {
ClientTestModel model = dao.queryObject(ssb2, new DalHints().inAllTableShards());
assertTrue(model != null);
} catch (Exception e) {
Assert.fail();
}
}
use of com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryObjectTableShardBy.
@Test
public void testQueryObjectTableShardBy() throws SQLException {
int index = 1;
List<String> list = new ArrayList<>();
list.add("0");
list.add("1");
StatementParameters parameters = new StatementParameters();
parameters.setInParameter(index, "tableIndex", Types.INTEGER, list);
SelectSqlBuilder ssb1 = new SelectSqlBuilder();
ssb1.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).requireSingle();
try {
ClientTestModel model1 = dao.queryObject(ssb1, new DalHints().tableShardBy("tableIndex"));
} catch (Exception e) {
assertEquals("It is expected to return only 1 result. But the actually count is more than 1", e.getMessage());
}
SelectSqlBuilder ssb2 = new SelectSqlBuilder();
ssb2.selectAll().from(TABLE_NAME).where(" tableIndex in (?) ").with(parameters).requireFirst();
try {
ClientTestModel model2 = dao.queryObject(ssb2, new DalHints().tableShardBy("tableIndex"));
assertTrue(model2 != null);
} catch (Exception e) {
Assert.fail();
}
}
Aggregations