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