提交 390fe075 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Optimize STRINGDECODE path in StringUtils.quoteStringSQL()

上级 5a1c0bb5
...@@ -301,8 +301,8 @@ public class Trace { ...@@ -301,8 +301,8 @@ public class Trace {
buff.append(' '); buff.append(' ');
} }
buff.append("*/"); buff.append("*/");
StringUtils.javaEncode(sql, buff); StringUtils.javaEncode(sql, buff, false);
StringUtils.javaEncode(params, buff); StringUtils.javaEncode(params, buff, false);
buff.append(';'); buff.append(';');
sql = buff.toString(); sql = buff.toString();
traceWriter.write(TraceSystem.INFO, module, sql, null); traceWriter.write(TraceSystem.INFO, module, sql, null);
......
...@@ -149,8 +149,9 @@ public class StringUtils { ...@@ -149,8 +149,9 @@ public class StringUtils {
// need to start from the beginning because maybe there was a \ // need to start from the beginning because maybe there was a \
// that was not quoted // that was not quoted
builder.setLength(builderLength); builder.setLength(builderLength);
builder.append("STRINGDECODE("); builder.append("STRINGDECODE('");
return quoteStringSQL(builder, javaEncode(s)).append(')'); javaEncode(s, builder, true);
return builder.append("')");
} }
builder.append(c); builder.append(c);
} }
...@@ -167,11 +168,11 @@ public class StringUtils { ...@@ -167,11 +168,11 @@ public class StringUtils {
*/ */
public static String javaEncode(String s) { public static String javaEncode(String s) {
StringBuilder buff = new StringBuilder(s.length()); StringBuilder buff = new StringBuilder(s.length());
javaEncode(s, buff); javaEncode(s, buff, false);
return buff.toString(); return buff.toString();
} }
public static void javaEncode(String s, StringBuilder buff) { public static void javaEncode(String s, StringBuilder buff, boolean forSQL) {
int length = s.length(); int length = s.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char c = s.charAt(i); char c = s.charAt(i);
...@@ -201,27 +202,31 @@ public class StringUtils { ...@@ -201,27 +202,31 @@ public class StringUtils {
// double quote // double quote
buff.append("\\\""); buff.append("\\\"");
break; break;
case '\'':
// quote:
if (forSQL) {
buff.append('\'');
}
buff.append('\'');
break;
case '\\': case '\\':
// backslash // backslash
buff.append("\\\\"); buff.append("\\\\");
break; break;
default: default:
int ch = c & 0xffff; if (c >= ' ' && (c < 0x80)) {
if (ch >= ' ' && (ch < 0x80)) {
buff.append(c); buff.append(c);
// not supported in properties files // not supported in properties files
// } else if (ch < 0xff) { // } else if (c < 0xff) {
// buff.append("\\"); // buff.append("\\");
// // make sure it's three characters (0x200 is octal 1000) // // make sure it's three characters (0x200 is octal 1000)
// buff.append(Integer.toOctalString(0x200 | ch).substring(1)); // buff.append(Integer.toOctalString(0x200 | c).substring(1));
} else { } else {
buff.append("\\u"); buff.append("\\u")
String hex = Integer.toHexString(ch); .append(HEX[c >>> 12])
// make sure it's four characters .append(HEX[c >>> 8 & 0xf])
for (int len = hex.length(); len < 4; len++) { .append(HEX[c >>> 4 & 0xf])
buff.append('0'); .append(HEX[c & 0xf]);
}
buff.append(hex);
} }
} }
} }
...@@ -340,7 +345,7 @@ public class StringUtils { ...@@ -340,7 +345,7 @@ public class StringUtils {
return "null"; return "null";
} }
StringBuilder builder = new StringBuilder(s.length() + 2).append('"'); StringBuilder builder = new StringBuilder(s.length() + 2).append('"');
javaEncode(s, builder); javaEncode(s, builder, false);
return builder.append('"').toString(); return builder.append('"').toString();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论