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