1 package com.github.mygreen.splate;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.charset.StandardCharsets;
7 import java.security.MessageDigest;
8 import java.security.NoSuchAlgorithmException;
9 import java.util.concurrent.atomic.AtomicReference;
10
11 import lombok.AccessLevel;
12 import lombok.NoArgsConstructor;
13 import lombok.NonNull;
14
15
16
17
18
19
20
21
22
23 @NoArgsConstructor(access = AccessLevel.PRIVATE)
24 public class SqlUtils {
25
26
27
28
29 public static final String[] EMPTY_STRINGS = new String[0];
30
31
32
33
34
35
36
37
38
39
40 public static final String replace(final String text, final String fromText, final String toText) {
41
42 if (text == null || fromText == null || toText == null) {
43 return null;
44 }
45 StringBuilder buf = new StringBuilder(100);
46 int pos2 = 0;
47 while (true) {
48 int pos = text.indexOf(fromText, pos2);
49 if (pos == 0) {
50 buf.append(toText);
51 pos2 = fromText.length();
52 } else if (pos > 0) {
53 buf.append(text, pos2, pos);
54 buf.append(toText);
55 pos2 = pos + fromText.length();
56 } else {
57 buf.append(text.substring(pos2));
58 break;
59 }
60 }
61 return buf.toString();
62 }
63
64
65
66
67
68
69
70 public static final boolean isEmpty(final String text) {
71 return text == null || text.length() == 0;
72 }
73
74
75
76
77
78
79
80
81
82
83 public static String readStream(final InputStream in, final String encoding) throws IOException {
84
85 ByteArrayOutputStream out = new ByteArrayOutputStream();
86 try(in; out) {
87 byte[] buf = new byte[1024 * 8];
88 int length = 0;
89 while((length = in.read(buf)) != -1){
90 out.write(buf, 0, length);
91 }
92
93 return new String(out.toByteArray(), encoding);
94
95 }
96 }
97
98
99
100
101
102
103 public static String getMessageDigest(final String text) {
104
105 try {
106 MessageDigest md = MessageDigest.getInstance("SHA-256");
107 md.update(text.getBytes(StandardCharsets.UTF_8));
108
109 byte[] hash = md.digest();
110 StringBuilder sb = new StringBuilder();
111 for (byte b : hash) {
112 sb.append(String.format("%02x", b));
113 }
114
115 return sb.toString();
116
117 } catch (NoSuchAlgorithmException e) {
118 throw new IllegalArgumentException("not such algorithm name.", e);
119 }
120
121 }
122
123
124
125
126
127
128
129 public static Position resolveSqlPosition(final @NonNull String sql, final int position) {
130
131
132 final String before = sql.substring(0, position);
133
134 int row = 1;
135
136 final String[] searchWords = {"\r\n", "\r", "\n"};
137
138 AtomicReference<CharSequence> foundStr = new AtomicReference<>();
139 int lastIndex = 0;
140 int index = 0;
141 while((index = indexOfAny(before, index, foundStr, searchWords)) >= 0) {
142
143 row++;
144
145 index += foundStr.get().length();
146
147
148 lastIndex = index;
149 }
150
151
152 int col = position - lastIndex;
153 if(lastIndex > 0) {
154 col--;
155 }
156
157
158 final String after = sql.substring(lastIndex);
159 String line = after;
160 if(after != null && (index = indexOfAny(after, 0, null, searchWords)) >= 0) {
161 line = after.substring(0, index);
162 }
163
164 final Position result = new Position();
165 result.setCol(col);
166 result.setRow(row);
167 result.setLine(line);
168
169 return result;
170
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public static int indexOfAny(final CharSequence str, final int startPos,
192 final AtomicReference<CharSequence> foundStr, final CharSequence... searchStrs) {
193
194 if (str == null || searchStrs == null || searchStrs.length == 0) {
195 return INDEX_NOT_FOUND;
196 }
197
198
199 int ret = Integer.MAX_VALUE;
200
201 int tmp = 0;
202 for (final CharSequence search : searchStrs) {
203 if (search == null) {
204 continue;
205 }
206 tmp = indexOf(str, search, startPos);
207 if (tmp == INDEX_NOT_FOUND) {
208 continue;
209 }
210
211 if (tmp < ret) {
212 ret = tmp;
213
214
215 if(foundStr != null) {
216 foundStr.set(search);
217 }
218 }
219 }
220
221 return ret == Integer.MAX_VALUE ? INDEX_NOT_FOUND : ret;
222 }
223
224
225
226
227
228 private static final int INDEX_NOT_FOUND = -1;
229
230
231
232
233
234
235
236
237
238
239
240 private static int indexOf(final CharSequence cs, final CharSequence searchChar, final int start) {
241 if (cs instanceof String) {
242 return ((String) cs).indexOf(searchChar.toString(), start);
243 } else if (cs instanceof StringBuilder) {
244 return ((StringBuilder) cs).indexOf(searchChar.toString(), start);
245 } else if (cs instanceof StringBuffer) {
246 return ((StringBuffer) cs).indexOf(searchChar.toString(), start);
247 }
248 return cs.toString().indexOf(searchChar.toString(), start);
249 }
250 }