提交 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) {
...@@ -95,25 +98,27 @@ class TableDefinition<T> { ...@@ -95,25 +98,27 @@ 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;
schemaName = null; schemaName = null;
tableName = clazz.getSimpleName(); tableName = clazz.getSimpleName();
} }
Class<T> getModelClass() { Class<T> getModelClass() {
return clazz; return clazz;
} }
...@@ -140,21 +145,22 @@ class TableDefinition<T> { ...@@ -140,21 +145,22 @@ 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
.contains(fieldDefinition.columnName); .contains(fieldDefinition.columnName);
} }
} }
<A> String getColumnName(A fieldObject) { <A> String getColumnName(A fieldObject) {
FieldDefinition def = fieldMap.get(fieldObject); FieldDefinition def = fieldMap.get(fieldObject);
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,18 +213,20 @@ class TableDefinition<T> { ...@@ -206,18 +213,20 @@ 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;
boolean trimString = false; boolean trimString = false;
boolean allowNull = true; boolean allowNull = true;
String defaultValue = ""; String defaultValue = "";
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,18 +245,20 @@ class TableDefinition<T> { ...@@ -236,18 +245,20 @@ 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
...@@ -336,7 +347,7 @@ class TableDefinition<T> { ...@@ -336,7 +347,7 @@ class TableDefinition<T> {
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(db.getDialect().tableName(schemaName, tableName)).append(" SET "); buff.append(db.getDialect().tableName(schemaName, tableName)).append(" SET ");
buff.resetCount(); buff.resetCount();
for (FieldDefinition field : fields) { for (FieldDefinition field : fields) {
if (!field.isPrimaryKey) { if (!field.isPrimaryKey) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
...@@ -362,10 +373,10 @@ class TableDefinition<T> { ...@@ -362,10 +373,10 @@ class TableDefinition<T> {
aliasValue, value, CompareType.EQUAL)); aliasValue, value, CompareType.EQUAL));
} }
} }
stat.setSQL(buff.toString()); stat.setSQL(buff.toString());
query.appendWhere(stat); query.appendWhere(stat);
StatementLogger.update(stat.getSQL()); StatementLogger.update(stat.getSQL());
stat.executeUpdate(); stat.executeUpdate();
} }
void delete(Db db, Object obj) { void delete(Db db, Object obj) {
...@@ -408,36 +419,38 @@ class TableDefinition<T> { ...@@ -408,36 +419,38 @@ class TableDefinition<T> {
} }
SQLStatement stat = new SQLStatement(db); SQLStatement stat = new SQLStatement(db);
StatementBuilder buff; StatementBuilder buff;
if (memoryTable && if (memoryTable &&
db.getConnection().getClass() db.getConnection().getClass()
.getCanonicalName().equals("org.h2.jdbc.JdbcConnection")) .getCanonicalName().equals("org.h2.jdbc.JdbcConnection"))
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) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(field.columnName).append(' ').append(field.dataType); buff.append(field.columnName).append(' ').append(field.dataType);
// FIELD LENGTH // FIELD LENGTH
if (field.maxLength > 0) { if (field.maxLength > 0) {
buff.append('(').append(field.maxLength).append(')'); buff.append('(').append(field.maxLength).append(')');
} }
// AUTO_INCREMENT // AUTO_INCREMENT
if (field.isAutoIncrement) { if (field.isAutoIncrement) {
buff.append(" AUTO_INCREMENT"); buff.append(" AUTO_INCREMENT");
} }
// NOT NULL // NOT NULL
if (!field.allowNull) { if (!field.allowNull) {
buff.append(" NOT NULL"); buff.append(" NOT NULL");
} }
// DEFAULT... // DEFAULT...
if (!field.isAutoIncrement && !field.isPrimaryKey) { if (!field.isAutoIncrement && !field.isPrimaryKey) {
String dv = field.defaultValue; String dv = field.defaultValue;
if (!StringUtils.isNullOrEmpty(dv)) { if (!StringUtils.isNullOrEmpty(dv)) {
if (ModelUtils.isProperlyFormattedDefaultValue(dv) if (ModelUtils.isProperlyFormattedDefaultValue(dv)
&& ModelUtils.isValidDefaultValue(field.field.getType(), dv)) { && ModelUtils.isValidDefaultValue(field.field.getType(), dv)) {
...@@ -446,7 +459,8 @@ class TableDefinition<T> { ...@@ -446,7 +459,8 @@ 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(");
...@@ -469,10 +483,10 @@ class TableDefinition<T> { ...@@ -469,10 +483,10 @@ class TableDefinition<T> {
StatementLogger.create(stat.getSQL()); StatementLogger.create(stat.getSQL());
stat.executeUpdate(); stat.executeUpdate();
} }
// Table is created IF NOT EXISTS, otherwise statement is ignored // Table is created IF NOT EXISTS, otherwise statement is ignored
// But we still need to process potential Upgrade // But we still need to process potential Upgrade
db.upgradeTable(this); db.upgradeTable(this);
return this; return this;
} }
...@@ -489,7 +503,7 @@ class TableDefinition<T> { ...@@ -489,7 +503,7 @@ class TableDefinition<T> {
return null; return null;
return cols; return cols;
} }
void mapObject(Object obj) { void mapObject(Object obj) {
fieldMap.clear(); fieldMap.clear();
initObject(obj, fieldMap); initObject(obj, fieldMap);
...@@ -510,7 +524,7 @@ class TableDefinition<T> { ...@@ -510,7 +524,7 @@ class TableDefinition<T> {
// Allow control over createTableIfRequired() // Allow control over createTableIfRequired()
createTableIfRequired = tableAnnotation.createIfRequired(); createTableIfRequired = tableAnnotation.createIfRequired();
// Model Version // Model Version
if (tableAnnotation.version() > 0) if (tableAnnotation.version() > 0)
tableVersion = tableAnnotation.version(); tableVersion = tableAnnotation.version();
...@@ -520,7 +534,7 @@ class TableDefinition<T> { ...@@ -520,7 +534,7 @@ class TableDefinition<T> {
if (primaryKey != null) if (primaryKey != null)
setPrimaryKey(primaryKey); setPrimaryKey(primaryKey);
} }
if (clazz.isAnnotationPresent(JQIndex.class)) { if (clazz.isAnnotationPresent(JQIndex.class)) {
JQIndex indexAnnotation = clazz.getAnnotation(JQIndex.class); JQIndex indexAnnotation = clazz.getAnnotation(JQIndex.class);
...@@ -531,7 +545,7 @@ class TableDefinition<T> { ...@@ -531,7 +545,7 @@ class TableDefinition<T> {
addIndexes(IndexType.UNIQUE_HASH, indexAnnotation.uniqueHash()); addIndexes(IndexType.UNIQUE_HASH, indexAnnotation.uniqueHash());
} }
} }
void addIndexes(IndexType type, String [] indexes) { void addIndexes(IndexType type, String [] indexes) {
for (String index:indexes) { for (String index:indexes) {
List<String> validatedColumns = getColumns(index); List<String> validatedColumns = getColumns(index);
...@@ -540,7 +554,7 @@ class TableDefinition<T> { ...@@ -540,7 +554,7 @@ class TableDefinition<T> {
addIndex(type, validatedColumns); addIndex(type, validatedColumns);
} }
} }
List<IndexDefinition> getIndexes(IndexType type) { List<IndexDefinition> getIndexes(IndexType type) {
List<IndexDefinition> list = Utils.newArrayList(); List<IndexDefinition> list = Utils.newArrayList();
for (IndexDefinition def:indexes) for (IndexDefinition def:indexes)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论