提交 10ce4998 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add IndexColumn.writeColumns()

上级 15609e26
......@@ -21,7 +21,6 @@ import org.h2.table.Column;
import org.h2.table.IndexColumn;
import org.h2.table.Table;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.value.DataType;
import org.h2.value.Value;
......@@ -412,12 +411,7 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
* @return the list of columns
*/
private String getColumnListSQL() {
StatementBuilder buff = new StatementBuilder();
for (IndexColumn c : indexColumns) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL());
}
return buff.toString();
return IndexColumn.writeColumns(new StringBuilder(), indexColumns).toString();
}
@Override
......
......@@ -142,7 +142,8 @@ public class PageDataIndex extends PageIndex {
StringBuilder builder = new StringBuilder("PRIMARY KEY ON ");
table.getSQL(builder);
if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
builder.append('(').append(indexColumns[mainIndexColumn].getSQL()).append(')');
builder.append('(');
indexColumns[mainIndexColumn].getSQL(builder).append(')');
}
DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, builder.toString());
e.setSource(this);
......
......@@ -119,7 +119,8 @@ public class MVPrimaryIndex extends BaseIndex {
StringBuilder builder = new StringBuilder("PRIMARY KEY ON ");
table.getSQL(builder);
if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
builder.append('(').append(indexColumns[mainIndexColumn].getSQL()).append(')');
builder.append('(');
indexColumns[mainIndexColumn].getSQL(builder).append(')');
}
int errorCode = ErrorCode.CONCURRENT_UPDATE_1;
if (map.get(key) != null) {
......
......@@ -136,7 +136,7 @@ public class SortOrder implements Comparator<Value[]> {
/**
* Appends type information (DESC, NULLS FIRST, NULLS LAST) to the specified statement builder.
* @param builder statement builder
* @param builder string builder
* @param type sort type
*/
public static void typeToString(StringBuilder builder, int type) {
......
......@@ -30,15 +30,58 @@ public class IndexColumn {
public int sortType = SortOrder.ASCENDING;
/**
* Get the SQL snippet for this index column.
* Appends the specified columns to the specified builder.
*
* @return the SQL snippet
* @param builder
* string builder
* @param columns
* index columns
* @return the specified string builder
*/
public String getSQL() {
StringBuilder builder = new StringBuilder();
column.getSQL(builder);
SortOrder.typeToString(builder, sortType);
return builder.toString();
public static StringBuilder writeColumns(StringBuilder builder, IndexColumn[] columns) {
for (int i = 0, l = columns.length; i < l; i++) {
if (i > 0) {
builder.append(", ");
}
columns[i].getSQL(builder);
}
return builder;
}
/**
* Appends the specified columns to the specified builder.
*
* @param builder
* string builder
* @param columns
* index columns
* @param separator
* separator
* @param suffix
* additional SQL to append after each column
* @return the specified string builder
*/
public static StringBuilder writeColumns(StringBuilder builder, IndexColumn[] 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;
}
/**
* Appends the SQL snippet for this index column to the specified string builder.
*
* @param builder
* string builder
* @return the specified string builder
*/
public StringBuilder getSQL(StringBuilder builder) {
SortOrder.typeToString(column.getSQL(builder), sortType);
return builder;
}
/**
......@@ -71,6 +114,6 @@ public class IndexColumn {
@Override
public String toString() {
return "IndexColumn " + getSQL();
return getSQL(new StringBuilder("IndexColumn ")).toString();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论