1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jdbc;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.springframework.util.Assert;
25
26 public class DefaultSqlReader implements SqlReader {
27
28 public static final String DEFAULT_DELIMITER = "/";
29 public static final DelimiterMode DEFAULT_DELIMITER_MODE = DelimiterMode.OWN_LINE;
30 public static final LineSeparator DEFAULT_LINE_SEPARATOR = LineSeparator.LF;
31 public static final List<String> DEFAULT_COMMENT_TOKENS = Arrays.asList("#", "--");
32 public static final boolean DEFAULT_IS_TRIM = true;
33 public static final boolean DEFAULT_IS_IGNORE_COMMENTS = true;
34
35 String delimiter = DEFAULT_DELIMITER;
36 DelimiterMode delimiterMode = DEFAULT_DELIMITER_MODE;
37 LineSeparator lineSeparator = DEFAULT_LINE_SEPARATOR;
38 boolean trim = DEFAULT_IS_TRIM;
39 boolean ignoreComments = DEFAULT_IS_IGNORE_COMMENTS;
40 List<String> commentTokens = DEFAULT_COMMENT_TOKENS;
41
42 @Override
43 public String getSqlStatement(BufferedReader reader) throws IOException {
44 Assert.notNull(delimiter, "delimiter is null");
45 String line = reader.readLine();
46 String trimmedLine = StringUtils.trimToNull(line);
47 StringBuilder sb = new StringBuilder();
48 while (line != null) {
49 if (isEndOfSqlStatement(trimmedLine, delimiter, delimiterMode)) {
50 return getReturnValue(sb.toString() + line, trim, lineSeparator);
51 }
52 if (!ignore(ignoreComments, sb, trimmedLine, commentTokens)) {
53 sb.append(line + lineSeparator.getValue());
54 }
55 line = reader.readLine();
56 trimmedLine = StringUtils.trimToNull(line);
57 }
58 return getReturnValue(sb.toString(), trim, lineSeparator);
59 }
60
61 protected String getReturnValue(String sql, boolean trim, LineSeparator lineSeparator) {
62 if (StringUtils.endsWith(sql, delimiter)) {
63 int endIndex = sql.length() - delimiter.length();
64 sql = StringUtils.substring(sql, 0, endIndex);
65 }
66 if (trim) {
67 sql = StringUtils.trimToNull(sql);
68 }
69 if (sql == null) {
70 return null;
71 } else if (StringUtils.endsWith(sql, lineSeparator.getValue())) {
72 int endIndex = sql.length() - lineSeparator.getValue().length();
73 return StringUtils.substring(sql, 0, endIndex);
74 } else {
75 return sql;
76 }
77 }
78
79 protected boolean isEndOfSqlStatement(String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
80 switch (delimiterMode) {
81 case END_OF_LINE:
82 return StringUtils.endsWith(trimmedLine, delimiter);
83 case OWN_LINE:
84 return StringUtils.equals(trimmedLine, delimiter);
85 default:
86 throw new IllegalArgumentException("Delimiter mode '" + delimiterMode + "' is unknown");
87 }
88 }
89
90 protected boolean proceed(String line, String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
91 if (line == null) {
92 return false;
93 }
94 boolean endOfSqlStatement = isEndOfSqlStatement(trimmedLine, delimiter, delimiterMode);
95 return !endOfSqlStatement;
96 }
97
98 protected boolean ignore(boolean ignoreComments, StringBuilder sql, String trimmedLine, List<String> commentTokens) {
99 if (!ignoreComments) {
100 return false;
101 }
102 if (!StringUtils.isBlank(sql.toString())) {
103 return false;
104 }
105 boolean isComment = isSqlComment(trimmedLine, commentTokens);
106 return isComment;
107 }
108
109 protected boolean isSqlComment(String trimmedLine, List<String> commentTokens) {
110 for (String commentToken : commentTokens) {
111 if (StringUtils.startsWith(trimmedLine, commentToken)) {
112 return true;
113 }
114 }
115 return false;
116 }
117
118 public String getDelimiter() {
119 return delimiter;
120 }
121
122 public void setDelimiter(String delimiter) {
123 this.delimiter = delimiter;
124 }
125
126 public boolean isTrim() {
127 return trim;
128 }
129
130 public void setTrim(boolean trim) {
131 this.trim = trim;
132 }
133
134 public boolean isIgnoreComments() {
135 return ignoreComments;
136 }
137
138 public void setIgnoreComments(boolean ignoreComments) {
139 this.ignoreComments = ignoreComments;
140 }
141
142 public LineSeparator getLineSeparator() {
143 return lineSeparator;
144 }
145
146 public void setLineSeparator(LineSeparator lineSeparator) {
147 this.lineSeparator = lineSeparator;
148 }
149
150 public DelimiterMode getDelimiterMode() {
151 return delimiterMode;
152 }
153
154 public void setDelimiterMode(DelimiterMode delimiterMode) {
155 this.delimiterMode = delimiterMode;
156 }
157
158 public List<String> getCommentTokens() {
159 return commentTokens;
160 }
161
162 public void setCommentTokens(List<String> commentTokens) {
163 this.commentTokens = commentTokens;
164 }
165
166 }