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);
    }
    
    */
    
}

运行后效果图:

 

posted @ 2019-03-22 16:09  ラピスラズリ(Dawn)  阅读(474)  评论(0编辑  收藏  举报