java-sql-generartor让我们复制一个轮子叭

java-sql-generartor让我们复制一个轮子叭,第1张

这个是鱼总用前端写的生成器

俺前端又不会那么多活儿,咱就用java写了个效果一样的

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public class TestJson {
    Map<String,String> map = new HashMap<>();
    @Test
    public void test(){
        addMap("main","select (a / b - 1) from (@查整体1^) a, (@查整体2^) b");
        addMap("查整体1","@查年级^ union (@查1班^) union (@查2班^) where date = 今天");
        addMap("查整体2","@查年级^ union (@查1班^) union (@查2班^) where date = 昨天");
        addMap("查年级","@查汇总_性别汇总^ union @查汇总_性别分组^ union @查汇总_爱好汇总^ union @查汇总_爱好分组^ union @查汇总_电脑类别汇总^ union @查汇总_电脑类别分组^");
        addMap("查汇总_性别汇总","@查除电脑关联表^");
        addMap("查汇总_性别分组","@查除电脑关联表^ group by 性别");
        addMap("查汇总_爱好汇总","@查除电脑关联表^");
        addMap("查汇总_爱好分组","@查除电脑关联表^ where 爱好 in (xx) group by 爱好");
        addMap("查汇总_电脑类别汇总","@查除三连和学习表^");
        addMap("查汇总_电脑类别分组","@查除三连和学习表^ group by 电脑类别");
        addMap("查1班","@查1班_性别汇总^ union @查1班_性别分组^ union @查1班_爱好汇总^ union @查1班_爱好分组^ union @查1班_电脑类别汇总^ union @查1班_电脑类别分组^");
        addMap("查1班_性别汇总","@查除电脑关联表^ where 1班");
        addMap("查1班_性别分组","@查除电脑关联表^ where 1班 group by 性别");
        addMap("查1班_爱好汇总","@查除电脑关联表^ where 爱好 in (xx) group by 爱好");
        addMap("查1班_爱好分组","@查除三连和学习表^");
        addMap("查1班_电脑类别汇总","@查除三连和学习表^ where 1班");
        addMap("查1班_电脑类别分组","@查除三连和学习表^ where 1班 group by 电脑类别");
        addMap("查2班","@查2班_性别汇总^ union @查2班_性别分组^ union @查2班_电脑类别汇总^ union @查2班_电脑类别分组^");
        addMap("查2班_性别汇总","@查除电脑关联表^ where 2班");
        addMap("查2班_性别分组","@查除电脑关联表^ where 2班 group by 性别");
        addMap("查2班_爱好汇总","@查除电脑关联表^ where 爱好 in (xx) group by 爱好");
        addMap("查2班_爱好分组","@查除三连和学习表^");
        addMap("查2班_电脑类别汇总","@查除三连和学习表^ where 2班");
        addMap("查2班_电脑类别分组","@查除三连和学习表^ where 2班 group by 电脑类别");
        addMap("查所有关联表","@查信息表^ left join (@查三连表^) left join (@查学习表^) left join (@查电脑表^) left join (@查全校信息^)");
        addMap("查除电脑关联表","@查信息表^ left join (@查三连表^) left join (@查学习表^) left join (@查全校信息^)");
        addMap("查除三连和学习表","@查信息表^ left join (@查电脑表^) left join (@查全校信息^)");
        addMap("查全校信息","select 字段 from 信息表");
        addMap("查电脑表","select 字段 from 电脑表 where 年级 = 1");
        addMap("查学习表","select 字段 from 学习表 where 年级 = 1");
        addMap("查三连表","select 字段 from 三连表 where 年级 = 1");
        addMap("查信息表","select 字段 from 信息表 where 年级 = 1");
        zhenghe();
        System.out.println(map.get("main"));
    }

    /**
     * "main": "select @身高差() from (@学生表(id = 1)) s1, (@学生表(id = 2)) s2",
     * "身高差": "(s1.height - s2.height) as 身高差",
     * "学生表": "select * from student where id = #{id}"
     * @param role
     * @param sql
     */
    public void addMap(String role,String sql){
        map.put(role,sql);
    }

    public void zhenghe(){
        Set<Map.Entry<String, String>> entries = map.entrySet();
        entries.forEach(new Consumer<Map.Entry<String, String>>() {
            @Override
            public void accept(Map.Entry<String, String> entry) {
                String mainSql = map.get("main");
                while(mainSql.contains("@")){
                    int index1 = mainSql.indexOf("@");
                    int index2 = mainSql.indexOf("^");
                    String role = mainSql.substring(index1+1,index2);
                    if(map.containsKey(role)){
                        mainSql=mainSql.replace("@"+role+"^",map.get(role));
                        map.put("main",mainSql);
                    }else{
                        System.out.println("不存在该表");
                        return;
                    }

                }
            }
        });
    }
}

输出结果咱们放到sql中美化一哈


嗯完美 虽然不知道啥时候俺能用得上 但是俺也是个能复制轮子的人了就很欣慰。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/922125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存