Search in sources :

Example 1 with StreamTransformVisitor

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

the class SqlTaskNode method reflectTableCols.

// 依赖的数据库表,可能有相同的表明,但是来自两个数据库的情况
// private static final Map<String /* tableName */, List<TableTupleCreator>> dumpNodes;
// static {
// try {
// 
// List<TableTupleCreator> tables = null;
// Map<String /* tableName */, List<TableTupleCreator>> builder = Maps.newHashMap();
// 
// File f = new File(
// "D:\\j2ee_solution\\eclipse-java-oxygen-mars-develop\\workspace\\tis-mars\\tis-sql-parser\\src\\main\\resources\\dump_tabs\\dump_tabs.txt");
// LineIterator lineIt = FileUtils.lineIterator(f, "utf8");
// String line = null;
// TableTupleCreator tupleCreator = null;
// 
// EntityName entityName = null;
// while (lineIt.hasNext()) {
// line = lineIt.nextLine();
// 
// entityName = EntityName.parse(line);
// 
// tupleCreator = new TableTupleCreator(line, NodeType.DUMP);
// 
// tables = builder.get(entityName.getTabName());
// if (tables == null) {
// tables = Lists.newArrayList();
// builder.put(entityName.getTabName(), tables);
// }
// 
// tupleCreator.setRealEntityName(entityName);
// tables.add(tupleCreator);
// 
// }
// dumpNodes = Collections.unmodifiableMap(builder);// builder.build();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// 
// }
public static List<ColumnMetaData> reflectTableCols(String sql) {
    if (StringUtils.isEmpty(sql)) {
        throw new IllegalArgumentException("param sql can not be null");
    }
    List<ColumnMetaData> result = Lists.newArrayList();
    Query query = parseQuery(sql);
    StreamTransformVisitor v = new StreamTransformVisitor(null);
    query.accept(v, new StackableAstVisitorContext<>(1));
    ColRef colsRef = v.getColsRef();
    int index = 0;
    for (Map.Entry<ColName, IDataTupleCreator> /* colName */
    entry : colsRef.getColRefMap().entrySet()) {
        // int index, String key, int type, boolean pk
        result.add(new ColumnMetaData(index++, StringUtils.lowerCase(entry.getKey().getName()), new DataType(-1), /**
         * 暂时无法取到类型,先用-1占一下位置
         */
        false));
    }
    return result;
}
Also used : StreamTransformVisitor(com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor) Query(com.facebook.presto.sql.tree.Query) ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) DataType(com.qlangtech.tis.plugin.ds.DataType) ColumnMetaData(com.qlangtech.tis.plugin.ds.ColumnMetaData) Map(java.util.Map)

Example 2 with StreamTransformVisitor

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

the class SqlTaskNode method parse.

public TableTupleCreator parse(boolean parseAllRefTab) {
    if (tupleCterator != null) {
        return this.tupleCterator;
    }
    this.tupleCterator = new TableTupleCreator(this.exportName.getTabName(), nodetype);
    tupleCterator.setRealEntityName(this.exportName);
    try {
        Query query = parseQuery(this.getContent());
        StreamTransformVisitor v = new StreamTransformVisitor(this.dumpNodesContext);
        query.accept(v, new StackableAstVisitorContext<>(1));
        ColRef colsRefs = v.getColsRef();
        tupleCterator.setColsRefs(colsRefs);
        // TaskNode 識別
        if (parseAllRefTab) {
            for (Map.Entry<String, IDataTupleCreator> /**
             * ref
             */
            entry : colsRefs.getBaseRefEntities()) {
                entry.getValue().accept(taskNodeRecognizedVisitor);
            }
        }
        return tupleCterator;
    } catch (Exception e) {
        throw new RuntimeException("exportName:" + this.exportName, e);
    }
}
Also used : ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) StreamTransformVisitor(com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) Query(com.facebook.presto.sql.tree.Query) Map(java.util.Map)

Aggregations

Query (com.facebook.presto.sql.tree.Query)2 IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)2 ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)2 StreamTransformVisitor (com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor)2 Map (java.util.Map)2 ColumnMetaData (com.qlangtech.tis.plugin.ds.ColumnMetaData)1 DataType (com.qlangtech.tis.plugin.ds.DataType)1 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)1