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

Add quoteIdentifier() variants with StringBuilder argument

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