1 | |
package liquibase.change; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.MSSQLDatabase; |
5 | |
import liquibase.statement.SqlStatement; |
6 | |
import liquibase.statement.core.RawSqlStatement; |
7 | |
import liquibase.util.StringUtils; |
8 | |
|
9 | |
import java.util.ArrayList; |
10 | |
import java.util.List; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public abstract class AbstractSQLChange extends AbstractChange { |
21 | |
|
22 | |
private boolean stripComments; |
23 | |
private boolean splitStatements; |
24 | |
private String endDelimiter; |
25 | |
private String sql; |
26 | |
|
27 | |
protected AbstractSQLChange(String tagName, String changeName, int priority) { |
28 | 55 | super(tagName, changeName, priority); |
29 | 55 | stripComments = false; |
30 | 55 | splitStatements = true; |
31 | 55 | } |
32 | |
|
33 | |
@Override |
34 | |
public boolean supports(Database database) { |
35 | 30 | return true; |
36 | |
} |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public void setStripComments(Boolean stripComments) { |
43 | 6 | this.stripComments = stripComments; |
44 | 6 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public boolean isStrippingComments() { |
51 | 6 | return stripComments; |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public void setSplitStatements(Boolean splitStatements) { |
62 | 6 | this.splitStatements = splitStatements; |
63 | 6 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public boolean isSplittingStatements() { |
70 | 6 | return splitStatements; |
71 | |
} |
72 | |
|
73 | |
public String getSql() { |
74 | 20 | return sql; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public void setSql(String sql) { |
81 | 38 | this.sql = StringUtils.trimToNull(sql); |
82 | 38 | } |
83 | |
|
84 | |
public String getEndDelimiter() { |
85 | 9 | return endDelimiter; |
86 | |
} |
87 | |
|
88 | |
public void setEndDelimiter(String endDelimiter) { |
89 | 5 | this.endDelimiter = endDelimiter; |
90 | 5 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public SqlStatement[] generateStatements(Database database) { |
102 | |
|
103 | 2 | List<SqlStatement> returnStatements = new ArrayList<SqlStatement>(); |
104 | |
|
105 | 2 | if (StringUtils.trimToNull(getSql()) == null) { |
106 | 0 | return new SqlStatement[0]; |
107 | |
} |
108 | |
|
109 | 2 | String processedSQL = getSql().replaceAll("\r\n", "\n").replaceAll("\r", "\n"); |
110 | 5 | for (String statement : StringUtils.processMutliLineSQL(processedSQL, isStrippingComments(), |
111 | |
isSplittingStatements(), getEndDelimiter())) { |
112 | 3 | if (database instanceof MSSQLDatabase) { |
113 | 0 | statement = statement.replaceAll("\n", "\r\n"); |
114 | |
} |
115 | |
|
116 | 3 | returnStatements.add(new RawSqlStatement(statement, getEndDelimiter())); |
117 | |
} |
118 | |
|
119 | 2 | return returnStatements.toArray(new SqlStatement[returnStatements.size()]); |
120 | |
} |
121 | |
} |