Search in sources :

Example 1 with MergeData

use of com.qlangtech.tis.sql.parser.stream.generate.MergeData in project tis by qlangtech.

the class TableRelation method createSelectParentByChild.

/**
 * 通过子记录查询父记录
 *
 * @param context
 */
public String createSelectParentByChild(MergeData context, FlatTableRelation currentTableRelation, FlatTableRelation nextRelation, PrimaryTableMeta primary) {
    String methodToken = nextRelation.getJoinerKeysQueryMethodToken();
    context.addGlobalScript(methodToken, nextRelation.buildQueryHeaderByTailerInfo(currentTableRelation.getTailerKeys().stream().map((rr) -> rr.getHeadLinkKey()).collect(Collectors.toSet())));
    FuncFormat f = nextRelation.buildInvokeQueryHeaderByTailerInfo();
    // 执行处理结果
    FuncFormat p = nextRelation.buildInvokeQueryHeaderByTailerInfoResultProcess(primary, currentTableRelation);
    f.appendLine(p);
    return f.toString();
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) DependencyNode(com.qlangtech.tis.sql.parser.meta.DependencyNode) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Lists(com.google.common.collect.Lists) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) PrimaryLinkKey(com.qlangtech.tis.sql.parser.meta.PrimaryLinkKey) FlatTableRelation(com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation) MergeData(com.qlangtech.tis.sql.parser.stream.generate.MergeData) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy) Collectors(java.util.stream.Collectors) FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat)

Example 2 with MergeData

use of com.qlangtech.tis.sql.parser.stream.generate.MergeData in project tis by qlangtech.

the class TableRelation method createParentPKLiteria.

/**
 * 生成主表的PK键 scala代码
 *
 * @param context
 * @return
 */
public String createParentPKLiteria(MergeData context) {
    Stack<FlatTableRelation> unprocessed = context.getUnprocessedTableRelations();
    FlatTableRelation un = null;
    try {
        FlatTableRelation currentTableRelation = getCurrentTableRelation(true);
        PrimaryTableMeta primary = getPrimaryTableMeta(context, currentTableRelation.getHeaderEntity());
        LinkKeys pkJoinerKey = null;
        for (LinkKeys lk : currentTableRelation.getHeaderKeys()) {
            if (StringUtils.equals(lk.getHeadLinkKey(), primary.getDBPrimayKeyName().getName())) {
                pkJoinerKey = lk;
            }
        }
        if (!unprocessed.empty()) {
            // 连接栈不为空
            un = unprocessed.pop();
            if (!unprocessed.empty()) {
                // FIXME: 或许将来支持更加复杂的增量流式处理,可以支持外表连接
                throw new IllegalStateException("un process rel can not exceed 2,but now size:" + unprocessed.size() + "pre:" + un.toString() + ",others:" + unprocessed.stream().map((r) -> r.toString()).collect(Collectors.joining(",")));
            }
            if (currentTableRelation.isLinkable(un)) {
                return primary.createCompositePK(currentTableRelation, un) + "/*gencode1*/";
            } else {
                // SqlTaskNodeMeta.DependencyNode headerNode = un.getHeader();
                return createSelectParentByChild(context, currentTableRelation, un, primary);
            }
        } else {
            TableRelation.FinalLinkKey finalLinkKey = FlatTableRelation.getFinalLinkKey(primary.getDBPrimayKeyName().getName(), currentTableRelation);
            if (finalLinkKey.success) {
                // 连接栈为空
                return "return " + primary.createCompositePK(currentTableRelation) + "/*gencode2*/";
            } else {
                // 例如:orderDetail是主表,以orderid作为pk,外表totalpayinfo 为外表(连接键为: totalpay_id -> totalpay_id,所以连接过程会中断
                return createSelectParentByChild(context, currentTableRelation, currentTableRelation, primary);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("parent:" + this.getParent().parseEntityName() + ",child:" + this.getChild().parseEntityName(), e);
    } finally {
        context.getUnprocessedTableRelations().clear();
    }
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) DependencyNode(com.qlangtech.tis.sql.parser.meta.DependencyNode) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Lists(com.google.common.collect.Lists) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) PrimaryLinkKey(com.qlangtech.tis.sql.parser.meta.PrimaryLinkKey) FlatTableRelation(com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation) MergeData(com.qlangtech.tis.sql.parser.stream.generate.MergeData) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy) Collectors(java.util.stream.Collectors) FlatTableRelation(com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation) FlatTableRelation(com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation)

Aggregations

Lists (com.google.common.collect.Lists)2 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)2 DependencyNode (com.qlangtech.tis.sql.parser.meta.DependencyNode)2 PrimaryLinkKey (com.qlangtech.tis.sql.parser.meta.PrimaryLinkKey)2 FlatTableRelation (com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation)2 MergeData (com.qlangtech.tis.sql.parser.stream.generate.MergeData)2 EntityName (com.qlangtech.tis.sql.parser.tuple.creator.EntityName)2 FuncFormat (com.qlangtech.tis.sql.parser.visitor.FuncFormat)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang.StringUtils)2