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

Add Column.writeColumns()

上级 4656131a
...@@ -269,37 +269,34 @@ public class Insert extends CommandWithValues implements ResultTarget { ...@@ -269,37 +269,34 @@ public class Insert extends CommandWithValues implements ResultTarget {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("INSERT INTO "); StringBuilder builder = new StringBuilder("INSERT INTO ");
table.getSQL(buff.builder()).append('('); table.getSQL(builder).append('(');
for (Column c : columns) { Column.writeColumns(builder, columns);
buff.appendExceptFirst(", "); builder.append(")\n");
c.getSQL(buff.builder());
}
buff.append(")\n");
if (insertFromSelect) { if (insertFromSelect) {
buff.append("DIRECT "); builder.append("DIRECT ");
} }
if (sortedInsertMode) { if (sortedInsertMode) {
buff.append("SORTED "); builder.append("SORTED ");
} }
if (!valuesExpressionList.isEmpty()) { if (!valuesExpressionList.isEmpty()) {
buff.append("VALUES "); builder.append("VALUES ");
int row = 0; int row = 0;
if (valuesExpressionList.size() > 1) { if (valuesExpressionList.size() > 1) {
buff.append('\n'); builder.append('\n');
} }
for (Expression[] expr : valuesExpressionList) { for (Expression[] expr : valuesExpressionList) {
if (row++ > 0) { if (row++ > 0) {
buff.append(",\n"); builder.append(",\n");
} }
buff.append('('); builder.append('(');
Expression.writeExpressions(buff.builder(), expr); Expression.writeExpressions(builder, expr);
buff.append(')'); builder.append(')');
} }
} else { } else {
buff.append(query.getPlanSQL()); builder.append(query.getPlanSQL());
} }
return buff.toString(); return builder.toString();
} }
@Override @Override
......
...@@ -206,38 +206,31 @@ public class Merge extends CommandWithValues { ...@@ -206,38 +206,31 @@ public class Merge extends CommandWithValues {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("MERGE INTO "); StringBuilder builder = new StringBuilder("MERGE INTO ");
targetTable.getSQL(buff.builder()).append('('); targetTable.getSQL(builder).append('(');
for (Column c : columns) { Column.writeColumns(builder, columns);
buff.appendExceptFirst(", "); builder.append(')');
c.getSQL(buff.builder());
}
buff.append(')');
if (keys != null) { if (keys != null) {
buff.append(" KEY("); builder.append(" KEY(");
buff.resetCount(); Column.writeColumns(builder, keys);
for (Column c : keys) { builder.append(')');
buff.appendExceptFirst(", ");
c.getSQL(buff.builder());
}
buff.append(')');
} }
buff.append('\n'); builder.append('\n');
if (!valuesExpressionList.isEmpty()) { if (!valuesExpressionList.isEmpty()) {
buff.append("VALUES "); builder.append("VALUES ");
int row = 0; int row = 0;
for (Expression[] expr : valuesExpressionList) { for (Expression[] expr : valuesExpressionList) {
if (row++ > 0) { if (row++ > 0) {
buff.append(", "); builder.append(", ");
} }
buff.append('('); builder.append('(');
Expression.writeExpressions(buff.builder(), expr); Expression.writeExpressions(builder, expr);
buff.append(')'); builder.append(')');
} }
} else { } else {
buff.append(query.getPlanSQL()); builder.append(query.getPlanSQL());
} }
return buff.toString(); return builder.toString();
} }
@Override @Override
......
...@@ -195,29 +195,26 @@ public class Replace extends CommandWithValues { ...@@ -195,29 +195,26 @@ public class Replace extends CommandWithValues {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("REPLACE INTO "); StringBuilder builder = new StringBuilder("REPLACE INTO ");
table.getSQL(buff.builder()).append('('); table.getSQL(builder).append('(');
for (Column c : columns) { Column.writeColumns(builder, columns);
buff.appendExceptFirst(", "); builder.append(')');
c.getSQL(buff.builder()); builder.append('\n');
}
buff.append(')');
buff.append('\n');
if (!valuesExpressionList.isEmpty()) { if (!valuesExpressionList.isEmpty()) {
buff.append("VALUES "); builder.append("VALUES ");
int row = 0; int row = 0;
for (Expression[] expr : valuesExpressionList) { for (Expression[] expr : valuesExpressionList) {
if (row++ > 0) { if (row++ > 0) {
buff.append(", "); builder.append(", ");
} }
buff.append('('); builder.append('(');
Expression.writeExpressions(buff.builder(), expr); Expression.writeExpressions(builder, expr);
buff.append(')'); builder.append(')');
} }
} else { } else {
buff.append(query.getPlanSQL()); builder.append(query.getPlanSQL());
} }
return buff.toString(); return builder.toString();
} }
@Override @Override
......
...@@ -86,6 +86,25 @@ public class Column { ...@@ -86,6 +86,25 @@ public class Column {
private boolean visible = true; private boolean visible = true;
private Domain domain; private Domain domain;
/**
* Appends the specified columns to the specified builder.
*
* @param builder
* string builder
* @param columns
* columns
* @return the specified string builder
*/
public static StringBuilder writeColumns(StringBuilder builder, Column[] columns) {
for (int i = 0, l = columns.length; i < l; i++) {
if (i > 0) {
builder.append(", ");
}
columns[i].getSQL(builder);
}
return builder;
}
public Column(String name, int valueType) { public Column(String name, int valueType) {
this(name, TypeInfo.getTypeInfo(valueType)); this(name, TypeInfo.getTypeInfo(valueType));
} }
......
...@@ -35,7 +35,6 @@ import org.h2.result.Row; ...@@ -35,7 +35,6 @@ import org.h2.result.Row;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.schema.Schema; import org.h2.schema.Schema;
import org.h2.util.ColumnNamer; import org.h2.util.ColumnNamer;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.TypeInfo; import org.h2.value.TypeInfo;
...@@ -323,40 +322,33 @@ public class TableView extends Table { ...@@ -323,40 +322,33 @@ public class TableView extends Table {
return getCreateSQL(orReplace, force, getSQL()); return getCreateSQL(orReplace, force, getSQL());
} }
private String getCreateSQL(boolean orReplace, boolean force, private String getCreateSQL(boolean orReplace, boolean force, String quotedName) {
String quotedName) { StringBuilder builder = new StringBuilder("CREATE ");
StatementBuilder buff = new StatementBuilder("CREATE ");
if (orReplace) { if (orReplace) {
buff.append("OR REPLACE "); builder.append("OR REPLACE ");
} }
if (force) { if (force) {
buff.append("FORCE "); builder.append("FORCE ");
} }
buff.append("VIEW "); builder.append("VIEW ");
if (isTableExpression) { if (isTableExpression) {
buff.append("TABLE_EXPRESSION "); builder.append("TABLE_EXPRESSION ");
} }
buff.append(quotedName); builder.append(quotedName);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); builder.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment); StringUtils.quoteStringSQL(builder, comment);
} }
if (columns != null && columns.length > 0) { if (columns != null && columns.length > 0) {
buff.append('('); builder.append('(');
for (Column c : columns) { Column.writeColumns(builder, columns);
buff.appendExceptFirst(", "); builder.append(')');
c.getSQL(buff.builder());
}
buff.append(')');
} else if (columnTemplates != null) { } else if (columnTemplates != null) {
buff.append('('); builder.append('(');
for (Column c : columnTemplates) { Column.writeColumns(builder, columnTemplates);
buff.appendExceptFirst(", "); builder.append(')');
buff.append(c.getName());
}
buff.append(')');
} }
return buff.append(" AS\n").append(querySQL).toString(); return builder.append(" AS\n").append(querySQL).toString();
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论