zoukankan      html  css  js  c++  java
  • SQL Builder 1.04

    解析效果:

    select id,code,name,utime,src,ctime from stock where id<20 and code like '%6%' order by id,code
    Index                Type No              Text                 Type Desc            
    ------------------------------------------------------------------------------------
    0                    1                    select               KW:select            
    1                    2                    id                   Text                 
    2                    3                    ,                    Comma                
    3                    2                    code                 Text                 
    4                    3                    ,                    Comma                
    5                    2                    name                 Text                 
    6                    3                    ,                    Comma                
    7                    2                    utime                Text                 
    8                    3                    ,                    Comma                
    9                    2                    src                  Text                 
    10                   3                    ,                    Comma                
    11                   2                    ctime                Text                 
    12                   4                    from                 KW:from              
    13                   2                    stock                Text                 
    14                   5                    where                KW:where             
    15                   2                    id                   Text                 
    16                   16                   <                    <                    
    17                   2                    20                   Text                 
    18                   6                    and                  KW:and               
    19                   2                    code                 Text                 
    20                   49                   like                 KW:like              
    21                   2                    '%6%'                Text                 
    22                   9                    order                KW:order             
    23                   10                   by                   KW:by                
    24                   2                    id                   Text                 
    25                   3                    ,                    Comma                
    26                   2                    code                 Text                 
    
    select
        id,
        code,
        name,
        utime,
        src,
        ctime
    from
        stock
    where
        id<20 and
        code like'%6%'
    order by
        id,
        code
        
    
    Text         Depth        Parent       Prev         Next         Child Cnt    
    ------------------------------------------------------------------------------------
    NULL         0            NULL         NULL         NULL         4            
    select       0            null         NULL         from         11           
    id           1            select       NULL         ,            0            
    ,            1            select       id           code         0            
    code         1            select       ,            ,            0            
    ,            1            select       code         name         0            
    name         1            select       ,            ,            0            
    ,            1            select       name         utime        0            
    utime        1            select       ,            ,            0            
    ,            1            select       utime        src          0            
    src          1            select       ,            ,            0            
    ,            1            select       src          ctime        0            
    ctime        1            select       ,            NULL         0            
    from         0            null         select       where        1            
    stock        1            from         NULL         NULL         0            
    where        0            null         from         order by     3            
    id<20        1            where        NULL         and          0            
    and          1            where        id<20        code like'%6%' 0            
    code like'%6%' 1            where        and          NULL         0            
    order by     0            null         where        NULL         4            
    id           1            order by     NULL         ,            0            
    ,            1            order by     id           code         0            
    code         1            order by     ,                         0            
                 1            order by     code         NULL         0            

    程序:

    package com.heyang.easysql.builder04;
    
    import java.util.List;
    
    import com.heyang.easysql.lex.Lexer;
    import com.heyang.easysql.node.Node;
    import com.heyang.easysql.token.Token;
    import com.heyang.easysql.util.EasySqlUtil;
    
    /**
     * SQL Builder 1.04
     * @author Heyang
     *
     */
    public class Builder {
        private Node root;
        
        public Builder(List<Token> tokens) {
            root=new Node(Node.TYPE_TRANSPARENT);
            
            int fromIndex=-1;
            int whereIndex=-1;
            int orderbyIndex=-1;
            for(int i=0;i<tokens.size();i++) {
                Token t=tokens.get(i);
                if(t.getType()==Token.TYPE_FROM) {
                    fromIndex=i;
                }
                if(t.getType()==Token.TYPE_WHERE) {
                    whereIndex=i;
                }
                if(t.getType()==Token.TYPE_ORDER) {
                    int nextIndex=i+1;
                    if(nextIndex<tokens.size()) {
                        Token nextToken=tokens.get(nextIndex);
                        if(nextToken.getType()==Token.TYPE_BY) {
                            orderbyIndex=i;
                        }
                    }
                }
            }
            
            if(fromIndex!=-1) {
                List<Token> subTokens=tokens.subList(0, fromIndex);
                Node select=new Node(subTokens.get(0).getText());
                root.addChild(select);
                
                for(int i=1;i<subTokens.size();i++) {
                    Token t=subTokens.get(i);
                    if(t.getType()==Token.TYPE_TEXT) {
                        select.addChild(new Node(t.getText()));
                    }else if(t.getType()==Token.TYPE_COMMA) {
                        select.addChild(new Node(t.getText(),Node.TYPE_JOINT));
                    }
                }
            }
            
            if(fromIndex!=-1 && whereIndex!=-1) {
                List<Token> subTokens=tokens.subList(fromIndex, whereIndex);
                Node from=new Node(subTokens.get(0).getText());
                root.addChild(from);
                
                for(int i=1;i<subTokens.size();i++) {
                    Token t=subTokens.get(i);
                    if(t.getType()==Token.TYPE_TEXT) {
                        from.addChild(new Node(t.getText()));
                    }else if(t.getType()==Token.TYPE_COMMA) {
                        from.addChild(new Node(t.getText(),Node.TYPE_JOINT));
                    }
                }
            }
            
            if(whereIndex!=-1 && orderbyIndex!=-1) {
                List<Token> subTokens=tokens.subList(whereIndex, orderbyIndex);
                Node where=new Node(subTokens.get(0).getText());
                root.addChild(where);
                
                String bundle="";
                for(int i=1;i<subTokens.size();i++) {
                    Token t=subTokens.get(i);
                    
                    if(t.getType()==Token.TYPE_AND || t.getType()==Token.TYPE_OR) {
                        where.addChild(new Node(bundle));
                        bundle="";
                        where.addChild(new Node(t.getText(),Node.TYPE_JOINT));
                    }else if(t.getType()==Token.TYPE_LIKE){
                        bundle+=" "+t.getText();
                    }else {
                        bundle+=t.getText();
                    }
                }
                
                where.addChild(new Node(bundle));
            }
            
            if(orderbyIndex!=-1) {
                List<Token> subTokens=tokens.subList(orderbyIndex, tokens.size());
                Node orderby=new Node("order by");
                root.addChild(orderby);
                
                String bundle="";
                for(int i=1;i<subTokens.size();i++) {
                    Token t=subTokens.get(i);
                    
                    if(t.getType()==Token.TYPE_TEXT) {
                        orderby.addChild(new Node(t.getText()));
                    }else if(t.getType()==Token.TYPE_COMMA) {
                        orderby.addChild(new Node(t.getText(),Node.TYPE_JOINT));
                    }
                }
                
                orderby.addChild(new Node(bundle));
            }
        }
        
        public void printRoot() {
            System.out.println(root);
            root.printHeaders();
            root.printTree();
        }
        
        public static void main(String[] args) throws Exception{
            String sql=EasySqlUtil.removeExtraSpace(EasySqlUtil.readSqlFromFile("c:\temp\18.sql"));
            System.out.println(sql);
            Lexer l=new Lexer(sql);
            List<Token> tokens=l.getTokenList();
            l.printTokenList();
            
            Builder builder=new Builder(tokens);
            builder.printRoot();
        }
    }

    --2020年5月15日--

  • 相关阅读:
    Docker 安装 Logstash
    Docker 安装 Kibana
    go命令行参数
    小程序 表单提交
    小程序picker日历
    小程序,验证码倒计时效果
    flex图片垂直居中
    小程序单选框样式
    wechat-wxparse 使用
    小程序Banner跳转
  • 原文地址:https://www.cnblogs.com/heyang78/p/12893391.html
Copyright © 2011-2022 走看看