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

Use DbObject.getSQL(StringBuilder) where possible

上级 4f34f37d
...@@ -208,8 +208,9 @@ public class AlterTableAddConstraint extends SchemaCommand { ...@@ -208,8 +208,9 @@ public class AlterTableAddConstraint extends SchemaCommand {
} }
session.getUser().checkRight(refTable, Right.ALL); session.getUser().checkRight(refTable, Right.ALL);
if (!refTable.canReference()) { if (!refTable.canReference()) {
throw DbException.getUnsupportedException("Reference " + StringBuilder builder = new StringBuilder("Reference ");
refTable.getSQL()); refTable.getSQL(builder);
throw DbException.getUnsupportedException(builder.toString());
} }
boolean isOwner = false; boolean isOwner = false;
IndexColumn.mapColumns(indexColumns, table); IndexColumn.mapColumns(indexColumns, table);
......
...@@ -296,7 +296,9 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -296,7 +296,9 @@ public class AlterTableAlterColumn extends CommandWithColumns {
// (because the column to drop is referenced or so) // (because the column to drop is referenced or so)
checkViews(table, newTable); checkViews(table, newTable);
} catch (DbException e) { } catch (DbException e) {
execute("DROP TABLE " + newTable.getSQL(), true); StringBuilder builder = new StringBuilder("DROP TABLE ");
newTable.getSQL(builder);
execute(builder.toString(), true);
throw e; throw e;
} }
String tableName = table.getName(); String tableName = table.getName();
...@@ -304,7 +306,9 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -304,7 +306,9 @@ public class AlterTableAlterColumn extends CommandWithColumns {
for (TableView view : dependentViews) { for (TableView view : dependentViews) {
table.removeDependentView(view); table.removeDependentView(view);
} }
execute("DROP TABLE " + table.getSQL() + " IGNORE", true); StringBuilder builder = new StringBuilder("DROP TABLE ");
table.getSQL(builder).append(" IGNORE");
execute(builder.toString(), true);
db.renameSchemaObject(session, newTable, tableName); db.renameSchemaObject(session, newTable, tableName);
for (DbObject child : newTable.getChildren()) { for (DbObject child : newTable.getChildren()) {
if (child instanceof Sequence) { if (child instanceof Sequence) {
...@@ -475,7 +479,8 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -475,7 +479,8 @@ public class AlterTableAlterColumn extends CommandWithColumns {
} }
} }
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("INSERT INTO ").append(newTable.getSQL()); buff.append("INSERT INTO ");
newTable.getSQL(buff);
buff.append(" SELECT "); buff.append(" SELECT ");
if (columnList.length() == 0) { if (columnList.length() == 0) {
// special case: insert into test select * from // special case: insert into test select * from
...@@ -483,13 +488,16 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -483,13 +488,16 @@ public class AlterTableAlterColumn extends CommandWithColumns {
} else { } else {
buff.append(columnList); buff.append(columnList);
} }
buff.append(" FROM ").append(table.getSQL()); buff.append(" FROM ");
table.getSQL(buff);
try { try {
execute(buff.toString(), true); execute(buff.toString(), true);
} catch (Throwable t) { } catch (Throwable t) {
// data was not inserted due to data conversion error or some // data was not inserted due to data conversion error or some
// unexpected reason // unexpected reason
execute("DROP TABLE " + newTable.getSQL(), true); StringBuilder builder = new StringBuilder("DROP TABLE ");
newTable.getSQL(builder);
execute(builder.toString(), true);
throw t; throw t;
} }
for (String sql : children) { for (String sql : children) {
...@@ -579,9 +587,10 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -579,9 +587,10 @@ public class AlterTableAlterColumn extends CommandWithColumns {
} }
private void checkNoNullValues(Table table) { private void checkNoNullValues(Table table) {
String sql = "SELECT COUNT(*) FROM " + StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM ");
table.getSQL() + " WHERE " + table.getSQL(builder).append(" WHERE ");
oldColumn.getSQL() + " IS NULL"; builder.append(oldColumn.getSQL()).append(" IS NULL");
String sql = builder.toString();
Prepared command = session.prepare(sql); Prepared command = session.prepare(sql);
ResultInterface result = command.query(0); ResultInterface result = command.query(0);
result.next(); result.next();
......
...@@ -113,7 +113,8 @@ public class Analyze extends DefineCommand { ...@@ -113,7 +113,8 @@ public class Analyze extends DefineCommand {
buff.append("SELECTIVITY(").append(col.getSQL()).append(')'); buff.append("SELECTIVITY(").append(col.getSQL()).append(')');
} }
} }
buff.append(" FROM ").append(table.getSQL()); buff.append(" FROM ");
table.getSQL(buff.builder());
if (sample > 0) { if (sample > 0) {
buff.append(" FETCH FIRST ROW ONLY SAMPLE_SIZE ? "); buff.append(" FETCH FIRST ROW ONLY SAMPLE_SIZE ? ");
} }
......
...@@ -64,9 +64,9 @@ public class CreateDomain extends DefineCommand { ...@@ -64,9 +64,9 @@ public class CreateDomain extends DefineCommand {
} }
Table table = session.getDatabase().getFirstUserTable(); Table table = session.getDatabase().getFirstUserTable();
if (table != null) { if (table != null) {
throw DbException.get( StringBuilder builder = new StringBuilder(typeName).append(" (");
ErrorCode.DOMAIN_ALREADY_EXISTS_1, table.getSQL(builder).append(')');
typeName + " (" + table.getSQL() + ")"); throw DbException.get(ErrorCode.DOMAIN_ALREADY_EXISTS_1, builder.toString());
} }
} }
int id = getObjectId(); int id = getObjectId();
......
...@@ -270,7 +270,7 @@ public class Insert extends CommandWithValues implements ResultTarget { ...@@ -270,7 +270,7 @@ public class Insert extends CommandWithValues implements ResultTarget {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("INSERT INTO "); StatementBuilder buff = new StatementBuilder("INSERT INTO ");
buff.append(table.getSQL()).append('('); table.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
...@@ -405,7 +405,7 @@ public class Insert extends CommandWithValues implements ResultTarget { ...@@ -405,7 +405,7 @@ public class Insert extends CommandWithValues implements ResultTarget {
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(table.getSQL()).append(" SET "); table.getSQL(buff.builder()).append(" SET ");
for (Column column : duplicateKeyAssignmentMap.keySet()) { for (Column column : duplicateKeyAssignmentMap.keySet()) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
Expression ex = duplicateKeyAssignmentMap.get(column); Expression ex = duplicateKeyAssignmentMap.get(column);
......
...@@ -207,7 +207,7 @@ public class Merge extends CommandWithValues { ...@@ -207,7 +207,7 @@ public class Merge extends CommandWithValues {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("MERGE INTO "); StatementBuilder buff = new StatementBuilder("MERGE INTO ");
buff.append(targetTable.getSQL()).append('('); targetTable.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
...@@ -276,7 +276,7 @@ public class Merge extends CommandWithValues { ...@@ -276,7 +276,7 @@ public class Merge extends CommandWithValues {
keys = idx.getColumns(); keys = idx.getColumns();
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(targetTable.getSQL()).append(" SET "); targetTable.getSQL(buff.builder()).append(" SET ");
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append("=?"); buff.append(c.getSQL()).append("=?");
......
...@@ -390,7 +390,7 @@ public class MergeUsing extends Prepared { ...@@ -390,7 +390,7 @@ public class MergeUsing extends Prepared {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StringBuilder builder = new StringBuilder("MERGE INTO "); StringBuilder builder = new StringBuilder("MERGE INTO ");
builder.append(targetTable.getSQL()).append('\n').append("USING ").append(query.getPlanSQL()); targetTable.getSQL(builder).append('\n').append("USING ").append(query.getPlanSQL());
// TODO add aliases and WHEN clauses to make plan SQL more like original SQL // TODO add aliases and WHEN clauses to make plan SQL more like original SQL
return builder.toString(); return builder.toString();
} }
......
...@@ -196,7 +196,7 @@ public class Replace extends CommandWithValues { ...@@ -196,7 +196,7 @@ public class Replace extends CommandWithValues {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("REPLACE INTO "); StatementBuilder buff = new StatementBuilder("REPLACE INTO ");
buff.append(table.getSQL()).append('('); table.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
...@@ -270,7 +270,7 @@ public class Replace extends CommandWithValues { ...@@ -270,7 +270,7 @@ public class Replace extends CommandWithValues {
} }
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(table.getSQL()).append(" SET "); table.getSQL(buff.builder()).append(" SET ");
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append("=?"); buff.append(c.getSQL()).append("=?");
......
...@@ -291,10 +291,10 @@ public class ScriptCommand extends ScriptBase { ...@@ -291,10 +291,10 @@ public class ScriptCommand extends ScriptBase {
} }
if (TableType.TABLE == tableType) { if (TableType.TABLE == tableType) {
if (table.canGetRowCount()) { if (table.canGetRowCount()) {
String rowcount = "-- " + StringBuilder builder = new StringBuilder("-- ").append(table.getRowCountApproximation())
table.getRowCountApproximation() + .append(" +/- SELECT COUNT(*) FROM ");
" +/- SELECT COUNT(*) FROM " + table.getSQL(); table.getSQL(builder);
add(rowcount, false); add(builder.toString(), false);
} }
if (data) { if (data) {
count = generateInsertValues(count, table); count = generateInsertValues(count, table);
...@@ -389,7 +389,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -389,7 +389,7 @@ public class ScriptCommand extends ScriptBase {
Cursor cursor = index.find(session, null, null); Cursor cursor = index.find(session, null, null);
Column[] columns = table.getColumns(); Column[] columns = table.getColumns();
StatementBuilder buff = new StatementBuilder("INSERT INTO "); StatementBuilder buff = new StatementBuilder("INSERT INTO ");
buff.append(table.getSQL()).append('('); table.getSQL(buff.builder()).append('(');
for (Column col : columns) { for (Column col : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
Parser.quoteIdentifier(buff.builder(), col.getName()); Parser.quoteIdentifier(buff.builder(), col.getName());
......
...@@ -1474,8 +1474,8 @@ public class Select extends Query { ...@@ -1474,8 +1474,8 @@ public class Select extends Query {
// since using a with statement will re-create the common table expression // since using a with statement will re-create the common table expression
// views. // views.
} else { } else {
buff.append("WITH RECURSIVE ") buff.append("WITH RECURSIVE ");
.append(t.getSchema().getSQL()).append('.'); t.getSchema().getSQL(buff.builder()).append('.');
Parser.quoteIdentifier(buff.builder(), t.getName()) Parser.quoteIdentifier(buff.builder(), t.getName())
.append('('); .append('(');
buff.resetCount(); buff.resetCount();
...@@ -1483,7 +1483,8 @@ public class Select extends Query { ...@@ -1483,7 +1483,8 @@ public class Select extends Query {
buff.appendExceptFirst(","); buff.appendExceptFirst(",");
buff.append(c.getName()); buff.append(c.getName());
} }
buff.append(") AS ").append(t.getSQL()).append('\n'); buff.append(") AS ");
t.getSQL(buff.builder()).append('\n');
} }
} }
} }
......
...@@ -50,7 +50,7 @@ public class ConstraintCheck extends Constraint { ...@@ -50,7 +50,7 @@ public class ConstraintCheck extends Constraint {
@Override @Override
public String getCreateSQLForCopy(Table forTable, String quotedName) { public String getCreateSQLForCopy(Table forTable, String quotedName) {
StringBuilder buff = new StringBuilder("ALTER TABLE "); StringBuilder buff = new StringBuilder("ALTER TABLE ");
buff.append(forTable.getSQL()).append(" ADD CONSTRAINT "); forTable.getSQL(buff).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) { if (forTable.isHidden()) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
...@@ -143,9 +143,8 @@ public class ConstraintCheck extends Constraint { ...@@ -143,9 +143,8 @@ public class ConstraintCheck extends Constraint {
// don't check at startup // don't check at startup
return; return;
} }
StringBuilder builder = new StringBuilder().append("SELECT 1 FROM ") StringBuilder builder = new StringBuilder().append("SELECT 1 FROM ");
.append(filter.getTable().getSQL()) filter.getTable().getSQL(builder).append(" WHERE NOT(");
.append(" WHERE NOT(");
expr.getSQL(builder).append(')'); expr.getSQL(builder).append(')');
String sql = builder.toString(); String sql = builder.toString();
ResultInterface r = session.prepare(sql).query(1); ResultInterface r = session.prepare(sql).query(1);
......
...@@ -80,8 +80,7 @@ public class ConstraintReferential extends Constraint { ...@@ -80,8 +80,7 @@ public class ConstraintReferential extends Constraint {
public String getCreateSQLForCopy(Table forTable, Table forRefTable, public String getCreateSQLForCopy(Table forTable, Table forRefTable,
String quotedName, boolean internalIndex) { String quotedName, boolean internalIndex) {
StatementBuilder buff = new StatementBuilder("ALTER TABLE "); StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
String mainTable = forTable.getSQL(); forTable.getSQL(buff.builder()).append(" ADD CONSTRAINT ");
buff.append(mainTable).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) { if (forTable.isHidden()) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
...@@ -99,17 +98,17 @@ public class ConstraintReferential extends Constraint { ...@@ -99,17 +98,17 @@ public class ConstraintReferential extends Constraint {
} }
buff.append(')'); buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) { if (internalIndex && indexOwner && forTable == this.table) {
buff.append(" INDEX ").append(index.getSQL()); buff.append(" INDEX ");
index.getSQL(buff.builder());
} }
buff.append(" REFERENCES "); buff.append(" REFERENCES ");
String quotedRefTable;
if (this.table == this.refTable) { if (this.table == this.refTable) {
// self-referencing constraints: need to use new table // self-referencing constraints: need to use new table
quotedRefTable = forTable.getSQL(); forTable.getSQL(buff.builder());
} else { } else {
quotedRefTable = forRefTable.getSQL(); forRefTable.getSQL(buff.builder());
} }
buff.append(quotedRefTable).append('('); buff.append('(');
buff.resetCount(); buff.resetCount();
for (IndexColumn r : refCols) { for (IndexColumn r : refCols) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
...@@ -117,7 +116,8 @@ public class ConstraintReferential extends Constraint { ...@@ -117,7 +116,8 @@ public class ConstraintReferential extends Constraint {
} }
buff.append(')'); buff.append(')');
if (internalIndex && refIndexOwner && forTable == this.table) { if (internalIndex && refIndexOwner && forTable == this.table) {
buff.append(" INDEX ").append(refIndex.getSQL()); buff.append(" INDEX ");
refIndex.getSQL(buff.builder());
} }
if (deleteAction != ConstraintActionType.RESTRICT) { if (deleteAction != ConstraintActionType.RESTRICT) {
buff.append(" ON DELETE ").append(deleteAction.getSqlName()); buff.append(" ON DELETE ").append(deleteAction.getSqlName());
...@@ -139,12 +139,14 @@ public class ConstraintReferential extends Constraint { ...@@ -139,12 +139,14 @@ public class ConstraintReferential extends Constraint {
*/ */
private String getShortDescription(Index searchIndex, SearchRow check) { private String getShortDescription(Index searchIndex, SearchRow check) {
StatementBuilder buff = new StatementBuilder(getName()); StatementBuilder buff = new StatementBuilder(getName());
buff.append(": ").append(table.getSQL()).append(" FOREIGN KEY("); buff.append(": ");
table.getSQL(buff.builder()).append(" FOREIGN KEY(");
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
} }
buff.append(") REFERENCES ").append(refTable.getSQL()).append('('); buff.append(") REFERENCES ");
refTable.getSQL(buff.builder()).append('(');
buff.resetCount(); buff.resetCount();
for (IndexColumn r : refColumns) { for (IndexColumn r : refColumns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
...@@ -491,7 +493,8 @@ public class ConstraintReferential extends Constraint { ...@@ -491,7 +493,8 @@ public class ConstraintReferential extends Constraint {
} }
StatementBuilder buff = new StatementBuilder(); StatementBuilder buff = new StatementBuilder();
if (deleteAction == ConstraintActionType.CASCADE) { if (deleteAction == ConstraintActionType.CASCADE) {
buff.append("DELETE FROM ").append(table.getSQL()); buff.append("DELETE FROM ");
table.getSQL(buff.builder());
} else { } else {
appendUpdate(buff); appendUpdate(buff);
} }
...@@ -567,7 +570,8 @@ public class ConstraintReferential extends Constraint { ...@@ -567,7 +570,8 @@ public class ConstraintReferential extends Constraint {
} }
private void appendUpdate(StatementBuilder buff) { private void appendUpdate(StatementBuilder buff) {
buff.append("UPDATE ").append(table.getSQL()).append(" SET "); buff.append("UPDATE ");
table.getSQL(buff.builder()).append(" SET ");
buff.resetCount(); buff.resetCount();
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
...@@ -622,7 +626,8 @@ public class ConstraintReferential extends Constraint { ...@@ -622,7 +626,8 @@ public class ConstraintReferential extends Constraint {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
} }
buff.append(" FROM ").append(table.getSQL()).append(" WHERE "); buff.append(" FROM ");
table.getSQL(buff.builder()).append(" WHERE ");
buff.resetCount(); buff.resetCount();
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
...@@ -634,8 +639,8 @@ public class ConstraintReferential extends Constraint { ...@@ -634,8 +639,8 @@ public class ConstraintReferential extends Constraint {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); buff.append(c.getSQL());
} }
buff.append(") C WHERE NOT EXISTS(SELECT 1 FROM "). buff.append(") C WHERE NOT EXISTS(SELECT 1 FROM ");
append(refTable.getSQL()).append(" P WHERE "); refTable.getSQL(buff.builder()).append(" P WHERE ");
buff.resetCount(); buff.resetCount();
int i = 0; int i = 0;
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
......
...@@ -46,7 +46,7 @@ public class ConstraintUnique extends Constraint { ...@@ -46,7 +46,7 @@ public class ConstraintUnique extends Constraint {
private String getCreateSQLForCopy(Table forTable, String quotedName, private String getCreateSQLForCopy(Table forTable, String quotedName,
boolean internalIndex) { boolean internalIndex) {
StatementBuilder buff = new StatementBuilder("ALTER TABLE "); StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
buff.append(forTable.getSQL()).append(" ADD CONSTRAINT "); forTable.getSQL(buff.builder()).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) { if (forTable.isHidden()) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
...@@ -62,7 +62,8 @@ public class ConstraintUnique extends Constraint { ...@@ -62,7 +62,8 @@ public class ConstraintUnique extends Constraint {
} }
buff.append(')'); buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) { if (internalIndex && indexOwner && forTable == this.table) {
buff.append(" INDEX ").append(index.getSQL()); buff.append(" INDEX ");
index.getSQL(buff.builder());
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -102,7 +102,9 @@ public class Comment extends DbObjectBase { ...@@ -102,7 +102,9 @@ public class Comment extends DbObjectBase {
* @return the key name * @return the key name
*/ */
static String getKey(DbObject obj) { static String getKey(DbObject obj) {
return getTypeName(obj.getType()) + " " + obj.getSQL(); StringBuilder builder = new StringBuilder(getTypeName(obj.getType())).append(' ');
obj.getSQL(builder);
return builder.toString();
} }
/** /**
......
...@@ -28,12 +28,16 @@ public class Domain extends DbObjectBase { ...@@ -28,12 +28,16 @@ public class Domain extends DbObjectBase {
@Override @Override
public String getDropSQL() { public String getDropSQL() {
return "DROP DOMAIN IF EXISTS " + getSQL(); StringBuilder builder = new StringBuilder("DROP DOMAIN IF EXISTS ");
return getSQL(builder).toString();
} }
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
return "CREATE DOMAIN " + getSQL() + " AS " + column.getCreateSQL(); StringBuilder builder = new StringBuilder("CREATE DOMAIN ");
getSQL(builder).append(" AS ");
builder.append(column.getCreateSQL());
return builder.toString();
} }
public Column getColumn() { public Column getColumn() {
......
...@@ -135,18 +135,21 @@ public class Right extends DbObjectBase { ...@@ -135,18 +135,21 @@ public class Right extends DbObjectBase {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("GRANT "); buff.append("GRANT ");
if (grantedRole != null) { if (grantedRole != null) {
buff.append(grantedRole.getSQL()); grantedRole.getSQL(buff);
} else { } else {
buff.append(getRights()); buff.append(getRights());
if (object != null) { if (object != null) {
if (object instanceof Schema) { if (object instanceof Schema) {
buff.append(" ON SCHEMA ").append(object.getSQL()); buff.append(" ON SCHEMA ");
object.getSQL(buff);
} else if (object instanceof Table) { } else if (object instanceof Table) {
buff.append(" ON ").append(object.getSQL()); buff.append(" ON ");
object.getSQL(buff);
} }
} }
} }
buff.append(" TO ").append(grantee.getSQL()); buff.append(" TO ");
grantee.getSQL(buff);
return buff.toString(); return buff.toString();
} }
......
...@@ -45,7 +45,7 @@ public class Role extends RightOwner { ...@@ -45,7 +45,7 @@ public class Role extends RightOwner {
if (ifNotExists) { if (ifNotExists) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
buff.append(getSQL()); getSQL(buff);
return buff.toString(); return buff.toString();
} }
......
...@@ -393,8 +393,10 @@ public class Session extends SessionWithState implements TransactionStore.Rollba ...@@ -393,8 +393,10 @@ public class Session extends SessionWithState implements TransactionStore.Rollba
localTempTables = database.newStringMap(); localTempTables = database.newStringMap();
} }
if (localTempTables.get(table.getName()) != null) { if (localTempTables.get(table.getName()) != null) {
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, StringBuilder builder = new StringBuilder();
table.getSQL()+" AS "+table.getName()); table.getSQL(builder).append(" AS ");
Parser.quoteIdentifier(table.getName());
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, builder.toString());
} }
modificationId++; modificationId++;
localTempTables.put(table.getName(), table); localTempTables.put(table.getName(), table);
......
...@@ -50,7 +50,7 @@ public class Setting extends DbObjectBase { ...@@ -50,7 +50,7 @@ public class Setting extends DbObjectBase {
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
StringBuilder buff = new StringBuilder("SET "); StringBuilder buff = new StringBuilder("SET ");
buff.append(getSQL()).append(' '); getSQL(buff).append(' ');
if (stringValue != null) { if (stringValue != null) {
buff.append(stringValue); buff.append(stringValue);
} else { } else {
......
...@@ -157,7 +157,7 @@ public class User extends RightOwner { ...@@ -157,7 +157,7 @@ public class User extends RightOwner {
*/ */
public String getCreateSQL(boolean password) { public String getCreateSQL(boolean password) {
StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS "); StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS ");
buff.append(getSQL()); getSQL(buff);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment); StringUtils.quoteStringSQL(buff, comment);
......
...@@ -59,13 +59,16 @@ public class UserAggregate extends DbObjectBase { ...@@ -59,13 +59,16 @@ public class UserAggregate extends DbObjectBase {
@Override @Override
public String getDropSQL() { public String getDropSQL() {
return "DROP AGGREGATE IF EXISTS " + getSQL(); StringBuilder builder = new StringBuilder("DROP AGGREGATE IF EXISTS ");
return getSQL(builder).toString();
} }
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
return "CREATE FORCE AGGREGATE " + getSQL() + StringBuilder builder = new StringBuilder("CREATE FORCE AGGREGATE ");
" FOR " + Parser.quoteIdentifier(className); getSQL(builder).append(" FOR ");
Parser.quoteIdentifier(builder, className);
return builder.toString();
} }
@Override @Override
......
...@@ -55,7 +55,7 @@ public class SequenceValue extends Expression { ...@@ -55,7 +55,7 @@ public class SequenceValue extends Expression {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
builder.append("(NEXT VALUE FOR "); builder.append("(NEXT VALUE FOR ");
return builder.append(sequence.getSQL()).append(')'); return sequence.getSQL(builder).append(')');
} }
@Override @Override
......
...@@ -93,12 +93,15 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -93,12 +93,15 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
* @return the exception * @return the exception
*/ */
protected DbException getDuplicateKeyException(String key) { protected DbException getDuplicateKeyException(String key) {
String sql = getName() + " ON " + table.getSQL() + StringBuilder builder = new StringBuilder();
"(" + getColumnListSQL() + ")"; getSQL(builder).append(" ON ");
table.getSQL(builder).append('(');
builder.append(getColumnListSQL());
builder.append(')');
if (key != null) { if (key != null) {
sql += " VALUES " + key; builder.append(" VALUES ").append(key);
} }
DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, sql); DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, builder.toString());
e.setSource(this); e.setSource(this);
return e; return e;
} }
...@@ -426,7 +429,8 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -426,7 +429,8 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
buff.append(quotedName); buff.append(quotedName);
buff.append(" ON ").append(targetTable.getSQL()); buff.append(" ON ");
targetTable.getSQL(buff);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment); StringUtils.quoteStringSQL(buff, comment);
......
...@@ -229,7 +229,7 @@ public class PageBtreeLeaf extends PageBtree { ...@@ -229,7 +229,7 @@ public class PageBtreeLeaf extends PageBtree {
SearchRow delete = getRow(at); SearchRow delete = getRow(at);
if (index.compareRows(row, delete) != 0 || delete.getKey() != row.getKey()) { if (index.compareRows(row, delete) != 0 || delete.getKey() != row.getKey()) {
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1,
index.getSQL() + ": " + row); index.getSQL(new StringBuilder()).append(": ").append(row).toString());
} }
index.getPageStore().logUndo(this, data); index.getPageStore().logUndo(this, data);
if (entryCount == 1) { if (entryCount == 1) {
......
...@@ -139,11 +139,12 @@ public class PageDataIndex extends PageIndex { ...@@ -139,11 +139,12 @@ public class PageDataIndex extends PageIndex {
} }
public DbException getNewDuplicateKeyException() { public DbException getNewDuplicateKeyException() {
String sql = "PRIMARY KEY ON " + table.getSQL(); StringBuilder builder = new StringBuilder("PRIMARY KEY ON ");
table.getSQL(builder);
if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) { if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
sql += "(" + indexColumns[mainIndexColumn].getSQL() + ")"; builder.append('(').append(indexColumns[mainIndexColumn].getSQL()).append(')');
} }
DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, sql); DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, builder.toString());
e.setSource(this); e.setSource(this);
return e; return e;
} }
...@@ -478,7 +479,7 @@ public class PageDataIndex extends PageIndex { ...@@ -478,7 +479,7 @@ public class PageDataIndex extends PageIndex {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
return table.getSQL() + ".tableScan"; return table.getSQL(new StringBuilder()).append(".tableScan").toString();
} }
int getMemoryPerPage() { int getMemoryPerPage() {
......
...@@ -416,7 +416,8 @@ public class PageDataLeaf extends PageData { ...@@ -416,7 +416,8 @@ public class PageDataLeaf extends PageData {
int i = find(key); int i = find(key);
if (keys == null || keys[i] != key) { if (keys == null || keys[i] != key) {
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1,
index.getSQL() + ": " + key + " " + (keys == null ? -1 : keys[i])); index.getSQL(new StringBuilder()).append(": ").append(key).append(' ')
.append(keys == null ? -1 : keys[i]).toString());
} }
index.getPageStore().logUndo(this, data); index.getPageStore().logUndo(this, data);
if (entryCount == 1) { if (entryCount == 1) {
......
...@@ -193,7 +193,7 @@ public class ScanIndex extends BaseIndex { ...@@ -193,7 +193,7 @@ public class ScanIndex extends BaseIndex {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
return table.getSQL() + ".tableScan"; return table.getSQL(new StringBuilder()).append(".tableScan").toString();
} }
} }
...@@ -70,7 +70,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -70,7 +70,7 @@ public class MVPrimaryIndex extends BaseIndex {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
return table.getSQL() + ".tableScan"; return table.getSQL(new StringBuilder()).append(".tableScan").toString();
} }
public void setMainIndexColumn(int mainIndexColumn) { public void setMainIndexColumn(int mainIndexColumn) {
...@@ -116,16 +116,18 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -116,16 +116,18 @@ public class MVPrimaryIndex extends BaseIndex {
try { try {
Value oldValue = map.putIfAbsent(key, ValueArray.get(row.getValueList())); Value oldValue = map.putIfAbsent(key, ValueArray.get(row.getValueList()));
if (oldValue != null) { if (oldValue != null) {
String sql = "PRIMARY KEY ON " + table.getSQL(); StringBuilder builder = new StringBuilder("PRIMARY KEY ON ");
table.getSQL(builder);
if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) { if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
sql += "(" + indexColumns[mainIndexColumn].getSQL() + ")"; builder.append('(').append(indexColumns[mainIndexColumn].getSQL()).append(')');
} }
int errorCode = ErrorCode.CONCURRENT_UPDATE_1; int errorCode = ErrorCode.CONCURRENT_UPDATE_1;
if (map.get(key) != null) { if (map.get(key) != null) {
// committed // committed
errorCode = ErrorCode.DUPLICATE_KEY_1; errorCode = ErrorCode.DUPLICATE_KEY_1;
} }
DbException e = DbException.get(errorCode, sql + " " + oldValue); builder.append(' ').append(oldValue);
DbException e = DbException.get(errorCode, builder.toString());
e.setSource(this); e.setSource(this);
throw e; throw e;
} }
...@@ -154,8 +156,9 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -154,8 +156,9 @@ public class MVPrimaryIndex extends BaseIndex {
try { try {
Value old = map.remove(ValueLong.get(row.getKey())); Value old = map.remove(ValueLong.get(row.getKey()));
if (old == null) { if (old == null) {
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, StringBuilder builder = new StringBuilder();
getSQL() + ": " + row.getKey()); getSQL(builder).append(": ").append(row.getKey());
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, builder.toString());
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
throw mvTable.convertException(e); throw mvTable.convertException(e);
...@@ -194,8 +197,9 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -194,8 +197,9 @@ public class MVPrimaryIndex extends BaseIndex {
try { try {
Value existing = map.put(ValueLong.get(key), ValueArray.get(newRow.getValueList())); Value existing = map.put(ValueLong.get(key), ValueArray.get(newRow.getValueList()));
if (existing == null) { if (existing == null) {
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, StringBuilder builder = new StringBuilder();
getSQL() + ": " + key); getSQL(builder).append(": ").append(key);
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, builder.toString());
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
throw mvTable.convertException(e); throw mvTable.convertException(e);
...@@ -286,7 +290,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -286,7 +290,7 @@ public class MVPrimaryIndex extends BaseIndex {
return getRow(session, key, (ValueArray) v); return getRow(session, key, (ValueArray) v);
} }
private Row getRow(Session session, long key, ValueArray array) { private static Row getRow(Session session, long key, ValueArray array) {
Row row = session.createRow(array.getList(), 0); Row row = session.createRow(array.getList(), 0);
row.setKey(key); row.setKey(key);
return row; return row;
......
...@@ -231,8 +231,9 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -231,8 +231,9 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
try { try {
Value old = map.remove(array); Value old = map.remove(array);
if (old == null) { if (old == null) {
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, StringBuilder builder = new StringBuilder();
getSQL() + ": " + row.getKey()); getSQL(builder).append(": ").append(row.getKey());
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, builder.toString());
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
throw mvTable.convertException(e); throw mvTable.convertException(e);
......
...@@ -183,9 +183,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -183,9 +183,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
try { try {
Value old = map.remove(key); Value old = map.remove(key);
if (old == null) { if (old == null) {
old = map.remove(key); StringBuilder builder = new StringBuilder();
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL(builder).append(": ").append(row.getKey());
getSQL() + ": " + row.getKey()); throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, builder.toString());
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
throw mvTable.convertException(e); throw mvTable.convertException(e);
......
...@@ -38,7 +38,8 @@ public class Constant extends SchemaObjectBase { ...@@ -38,7 +38,8 @@ public class Constant extends SchemaObjectBase {
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
StringBuilder builder = new StringBuilder().append("CREATE CONSTANT ").append(getSQL()).append(" VALUE "); StringBuilder builder = new StringBuilder("CREATE CONSTANT ");
getSQL(builder).append(" VALUE ");
return value.getSQL(builder).toString(); return value.getSQL(builder).toString();
} }
......
...@@ -110,8 +110,10 @@ public class Schema extends DbObjectBase { ...@@ -110,8 +110,10 @@ public class Schema extends DbObjectBase {
if (system) { if (system) {
return null; return null;
} }
return "CREATE SCHEMA IF NOT EXISTS " + StringBuilder builder = new StringBuilder("CREATE SCHEMA IF NOT EXISTS ");
getSQL() + " AUTHORIZATION " + owner.getSQL(); getSQL(builder).append(" AUTHORIZATION ");
owner.getSQL(builder);
return builder.toString();
} }
@Override @Override
......
...@@ -211,7 +211,8 @@ public class Sequence extends SchemaObjectBase { ...@@ -211,7 +211,8 @@ public class Sequence extends SchemaObjectBase {
if (getBelongsToTable()) { if (getBelongsToTable()) {
return null; return null;
} }
return "DROP SEQUENCE IF EXISTS " + getSQL(); StringBuilder builder = new StringBuilder("DROP SEQUENCE IF EXISTS ");
return getSQL(builder).toString();
} }
@Override @Override
...@@ -223,7 +224,7 @@ public class Sequence extends SchemaObjectBase { ...@@ -223,7 +224,7 @@ public class Sequence extends SchemaObjectBase {
public synchronized String getCreateSQL() { public synchronized String getCreateSQL() {
long v = writeWithMargin ? valueWithMargin : value; long v = writeWithMargin ? valueWithMargin : value;
StringBuilder buff = new StringBuilder("CREATE SEQUENCE "); StringBuilder buff = new StringBuilder("CREATE SEQUENCE ");
buff.append(getSQL()).append(" START WITH ").append(v); getSQL(buff).append(" START WITH ").append(v);
if (increment != 1) { if (increment != 1) {
buff.append(" INCREMENT BY ").append(increment); buff.append(" INCREMENT BY ").append(increment);
} }
......
...@@ -335,7 +335,8 @@ public class TriggerObject extends SchemaObjectBase { ...@@ -335,7 +335,8 @@ public class TriggerObject extends SchemaObjectBase {
buff.append(" AFTER "); buff.append(" AFTER ");
} }
buff.append(getTypeNameList()); buff.append(getTypeNameList());
buff.append(" ON ").append(targetTable.getSQL()); buff.append(" ON ");
targetTable.getSQL(buff);
if (rowBased) { if (rowBased) {
buff.append(" FOR EACH ROW"); buff.append(" FOR EACH ROW");
} }
......
...@@ -490,7 +490,8 @@ public class Column { ...@@ -490,7 +490,8 @@ public class Column {
buff.append(" NULL_TO_DEFAULT"); buff.append(" NULL_TO_DEFAULT");
} }
if (sequence != null) { if (sequence != null) {
buff.append(" SEQUENCE ").append(sequence.getSQL()); buff.append(" SEQUENCE ");
sequence.getSQL(buff);
} }
if (selectivity != 0) { if (selectivity != 0) {
buff.append(" SELECTIVITY ").append(selectivity); buff.append(" SELECTIVITY ").append(selectivity);
......
...@@ -79,7 +79,9 @@ public abstract class TableBase extends Table { ...@@ -79,7 +79,9 @@ public abstract class TableBase extends Table {
@Override @Override
public String getDropSQL() { public String getDropSQL() {
return "DROP TABLE IF EXISTS " + getSQL() + " CASCADE"; StringBuilder builder = new StringBuilder("DROP TABLE IF EXISTS ");
getSQL(builder).append(" CASCADE");
return builder.toString();
} }
@Override @Override
...@@ -106,7 +108,7 @@ public abstract class TableBase extends Table { ...@@ -106,7 +108,7 @@ public abstract class TableBase extends Table {
if (isHidden) { if (isHidden) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
buff.append(getSQL()); getSQL(buff.builder());
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment); StringUtils.quoteStringSQL(buff.builder(), comment);
......
...@@ -791,10 +791,10 @@ public class TableFilter implements ColumnResolver { ...@@ -791,10 +791,10 @@ 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('.'); table.getSchema().getSQL(builder).append('.');
Parser.quoteIdentifier(builder, table.getName()); Parser.quoteIdentifier(builder, table.getName());
} else { } else {
builder.append(table.getSQL()); table.getSQL(builder);
} }
if (table.isView() && ((TableView) table).isInvalid()) { if (table.isView() && ((TableView) table).isInvalid()) {
throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, table.getName(), "not compiled"); throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, table.getName(), "not compiled");
......
...@@ -354,7 +354,8 @@ public class TableLink extends Table { ...@@ -354,7 +354,8 @@ public class TableLink extends Table {
@Override @Override
public String getDropSQL() { public String getDropSQL() {
return "DROP TABLE IF EXISTS " + getSQL(); StringBuilder builder = new StringBuilder("DROP TABLE IF EXISTS ");
return getSQL(builder).toString();
} }
@Override @Override
...@@ -368,7 +369,8 @@ public class TableLink extends Table { ...@@ -368,7 +369,8 @@ public class TableLink extends Table {
} }
buff.append("TEMPORARY "); buff.append("TEMPORARY ");
} }
buff.append("LINKED TABLE ").append(getSQL()); buff.append("LINKED TABLE ");
getSQL(buff);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff, comment); StringUtils.quoteStringSQL(buff, comment);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.table; package org.h2.table;
import org.h2.command.Parser;
import org.h2.command.ddl.CreateSynonymData; import org.h2.command.ddl.CreateSynonymData;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
...@@ -67,12 +68,18 @@ public class TableSynonym extends SchemaObjectBase { ...@@ -67,12 +68,18 @@ public class TableSynonym extends SchemaObjectBase {
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
return "CREATE SYNONYM " + getSQL() + " FOR " + data.synonymForSchema.getName() + "." + data.synonymFor; StringBuilder builder = new StringBuilder("CREATE SYNONYM ");
getSQL(builder).append(" FOR ");
Parser.quoteIdentifier(data.synonymForSchema.getName());
builder.append('.');
Parser.quoteIdentifier(data.synonymFor);
return builder.toString();
} }
@Override @Override
public String getDropSQL() { public String getDropSQL() {
return "DROP SYNONYM " + getSQL(); StringBuilder builder = new StringBuilder("DROP SYNONYM ");
return getSQL(builder).toString();
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论