博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java将很长的一条sql语句,自动换行输出(修改版)2019-06-01(bug未修复)
阅读量:4330 次
发布时间:2019-06-06

本文共 4326 字,大约阅读时间需要 14 分钟。

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
replaceMap = new HashMap
(); 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
> entrySet = replaceMap.entrySet(); Iterator
> 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

你可能感兴趣的文章
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>