Sql format 代码,Sql格式化工具代码

public class SQLFormatUtil {

	public static String formatSql(String sql) {
		StringBuilder stringBuilder = new StringBuilder();
	    int indentationLevel = 0;
		boolean isOpenFunction = false;
	    
	    for (int i = 0; i < sql.length(); i++) {
	      char c = sql.charAt(i);
	      if (c == 'S' || c == 's') {
			  String subSql = sql.substring(i, i + 7);
			  if ("select ".equalsIgnoreCase(subSql)) {
				  appendIndentation(stringBuilder,indentationLevel);
				  stringBuilder.append(subSql);
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel + 1);
				  i += 6;
			  } else {
				  stringBuilder.append(c);
			  }
		  } else if (c == 'A' || c == 'a') {
			  String subSql = sql.substring(i, i+3);
			  if ("as ".equalsIgnoreCase(subSql)) {
				  stringBuilder.append(subSql);
				  i += 2;
			  } else {
				  stringBuilder.append(c);
			  }
		  } else if (c == ',') {
			  String subSql = sql.substring(i, i + 2);
	      	  if (", ".equals(subSql)) {
				  stringBuilder.append(c);
				  stringBuilder.append("\n");
				  i += 1;
			  }
			  appendIndentation(stringBuilder,indentationLevel + 1);
		  } else if (Character.isWhitespace(c)) {
			  stringBuilder.append(c);
		  } else if (c == 'F' || c == 'f') {
	      	  String subSql = sql.substring(i, i + 5);
	      	  if ("from ".equalsIgnoreCase(subSql)) {
	      	  	 stringBuilder.append("\n");
				 appendIndentation(stringBuilder,indentationLevel);
	      	  	 stringBuilder.append(subSql);
	      	  	 stringBuilder.append("\n");
	      	  	 appendIndentation(stringBuilder,indentationLevel + 1);
	      	  	 i += 4;
			  }
		  } else if (c == 'J' || c == 'j') {
			  String subSql = sql.substring(i, i+5);
			  if ("Join ".equalsIgnoreCase(subSql)) {
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel + 1);
				  stringBuilder.append(subSql);
				  i = i + 4;
			  } else {
			  	stringBuilder.append(c);
			  }
		  } else if (c == '(') {
			  int a = sql.substring(0 , i).lastIndexOf(" ");
			  String subSql = sql.substring(a + 1,i);
			  stringBuilder.append(c);
			  if ("sum".equalsIgnoreCase(subSql) || "min".equalsIgnoreCase(subSql) || "max".equalsIgnoreCase(subSql) || "avg".equalsIgnoreCase(subSql) || "count".equalsIgnoreCase(subSql)) {
			  	 isOpenFunction = true;
			  } else {
				  stringBuilder.append("\n");
				  indentationLevel += 2;
			  }
		  } else if (c == ')') {
	      	  if (isOpenFunction) {
				  isOpenFunction = false;
				  stringBuilder.append(c);
			  } else {
				  stringBuilder.append("\n");
				  indentationLevel -= 1;
				  appendIndentation(stringBuilder,indentationLevel);
				  stringBuilder.append(c);
				  indentationLevel -= 1;
			  }
		  } else if (c == 'W' || c == 'w') {
			  String subSql = sql.substring(i, i + 6);
			  if ("where ".equalsIgnoreCase(subSql)) {
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel);
				  stringBuilder.append(subSql);
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel + 1);
				  i += 5;
			  }
		  } else if (c == 'I' || c == 'i') {
			  String subSql = sql.substring(i, i+2);
			  if (!(Character.isLetter(sql.charAt(i - 1)) || Character.isLetter(sql.charAt(i + 3)))){
				  if ("in".equalsIgnoreCase(subSql)) {
					  stringBuilder.append(" ");
					  stringBuilder.append(subSql);
					  i += 1;
				  }
			  } else {
				  stringBuilder.append(c);
			  }
		  } else if (c == 'G' || c == 'g') {
			  String subSql = sql.substring(i, i+9);
			  if ("group by ".equalsIgnoreCase(subSql)) {
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel);
				  stringBuilder.append(subSql);
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel + 1);
				  i += 8;
			  } else {
				  stringBuilder.append(c);
			  }
		  } else if (c == 'O' || c == 'o') {
			  String subSql = sql.substring(i, i+9);
			  if ("order by ".equalsIgnoreCase(subSql)) {
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel);
				  stringBuilder.append(subSql);
				  stringBuilder.append("\n");
				  appendIndentation(stringBuilder,indentationLevel + 1);
				  i += 8;
			  } else {
				  stringBuilder.append(c);
			  }
		  } else if (c == 'D' || c == 'd') {
			  String subSql = sql.substring(i, i+4);
			  if ("desc".equalsIgnoreCase(subSql)) {
				  stringBuilder.append(subSql);
				  i += 8;
			  } else {
				  stringBuilder.append(c);
			  }
		  } else {
			  stringBuilder.append(c);
		  }
	    }
	    return stringBuilder.toString();
	}
	
	private static void appendIndentation(StringBuilder stringBuilder, int indentationLevel) {
	    for (int i = 0; i < indentationLevel; i++) {
	      stringBuilder.append("  ");
	    }
	}

	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值