提交 c6e9d93c authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Fix bug about using an (unordered) set for index and primary key columns
- Non-private fields first (Checkstyle reports this)
- Comments are on a separate line (Checkstyle)
- Space before and after "=" (Eclipse reports this)
- Comment, specially comment fragements, are lowercase
上级 e857a250
......@@ -16,7 +16,6 @@ import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.h2.jaqu.Table.IndexType;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQIndex;
......@@ -26,6 +25,7 @@ import org.h2.jaqu.util.StatementLogger;
import org.h2.jaqu.util.StatementBuilder;
import org.h2.jaqu.util.StringUtils;
import org.h2.jaqu.util.Utils;
//## Java 1.5 end ##
/**
* A table definition contains the index definitions of a table, the field
......@@ -44,7 +44,8 @@ class TableDefinition<T> {
static class IndexDefinition {
IndexType type;
String indexName;
Set<String> columnNames;
List<String> columnNames;
}
//## Java 1.5 end ##
......@@ -78,8 +79,10 @@ class TableDefinition<T> {
void setValue(Object obj, Object o) {
try {
if (!field.isAccessible())
int setAccessibleShouldNotBeRequiredHere;
if (!field.isAccessible()) {
field.setAccessible(true);
}
o = Utils.convert(o, field.getType());
field.set(obj, o);
} catch (Exception e) {
......@@ -96,17 +99,19 @@ class TableDefinition<T> {
}
}
private boolean createTableIfRequired = true;
String schemaName;
String tableName;
private boolean createTableIfRequired = true;
private Class<T> clazz;
private ArrayList<FieldDefinition> fields = Utils.newArrayList();
private IdentityHashMap<Object, FieldDefinition> fieldMap =
Utils.newIdentityHashMap();
private Set<String> primaryKeyColumnNames;
private List<String> primaryKeyColumnNames;
private ArrayList<IndexDefinition> indexes = Utils.newArrayList();
private boolean memoryTable = false;
int tableVersion = 0;
private boolean memoryTable;
int tableVersion;
TableDefinition(Class<T> clazz) {
this.clazz = clazz;
......@@ -140,7 +145,8 @@ class TableDefinition<T> {
}
void setPrimaryKey(List<String> primaryKeyColumnNames) {
this.primaryKeyColumnNames = Utils.newHashSet(primaryKeyColumnNames);
int duplicateFunctionIsItRequired;
this.primaryKeyColumnNames = Utils.newArrayList(primaryKeyColumnNames);
// set isPrimaryKey flag for all field definitions
for (FieldDefinition fieldDefinition : fieldMap.values()) {
fieldDefinition.isPrimaryKey = this.primaryKeyColumnNames
......@@ -153,8 +159,8 @@ class TableDefinition<T> {
return def == null ? null : def.columnName;
}
private Set<String> mapColumnNames(Object[] columns) {
Set<String> columnNames = Utils.newHashSet();
private ArrayList<String> mapColumnNames(Object[] columns) {
ArrayList<String> columnNames = Utils.newArrayList();
for (Object column : columns) {
columnNames.add(getColumnName(column));
}
......@@ -170,9 +176,10 @@ class TableDefinition<T> {
}
void addIndex(IndexType type, List<String> columnNames) {
int whyRequiredCopyOfAboveFunction;
IndexDefinition index = new IndexDefinition();
index.indexName = tableName + "_" + indexes.size();
index.columnNames = Utils.newHashSet(columnNames);
index.columnNames = Utils.newArrayList(columnNames);
index.type = type;
indexes.add(index);
}
......@@ -206,7 +213,8 @@ class TableDefinition<T> {
}
for (Field f : classFields) {
String columnName = f.getName(); // default to field name
// default to field name
String columnName = f.getName();
boolean isAutoIncrement = false;
boolean isPrimaryKey = false;
int maxLength = 0;
......@@ -216,8 +224,9 @@ class TableDefinition<T> {
boolean hasAnnotation = f.isAnnotationPresent(JQColumn.class);
if (hasAnnotation) {
JQColumn col = f.getAnnotation(JQColumn.class);
if (!StringUtils.isNullOrEmpty(col.name()))
if (!StringUtils.isNullOrEmpty(col.name())) {
columnName = col.name();
}
isAutoIncrement = col.autoIncrement();
isPrimaryKey = col.primaryKey();
maxLength = col.maxLength();
......@@ -236,19 +245,21 @@ class TableDefinition<T> {
fieldDef.maxLength = maxLength;
fieldDef.trimString = trimString;
fieldDef.allowNull = allowNull;
fieldDef.defaultValue= defaultValue;
fieldDef.defaultValue = defaultValue;
fieldDef.dataType = ModelUtils.getDataType(fieldDef, strictTypeMapping);
fields.add(fieldDef);
}
}
List<String> primaryKey = Utils.newArrayList();
for (FieldDefinition fieldDef : fields) {
if (fieldDef.isPrimaryKey)
if (fieldDef.isPrimaryKey) {
primaryKey.add(fieldDef.columnName);
}
if (primaryKey.size() > 0)
}
if (primaryKey.size() > 0) {
setPrimaryKey(primaryKey);
}
}
// Optionally truncates strings to maxLength
private Object getValue(Object obj, FieldDefinition field) {
......@@ -414,6 +425,8 @@ class TableDefinition<T> {
buff = new StatementBuilder("CREATE MEMORY TABLE IF NOT EXISTS ");
else
buff = new StatementBuilder("CREATE TABLE IF NOT EXISTS ");
int todoChangeToGetTableNameChangeAllMethodsInDialectInterface;
buff.append(db.getDialect().tableName(schemaName, tableName)).append('(');
for (FieldDefinition field : fields) {
......@@ -447,6 +460,7 @@ class TableDefinition<T> {
}
}
int reviewJavadoc;
// PRIMARY KEY...
if (primaryKeyColumnNames != null && primaryKeyColumnNames.size() > 0) {
buff.append(", PRIMARY KEY(");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论