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

Add StringUtils.quoteStringSQL(StringBuilder, String)

上级 f98b8066
...@@ -490,7 +490,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -490,7 +490,7 @@ public class ScriptCommand extends ScriptBase {
if (len == 0) { if (len == 0) {
break; break;
} }
buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))). StringUtils.quoteStringSQL(buff, new String(chars, 0, len)).
append(", NULL)"); append(", NULL)");
String sql = buff.toString(); String sql = buff.toString();
add(sql, true); add(sql, true);
......
...@@ -56,7 +56,8 @@ public class ConstraintCheck extends Constraint { ...@@ -56,7 +56,8 @@ public class ConstraintCheck extends Constraint {
} }
buff.append(quotedName); buff.append(quotedName);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
} }
buff.append(" CHECK("); buff.append(" CHECK(");
expr.getUnenclosedSQL(buff).append(") NOCHECK"); expr.getUnenclosedSQL(buff).append(") NOCHECK");
......
...@@ -87,7 +87,8 @@ public class ConstraintReferential extends Constraint { ...@@ -87,7 +87,8 @@ public class ConstraintReferential extends Constraint {
} }
buff.append(quotedName); buff.append(quotedName);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
} }
IndexColumn[] cols = columns; IndexColumn[] cols = columns;
IndexColumn[] refCols = refColumns; IndexColumn[] refCols = refColumns;
......
...@@ -52,7 +52,8 @@ public class ConstraintUnique extends Constraint { ...@@ -52,7 +52,8 @@ public class ConstraintUnique extends Constraint {
} }
buff.append(quotedName); buff.append(quotedName);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
} }
buff.append(' ').append(getConstraintType().getSqlName()).append('('); buff.append(' ').append(getConstraintType().getSqlName()).append('(');
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
......
...@@ -74,7 +74,7 @@ public class Comment extends DbObjectBase { ...@@ -74,7 +74,7 @@ public class Comment extends DbObjectBase {
if (commentText == null) { if (commentText == null) {
buff.append("NULL"); buff.append("NULL");
} else { } else {
buff.append(StringUtils.quoteStringSQL(commentText)); StringUtils.quoteStringSQL(buff, commentText);
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -223,7 +223,8 @@ public class FunctionAlias extends SchemaObjectBase { ...@@ -223,7 +223,8 @@ public class FunctionAlias extends SchemaObjectBase {
buff.append(" NOBUFFER"); buff.append(" NOBUFFER");
} }
if (source != null) { if (source != null) {
buff.append(" AS ").append(StringUtils.quoteStringSQL(source)); buff.append(" AS ");
StringUtils.quoteStringSQL(buff, source);
} else { } else {
buff.append(" FOR ").append(Parser.quoteIdentifier( buff.append(" FOR ").append(Parser.quoteIdentifier(
className + "." + methodName)); className + "." + methodName));
......
...@@ -159,7 +159,8 @@ public class User extends RightOwner { ...@@ -159,7 +159,8 @@ public class User extends RightOwner {
StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS "); StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS ");
buff.append(getSQL()); buff.append(getSQL());
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
} }
if (password) { if (password) {
buff.append(" SALT '"). buff.append(" SALT '").
......
...@@ -410,7 +410,8 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -410,7 +410,8 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
buff.append(quotedName); buff.append(quotedName);
buff.append(" ON ").append(targetTable.getSQL()); buff.append(" ON ").append(targetTable.getSQL());
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
} }
buff.append('(').append(getColumnListSQL()).append(')'); buff.append('(').append(getColumnListSQL()).append(')');
return buff.toString(); return buff.toString();
......
...@@ -347,7 +347,8 @@ public class TriggerObject extends SchemaObjectBase { ...@@ -347,7 +347,8 @@ public class TriggerObject extends SchemaObjectBase {
if (triggerClassName != null) { if (triggerClassName != null) {
buff.append(" CALL ").append(Parser.quoteIdentifier(triggerClassName)); buff.append(" CALL ").append(Parser.quoteIdentifier(triggerClassName));
} else { } else {
buff.append(" AS ").append(StringUtils.quoteStringSQL(triggerSource)); buff.append(" AS ");
StringUtils.quoteStringSQL(buff, triggerSource);
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -573,7 +573,8 @@ public class Column { ...@@ -573,7 +573,8 @@ public class Column {
buff.append(" SELECTIVITY ").append(selectivity); buff.append(" SELECTIVITY ").append(selectivity);
} }
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
} }
if (checkConstraint != null) { if (checkConstraint != null) {
buff.append(" CHECK ").append(checkConstraintSQL); buff.append(" CHECK ").append(checkConstraintSQL);
......
...@@ -71,19 +71,13 @@ public class LinkSchema { ...@@ -71,19 +71,13 @@ public class LinkSchema {
append(StringUtils.quoteIdentifier(targetSchema)). append(StringUtils.quoteIdentifier(targetSchema)).
append('.'). append('.').
append(StringUtils.quoteIdentifier(table)). append(StringUtils.quoteIdentifier(table)).
append('('). append('(');
append(StringUtils.quoteStringSQL(driver)). StringUtils.quoteStringSQL(buff, driver).append(", ");
append(", "). StringUtils.quoteStringSQL(buff, url).append(", ");
append(StringUtils.quoteStringSQL(url)). StringUtils.quoteStringSQL(buff, user).append(", ");
append(", "). StringUtils.quoteStringSQL(buff, password).append(", ");
append(StringUtils.quoteStringSQL(user)). StringUtils.quoteStringSQL(buff, sourceSchema).append(", ");
append(", "). StringUtils.quoteStringSQL(buff, table).append(')');
append(StringUtils.quoteStringSQL(password)).
append(", ").
append(StringUtils.quoteStringSQL(sourceSchema)).
append(", ").
append(StringUtils.quoteStringSQL(table)).
append(')');
stat.execute(buff.toString()); stat.execute(buff.toString());
result.addRow(table); result.addRow(table);
} }
......
...@@ -108,7 +108,8 @@ public abstract class TableBase extends Table { ...@@ -108,7 +108,8 @@ public abstract class TableBase extends Table {
} }
buff.append(getSQL()); buff.append(getSQL());
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
} }
buff.append("(\n "); buff.append("(\n ");
for (Column column : columns) { for (Column column : columns) {
......
...@@ -372,19 +372,15 @@ public class TableLink extends Table { ...@@ -372,19 +372,15 @@ public class TableLink extends Table {
} }
buff.append("LINKED TABLE ").append(getSQL()); buff.append("LINKED TABLE ").append(getSQL());
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment);
} }
buff.append('('). buff.append('(');
append(StringUtils.quoteStringSQL(driver)). StringUtils.quoteStringSQL(buff, driver).append(", ");
append(", "). StringUtils.quoteStringSQL(buff, url).append(", ");
append(StringUtils.quoteStringSQL(url)). StringUtils.quoteStringSQL(buff, user).append(", ");
append(", "). StringUtils.quoteStringSQL(buff, password).append(", ");
append(StringUtils.quoteStringSQL(user)). StringUtils.quoteStringSQL(buff, originalTable).append(')');
append(", ").
append(StringUtils.quoteStringSQL(password)).
append(", ").
append(StringUtils.quoteStringSQL(originalTable)).
append(')');
if (emitUpdates) { if (emitUpdates) {
buff.append(" EMIT UPDATES"); buff.append(" EMIT UPDATES");
} }
......
...@@ -348,7 +348,8 @@ public class TableView extends Table { ...@@ -348,7 +348,8 @@ public class TableView extends Table {
} }
buff.append(quotedName); buff.append(quotedName);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment)); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment);
} }
if (columns != null && columns.length > 0) { if (columns != null && columns.length > 0) {
buff.append('('); buff.append('(');
......
...@@ -121,22 +121,40 @@ public class StringUtils { ...@@ -121,22 +121,40 @@ public class StringUtils {
if (s == null) { if (s == null) {
return "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(); int length = s.length();
StringBuilder buff = new StringBuilder(length + 2); builder.append('\'');
buff.append('\'');
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char c = s.charAt(i); char c = s.charAt(i);
if (c == '\'') { if (c == '\'') {
buff.append(c); builder.append(c);
} else if (c < ' ' || c > 127) { } else if (c < ' ' || c > 127) {
// 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
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 builder.append('\'');
return buff.toString();
} }
/** /**
...@@ -321,7 +339,9 @@ public class StringUtils { ...@@ -321,7 +339,9 @@ public class StringUtils {
if (s == null) { if (s == null) {
return "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 { ...@@ -95,7 +95,7 @@ public class ValueEnumBase extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(label)); return StringUtils.quoteStringSQL(builder, label);
} }
@Override @Override
......
...@@ -572,7 +572,7 @@ public class ValueLob extends Value { ...@@ -572,7 +572,7 @@ public class ValueLob extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString())); StringUtils.quoteStringSQL(builder, getString());
} else { } else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\''); builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
} }
......
...@@ -463,7 +463,7 @@ public class ValueLobDb extends Value { ...@@ -463,7 +463,7 @@ public class ValueLobDb extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString())); StringUtils.quoteStringSQL(builder, getString());
} else { } else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\''); builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
} }
......
...@@ -35,7 +35,7 @@ public class ValueString extends Value { ...@@ -35,7 +35,7 @@ public class ValueString extends Value {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(value)); return StringUtils.quoteStringSQL(builder, value);
} }
@Override @Override
......
...@@ -48,7 +48,8 @@ public class ValueStringIgnoreCase extends ValueString { ...@@ -48,7 +48,8 @@ public class ValueStringIgnoreCase extends ValueString {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论