提交 15609e26 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add Column.writeColumns(StringBuilder, Column[], String, String)

上级 950f82d5
...@@ -26,7 +26,6 @@ import org.h2.result.Row; ...@@ -26,7 +26,6 @@ import org.h2.result.Row;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.Value; import org.h2.value.Value;
/** /**
...@@ -268,20 +267,11 @@ public class Merge extends CommandWithValues { ...@@ -268,20 +267,11 @@ public class Merge extends CommandWithValues {
} }
keys = idx.getColumns(); keys = idx.getColumns();
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StringBuilder builder = new StringBuilder("UPDATE ");
targetTable.getSQL(buff.builder()).append(" SET "); targetTable.getSQL(builder).append(" SET ");
for (Column c : columns) { Column.writeColumns(builder, columns, ", ", "=?").append(" WHERE ");
buff.appendExceptFirst(", "); Column.writeColumns(builder, keys, " AND ", "=?");
c.getSQL(buff.builder()).append("=?"); update = session.prepare(builder.toString());
}
buff.append(" WHERE ");
buff.resetCount();
for (Column c : keys) {
buff.appendExceptFirst(" AND ");
c.getSQL(buff.builder()).append("=?");
}
String sql = buff.toString();
update = session.prepare(sql);
} }
@Override @Override
......
...@@ -24,7 +24,6 @@ import org.h2.result.ResultInterface; ...@@ -24,7 +24,6 @@ import org.h2.result.ResultInterface;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.util.StatementBuilder;
import org.h2.value.Value; import org.h2.value.Value;
/** /**
...@@ -266,20 +265,11 @@ public class Replace extends CommandWithValues { ...@@ -266,20 +265,11 @@ public class Replace extends CommandWithValues {
return; return;
} }
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StringBuilder builder = new StringBuilder("UPDATE ");
table.getSQL(buff.builder()).append(" SET "); table.getSQL(builder).append(" SET ");
for (Column c : columns) { Column.writeColumns(builder, columns, ", ", "=?").append(" WHERE ");
buff.appendExceptFirst(", "); Column.writeColumns(builder, keys, " AND ", "=?");
c.getSQL(buff.builder()).append("=?"); update = session.prepare(builder.toString());
}
buff.append(" WHERE ");
buff.resetCount();
for (Column c : keys) {
buff.appendExceptFirst(" AND ");
c.getSQL(buff.builder()).append("=?");
}
String sql = buff.toString();
update = session.prepare(sql);
} }
@Override @Override
......
...@@ -1484,18 +1484,13 @@ public class Select extends Query { ...@@ -1484,18 +1484,13 @@ public class Select extends Query {
} }
} }
} }
buff.resetCount();
buff.append("SELECT"); buff.append("SELECT");
if (isAnyDistinct()) { if (isAnyDistinct()) {
buff.append(" DISTINCT"); buff.append(" DISTINCT");
if (distinctExpressions != null) { if (distinctExpressions != null) {
buff.append(" ON("); buff.append(" ON(");
for (Expression distinctExpression: distinctExpressions) { Expression.writeExpressions(buff.builder(), distinctExpressions);
buff.appendExceptFirst(", ");
distinctExpression.getSQL(buff.builder());
}
buff.append(')'); buff.append(')');
buff.resetCount();
} }
} }
for (int i = 0; i < visibleColumnCount; i++) { for (int i = 0; i < visibleColumnCount; i++) {
......
...@@ -105,6 +105,30 @@ public class Column { ...@@ -105,6 +105,30 @@ public class Column {
return builder; return builder;
} }
/**
* Appends the specified columns to the specified builder.
*
* @param builder
* string builder
* @param columns
* columns
* @param separator
* separator
* @param suffix
* additional SQL to append after each column
* @return the specified string builder
*/
public static StringBuilder writeColumns(StringBuilder builder, Column[] columns, String separator,
String suffix) {
for (int i = 0, l = columns.length; i < l; i++) {
if (i > 0) {
builder.append(separator);
}
columns[i].getSQL(builder).append(suffix);
}
return builder;
}
public Column(String name, int valueType) { public Column(String name, int valueType) {
this(name, TypeInfo.getTypeInfo(valueType)); this(name, TypeInfo.getTypeInfo(valueType));
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论