提交 f7dc512d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add and use StringUtils.trimSubstring(String, int)

上级 116b5276
...@@ -1303,41 +1303,40 @@ public class WebApp { ...@@ -1303,41 +1303,40 @@ public class WebApp {
return buff.toString(); return buff.toString();
} else if (isBuiltIn(sql, "@edit")) { } else if (isBuiltIn(sql, "@edit")) {
edit = true; edit = true;
sql = sql.substring("@edit".length()).trim(); sql = StringUtils.trimSubstring(sql, "@edit".length());
session.put("resultSetSQL", sql); session.put("resultSetSQL", sql);
} }
if (isBuiltIn(sql, "@list")) { if (isBuiltIn(sql, "@list")) {
list = true; list = true;
sql = sql.substring("@list".length()).trim(); sql = StringUtils.trimSubstring(sql, "@list".length());
} }
if (isBuiltIn(sql, "@meta")) { if (isBuiltIn(sql, "@meta")) {
metadata = true; metadata = true;
sql = sql.substring("@meta".length()).trim(); sql = StringUtils.trimSubstring(sql, "@meta".length());
} }
if (isBuiltIn(sql, "@generated")) { if (isBuiltIn(sql, "@generated")) {
generatedKeys = Statement.RETURN_GENERATED_KEYS; generatedKeys = Statement.RETURN_GENERATED_KEYS;
sql = sql.substring("@generated".length()).trim(); sql = StringUtils.trimSubstring(sql, "@generated".length());
} else if (isBuiltIn(sql, "@history")) { } else if (isBuiltIn(sql, "@history")) {
buff.append(getCommandHistoryString()); buff.append(getCommandHistoryString());
return buff.toString(); return buff.toString();
} else if (isBuiltIn(sql, "@loop")) { } else if (isBuiltIn(sql, "@loop")) {
sql = sql.substring("@loop".length()).trim(); sql = StringUtils.trimSubstring(sql, "@loop".length());
int idx = sql.indexOf(' '); int idx = sql.indexOf(' ');
int count = Integer.decode(sql.substring(0, idx)); int count = Integer.decode(sql.substring(0, idx));
sql = sql.substring(idx).trim(); sql = StringUtils.trimSubstring(sql, idx);
return executeLoop(conn, count, sql); return executeLoop(conn, count, sql);
} else if (isBuiltIn(sql, "@maxrows")) { } else if (isBuiltIn(sql, "@maxrows")) {
int maxrows = (int) Double.parseDouble( int maxrows = (int) Double.parseDouble(StringUtils.trimSubstring(sql, "@maxrows".length()));
sql.substring("@maxrows".length()).trim());
session.put("maxrows", Integer.toString(maxrows)); session.put("maxrows", Integer.toString(maxrows));
return "${text.result.maxrowsSet}"; return "${text.result.maxrowsSet}";
} else if (isBuiltIn(sql, "@parameter_meta")) { } else if (isBuiltIn(sql, "@parameter_meta")) {
sql = sql.substring("@parameter_meta".length()).trim(); sql = StringUtils.trimSubstring(sql, "@parameter_meta".length());
PreparedStatement prep = conn.prepareStatement(sql); PreparedStatement prep = conn.prepareStatement(sql);
buff.append(getParameterResultSet(prep.getParameterMetaData())); buff.append(getParameterResultSet(prep.getParameterMetaData()));
return buff.toString(); return buff.toString();
} else if (isBuiltIn(sql, "@password_hash")) { } else if (isBuiltIn(sql, "@password_hash")) {
sql = sql.substring("@password_hash".length()).trim(); sql = StringUtils.trimSubstring(sql, "@password_hash".length());
String[] p = split(sql); String[] p = split(sql);
return StringUtils.convertBytesToHex( return StringUtils.convertBytesToHex(
SHA256.getKeyPasswordHash(p[0], p[1].toCharArray())); SHA256.getKeyPasswordHash(p[0], p[1].toCharArray()));
...@@ -1349,7 +1348,7 @@ public class WebApp { ...@@ -1349,7 +1348,7 @@ public class WebApp {
profiler.startCollecting(); profiler.startCollecting();
return "Ok"; return "Ok";
} else if (isBuiltIn(sql, "@sleep")) { } else if (isBuiltIn(sql, "@sleep")) {
String s = sql.substring("@sleep".length()).trim(); String s = StringUtils.trimSubstring(sql, "@sleep".length());
int sleep = 1; int sleep = 1;
if (s.length() > 0) { if (s.length() > 0) {
sleep = Integer.parseInt(s); sleep = Integer.parseInt(s);
...@@ -1357,7 +1356,7 @@ public class WebApp { ...@@ -1357,7 +1356,7 @@ public class WebApp {
Thread.sleep(sleep * 1000); Thread.sleep(sleep * 1000);
return "Ok"; return "Ok";
} else if (isBuiltIn(sql, "@transaction_isolation")) { } else if (isBuiltIn(sql, "@transaction_isolation")) {
String s = sql.substring("@transaction_isolation".length()).trim(); String s = StringUtils.trimSubstring(sql, "@transaction_isolation".length());
if (s.length() > 0) { if (s.length() > 0) {
int level = Integer.parseInt(s); int level = Integer.parseInt(s);
conn.setTransactionIsolation(level); conn.setTransactionIsolation(level);
...@@ -1446,7 +1445,7 @@ public class WebApp { ...@@ -1446,7 +1445,7 @@ public class WebApp {
Random random = new Random(1); Random random = new Random(1);
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (isBuiltIn(sql, "@statement")) { if (isBuiltIn(sql, "@statement")) {
sql = sql.substring("@statement".length()).trim(); sql = StringUtils.trimSubstring(sql, "@statement".length());
prepared = false; prepared = false;
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
for (int i = 0; !stop && i < count; i++) { for (int i = 0; !stop && i < count; i++) {
......
...@@ -328,7 +328,7 @@ class WebThread extends WebApp implements Runnable { ...@@ -328,7 +328,7 @@ class WebThread extends WebApp implements Runnable {
} }
private static String getHeaderLineValue(String line) { private static String getHeaderLineValue(String line) {
return line.substring(line.indexOf(':') + 1).trim(); return StringUtils.trimSubstring(line, line.indexOf(':') + 1);
} }
@Override @Override
......
...@@ -210,8 +210,7 @@ public class RunScript extends Tool { ...@@ -210,8 +210,7 @@ public class RunScript extends Tool {
} }
if (trim.startsWith("@") && StringUtils.toUpperEnglish(trim). if (trim.startsWith("@") && StringUtils.toUpperEnglish(trim).
startsWith("@INCLUDE")) { startsWith("@INCLUDE")) {
sql = trim; sql = StringUtils.trimSubstring(sql, "@INCLUDE".length());
sql = sql.substring("@INCLUDE".length()).trim();
if (!FileUtils.isAbsolute(sql)) { if (!FileUtils.isAbsolute(sql)) {
sql = path + SysProperties.FILE_SEPARATOR + sql; sql = path + SysProperties.FILE_SEPARATOR + sql;
} }
......
...@@ -248,7 +248,7 @@ public class Shell extends Tool implements Runnable { ...@@ -248,7 +248,7 @@ public class Shell extends Tool implements Runnable {
println("No history"); println("No history");
} }
} else if (lower.startsWith("autocommit")) { } else if (lower.startsWith("autocommit")) {
lower = lower.substring("autocommit".length()).trim(); lower = StringUtils.trimSubstring(lower, "autocommit".length());
if ("true".equals(lower)) { if ("true".equals(lower)) {
conn.setAutoCommit(true); conn.setAutoCommit(true);
} else if ("false".equals(lower)) { } else if ("false".equals(lower)) {
...@@ -258,7 +258,7 @@ public class Shell extends Tool implements Runnable { ...@@ -258,7 +258,7 @@ public class Shell extends Tool implements Runnable {
} }
println("Autocommit is now " + conn.getAutoCommit()); println("Autocommit is now " + conn.getAutoCommit());
} else if (lower.startsWith("maxwidth")) { } else if (lower.startsWith("maxwidth")) {
lower = lower.substring("maxwidth".length()).trim(); lower = StringUtils.trimSubstring(lower, "maxwidth".length());
try { try {
maxColumnSize = Integer.parseInt(lower); maxColumnSize = Integer.parseInt(lower);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
......
...@@ -249,7 +249,7 @@ public class Profiler implements Runnable { ...@@ -249,7 +249,7 @@ public class Profiler implements Runnable {
if (!line.startsWith("at ")) { if (!line.startsWith("at ")) {
break; break;
} }
line = line.substring(3).trim(); line = StringUtils.trimSubstring(line, 3);
stack.add(line); stack.add(line);
} }
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
......
...@@ -848,7 +848,20 @@ public class StringUtils { ...@@ -848,7 +848,20 @@ public class StringUtils {
} }
/** /**
* Trim a character from a substring. Equivalence of {@code substring(begin, end).trim()}. * Trim a character from a substring. Equivalent of
* {@code substring(beginIndex).trim()}.
*
* @param s the string
* @param beginIndex start index of substring (inclusive)
* @return trimmed substring
*/
public static String trimSubstring(String s, int beginIndex) {
return trimSubstring(s, beginIndex, s.length());
}
/**
* Trim a character from a substring. Equivalent of
* {@code substring(beginIndex, endIndex).trim()}.
* *
* @param s the string * @param s the string
* @param beginIndex start index of substring (inclusive) * @param beginIndex start index of substring (inclusive)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论