提交 95cd3025 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add quoteIdentifier() variants with StringBuilder argument

上级 ace8433a
......@@ -7718,6 +7718,24 @@ public class Parser {
return StringUtils.quoteIdentifier(s);
}
/**
* Add double quotes around an identifier if required and appends it to the
* specified string builder.
*
* @param builder string builder to append to
* @param s the identifier
* @return the specified builder
*/
public static StringBuilder quoteIdentifier(StringBuilder builder, String s) {
if (s == null) {
return builder.append("\"\"");
}
if (ParserUtil.isSimpleIdentifier(s)) {
return builder.append(s);
}
return StringUtils.quoteIdentifier(builder, s);
}
public void setLiteralsChecked(boolean literalsChecked) {
this.literalsChecked = literalsChecked;
}
......
......@@ -392,7 +392,7 @@ public class ScriptCommand extends ScriptBase {
buff.append(table.getSQL()).append('(');
for (Column col : columns) {
buff.appendExceptFirst(", ");
buff.append(Parser.quoteIdentifier(col.getName()));
Parser.quoteIdentifier(buff.builder(), col.getName());
}
buff.append(") VALUES");
if (!simple) {
......
......@@ -1308,7 +1308,8 @@ public class Select extends Query {
// views.
} else {
buff.append("WITH RECURSIVE ")
.append(t.getSchema().getSQL()).append('.').append(Parser.quoteIdentifier(t.getName()))
.append(t.getSchema().getSQL()).append('.');
Parser.quoteIdentifier(buff.builder(), t.getName())
.append('(');
buff.resetCount();
for (Column c : t.getColumns()) {
......
......@@ -568,7 +568,7 @@ public class ConstraintReferential extends Constraint {
buff.resetCount();
for (IndexColumn c : columns) {
buff.appendExceptFirst(" , ");
buff.append(Parser.quoteIdentifier(c.column.getName())).append("=?");
Parser.quoteIdentifier(buff.builder(), c.column.getName()).append("=?");
}
}
......@@ -577,7 +577,7 @@ public class ConstraintReferential extends Constraint {
buff.resetCount();
for (IndexColumn c : columns) {
buff.appendExceptFirst(" AND ");
buff.append(Parser.quoteIdentifier(c.column.getName())).append("=?");
Parser.quoteIdentifier(buff.builder(), c.column.getName()).append("=?");
}
}
......
......@@ -58,7 +58,7 @@ public class ConstraintUnique extends Constraint {
buff.append(' ').append(getConstraintType().getSqlName()).append('(');
for (IndexColumn c : columns) {
buff.appendExceptFirst(", ");
buff.append(Parser.quoteIdentifier(c.column.getName()));
Parser.quoteIdentifier(buff.builder(), c.column.getName());
}
buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) {
......
......@@ -226,8 +226,8 @@ public class FunctionAlias extends SchemaObjectBase {
buff.append(" AS ");
StringUtils.quoteStringSQL(buff, source);
} else {
buff.append(" FOR ").append(Parser.quoteIdentifier(
className + "." + methodName));
buff.append(" FOR ");
Parser.quoteIdentifier(buff, className + "." + methodName);
}
return buff.toString();
}
......
......@@ -79,7 +79,8 @@ public class Alias extends Expression {
@Override
public StringBuilder getSQL(StringBuilder builder) {
return expr.getSQL(builder).append(" AS ").append(Parser.quoteIdentifier(alias));
expr.getSQL(builder).append(" AS ");
return Parser.quoteIdentifier(builder, alias);
}
@Override
......
......@@ -58,17 +58,27 @@ public class ExpressionColumn extends Expression {
public StringBuilder getSQL(StringBuilder builder) {
boolean quote = database.getSettings().databaseToUpper;
if (schemaName != null) {
String s = quote ? Parser.quoteIdentifier(schemaName) : schemaName;
builder.append(s).append('.');
if (quote) {
Parser.quoteIdentifier(builder, schemaName);
} else {
builder.append(schemaName);
}
builder.append('.');
}
if (tableAlias != null) {
String a = quote ? Parser.quoteIdentifier(tableAlias) : tableAlias;
builder.append(a).append('.');
if (quote) {
Parser.quoteIdentifier(builder, tableAlias);
} else {
builder.append(tableAlias);
}
builder.append('.');
}
if (column != null) {
builder.append(column.getSQL());
} else if (quote) {
Parser.quoteIdentifier(builder, columnName);
} else {
builder.append(quote ? Parser.quoteIdentifier(columnName) : columnName);
builder.append(columnName);
}
return builder;
}
......
......@@ -92,11 +92,9 @@ public class JavaFunction extends Expression implements FunctionCall {
// TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled
if (functionAlias.getDatabase().getSettings().functionsInSchema ||
!functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) {
builder.append(
Parser.quoteIdentifier(functionAlias.getSchema().getName()))
.append('.');
Parser.quoteIdentifier(builder, functionAlias.getSchema().getName()).append('.');
}
builder.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
Parser.quoteIdentifier(builder, functionAlias.getName()).append('(');
for (int i = 0; i < args.length; i++) {
if (i > 0) {
builder.append(", ");
......
......@@ -42,7 +42,8 @@ public class Variable extends Expression {
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append('@').append(Parser.quoteIdentifier(name));
builder.append('@');
return Parser.quoteIdentifier(builder, name);
}
@Override
......
......@@ -118,7 +118,7 @@ public class Wildcard extends Expression {
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (table != null) {
builder.append(StringUtils.quoteIdentifier(table)).append('.');
StringUtils.quoteIdentifier(builder, table).append('.');
}
builder.append('*');
if (exceptColumns != null) {
......
......@@ -68,7 +68,7 @@ public class JavaAggregate extends AbstractAggregate {
@Override
public StringBuilder getSQL(StringBuilder builder) {
builder.append(Parser.quoteIdentifier(userAggregate.getName())).append('(');
Parser.quoteIdentifier(builder, userAggregate.getName()).append('(');
for (int i = 0; i < args.length; i++) {
if (i > 0) {
builder.append(", ");
......
......@@ -772,13 +772,13 @@ public class FullText {
if(!multiThread) {
buff.append(", ROLLBACK");
}
buff.append(" ON ").
append(StringUtils.quoteIdentifier(schema)).
append('.').
append(StringUtils.quoteIdentifier(table)).
buff.append(" ON ");
StringUtils.quoteIdentifier(buff, schema).
append('.');
StringUtils.quoteIdentifier(buff, table).
append(" FOR EACH ROW CALL \"").
append(FullText.FullTextTrigger.class.getName()).
append('\"');
append('"');
stat.execute(buff.toString());
}
}
......@@ -1145,7 +1145,7 @@ public class FullText {
StatementBuilder buff = new StatementBuilder();
for (int columnIndex : index.keys) {
buff.appendExceptFirst(" AND ");
buff.append(StringUtils.quoteIdentifier(index.columns[columnIndex]));
StringUtils.quoteIdentifier(buff.builder(), index.columns[columnIndex]);
Object o = row[columnIndex];
if (o == null) {
buff.append(" IS NULL");
......
......@@ -281,19 +281,19 @@ public class FullTextLucene extends FullText {
StringUtils.quoteIdentifier(TRIGGER_PREFIX + table);
stat.execute("DROP TRIGGER IF EXISTS " + trigger);
if (create) {
StringBuilder buff = new StringBuilder(
StringBuilder builder = new StringBuilder(
"CREATE TRIGGER IF NOT EXISTS ");
// the trigger is also called on rollback because transaction
// rollback will not undo the changes in the Lucene index
buff.append(trigger).
append(" AFTER INSERT, UPDATE, DELETE, ROLLBACK ON ").
append(StringUtils.quoteIdentifier(schema)).
append('.').
append(StringUtils.quoteIdentifier(table)).
builder.append(trigger).
append(" AFTER INSERT, UPDATE, DELETE, ROLLBACK ON ");
StringUtils.quoteIdentifier(builder, schema).
append('.');
StringUtils.quoteIdentifier(builder, table).
append(" FOR EACH ROW CALL \"").
append(FullTextLucene.FullTextTrigger.class.getName()).
append('\"');
stat.execute(buff.toString());
stat.execute(builder.toString());
}
}
......@@ -679,22 +679,25 @@ public class FullTextLucene extends FullText {
}
private String getQuery(Object[] row) throws SQLException {
StatementBuilder buff = new StatementBuilder();
StringBuilder builder = new StringBuilder();
if (schema != null) {
buff.append(StringUtils.quoteIdentifier(schema)).append('.');
StringUtils.quoteIdentifier(builder, schema).append('.');
}
buff.append(StringUtils.quoteIdentifier(table)).append(" WHERE ");
for (int columnIndex : keys) {
buff.appendExceptFirst(" AND ");
buff.append(StringUtils.quoteIdentifier(columns[columnIndex]));
StringUtils.quoteIdentifier(builder, table).append(" WHERE ");
for (int i = 0, length = keys.length; i < length; i++) {
if (i > 0) {
builder.append(" AND ");
}
int columnIndex = keys[i];
StringUtils.quoteIdentifier(builder, columns[columnIndex]);
Object o = row[columnIndex];
if (o == null) {
buff.append(" IS NULL");
builder.append(" IS NULL");
} else {
buff.append('=').append(FullText.quoteSQL(o, columnTypes[columnIndex]));
builder.append('=').append(FullText.quoteSQL(o, columnTypes[columnIndex]));
}
}
return buff.toString();
return builder.toString();
}
}
......
......@@ -161,7 +161,7 @@ public class UpdatableRow {
for (int i = 0; i < columnCount; i++) {
buff.appendExceptFirst(",");
String col = result.getColumnName(i);
buff.append(StringUtils.quoteIdentifier(col));
StringUtils.quoteIdentifier(buff.builder(), col);
if (set) {
buff.append("=? ");
}
......@@ -173,7 +173,7 @@ public class UpdatableRow {
buff.resetCount();
for (String k : key) {
buff.appendExceptFirst(" AND ");
buff.append(StringUtils.quoteIdentifier(k)).append("=?");
StringUtils.quoteIdentifier(buff.builder(), k).append("=?");
}
}
......@@ -204,11 +204,11 @@ public class UpdatableRow {
// return rs.getInt(1) == 0;
// }
private void appendTableName(StatementBuilder buff) {
private void appendTableName(StringBuilder builder) {
if (schemaName != null && schemaName.length() > 0) {
buff.append(StringUtils.quoteIdentifier(schemaName)).append('.');
StringUtils.quoteIdentifier(builder, schemaName).append('.');
}
buff.append(StringUtils.quoteIdentifier(tableName));
StringUtils.quoteIdentifier(builder, tableName);
}
/**
......@@ -221,7 +221,7 @@ public class UpdatableRow {
StatementBuilder buff = new StatementBuilder("SELECT ");
appendColumnList(buff, false);
buff.append(" FROM ");
appendTableName(buff);
appendTableName(buff.builder());
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
setKey(prep, 1, row);
......@@ -245,7 +245,7 @@ public class UpdatableRow {
*/
public void deleteRow(Value[] current) throws SQLException {
StatementBuilder buff = new StatementBuilder("DELETE FROM ");
appendTableName(buff);
appendTableName(buff.builder());
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
setKey(prep, 1, current);
......@@ -265,7 +265,7 @@ public class UpdatableRow {
*/
public void updateRow(Value[] current, Value[] updateRow) throws SQLException {
StatementBuilder buff = new StatementBuilder("UPDATE ");
appendTableName(buff);
appendTableName(buff.builder());
buff.append(" SET ");
appendColumnList(buff, true);
// TODO updatable result set: we could add all current values to the
......@@ -297,7 +297,7 @@ public class UpdatableRow {
*/
public void insertRow(Value[] row) throws SQLException {
StatementBuilder buff = new StatementBuilder("INSERT INTO ");
appendTableName(buff);
appendTableName(buff.builder());
buff.append('(');
appendColumnList(buff, false);
buff.append(")VALUES(");
......
......@@ -345,7 +345,8 @@ public class TriggerObject extends SchemaObjectBase {
buff.append(" QUEUE ").append(queueSize);
}
if (triggerClassName != null) {
buff.append(" CALL ").append(Parser.quoteIdentifier(triggerClassName));
buff.append(" CALL ");
Parser.quoteIdentifier(buff, triggerClassName);
} else {
buff.append(" AS ");
StringUtils.quoteStringSQL(buff, triggerSource);
......
......@@ -496,7 +496,7 @@ public class Column {
private String getCreateSQL(boolean includeName) {
StringBuilder buff = new StringBuilder();
if (includeName && name != null) {
buff.append(Parser.quoteIdentifier(name)).append(' ');
Parser.quoteIdentifier(buff, name).append(' ');
}
if (originalSQL != null) {
buff.append(originalSQL);
......
......@@ -49,8 +49,8 @@ public class LinkSchema {
try {
c2 = JdbcUtils.getConnection(driver, url, user, password);
stat = conn.createStatement();
stat.execute("CREATE SCHEMA IF NOT EXISTS " +
StringUtils.quoteIdentifier(targetSchema));
stat.execute(StringUtils.quoteIdentifier(new StringBuilder("CREATE SCHEMA IF NOT EXISTS "), targetSchema)
.toString());
//Workaround for PostgreSQL to avoid index names
if (url.startsWith("jdbc:postgresql:")) {
rs = c2.getMetaData().getTables(null, sourceSchema, null,
......@@ -61,16 +61,16 @@ public class LinkSchema {
while (rs.next()) {
String table = rs.getString("TABLE_NAME");
StringBuilder buff = new StringBuilder();
buff.append("DROP TABLE IF EXISTS ").
append(StringUtils.quoteIdentifier(targetSchema)).
append('.').
append(StringUtils.quoteIdentifier(table));
buff.append("DROP TABLE IF EXISTS ");
StringUtils.quoteIdentifier(buff, targetSchema).
append('.');
StringUtils.quoteIdentifier(buff, table);
stat.execute(buff.toString());
buff = new StringBuilder();
buff.append("CREATE LINKED TABLE ").
append(StringUtils.quoteIdentifier(targetSchema)).
append('.').
append(StringUtils.quoteIdentifier(table)).
buff.setLength(0);
buff.append("CREATE LINKED TABLE ");
StringUtils.quoteIdentifier(buff, targetSchema).
append('.');
StringUtils.quoteIdentifier(buff, table).
append('(');
StringUtils.quoteStringSQL(buff, driver).append(", ");
StringUtils.quoteStringSQL(buff, url).append(", ");
......
......@@ -1918,7 +1918,7 @@ public class MetaTable extends Table {
"SET SCHEMA_SEARCH_PATH ");
for (String p : path) {
buff.appendExceptFirst(", ");
buff.append(StringUtils.quoteIdentifier(p));
StringUtils.quoteIdentifier(buff.builder(), p);
}
add(rows,
// KEY
......@@ -1933,7 +1933,7 @@ public class MetaTable extends Table {
// KEY
"SCHEMA",
// SQL
"SET SCHEMA " + StringUtils.quoteIdentifier(schema)
StringUtils.quoteIdentifier(new StringBuilder("SET SCHEMA "), schema).toString()
);
}
break;
......
......@@ -125,7 +125,7 @@ public abstract class TableBase extends Table {
}
if (d == null || !tableEngine.endsWith(d)) {
buff.append("\nENGINE ");
buff.append(StringUtils.quoteIdentifier(tableEngine));
StringUtils.quoteIdentifier(buff.builder(), tableEngine);
}
}
if (!tableEngineParams.isEmpty()) {
......@@ -133,7 +133,7 @@ public abstract class TableBase extends Table {
buff.resetCount();
for (String parameter : tableEngineParams) {
buff.appendExceptFirst(", ");
buff.append(StringUtils.quoteIdentifier(parameter));
StringUtils.quoteIdentifier(buff.builder(), parameter);
}
}
if (!isPersistIndexes() && !isPersistData()) {
......
......@@ -791,7 +791,8 @@ public class TableFilter implements ColumnResolver {
return builder;
}
if (table.isView() && ((TableView) table).isRecursive()) {
builder.append(table.getSchema().getSQL()).append('.').append(Parser.quoteIdentifier(table.getName()));
builder.append(table.getSchema().getSQL()).append('.');
Parser.quoteIdentifier(builder, table.getName());
} else {
builder.append(table.getSQL());
}
......@@ -799,7 +800,8 @@ public class TableFilter implements ColumnResolver {
throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, table.getName(), "not compiled");
}
if (alias != null) {
builder.append(' ').append(Parser.quoteIdentifier(alias));
builder.append(' ');
Parser.quoteIdentifier(builder, alias);
}
if (indexHints != null) {
builder.append(" USE INDEX (");
......@@ -810,7 +812,7 @@ public class TableFilter implements ColumnResolver {
} else {
first = false;
}
builder.append(Parser.quoteIdentifier(index));
Parser.quoteIdentifier(builder, index);
}
builder.append(")");
}
......
......@@ -156,12 +156,13 @@ public class MultiDimension implements Comparator<long[]> {
public String generatePreparedQuery(String table, String scalarColumn,
String[] columns) {
StringBuilder buff = new StringBuilder("SELECT D.* FROM ");
buff.append(StringUtils.quoteIdentifier(table)).
append(" D, TABLE(_FROM_ BIGINT=?, _TO_ BIGINT=?) WHERE ").
append(StringUtils.quoteIdentifier(scalarColumn)).
StringUtils.quoteIdentifier(buff, table).
append(" D, TABLE(_FROM_ BIGINT=?, _TO_ BIGINT=?) WHERE ");
StringUtils.quoteIdentifier(buff, scalarColumn).
append(" BETWEEN _FROM_ AND _TO_");
for (String col : columns) {
buff.append(" AND ").append(StringUtils.quoteIdentifier(col)).
buff.append(" AND ");
StringUtils.quoteIdentifier(buff, col).
append("+1 BETWEEN ?+1 AND ?+1");
}
return buff.toString();
......
......@@ -759,17 +759,28 @@ public class StringUtils {
* @return the double quoted text
*/
public static String quoteIdentifier(String s) {
int length = s.length();
StringBuilder buff = new StringBuilder(length + 2);
buff.append('\"');
for (int i = 0; i < length; i++) {
return quoteIdentifier(new StringBuilder(s.length() + 2), s).toString();
}
/**
* Enclose a string with double quotes and append it to the specified
* string builder. A double quote inside the string is escaped using a
* double quote.
*
* @param builder string builder to append to
* @param s the text
* @return the specified builder
*/
public static StringBuilder quoteIdentifier(StringBuilder builder, String s) {
builder.append('"');
for (int i = 0, length = s.length(); i < length; i++) {
char c = s.charAt(i);
if (c == '"') {
buff.append(c);
builder.append(c);
}
buff.append(c);
builder.append(c);
}
return buff.append('\"').toString();
return builder.append('"');
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论