zoukankan      html  css  js  c++  java
  • SQL Node 1.05版

    输出:

    select
        a.f1,
        b.f2
    from
        table01 a,
        (
            select
                a
            from
                tb
        )
        b
    where
        a.f1=1 and
        b.f2=2 or
        b.f3=3
    order by
        a.f1,
        b.f2
    
    Text         Depth        Parent       Prev         Next         Child Cnt    
    ------------------------------------------------------------------------------------
    NULL         0            NULL         NULL         NULL         4            
    select       0            null         NULL         from         3            
    a.f1         1            select       NULL         ,            0            
    ,            1            select       a.f1         b.f2         0            
    b.f2         1            select       ,            NULL         0            
    from         0            null         select       where        3            
    table01 a    1            from         NULL         ,            0            
    ,            1            from         table01 a    null         0            
    NULL         1            from         ,            NULL         4            
    (            1            null         NULL         null         0            
    NULL         1            null         (            )            2            
    select       2            null         NULL         from         1            
    a            3            select       NULL         NULL         0            
    from         2            null         select       NULL         1            
    tb           3            from         NULL         NULL         0            
    )            1            null         null         b            0            
    b            1            null         )            NULL         0            
    where        0            null         from         order by     5            
    a.f1=1       1            where        NULL         and          0            
    and          1            where        a.f1=1       b.f2=2       0            
    b.f2=2       1            where        and          or           0            
    or           1            where        b.f2=2       b.f3=3       0            
    b.f3=3       1            where        or           NULL         0            
    order by     0            null         where        NULL         3            
    a.f1         1            order by     NULL         ,            0            
    ,            1            order by     a.f1         b.f2         0            
    b.f2         1            order by     ,            NULL         0            

    程序:

    package com.heyang.easysql.nod05;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * Sql Node v1.05
     * @author Heyang
     *
     */
    public class Node {
        private static final String FOUR_SPACE = "    ";
        private static final String ONE_SPACE = " ";
        private String text=null;
        private int depth=0;
        private Node parent=null;
        private Node prev=null;
        private Node next=null;
        private List<Node> children=null;
        public static final int TYPE_NORMAL=1;
        public static final int TYPE_JOINT=2;
        public static final int TYPE_TRANSPARENT=3;
        private int type=TYPE_NORMAL;
        
    
        public Node() {
            
        }
        
        public Node(int type) {
            this.type=type;
        }
    
        public Node(String text) {
            this.text=text;
        }
        
        public Node(String text,int type) {
            this.text=text;
            this.type=type;
        }
        
        public boolean isLeaf() {
            return children==null || children.size()==0;
        }
        
        private List<Node> addChild(Node child) {
            if(children==null) {
                children=new ArrayList<Node>();
            }
            
            if(children.size()>0) {
                Node last=children.get(children.size()-1);
                last.next=child;
                child.prev=last;
            }
            
            child.parent=this;
            child.depth=this.depth+(this.type==TYPE_TRANSPARENT?0:1);
                
            children.add(child);
            adjustDepth();
            
            return children;
        }
        
        private String getIndentSpace() {
            return String.join("", Collections.nCopies(this.depth, FOUR_SPACE));
        }
        
        private void adjustDepth() {
            if(children!=null) {
                for(Node child:children) {
                    child.depth=this.depth+(this.type==TYPE_TRANSPARENT?0:1);
                    child.adjustDepth();
                }
            }
        }
        
        public void printHeaders() {
            final String continuousStar = createRepeatedStr("-", 84);
            final String layout = "%-12s %-12s %-12s %-12s %-12s %-12s %s";
            StringBuilder sb = new StringBuilder();
    
            sb.append(String.format(layout, "Text", "Depth","Parent","Prev","Next","Child Cnt","
    "));
            sb.append(continuousStar + "
    ");
            System.out.print(sb.toString());
        }
        
        public void printTree() {
            final String layout = "%-12s %-12s %-12s %-12s %-12s %-12s %s";
            StringBuilder sb = new StringBuilder();
            
            String text=(this.text==null?"NULL":this.text);
            int count=(this.children==null?0:this.children.size());
            String parentText=(this.parent==null?"NULL":this.parent.text);
            String prevText=(this.prev==null?"NULL":this.prev.text);
            String nextText=(this.next==null?"NULL":this.next.text);
            
            sb.append(String.format(layout, text,String.valueOf(this.depth), parentText,prevText,nextText,String.valueOf(count),"
    "));
            System.out.print(sb.toString());
            
            if(count>0) {
                for(Node child:children) {
                    child.printTree();
                }
            }
        }
        
        private static String createRepeatedStr(String seed, int n) {
            return String.join("", Collections.nCopies(n, seed));
        }
        
        public String toString() {
            StringBuilder sb=new StringBuilder();
    
            final String tabs=getIndentSpace();
            
            if(this.text!=null) {
                if(this.type!=TYPE_JOINT) {
                    sb.append(tabs+this.text);
                }else {
                    if(",".equalsIgnoreCase(this.text)) {
                        sb.append(this.text);
                    }else {
                        sb.append(ONE_SPACE+this.text);
                    }
                }
                            
                if(this.next!=null) {
                    if(this.next.type!=TYPE_JOINT) {
                        sb.append("
    ");
                    }
                }else {
                    sb.append("
    ");
                }
            }
            
            if(children!=null) {
                for(Node child:children) {
                    sb.append(child.toString());
                }
            }
            
            return sb.toString();
        }
        
        public static void main(String[] args) {
            Node f1=new Node("a.f1");
            Node comma=new Node(",",Node.TYPE_JOINT);
            Node f2=new Node("b.f2");
            
            Node select=new Node("select");
            select.addChild(f1);
            select.addChild(comma);
            select.addChild(f2);
            
            Node t1=new Node("table01 a");
            Node tComma=new Node(",",Node.TYPE_JOINT);
    
            Node childSelect=new Node("select");
            childSelect.addChild(new Node("a"));
            
            Node childFrom=new Node("from");
            childFrom.addChild(new Node("tb"));
            
            Node subSelect=new Node();
            subSelect.addChild(childSelect);
            subSelect.addChild(childFrom);
            
            Node t2=new Node(Node.TYPE_TRANSPARENT);
            t2.addChild(new Node("("));
            t2.addChild(subSelect);
            t2.addChild(new Node(")"));
            t2.addChild(new Node("b"));
            
            Node from=new Node("from");
            from.addChild(t1);
            from.addChild(tComma);
            from.addChild(t2);
            
            Node w1=new Node("a.f1=1");
            Node wAnd=new Node("and",Node.TYPE_JOINT);
            Node w2=new Node("b.f2=2");
            Node wor=new Node("or",Node.TYPE_JOINT);
            Node w3=new Node("b.f3=3");
            
            Node where=new Node("where");
            where.addChild(w1);
            where.addChild(wAnd);
            where.addChild(w2);
            where.addChild(wor);
            where.addChild(w3);
            
            Node orderby=new Node("order by");
            orderby.addChild(new Node("a.f1"));
            orderby.addChild(new Node(",",Node.TYPE_JOINT));
            orderby.addChild(new Node("b.f2"));
            
            Node query=new Node(Node.TYPE_TRANSPARENT);
            query.addChild(select);
            query.addChild(from);
            query.addChild(where);
            query.addChild(orderby);
            
            System.out.println(query);
            query.printHeaders();
            query.printTree();
        }
    }

    --2020年5月15日--

  • 相关阅读:
    async和await是如何实现异步编程?
    HD-ACM算法专攻系列(23)——Crixalis's Equipment
    HD-ACM算法专攻系列(22)——Max Sum
    HD-ACM算法专攻系列(21)——Wooden Sticks
    HD-ACM算法专攻系列(20)——七夕节
    HD-ACM算法专攻系列(19)——Leftmost Digit
    搭建Prometheus平台,你必须考虑的6个因素
    实用教程丨使用K3s和MySQL运行Rancher 2.4
    Kubernetes Ingress简单入门
    一文讲透Cluster API的前世、今生与未来
  • 原文地址:https://www.cnblogs.com/heyang78/p/12891070.html
Copyright © 2011-2022 走看看