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(" ");
}
}
}
Sql format 代码,Sql格式化工具代码
最新推荐文章于 2024-06-09 09:31:34 发布