Search in sources :

Example 1 with FuncFormat

use of com.qlangtech.tis.sql.parser.visitor.FuncFormat in project tis by qlangtech.

the class FlatTableRelation method buildInvokeQueryHeaderByTailerInfo.

/**
 * 执行调用通过子查询父表逻辑
 *
 * @return
 */
public FuncFormat buildInvokeQueryHeaderByTailerInfo() {
    FuncFormat r = new FuncFormat();
    final String paramsList = this.getHeaderKeys().stream().map((m) -> {
        TisGroupBy.TisColumn col = new TisGroupBy.TisColumn(m.getHeadLinkKey());
        r.appendLine("val " + col.getJavaVarName() + " = row.getColumn(\"" + m.getTailerLinkKey() + "\")");
        return col.getJavaVarName();
    }).collect(Collectors.joining(","));
    EntityName headerEntity = this.getHeaderEntity();
    r.startLine("val " + (this.isHeaderMulti() ? headerEntity.entities() + ": List[RowMap] " : headerEntity.getJavaEntityName() + ": RowMap ") + " = this." + this.buildQueryHeaderByTailerInfoMethodName() + "(" + paramsList + ") ");
    return r;
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) DependencyNode(com.qlangtech.tis.sql.parser.meta.DependencyNode) StringUtils(org.apache.commons.lang.StringUtils) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) Set(java.util.Set) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy) TabCardinality(com.qlangtech.tis.sql.parser.er.TabCardinality) Collectors(java.util.stream.Collectors) List(java.util.List) TableRelation(com.qlangtech.tis.sql.parser.er.TableRelation) CollectionUtils(org.apache.commons.collections.CollectionUtils) Optional(java.util.Optional) PrimaryTableMeta(com.qlangtech.tis.sql.parser.er.PrimaryTableMeta) LinkKeys(com.qlangtech.tis.sql.parser.er.LinkKeys) FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Example 2 with FuncFormat

use of com.qlangtech.tis.sql.parser.visitor.FuncFormat in project tis by qlangtech.

the class FlatTableRelation method buildInvokeQueryHeaderByTailerInfoResultProcess.

/**
 * 创建查询结处理
 *
 * @param primary
 * @param preTableRelation 上一个表关联关系
 * @return
 */
public FuncFormat buildInvokeQueryHeaderByTailerInfoResultProcess(PrimaryTableMeta primary, FlatTableRelation preTableRelation) {
    FuncFormat f = new FuncFormat();
    EntityName headerEntity = this.getHeaderEntity();
    f.appendLine("val " + headerEntity.getJavaEntityName() + "Meta : AliasList = tabColumnMetaMap.get(\"" + headerEntity.getTabName() + "\");");
    // TableRelation.FinalLinkKey finalLinkKey = getFinalLinkKey(primary.getDBPrimayKeyName().getName(), preTableRelation);
    // final String pkColGetter = headerEntity.createColGetterLiteria(finalLinkKey.linkKeyName);
    // headerEntity.getJavaEntityName() + "Meta.getColMeta(\"" + finalLinkKey.linkKeyName + "\")";
    final String createCompositePKLiteria = primary.createCompositePK(headerEntity.getJavaEntityName() + "Meta", this.isHeaderMulti() ? "r" : headerEntity.getJavaEntityName(), true, preTableRelation);
    if (this.isHeaderMulti()) {
        // 遍历结果集
        f.buildRowMapTraverseLiteria(headerEntity, (mm) -> {
            mm.startLine("pushPojo2Queue(" + createCompositePKLiteria + ", row)").append("/*gencode3*/");
        // mm.startLine("pushPojo2Queue(new CompositePK("
        // + headerEntity.createColValLiteria(finalLinkKey.linkKeyName, "r") + ", r), row)");
        });
    } else {
        f.methodBody("if(" + headerEntity.getJavaEntityName() + " != null)", (mm) -> {
            mm.startLine("return " + createCompositePKLiteria).append("/*gencode4*/");
        // mm.startLine("return new CompositePK(" + headerEntity.createColValLiteria("dddddd")
        // + "," + headerEntity.getJavaEntityName() + ")").append("/*codegen4*/");
        });
    }
    f.startLine(" null");
    return f;
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName)

Example 3 with FuncFormat

use of com.qlangtech.tis.sql.parser.visitor.FuncFormat in project tis by qlangtech.

the class FlatTableRelation method buildQueryHeaderByTailerInfo.

/**
 * 通过子表查询主表(head),生成查询方法
 *
 * @param extraHeaderColKeys 额外的col
 * @return
 */
public FuncFormat buildQueryHeaderByTailerInfo(final Set<String> extraHeaderColKeys) {
    // final String paramsList = this.getJoinerKeys().stream().map((jk) -> {
    // TisGroupBy.TisColumn col = new TisGroupBy.TisColumn(jk.getChildKey());
    // return col.getJavaVarName() + " : String";
    // }).collect(Collectors.joining(","));
    // EntityName headerEntity = relation.getHeader().parseEntityName();
    final EntityName tailerEntity = this.getTailerEntity();
    final EntityName headEntity = this.getHeaderEntity();
    final String methodToken = "private def " + this.buildQueryHeaderByTailerInfoMethodName() + "(" + this.getJoinerKeysQueryMethodParamsLiteria() + ") : " + (this.isHeaderMulti() ? "List[RowMap]" : "RowMap") + " =";
    FuncFormat r = new FuncFormat();
    r.methodBody(methodToken, (m) -> {
        // 定义结果集对象
        m.startLine(headEntity.buildDefineRowMapListLiteria());
        // 创建查询对象
        m.startLine(headEntity.buildDefineCriteriaEqualLiteria());
        // << 设置查询条件
        // this.getJoinerKeys().stream().forEach((mm) -> {
        // TisGroupBy.TisColumn col = new TisGroupBy.TisColumn(mm.getChildKey());
        // m.appendLine(col.buildDefineParam());
        // });
        m.returnLine();
        m.startLine(headEntity.buildCreateCriteriaLiteria());
        this.getHeaderKeys().stream().forEach((mm) -> {
            TisGroupBy.TisColumn col = new TisGroupBy.TisColumn(mm.getHeadLinkKey());
            m.append(col.buildPropCriteriaEqualLiteria());
        });
        // 设置查询条件>>
        // 设置select的列
        Set<String> selCols = this.getHeaderKeys().stream().map((link) -> link.getHeadLinkKey()).collect(Collectors.toSet());
        selCols.addAll(extraHeaderColKeys);
        m.startLine(headEntity.buildAddSelectorColsLiteria(selCols));
        // 执行dao查询
        m.startLine(headEntity.buildExecuteQueryDAOLiteria());
        if (this.isHeaderMulti()) {
            m.startLine("return " + headEntity.entities());
        } else {
            m.methodBody("for ( ( r:RowMap) <- " + headEntity.entities() + ".asScala)", (rt) -> {
                rt.startLine("return r");
            });
            m.startLine("null");
        }
    });
    return r;
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) DependencyNode(com.qlangtech.tis.sql.parser.meta.DependencyNode) StringUtils(org.apache.commons.lang.StringUtils) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) Set(java.util.Set) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy) TabCardinality(com.qlangtech.tis.sql.parser.er.TabCardinality) Collectors(java.util.stream.Collectors) List(java.util.List) TableRelation(com.qlangtech.tis.sql.parser.er.TableRelation) CollectionUtils(org.apache.commons.collections.CollectionUtils) Optional(java.util.Optional) PrimaryTableMeta(com.qlangtech.tis.sql.parser.er.PrimaryTableMeta) LinkKeys(com.qlangtech.tis.sql.parser.er.LinkKeys) FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Example 4 with FuncFormat

use of com.qlangtech.tis.sql.parser.visitor.FuncFormat 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 5 with FuncFormat

use of com.qlangtech.tis.sql.parser.visitor.FuncFormat in project tis by qlangtech.

the class MapDataMethodCreator method getGenerateMapDataMethodBody.

/**
 * @return
 */
public String getGenerateMapDataMethodBody() {
    // streamIncrGenerateStrategy.getERRule();
    IERRules erRule = null;
    FuncFormat funcFormat = new FuncFormat();
    funcFormat.appendLine("val " + this.entityName.entities() + "ThreadLocal : ThreadLocal[Map[GroupKey, GroupValues]]  = addThreadLocalVal()");
    funcFormat.returnLine();
    // funcFormat.methodBody( //
    // "private Map<GroupKey, GroupValues> " + getMapDataMethodName() +
    // "(IRowValueGetter "
    // + this.entityName.getTabName() + ")",
    // 
    funcFormat.methodBody("private def " + getMapDataMethodName() + "( " + this.entityName.getJavaEntityName() + " : IRowValueGetter) : scala.collection.mutable.Map[GroupKey, GroupValues] =", (r) -> {
        r.startLine("var result :scala.collection.mutable.Map[GroupKey, GroupValues] = " + this.entityName.entities() + "ThreadLocal.get()");
        // r.startLine("var " + this.entityName.entities() + ": List[" + ROW_MAP_CLASS_NAME + "]  = null");
        r.startLine(this.entityName.buildDefineRowMapListLiteria());
        r.methodBody("if (result != null)", (m) -> {
            m.appendLine(" return result");
        });
        if (groups.getGroups().size() < 1) {
            throw new IllegalStateException("groups.getGroups().size() can not small than 1");
        }
        TableRelation parentRel = null;
        Optional<PrimaryTableMeta> ptab = erRule.isPrimaryTable(this.entityName.getTabName());
        if (ptab.isPresent()) {
            // 如果聚合表本身就是主表的话,那它只需要查询自己就行了
            PrimaryTableMeta p = ptab.get();
            parentRel = new TableRelation();
            // parentRel.setParent(null);
            // parentRel.setChild(null);
            parentRel.setCardinality(TabCardinality.ONE_N.getToken());
            // List<JoinerKey> joinerKeys = Lists.newArrayList();
            parentRel.setJoinerKeys(p.getPrimaryKeyNames().stream().map((rr) -> new JoinerKey(rr.getName(), rr.getName())).collect(Collectors.toList()));
        } else {
            Optional<TableRelation> firstParentRel = erRule.getFirstParent(this.entityName.getTabName());
            if (!firstParentRel.isPresent()) {
                throw new IllegalStateException("first parent table can not be null ,child table:" + this.entityName);
            }
            parentRel = firstParentRel.get();
        }
        if (!parentRel.isCardinalityEqual(TabCardinality.ONE_N)) {
            throw new IllegalStateException("rel" + parentRel + " execute aggreate mush be an rel cardinality:" + TabCardinality.ONE_N);
        }
        List<TisGroupBy.TisColumn> linkKeys = Lists.newArrayList();
        try {
            TisGroupBy.TisColumn col = null;
            for (LinkKeys linkKey : parentRel.getCurrentTableRelation(true).getTailerKeys()) {
                col = new TisGroupBy.TisColumn(linkKey.getHeadLinkKey());
                linkKeys.add(col);
                r.appendLine("val " + col.getJavaVarName() + ":String = " + entityName.getJavaEntityName() + ".getColumn(\"" + col.getColname() + "\")");
            }
        } catch (Exception e) {
            throw new RuntimeException(parentRel.toString(), e);
        }
        r.appendLine(this.entityName.buildDefineCriteriaEqualLiteria());
        r.startLine(this.entityName.buildCreateCriteriaLiteria());
        for (TisGroupBy.TisColumn g : linkKeys) {
            r.append(g.buildPropCriteriaEqualLiteria());
        }
        // 外键查询键也要出现在select列中
        final Set<String> selCols = Sets.newHashSet();
        selCols.addAll(this.relefantCols);
        this.groups.getGroups().stream().forEach((e) -> {
            selCols.add(e.getColname());
        });
        r.startLine(this.entityName.buildAddSelectorColsLiteria(selCols));
        r.startLine(this.entityName.buildExecuteQueryDAOLiteria());
        r.startLine("result = scala.collection.mutable.Map[GroupKey, GroupValues]()");
        r.startLine("var vals : Option[GroupValues] = null");
        r.startLine("var groupKey: GroupKey = null");
        r.buildRowMapTraverseLiteria(this.entityName, (m) -> {
            // 
            m.startLine("groupKey = GroupKey.createCacheKey(").append(groups.getGroups().stream().map((rr) -> "\"" + rr.getColname() + "\",r.getColumn(\"" + (rr.getColname()) + "\")").collect(Collectors.joining(",")));
            m.append(")").returnLine();
            m.appendLine("vals = result.get(groupKey)");
            m.appendLine("if (vals.isEmpty) {");
            m.appendLine("  result +=(groupKey.clone() -> new GroupValues(r)) ");
            m.appendLine("}else{");
            m.appendLine(" vals.get.addVal(r)");
            m.appendLine("}");
        });
        // r.methodBody("for ( ( r:" + ROW_MAP_CLASS_NAME + ") <- " + this.entityName.entities() + ".asScala)",
        // (m) -> {
        // 
        // m.startLine("groupKey = GroupKey.createCacheKey(")
        // .append(Joiner.on(",")
        // .join(groups.getGroups().stream()
        // .map((rr) -> "r.getColumn(\"" + (rr.getColname()) + "\")")
        // .iterator()));
        // m.append(")").returnLine();
        // 
        // m.appendLine("vals = result.get(groupKey)");
        // m.appendLine("if (vals.isEmpty) {");
        // // m.appendLine(" vals = new
        // // GroupValues();");
        // // m.appendLine("
        // // result.put(groupKey.clone(),
        // // vals);");
        // m.appendLine("  result +=(groupKey.clone() -> new GroupValues(r)) ");
        // m.appendLine("}else{");
        // m.appendLine(" vals.get.addVal(r)");
        // m.appendLine("}");
        // 
        // });
        r.appendLine(this.entityName.entities() + "ThreadLocal.set(result);");
        r.appendLine("return result;");
    });
    return funcFormat.toString();
}
Also used : FuncFormat(com.qlangtech.tis.sql.parser.visitor.FuncFormat) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Aggregations

FuncFormat (com.qlangtech.tis.sql.parser.visitor.FuncFormat)8 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)5 EntityName (com.qlangtech.tis.sql.parser.tuple.creator.EntityName)5 DependencyNode (com.qlangtech.tis.sql.parser.meta.DependencyNode)4 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang.StringUtils)3 LinkKeys (com.qlangtech.tis.sql.parser.er.LinkKeys)2 PrimaryTableMeta (com.qlangtech.tis.sql.parser.er.PrimaryTableMeta)2 TabCardinality (com.qlangtech.tis.sql.parser.er.TabCardinality)2 TableRelation (com.qlangtech.tis.sql.parser.er.TableRelation)2 FlatTableRelation (com.qlangtech.tis.sql.parser.stream.generate.FlatTableRelation)2 File (java.io.File)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CollectionUtils (org.apache.commons.collections.CollectionUtils)2 Lists (com.google.common.collect.Lists)1 PrimaryLinkKey (com.qlangtech.tis.sql.parser.meta.PrimaryLinkKey)1 MergeData (com.qlangtech.tis.sql.parser.stream.generate.MergeData)1 PrintStream (java.io.PrintStream)1