@date 2019/4/30 15:06 @desc sql格式化工具
| 10 | * @desc sql格式化工具 |
| 11 | */ |
| 12 | public class SqlFormatter { |
| 13 | private static final Set<String> BEGIN_CLAUSES = new HashSet<String>(); |
| 14 | private static final Set<String> END_CLAUSES = new HashSet<String>(); |
| 15 | private static final Set<String> LOGICAL = new HashSet<String>(); |
| 16 | private static final Set<String> QUANTIFIERS = new HashSet<String>(); |
| 17 | private static final Set<String> DML = new HashSet<String>(); |
| 18 | private static final Set<String> MISC = new HashSet<String>(); |
| 19 | static final String indentString = " "; |
| 20 | static final String initial = "\n "; |
| 21 | |
| 22 | public static String format(String source) { |
| 23 | return new FormatProcess(source).perform().trim(); |
| 24 | } |
| 25 | |
| 26 | static { |
| 27 | BEGIN_CLAUSES.add("left"); |
| 28 | BEGIN_CLAUSES.add("right"); |
| 29 | BEGIN_CLAUSES.add("inner"); |
| 30 | BEGIN_CLAUSES.add("outer"); |
| 31 | BEGIN_CLAUSES.add("group"); |
| 32 | BEGIN_CLAUSES.add("order"); |
| 33 | |
| 34 | END_CLAUSES.add("where"); |
| 35 | END_CLAUSES.add("set"); |
| 36 | END_CLAUSES.add("having"); |
| 37 | END_CLAUSES.add("join"); |
| 38 | END_CLAUSES.add("from"); |
| 39 | END_CLAUSES.add("by"); |
| 40 | END_CLAUSES.add("into"); |
| 41 | END_CLAUSES.add("union"); |
| 42 | END_CLAUSES.add("limit"); |
| 43 | |
| 44 | LOGICAL.add("and"); |
| 45 | LOGICAL.add("or"); |
| 46 | LOGICAL.add("when"); |
| 47 | LOGICAL.add("else"); |
| 48 | LOGICAL.add("end"); |
| 49 | |
| 50 | QUANTIFIERS.add("in"); |
| 51 | QUANTIFIERS.add("all"); |
| 52 | QUANTIFIERS.add("exists"); |
| 53 | QUANTIFIERS.add("some"); |
| 54 | QUANTIFIERS.add("any"); |
| 55 | |
| 56 | DML.add("insert"); |
| 57 | DML.add("update"); |
| 58 | DML.add("delete"); |
| 59 | |
| 60 | MISC.add("select"); |
| 61 | MISC.add("on"); |
| 62 | } |
| 63 | |
| 64 | private static class FormatProcess { |
| 65 | boolean beginLine = true; |
| 66 | boolean afterBeginBeforeEnd = false; |
| 67 | boolean afterByOrSetOrFromOrSelect = false; |
| 68 | boolean afterValues = false; |
| 69 | boolean afterOn = false; |
nothing calls this directly
no outgoing calls
no test coverage detected