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(new String[] { "#", "--" });
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 (proceed(line, trimmedLine, delimiter, delimiterMode)) {
49 if (!ignore(ignoreComments, sb, trimmedLine, commentTokens)) {
50 sb.append(line + lineSeparator.getValue());
51 }
52 line = reader.readLine();
53 trimmedLine = StringUtils.trimToNull(line);
54 }
55 return getReturnValue(sb.toString(), trim, lineSeparator);
56 }
57
58 protected String getReturnValue(String sql, boolean trim, LineSeparator lineSeparator) {
59 if (trim) {
60 sql = StringUtils.trimToNull(sql);
61 }
62 if (sql == null) {
63 return null;
64 } else if (StringUtils.endsWith(sql, lineSeparator.getValue())) {
65 int endIndex = sql.length() - lineSeparator.getValue().length();
66 return StringUtils.substring(sql, 0, endIndex);
67 } else {
68 return sql;
69 }
70 }
71
72 protected boolean isEndOfSqlStatement(String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
73 switch (delimiterMode) {
74 case END_OF_LINE:
75 return StringUtils.endsWith(trimmedLine, delimiter);
76 case OWN_LINE:
77 return StringUtils.equals(trimmedLine, delimiter);
78 default:
79 throw new IllegalArgumentException("Delimiter mode '" + delimiterMode + "' is unknown");
80 }
81 }
82
83 protected boolean proceed(String line, String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
84 return line != null && !isEndOfSqlStatement(trimmedLine, delimiter, delimiterMode);
85 }
86
87 protected boolean ignore(boolean ignoreComments, StringBuilder sql, String trimmedLine, List<String> commentTokens) {
88 return ignoreComments && StringUtils.isBlank(sql.toString()) && isSqlComment(trimmedLine, commentTokens);
89 }
90
91 protected boolean isSqlComment(String trimmedLine, List<String> commentTokens) {
92 for (String commentToken : commentTokens) {
93 if (StringUtils.startsWith(trimmedLine, commentToken)) {
94 return true;
95 }
96 }
97 return false;
98 }
99
100 public String getDelimiter() {
101 return delimiter;
102 }
103
104 public void setDelimiter(String delimiter) {
105 this.delimiter = delimiter;
106 }
107
108 public boolean isTrim() {
109 return trim;
110 }
111
112 public void setTrim(boolean trim) {
113 this.trim = trim;
114 }
115
116 public boolean isIgnoreComments() {
117 return ignoreComments;
118 }
119
120 public void setIgnoreComments(boolean ignoreComments) {
121 this.ignoreComments = ignoreComments;
122 }
123
124 public LineSeparator getLineSeparator() {
125 return lineSeparator;
126 }
127
128 public void setLineSeparator(LineSeparator lineSeparator) {
129 this.lineSeparator = lineSeparator;
130 }
131
132 public DelimiterMode getDelimiterMode() {
133 return delimiterMode;
134 }
135
136 public void setDelimiterMode(DelimiterMode delimiterMode) {
137 this.delimiterMode = delimiterMode;
138 }
139
140 public List<String> getCommentTokens() {
141 return commentTokens;
142 }
143
144 public void setCommentTokens(List<String> commentTokens) {
145 this.commentTokens = commentTokens;
146 }
147
148 }