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

运行后效果图:

 

转载于:https://www.cnblogs.com/JimmySeraph/p/10578942.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值