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 super(tagName, changeName, priority);
29 stripComments = false;
30 splitStatements = true;
31 }
32
33 @Override
34 public boolean supports(Database database) {
35 return true;
36 }
37
38
39
40
41
42 public void setStripComments(Boolean stripComments) {
43 this.stripComments = stripComments;
44 }
45
46
47
48
49
50 public boolean isStrippingComments() {
51 return stripComments;
52 }
53
54
55
56
57
58
59
60
61 public void setSplitStatements(Boolean splitStatements) {
62 this.splitStatements = splitStatements;
63 }
64
65
66
67
68
69 public boolean isSplittingStatements() {
70 return splitStatements;
71 }
72
73 public String getSql() {
74 return sql;
75 }
76
77
78
79
80 public void setSql(String sql) {
81 this.sql = StringUtils.trimToNull(sql);
82 }
83
84 public String getEndDelimiter() {
85 return endDelimiter;
86 }
87
88 public void setEndDelimiter(String endDelimiter) {
89 this.endDelimiter = endDelimiter;
90 }
91
92
93
94
95
96
97
98
99
100
101 public SqlStatement[] generateStatements(Database database) {
102
103 List<SqlStatement> returnStatements = new ArrayList<SqlStatement>();
104
105 if (StringUtils.trimToNull(getSql()) == null) {
106 return new SqlStatement[0];
107 }
108
109 String processedSQL = getSql().replaceAll("\r\n", "\n").replaceAll("\r", "\n");
110 for (String statement : StringUtils.processMutliLineSQL(processedSQL, isStrippingComments(),
111 isSplittingStatements(), getEndDelimiter())) {
112 if (database instanceof MSSQLDatabase) {
113 statement = statement.replaceAll("\n", "\r\n");
114 }
115
116 returnStatements.add(new RawSqlStatement(statement, getEndDelimiter()));
117 }
118
119 return returnStatements.toArray(new SqlStatement[returnStatements.size()]);
120 }
121 }