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

Add StringUtils.quoteStringSQL(StringBuilder, String)

上级 f98b8066
......@@ -490,7 +490,7 @@ public class ScriptCommand extends ScriptBase {
if (len == 0) {
break;
}
buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))).
StringUtils.quoteStringSQL(buff, new String(chars, 0, len)).
append(", NULL)");
String sql = buff.toString();
add(sql, true);
......
......@@ -56,7 +56,8 @@ public class ConstraintCheck extends Constraint {
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
}
buff.append(" CHECK(");
expr.getUnenclosedSQL(buff).append(") NOCHECK");
......
......@@ -87,7 +87,8 @@ public class ConstraintReferential extends Constraint {
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
}
IndexColumn[] cols = columns;
IndexColumn[] refCols = refColumns;
......
......@@ -52,7 +52,8 @@ public class ConstraintUnique extends Constraint {
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
}
buff.append(' ').append(getConstraintType().getSqlName()).append('(');
for (IndexColumn c : columns) {
......
......@@ -74,7 +74,7 @@ public class Comment extends DbObjectBase {
if (commentText == null) {
buff.append("NULL");
} else {
buff.append(StringUtils.quoteStringSQL(commentText));
StringUtils.quoteStringSQL(buff, commentText);
}
return buff.toString();
}
......
......@@ -223,7 +223,8 @@ public class FunctionAlias extends SchemaObjectBase {
buff.append(" NOBUFFER");
}
if (source != null) {
buff.append(" AS ").append(StringUtils.quoteStringSQL(source));
buff.append(" AS ");
StringUtils.quoteStringSQL(buff, source);
} else {
buff.append(" FOR ").append(Parser.quoteIdentifier(
className + "." + methodName));
......
......@@ -159,7 +159,8 @@ public class User extends RightOwner {
StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS ");
buff.append(getSQL());
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
}
if (password) {
buff.append(" SALT '").
......
......@@ -410,7 +410,8 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
buff.append(quotedName);
buff.append(" ON ").append(targetTable.getSQL());
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
}
buff.append('(').append(getColumnListSQL()).append(')');
return buff.toString();
......
......@@ -347,7 +347,8 @@ public class TriggerObject extends SchemaObjectBase {
if (triggerClassName != null) {
buff.append(" CALL ").append(Parser.quoteIdentifier(triggerClassName));
} else {
buff.append(" AS ").append(StringUtils.quoteStringSQL(triggerSource));
buff.append(" AS ");
StringUtils.quoteStringSQL(buff, triggerSource);
}
return buff.toString();
}
......
......@@ -573,7 +573,8 @@ public class Column {
buff.append(" SELECTIVITY ").append(selectivity);
}
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
}
if (checkConstraint != null) {
buff.append(" CHECK ").append(checkConstraintSQL);
......
......@@ -71,19 +71,13 @@ public class LinkSchema {
append(StringUtils.quoteIdentifier(targetSchema)).
append('.').
append(StringUtils.quoteIdentifier(table)).
append('(').
append(StringUtils.quoteStringSQL(driver)).
append(", ").
append(StringUtils.quoteStringSQL(url)).
append(", ").
append(StringUtils.quoteStringSQL(user)).
append(", ").
append(StringUtils.quoteStringSQL(password)).
append(", ").
append(StringUtils.quoteStringSQL(sourceSchema)).
append(", ").
append(StringUtils.quoteStringSQL(table)).
append(')');
append('(');
StringUtils.quoteStringSQL(buff, driver).append(", ");
StringUtils.quoteStringSQL(buff, url).append(", ");
StringUtils.quoteStringSQL(buff, user).append(", ");
StringUtils.quoteStringSQL(buff, password).append(", ");
StringUtils.quoteStringSQL(buff, sourceSchema).append(", ");
StringUtils.quoteStringSQL(buff, table).append(')');
stat.execute(buff.toString());
result.addRow(table);
}
......
......@@ -108,7 +108,8 @@ public abstract class TableBase extends Table {
}
buff.append(getSQL());
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
}
buff.append("(\n ");
for (Column column : columns) {
......
......@@ -372,19 +372,15 @@ public class TableLink extends Table {
}
buff.append("LINKED TABLE ").append(getSQL());
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
}
buff.append('(').
append(StringUtils.quoteStringSQL(driver)).
append(", ").
append(StringUtils.quoteStringSQL(url)).
append(", ").
append(StringUtils.quoteStringSQL(user)).
append(", ").
append(StringUtils.quoteStringSQL(password)).
append(", ").
append(StringUtils.quoteStringSQL(originalTable)).
append(')');
buff.append('(');
StringUtils.quoteStringSQL(buff, driver).append(", ");
StringUtils.quoteStringSQL(buff, url).append(", ");
StringUtils.quoteStringSQL(buff, user).append(", ");
StringUtils.quoteStringSQL(buff, password).append(", ");
StringUtils.quoteStringSQL(buff, originalTable).append(')');
if (emitUpdates) {
buff.append(" EMIT UPDATES");
}
......
......@@ -348,7 +348,8 @@ public class TableView extends Table {
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
}
if (columns != null && columns.length > 0) {
buff.append('(');
......
......@@ -121,22 +121,40 @@ public class StringUtils {
if (s == null) {
return "NULL";
}
return quoteStringSQL(new StringBuilder(s.length() + 2), s).toString();
}
/**
* Convert a string to a SQL literal. Null is converted to NULL. The text is
* enclosed in single quotes. If there are any special characters, the
* method STRINGDECODE is used.
*
* @param builder
* string builder to append result to
* @param s the text to convert.
* @return the specified string builder
*/
public static StringBuilder quoteStringSQL(StringBuilder builder, String s) {
if (s == null) {
return builder.append("NULL");
}
int builderLength = builder.length();
int length = s.length();
StringBuilder buff = new StringBuilder(length + 2);
buff.append('\'');
builder.append('\'');
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == '\'') {
buff.append(c);
builder.append(c);
} else if (c < ' ' || c > 127) {
// need to start from the beginning because maybe there was a \
// that was not quoted
return "STRINGDECODE(" + quoteStringSQL(javaEncode(s)) + ")";
builder.setLength(builderLength);
builder.append("STRINGDECODE(");
return quoteStringSQL(builder, javaEncode(s)).append(')');
}
buff.append(c);
builder.append(c);
}
buff.append('\'');
return buff.toString();
return builder.append('\'');
}
/**
......@@ -321,7 +339,9 @@ public class StringUtils {
if (s == null) {
return "null";
}
return "\"" + javaEncode(s) + "\"";
StringBuilder builder = new StringBuilder(s.length() + 2).append('"');
javaEncode(s, builder);
return builder.append('"').toString();
}
/**
......
......@@ -95,7 +95,7 @@ public class ValueEnumBase extends Value {
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(label));
return StringUtils.quoteStringSQL(builder, label);
}
@Override
......
......@@ -572,7 +572,7 @@ public class ValueLob extends Value {
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString()));
StringUtils.quoteStringSQL(builder, getString());
} else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
}
......
......@@ -463,7 +463,7 @@ public class ValueLobDb extends Value {
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString()));
StringUtils.quoteStringSQL(builder, getString());
} else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
}
......
......@@ -35,7 +35,7 @@ public class ValueString extends Value {
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(value));
return StringUtils.quoteStringSQL(builder, value);
}
@Override
......
......@@ -48,7 +48,8 @@ public class ValueStringIgnoreCase extends ValueString {
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append("CAST(").append(StringUtils.quoteStringSQL(value)).append(" AS VARCHAR_IGNORECASE)");
builder.append("CAST(");
return StringUtils.quoteStringSQL(builder, value).append(" AS VARCHAR_IGNORECASE)");
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论