|
12 | 12 | */ |
13 | 13 | public class DDLFormatter { |
14 | 14 |
|
15 | | - private boolean noFormat; |
16 | | - private boolean sortCreateIndexStatements = true; |
17 | | - private boolean statementOnNewLine; |
18 | | - private boolean isMorePrettyFormat = false; |
| 15 | + private boolean noFormat; |
| 16 | + private boolean sortCreateIndexStatements = true; |
| 17 | + private boolean statementOnNewLine; |
| 18 | + private boolean isMorePrettyFormat = false; |
19 | 19 |
|
20 | | - static String newline = "\r\n"; //may be use "\n" |
| 20 | + static String newline = "\r\n"; // may be use "\n" |
21 | 21 |
|
22 | | - public String formatDDL(String ddl) { |
23 | | - if (noFormat) |
24 | | - return ddl; |
| 22 | + public String formatDDL(String ddl) { |
| 23 | + if (noFormat) |
| 24 | + return ddl; |
| 25 | + |
| 26 | + ddl = ddl.trim() + "\n"; |
25 | 27 |
|
26 | | - ddl = ddl.trim() + "\n"; |
27 | | - |
28 | 28 | // replace unix line endings with windows |
29 | 29 | ddl = ddl.replaceAll("\r\n", "\n"); |
30 | | - ddl = ddl.replaceAll("\n", "\r\n"); |
31 | | - |
32 | | - if (!isMorePrettyFormat) |
33 | | - return ddl; |
34 | | - |
35 | | - /* smart formatting */ |
36 | | - ddl = ddl.replaceAll(newline + "GRANT ", newline + newline + " GRANT "); |
37 | | - ddl = ddl.replaceAll(newline + "COMMENT ", newline + newline + " COMMENT "); |
38 | | - ddl = ddl.replaceAll(newline + " CREATE ", newline + "CREATE "); |
39 | | - ddl = ddl.replaceAll(newline + " ALTER ", newline + "ALTER "); |
40 | | - return ddl; |
41 | | - } |
42 | | - |
43 | | - public void setNoFormat(Boolean noFormat) { |
44 | | - this.noFormat = noFormat; |
45 | | - } |
46 | | - |
47 | | - @Deprecated |
48 | | - public void setStatementOnNewLine(Boolean statementOnNewLine) { |
49 | | - this.statementOnNewLine = statementOnNewLine; |
50 | | - } |
51 | | - |
52 | | - public void setIsMorePrettyFormat(boolean isMorePrettyFormat) { |
53 | | - this.isMorePrettyFormat = isMorePrettyFormat; |
54 | | - } |
55 | | - |
56 | | - public void setSortCreateIndexStatements(boolean sortCreateIndexStatements) { |
57 | | - this.sortCreateIndexStatements = sortCreateIndexStatements; |
58 | | - } |
59 | | - |
60 | | - public String replaceActualSequenceValueWithOne(String res) { |
61 | | - |
62 | | - String output; |
63 | | - Pattern p = Pattern.compile("CREATE SEQUENCE (.*) START WITH (\\d+) (.*)"); |
64 | | - Matcher m = p.matcher(res); |
65 | | - if (m.find()) { |
66 | | - output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + " START WITH 1 " + m.group(3) ); |
67 | | - if (!"1".equals(m.group(2))) |
68 | | - output = output + newline + "/* -- actual sequence value was replaced by scheme2ddl to 1 */"; |
69 | | - } |
70 | | - else { |
71 | | - output = res; |
72 | | - } |
73 | | - return output; |
74 | | - } |
75 | | - |
76 | | - /** |
77 | | - * Read input string with list of 'create index' statements and, tokenize and sort alphabetically. |
78 | | - * @param dependentDLL -string with list of 'create index' statements |
79 | | - * @return string with sorted alphabetically 'create index' statements |
80 | | - */ |
81 | | - public String sortIndexesInDDL(String dependentDLL) { |
82 | | - if (noFormat || !sortCreateIndexStatements){ |
83 | | - return dependentDLL; |
84 | | - } |
85 | | - String[] parts = dependentDLL.split("(?=CREATE INDEX)|(?=CREATE UNIQUE INDEX)|(?=CREATE BITMAP INDEX)"); |
86 | | - List<String> list = new ArrayList<String>(); |
87 | | - for (String part : parts) { |
88 | | - if (part != null && !part.trim().isEmpty()) { |
89 | | - list.add(part.trim()); |
90 | | - } |
91 | | - } |
92 | | - Collections.sort(list); |
93 | | - StringBuilder s = new StringBuilder(); |
94 | | - String prefix = "\n "; //to preserve formatting of dbms_metadata.get_depended_ddl output |
95 | | - for (String statement:list){ |
96 | | - s.append(prefix); |
97 | | - prefix = "\n"; |
98 | | - s.append(statement); |
99 | | - } |
100 | | - return s.toString(); |
101 | | - } |
| 30 | + ddl = ddl.replaceAll("\n", "\r\n"); |
| 31 | + |
| 32 | + if (!isMorePrettyFormat) |
| 33 | + return ddl; |
| 34 | + |
| 35 | + /* smart formatting */ |
| 36 | + ddl = ddl.replaceAll(newline + "GRANT ", newline + newline + " GRANT "); |
| 37 | + ddl = ddl.replaceAll(newline + "COMMENT ", newline + newline + " COMMENT "); |
| 38 | + ddl = ddl.replaceAll(newline + " CREATE ", newline + "CREATE "); |
| 39 | + ddl = ddl.replaceAll(newline + " ALTER ", newline + "ALTER "); |
| 40 | + return ddl; |
| 41 | + } |
| 42 | + |
| 43 | + public void setNoFormat(Boolean noFormat) { |
| 44 | + this.noFormat = noFormat; |
| 45 | + } |
| 46 | + |
| 47 | + @Deprecated |
| 48 | + public void setStatementOnNewLine(Boolean statementOnNewLine) { |
| 49 | + this.statementOnNewLine = statementOnNewLine; |
| 50 | + } |
| 51 | + |
| 52 | + public void setIsMorePrettyFormat(boolean isMorePrettyFormat) { |
| 53 | + this.isMorePrettyFormat = isMorePrettyFormat; |
| 54 | + } |
| 55 | + |
| 56 | + public void setSortCreateIndexStatements(boolean sortCreateIndexStatements) { |
| 57 | + this.sortCreateIndexStatements = sortCreateIndexStatements; |
| 58 | + } |
| 59 | + |
| 60 | + public String replaceActualSequenceValueWithOne(String res) { |
| 61 | + |
| 62 | + String output; |
| 63 | + Pattern p = Pattern.compile("CREATE SEQUENCE (.*) START WITH (\\d+) (.*)"); |
| 64 | + Matcher m = p.matcher(res); |
| 65 | + if (m.find()) { |
| 66 | + output = m.replaceFirst("CREATE SEQUENCE " + m.group(1) + " START WITH 1 " + m.group(3)); |
| 67 | + if (!"1".equals(m.group(2))) |
| 68 | + output = output + newline + "/* -- actual sequence value was replaced by scheme2ddl to 1 */"; |
| 69 | + } else { |
| 70 | + output = res; |
| 71 | + } |
| 72 | + return output; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Read input string with list of 'create index' statements and, tokenize and |
| 77 | + * sort alphabetically. |
| 78 | + * |
| 79 | + * @param dependentDLL |
| 80 | + * -string with list of 'create index' statements |
| 81 | + * @return string with sorted alphabetically 'create index' statements |
| 82 | + */ |
| 83 | + public String sortIndexesInDDL(String dependentDLL) { |
| 84 | + if (noFormat || !sortCreateIndexStatements) { |
| 85 | + return dependentDLL; |
| 86 | + } |
| 87 | + String[] parts = dependentDLL.split("(?=CREATE INDEX)|(?=CREATE UNIQUE INDEX)|(?=CREATE BITMAP INDEX)"); |
| 88 | + List<String> list = new ArrayList<String>(); |
| 89 | + for (String part : parts) { |
| 90 | + if (part != null && !part.trim().isEmpty()) { |
| 91 | + list.add(part.trim()); |
| 92 | + } |
| 93 | + } |
| 94 | + Collections.sort(list); |
| 95 | + StringBuilder s = new StringBuilder(); |
| 96 | + String prefix = "\n "; // to preserve formatting of dbms_metadata.get_depended_ddl output |
| 97 | + for (String statement : list) { |
| 98 | + s.append(prefix); |
| 99 | + prefix = "\n"; |
| 100 | + s.append(statement); |
| 101 | + } |
| 102 | + return s.toString(); |
| 103 | + } |
102 | 104 | } |
0 commit comments