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

Add Column.writeColumns()

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