use of indi.mybatis.flying.models.AggregateModel in project mybatis.flying by limeng32.
the class FlyingManager method buildFlyingModel.
private static void buildFlyingModel(FlyingModel flyingModel, JSONObject json, String originalSql, String id, boolean b, JSONObject innerJson, String outerPrefix) {
if (b) {
String temp = json.getString(FlyingKeyword.ACTION);
if (temp.endsWith("?")) {
temp = temp.substring(0, temp.length() - 1);
}
ActionType actionType = ActionType.forValue(temp);
flyingModel.setActionType(actionType);
dealKeyHandler(actionType, json.getString(FlyingKeyword.KEY_GENERATOR), originalSql, flyingModel);
}
flyingModel.setId(id);
flyingModel.setHasFlyingFeature(true);
if (innerJson == null) {
flyingModel.setIgnoreTag(json.getString(FlyingKeyword.IGNORE));
flyingModel.setWhiteListTag(json.getString(FlyingKeyword.WHITE_LIST));
flyingModel.setIndex(json.getString(FlyingKeyword.INDEX));
} else {
// inner json need ignore tag and white list tag
flyingModel.setIgnoreTag(innerJson.getString(FlyingKeyword.IGNORE));
flyingModel.setWhiteListTag(innerJson.getString(FlyingKeyword.WHITE_LIST));
// but not need index
}
flyingModel.setUnstablePrefix(json.getString(FlyingKeyword.PREFIX));
flyingModel.setPrefix(outerPrefix == null ? (flyingModel.getUnstablePrefix()) : (outerPrefix + flyingModel.getUnstablePrefix()));
if (json.getJSONObject("aggregate") != null) {
Map<String, AggregateModel> temp = JSONObject.parseObject(json.getJSONObject("aggregate").toJSONString(), new TypeReference<Map<String, AggregateModel>>() {
});
Map<String, Set<AggregateModel>> temp2 = new HashMap<>();
for (Map.Entry<String, AggregateModel> e : temp.entrySet()) {
if (e.getValue() != null) {
AggregateModel am = e.getValue();
if (temp2.containsKey(am.getColumn())) {
am.setAlias(e.getKey());
temp2.get(am.getColumn()).add(am);
} else {
Set<AggregateModel> set = new LinkedHashSet<>();
am.setAlias(e.getKey());
set.add(am);
temp2.put(am.getColumn(), set);
}
}
}
flyingModel.getAggregate().putAll(temp2);
}
if (json.getJSONArray("groupBy") != null) {
flyingModel.getGroupBy().addAll(JSONObject.parseObject(json.getJSONArray("groupBy").toJSONString(), new TypeReference<Set<String>>() {
}));
}
}
use of indi.mybatis.flying.models.AggregateModel in project mybatis.flying by limeng32.
the class PrefixTest method testSelect.
@Test
@DatabaseSetup(connection = "dataSource1", type = DatabaseOperation.CLEAN_INSERT, value = "/indi/mybatis/flying/test/prefixTest/testSelect.xml")
@ExpectedDatabase(connection = "dataSource1", override = false, assertionMode = DatabaseAssertionMode.NON_STRICT, value = "/indi/mybatis/flying/test/prefixTest/testSelect.result.xml")
@DatabaseTearDown(connection = "dataSource1", type = DatabaseOperation.DELETE_ALL, value = "/indi/mybatis/flying/test/prefixTest/testSelect.result.xml")
public void testSelect() {
Map<String, String> map = new HashMap<>();
map.put("id", "1");
Account_ account = accountService.selectAsd(1);
Assert.assertTrue(account.getActivated());
Assert.assertEquals("bob", account.getName());
Assert.assertEquals("bob@live.cn_", account.getEmail());
Assert.assertNull(account.getPassword());
Assert.assertEquals("a111", account.getActivateValue());
Assert.assertEquals(11, account.getOpLock().intValue());
Account_ ac = new Account_();
ac.setName("carl");
Permission pc = new Permission();
pc.setName("carl");
ac.setPermission(pc);
Collection<Account_> accountC = accountService.selectAllPrefix(ac);
System.out.println(JSONObject.toJSONString(accountC));
Account_[] accounts = accountC.toArray(new Account_[accountC.size()]);
Assert.assertEquals(3, accounts[0].getId().intValue());
Assert.assertEquals(23, accounts[0].getPermission().getFakeId().intValue());
Assert.assertEquals(3, accounts[0].getPermission().getId().intValue());
Assert.assertEquals("carl", accounts[0].getPermission().getName());
Assert.assertEquals(13, accounts[0].getRole().getId().intValue());
Assert.assertEquals("role3", accounts[0].getRole().getName());
Assert.assertEquals(113, accounts[0].getRoleDeputy().getId().intValue());
Assert.assertEquals("roleDeputy3", accounts[0].getRoleDeputy().getName());
Assert.assertEquals(4, accounts[1].getId().intValue());
Assert.assertEquals(24, accounts[1].getPermission().getFakeId().intValue());
Assert.assertEquals(4, accounts[1].getPermission().getId().intValue());
Assert.assertEquals("carl", accounts[1].getPermission().getName());
Assert.assertEquals(14, accounts[1].getRole().getId().intValue());
Assert.assertEquals("role4", accounts[1].getRole().getName());
Assert.assertEquals(114, accounts[1].getRoleDeputy().getId().intValue());
Assert.assertEquals("roleDeputy4", accounts[1].getRoleDeputy().getName());
accountMapper.selectGroupBy(new Account_());
int c = accountService.countAsd(ac);
Assert.assertEquals(2, c);
FlyingModel fm = FlyingManager.getFlyingModelFromCache("indi.mybatis.flying.mapper.AccountMapper.selectAllPrefix");
System.out.println("fm::" + JSONObject.toJSONString(fm));
Assert.assertEquals("noPassword", fm.getIgnoreTag());
Assert.assertEquals("use index(index1)", fm.getIndex());
Assert.assertNull(fm.getPrefix());
FlyingModel fm1 = fm.getProperties().get("permission");
Assert.assertNull(fm1.getIgnoreTag());
Assert.assertNull(fm1.getIndex());
Assert.assertEquals("permission__", fm1.getPrefix());
Assert.assertEquals("indi.mybatis.flying.mapper.PermissionMapper.select", fm1.getId());
FlyingModel fm2 = fm.getProperties().get("role");
Assert.assertNull(fm2.getIndex());
Assert.assertEquals("indi.mybatis.flying.mapper.RoleMapper.select", fm2.getId());
Assert.assertEquals("role__", fm2.getPrefix());
FlyingModel fm3 = fm.getProperties().get("roleDeputy");
Assert.assertNull(fm3.getIndex());
Assert.assertEquals("roleDeputy__", fm3.getPrefix());
FlyingModel fm10 = FlyingManager.getFlyingModelFromCache("indi.mybatis.flying.mapper.AccountMapper.selectGroupBy");
Assert.assertNotNull(fm10);
System.out.println("fm10::" + JSONObject.toJSONString(fm10));
Assert.assertEquals(2, fm10.getGroupBy().size());
Assert.assertEquals(2, fm10.getAggregate().size());
Set<AggregateModel> am = fm10.getAggregate().get("opLock");
Assert.assertEquals(2, am.size());
}
use of indi.mybatis.flying.models.AggregateModel in project mybatis.flying by limeng32.
the class SqlBuilder method dealMapperAnnotationIterationForSelectAll.
private static void dealMapperAnnotationIterationForSelectAll(boolean objectIsClass, Object object, StringBuilder selectSql, StringBuilder fromSql, StringBuilder whereSql, StringBuilder groupBySql, AtomicInteger index, FlyingModel flyingModel, TableName tn, Mapperable originFieldMapper, String fieldPerfix, TableName lastTableName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException {
Class<?> objectType = objectIsClass ? (Class<?>) object : object.getClass();
String ignoreTag = null;
String prefix = null;
String indexStr = null;
String whiteListTag = null;
Map<String, Set<AggregateModel>> aggregateMap = null;
Set<String> groupBySet = null;
boolean useWhiteList = false;
if (flyingModel != null) {
ignoreTag = flyingModel.getIgnoreTag();
prefix = flyingModel.getPrefix();
indexStr = flyingModel.getIndex();
whiteListTag = flyingModel.getWhiteListTag();
if (whiteListTag != null) {
useWhiteList = true;
}
if (!flyingModel.getAggregate().isEmpty()) {
aggregateMap = flyingModel.getAggregate();
}
if (!flyingModel.getGroupBy().isEmpty()) {
groupBySet = flyingModel.getGroupBy();
}
}
if (aggregateMap != null) {
// prepare for v1.1.0 System.out.println("aggregateJson::" + JSONObject.toJSONString(aggregateMap));
}
if (groupBySet != null) {
// prepare for v1.1.0 System.out.println("groupBySet::" + JSONObject.toJSONString(groupBySet));
}
Map<String, Object> dtoFieldMap = objectIsClass ? Collections.emptyMap() : PropertyUtils.describe(object);
Class<?> tempClass = getTableMappedClass(objectType);
TableMapper tableMapper = buildTableMapper(tempClass);
QueryMapper queryMapper = buildQueryMapper(objectType, tempClass);
TableName tableName = null;
int indexValue = index.getAndIncrement();
if (lastTableName == null) {
tableName = new TableName(tableMapper, indexValue, null);
} else {
tableName = new TableName(tableMapper, indexValue, lastTableName.getMap());
}
/*
* If originFieldMapper is null, it is considered to be the first traversal. In
* the first iteration, handle fromSql.
*
*/
if (originFieldMapper == null) {
fromSql.append(tableName.sqlSelect());
if (indexStr != null) {
fromSql.append(" ").append(indexStr);
}
}
if (flyingModel != null) {
for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
if ((!useWhiteList || fieldMapper.getWhiteListTagSet().contains(whiteListTag)) && (!fieldMapper.getIgnoreTagSet().contains(ignoreTag))) {
dtoFieldMap = dealSelectSql(flyingModel, fieldMapper, dtoFieldMap, index, selectSql, tableName, prefix);
}
if (aggregateMap != null) {
// TODO 在这里处理聚合函数
if (aggregateMap.containsKey(fieldMapper.getDbFieldName())) {
Set<AggregateModel> set = aggregateMap.get(fieldMapper.getDbFieldName());
for (AggregateModel am : set) {
selectSql.append(am.getFunction()).append("(").append(tableName.sqlWhere()).append(am.getColumn()).append(") as ").append(am.getAlias()).append(COMMA);
}
}
}
if (groupBySet != null) {
// TODO 在这里处理group by
if (groupBySet.contains(fieldMapper.getDbFieldName())) {
if (groupBySql.length() == 0) {
groupBySql.append(GROUP_BY_BLANK);
}
groupBySql.append(tableName.sqlWhere()).append(fieldMapper.getDbFieldName()).append(COMMA);
}
}
}
}
/*
* If the originFieldMapper and originTableName are not null, it can be
* considered a non-first traversal.In the non-first traversal, process
* fieldPerfix and fromSql.
*
*/
String temp = null;
if (originFieldMapper != null && tn != null) {
/* Processing fieldPerfix */
if (fieldPerfix == null) {
temp = originFieldMapper.getFieldName();
} else {
temp = new StringBuilder(fieldPerfix).append(DOT).append(originFieldMapper.getFieldName()).toString();
}
/* Processing fromSql */
fromSql.append(originFieldMapper.getAssociationType().value()).append(tableName.sqlSelect()).append(BLANK_ON_BLANK).append(tn.sqlWhere()).append(originFieldMapper.getDbFieldNameForJoin()).append(BLANK_EQUAL_BLANK).append(tableName.sqlWhere()).append(originFieldMapper.getDbAssociationUniqueKey());
ForeignAssociationMapper[] fams = originFieldMapper.getForeignAssociationMappers();
if (fams != null && fams.length > 0) {
for (ForeignAssociationMapper fam : fams) {
fromSql.append(BLANK_AND_BLANK).append(tn.sqlWhere()).append(fam.getDbFieldName()).append(fam.getCondition().value()).append(tableName.sqlWhere()).append(fam.getDbAssociationFieldName());
}
}
}
/* Handle the conditions in the fieldMapper */
for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
if (fieldMapper.isHasDelegate() && dtoFieldMap.get(fieldMapper.getDelegate().getFieldName()) != null) {
dealConditionEqual(whereSql, fieldMapper.getDelegate(), tableName, temp, false, 0);
continue;
}
Object value = dtoFieldMap.get(fieldMapper.getFieldName());
if (value == null) {
continue;
}
if (fieldMapper.isForeignKey()) {
dealMapperAnnotationIterationForSelectAll(value instanceof Class<?>, value, selectSql, fromSql, whereSql, null, index, flyingModel == null ? (null) : (flyingModel.getProperties().get(fieldMapper.getFieldName())), tableName, fieldMapper, temp, tableName);
} else {
dealConditionEqual(whereSql, fieldMapper, tableName, temp, false, 0);
}
}
/* Handle the "and" condition in the queryMapper */
for (ConditionMapper conditionMapper : queryMapper.getConditionMapperCache().values()) {
Object value = dtoFieldMap.get(conditionMapper.getFieldName());
if (value == null) {
continue;
}
dealConditionMapper(conditionMapper, value, whereSql, tableName, temp, false, 0);
}
/* Handle the "or" condition in the queryMapper */
for (OrMapper orMapper : queryMapper.getOrMapperCache().values()) {
Object value = dtoFieldMap.get(orMapper.getFieldName());
if (value == null) {
continue;
}
dealConditionOrMapper(orMapper, value, whereSql, tableName, temp);
}
}
Aggregations