zoukankan      html  css  js  c++  java
  • java将很长的一条sql语句,自动换行输出(修改版)2019-06-01(bug未修复)

    package org.jimmy.autosearch2019.test;
    
    import java.util.HashMap;
    
    public class AutoLinefeedSql {
    
        public static final String BLANK = " ";
        public static final String BLANK_REG = " {2,}";
        public static final String SEMICOLON = ";";
        public static final String COMMA = ",";
        public static final String COMMA_REG = ", +| +,";
        public static final int WORD_PER_LINE = 4;
        public static HashMap<Integer, Integer> replaceMap = new HashMap<Integer, Integer>();
        public static final String SINGLE_QUOTE_REG = "'";
        public static final String REPLACE_TEXT = "u9fa5";
        
        public static void main(String[] args) {
            String sql = "with result as( select t.str from( select '1' str union all select '2' str union all select '3' str union all select '4' str union all select '5' str union all select '6' str union all select '7' str ) t cross apply (select t2.str from( select '1' str union all select '2' str union all select '3' str union all select '4' str ) t2 where t.str = t2.str) t3) select * from result; go";
            sql = replaceSpecialStr(sql, SINGLE_QUOTE_REG, REPLACE_TEXT);
            System.out.println(autoLinefeed(sql, SINGLE_QUOTE_REG, REPLACE_TEXT));
        }
    
        /**
         * @author ラピスラズリ(Dawn)
         * @date 2019年6月1日 下午4:29:19
         * @detail 替换成一定不会出现在sql中的字符串,换行后再替换回来
         */
        public static String replaceSpecialStr(String sql, String reg, String text) {
            sql = sql.replaceAll(reg, text);
            return sql;
        }
        
        /**
         * @author ラピスラズリ(Dawn)
         * @date 2019年6月1日 下午4:29:55
         * @detail 自动换行sql
         */
        public static String autoLinefeed(String sql, String reg, String replaceText) {
            //空格数超过2就替换成1个空格
            sql = sql.replaceAll(BLANK_REG, BLANK);
            //,空格或空格,替换成,
            sql = sql.replaceAll(COMMA_REG, COMMA);
            StringBuffer linefeededTextSb = new StringBuffer();
            String[] sqlSubArr = sql.split(BLANK);
            for(int i = 0; i < sqlSubArr.length; i++) {
                String sqlSub = sqlSubArr[i].trim();
                if(sqlSub.indexOf(SEMICOLON) == sqlSub.length() - 1) {
                    linefeededTextSb.append(sqlSub + System.lineSeparator());
                }else {
                    if(i % WORD_PER_LINE == WORD_PER_LINE - 1) {
                        linefeededTextSb.append(sqlSub + System.lineSeparator());
                    }else {
                        linefeededTextSb.append(sqlSub + BLANK);
                    }
                }
            }
            sql = linefeededTextSb.toString();
            linefeededTextSb = new StringBuffer();
            sqlSubArr = sql.split(COMMA);
            if(sqlSubArr != null && sqlSubArr.length > 0) {
                for(int i = 0; i < sqlSubArr.length; i++) {
                    String sqlSub = sqlSubArr[i];
                    if(sqlSub.indexOf(SEMICOLON) == sqlSub.length() - 1) {
                        linefeededTextSb.append(sqlSub + System.lineSeparator());
                    }else {
                        if(i % WORD_PER_LINE == WORD_PER_LINE - 1) {
                            linefeededTextSb.append(sqlSub + COMMA + System.lineSeparator());
                        }else {
                            linefeededTextSb.append(sqlSub + COMMA);
                        }
                    }
                }
            }
            if(linefeededTextSb.lastIndexOf(COMMA) == linefeededTextSb.length() - 1) {
                linefeededTextSb.deleteCharAt(linefeededTextSb.length() - 1);
            }
            String text = linefeededTextSb.toString();
            text = text.replaceAll(replaceText, reg);
            return text;
        }
        
        /*public static void replaceSpecialStr() {
            Set<Entry<Integer, Integer>> entrySet = replaceMap.entrySet();
            Iterator<Entry<Integer, Integer>> iterator = entrySet.iterator();
            while(iterator.hasNext()) {
                int index = iterator.next().getValue();
            }
        }
        
        public static void initReplaceMap(String sql, int index, String text) {
            System.out.println("index:" + index + ",text:" + text);
            int singleQuoteIndex1 = index;
            int singleQuoteIndex2 = index + text.length() - 1;
            replaceMap.put(singleQuoteIndex1, singleQuoteIndex1);
            replaceMap.put(singleQuoteIndex2, singleQuoteIndex2);
            String singleQuote1 = sql.substring(singleQuoteIndex1, singleQuoteIndex1 + 1);
            System.out.println(singleQuote1);
            String singleQuote2 = sql.substring(singleQuoteIndex2, singleQuoteIndex2 + 1);
            System.out.println(singleQuote2);
        }
        
        */
        
    }

    运行后效果图:

    2015年10月-2016年3月 总计:5个月.
    2016年11月-2017年6月 总计:7个月.
    2017年7月-2018年4月 总计:9个月.
    2018年5月-2018年5月 总计:1个月.
    2018年6月-2018年12月 总计:6个月.
    2019年1月-2019年12月 总计11个月.
    2020年2月-2021年2月 总计13个月.
    所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
    本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
    英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
  • 相关阅读:
    paramiko连接sshd使用的hostkey
    jmete命令行停止失败的原因分析
    send-mail: fatal: parameter inet_interfaces: no local interface found for ::1
    linux安装mail服务使用外部MTA发送邮件
    crontab使用简介
    pip命令自动补全功能;设置代理;使用国内源
    grep使用正则表达式搜索IP地址
    python执行系统命令的几种方法
    js 字符串 replace replaceAll
    tomcat 和 jboss的热部署(热发布)问题
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/10578942.html
Copyright © 2011-2022 走看看