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

Cleanup:

- Rename 'Validation' to what it actually is: a 'ValidationRemark'.
- No need to check for null
- Don't use exclamation marks in error messages or warnings
- Don't use line comments for methods - use Javadoc comments
- Don't document private methods if the documentation just copies the method name
- Don't document parameter names if there is not actual documentation
- Don't use <p> and other HTML tags within Javadoc comments unless really necessary
- Don't use all uppercase (caps) method names
- Don't use empty lines (just '*') within Javadoc comments, except where necessary
 
上级 58468abb
...@@ -13,7 +13,7 @@ import org.h2.jaqu.DbInspector; ...@@ -13,7 +13,7 @@ import org.h2.jaqu.DbInspector;
import org.h2.jaqu.DbUpgrader; import org.h2.jaqu.DbUpgrader;
import org.h2.jaqu.DbVersion; import org.h2.jaqu.DbVersion;
import org.h2.jaqu.Table.JQDatabase; import org.h2.jaqu.Table.JQDatabase;
import org.h2.jaqu.Validation; import org.h2.jaqu.ValidationRemark;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.jaqu.SupportedTypes.SupportedTypes2; import org.h2.test.jaqu.SupportedTypes.SupportedTypes2;
...@@ -65,15 +65,15 @@ public class ModelsTest extends TestBase { ...@@ -65,15 +65,15 @@ public class ModelsTest extends TestBase {
} }
private void validateModel(DbInspector inspector, Object o) { private void validateModel(DbInspector inspector, Object o) {
List<Validation> remarks = inspector.validateModel(o, false); List<ValidationRemark> remarks = inspector.validateModel(o, false);
if (config.traceTest && remarks.size() > 0) { if (config.traceTest && remarks.size() > 0) {
trace("Validation remarks for " + o.getClass().getName()); trace("Validation remarks for " + o.getClass().getName());
for (Validation remark : remarks) { for (ValidationRemark remark : remarks) {
trace(remark.toString()); trace(remark.toString());
} }
trace(""); trace("");
} }
for (Validation remark : remarks) { for (ValidationRemark remark : remarks) {
assertFalse(remark.toString(), remark.isError()); assertFalse(remark.toString(), remark.isError());
} }
} }
......
...@@ -246,11 +246,10 @@ public class Db { ...@@ -246,11 +246,10 @@ public class Db {
} }
public synchronized void setDbUpgrader(DbUpgrader upgrader) { public synchronized void setDbUpgrader(DbUpgrader upgrader) {
if (upgrader == null) if (!upgrader.getClass().isAnnotationPresent(JQDatabase.class)) {
throw new RuntimeException("DbUpgrader may not be NULL!");
if (!upgrader.getClass().isAnnotationPresent(JQDatabase.class))
throw new RuntimeException("DbUpgrader must be annotated with " throw new RuntimeException("DbUpgrader must be annotated with "
+ JQDatabase.class.getSimpleName() + "!"); + JQDatabase.class.getSimpleName());
}
this.dbUpgrader = upgrader; this.dbUpgrader = upgrader;
upgradeChecked.clear(); upgradeChecked.clear();
} }
......
...@@ -82,7 +82,7 @@ public class DbInspector { ...@@ -82,7 +82,7 @@ public class DbInspector {
* @param throwOnError * @param throwOnError
* @return * @return
*/ */
public <T> List<Validation> validateModel(T model, boolean throwOnError) { public <T> List<ValidationRemark> validateModel(T model, boolean throwOnError) {
try { try {
TableInspector inspector = findTable(model); TableInspector inspector = findTable(model);
inspector.read(metaData); inspector.read(metaData);
......
...@@ -10,7 +10,7 @@ import org.h2.jaqu.Table.JQColumn; ...@@ -10,7 +10,7 @@ import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable; import org.h2.jaqu.Table.JQTable;
/** /**
* A JaQu system table to track database and table versions. * A system table to track database and table versions.
*/ */
@JQTable(name = "_jq_versions", primaryKey = "schemaName tableName", memoryTable = true) @JQTable(name = "_jq_versions", primaryKey = "schemaName tableName", memoryTable = true)
public class DbVersion { public class DbVersion {
...@@ -24,8 +24,6 @@ public class DbVersion { ...@@ -24,8 +24,6 @@ public class DbVersion {
@JQColumn(name = "version") @JQColumn(name = "version")
Integer version; Integer version;
private int todoReviewWholeClass;
public DbVersion() { public DbVersion() {
// nothing to do // nothing to do
} }
......
...@@ -132,8 +132,9 @@ public class Query<T> { ...@@ -132,8 +132,9 @@ public class Query<T> {
} }
public int update() { public int update() {
if (declarations.size() == 0) if (declarations.size() == 0) {
throw new RuntimeException("Please specify SET or INCREMENT before calling Update!"); throw new RuntimeException("Missing set or increment call.");
}
SQLStatement stat = new SQLStatement(db); SQLStatement stat = new SQLStatement(db);
stat.appendSQL("UPDATE "); stat.appendSQL("UPDATE ");
from.appendSQL(stat); from.appendSQL(stat);
...@@ -201,8 +202,9 @@ public class Query<T> { ...@@ -201,8 +202,9 @@ public class Query<T> {
int convertHereIsProbablyWrong; int convertHereIsProbablyWrong;
if (Clob.class.isAssignableFrom(o.getClass())) { if (Clob.class.isAssignableFrom(o.getClass())) {
value = (X) Utils.convert(o, String.class); value = (X) Utils.convert(o, String.class);
} else } else {
value = (X) o; value = (X) o;
}
result.add(value); result.add(value);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -380,10 +382,12 @@ public class Query<T> { ...@@ -380,10 +382,12 @@ public class Query<T> {
stat.appendSQL(" "); stat.appendSQL(" ");
} }
} }
if (limit > 0) if (limit > 0) {
db.getDialect().appendLimit(stat, limit); db.getDialect().appendLimit(stat, limit);
if (offset > 0) }
if (offset > 0) {
db.getDialect().appendOffset(stat, offset); db.getDialect().appendOffset(stat, offset);
}
StatementLogger.select(stat.getSQL()); StatementLogger.select(stat.getSQL());
return stat; return stat;
} }
......
...@@ -274,20 +274,23 @@ class TableDefinition<T> { ...@@ -274,20 +274,23 @@ class TableDefinition<T> {
} }
} }
// Optionally truncates strings to maxLength /**
* Optionally truncates strings to maxLength
*/
private Object getValue(Object obj, FieldDefinition field) { private Object getValue(Object obj, FieldDefinition field) {
Object value = field.getValue(obj); Object value = field.getValue(obj);
if (field.trimString && field.maxLength > 0) { if (field.trimString && field.maxLength > 0) {
if (value instanceof String) { if (value instanceof String) {
// Clip Strings // Clip Strings
String s = (String) value; String s = (String) value;
if (s.length() > field.maxLength) if (s.length() > field.maxLength) {
return s.substring(0, field.maxLength); return s.substring(0, field.maxLength);
}
return s; return s;
} }
return value; return value;
} else }
// Standard JaQu behavior // standard behavior
return value; return value;
} }
...@@ -310,8 +313,9 @@ class TableDefinition<T> { ...@@ -310,8 +313,9 @@ class TableDefinition<T> {
buff.append(')'); buff.append(')');
stat.setSQL(buff.toString()); stat.setSQL(buff.toString());
StatementLogger.insert(stat.getSQL()); StatementLogger.insert(stat.getSQL());
if (returnKey) if (returnKey) {
return stat.executeInsert(); return stat.executeInsert();
}
return stat.executeUpdate(); return stat.executeUpdate();
} }
...@@ -434,10 +438,11 @@ class TableDefinition<T> { ...@@ -434,10 +438,11 @@ class TableDefinition<T> {
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; int todoChangeToGetTableNameChangeAllMethodsInDialectInterface;
buff.append(db.getDialect().tableName(schemaName, tableName)).append('('); buff.append(db.getDialect().tableName(schemaName, tableName)).append('(');
...@@ -503,17 +508,26 @@ class TableDefinition<T> { ...@@ -503,17 +508,26 @@ class TableDefinition<T> {
return this; return this;
} }
// Retrieve list of columns from CSV whitespace notated index /**
* Retrieve list of columns from CSV whitespace notated index
*
* @param index the index columns
* @return the column list
*/
private List<String> getColumns(String index) { private List<String> getColumns(String index) {
List<String> cols = Utils.newArrayList(); List<String> cols = Utils.newArrayList();
if (index == null || index.length() == 0) if (index == null || index.length() == 0) {
return null; return null;
}
String[] cs = index.split("(,|\\s)"); String[] cs = index.split("(,|\\s)");
for (String c : cs) for (String c : cs) {
if (c != null && c.trim().length() > 0) if (c != null && c.trim().length() > 0) {
cols.add(c.trim()); cols.add(c.trim());
if (cols.size() == 0) }
}
if (cols.size() == 0) {
return null; return null;
}
return cols; return cols;
} }
...@@ -523,30 +537,34 @@ class TableDefinition<T> { ...@@ -523,30 +537,34 @@ class TableDefinition<T> {
if (clazz.isAnnotationPresent(JQSchema.class)) { if (clazz.isAnnotationPresent(JQSchema.class)) {
JQSchema schemaAnnotation = clazz.getAnnotation(JQSchema.class); JQSchema schemaAnnotation = clazz.getAnnotation(JQSchema.class);
// Setup Schema name mapping, if properly annotated // setup schema name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(schemaAnnotation.name())) if (!StringUtils.isNullOrEmpty(schemaAnnotation.name())) {
schemaName = schemaAnnotation.name(); schemaName = schemaAnnotation.name();
} }
}
if (clazz.isAnnotationPresent(JQTable.class)) { if (clazz.isAnnotationPresent(JQTable.class)) {
JQTable tableAnnotation = clazz.getAnnotation(JQTable.class); JQTable tableAnnotation = clazz.getAnnotation(JQTable.class);
// Setup Table name mapping, if properly annotated // setup table name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(tableAnnotation.name())) if (!StringUtils.isNullOrEmpty(tableAnnotation.name())) {
tableName = tableAnnotation.name(); tableName = tableAnnotation.name();
}
// 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();
}
// Setup the Primary Index, if properly annotated // setup the primary index, if properly annotated
List<String> primaryKey = getColumns(tableAnnotation.primaryKey()); List<String> primaryKey = getColumns(tableAnnotation.primaryKey());
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);
...@@ -562,17 +580,20 @@ class TableDefinition<T> { ...@@ -562,17 +580,20 @@ class TableDefinition<T> {
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);
if (validatedColumns == null) if (validatedColumns == null) {
return; return;
}
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) {
if (def.type.equals(type)) if (def.type.equals(type)) {
list.add(def); list.add(def);
}
}
return list; return list;
} }
......
...@@ -11,55 +11,26 @@ import org.h2.jaqu.TableInspector.ColumnInspector; ...@@ -11,55 +11,26 @@ import org.h2.jaqu.TableInspector.ColumnInspector;
import org.h2.jaqu.util.StringUtils; import org.h2.jaqu.util.StringUtils;
/** /**
* A Validation Remark is a result of running a model validation. * A validation remark is a result of running a model validation. Each remark
* <p> * has a level, associated component (schema, table, column, index), and a
* Each remark has a level, associated component (schema, table, column, index), * message.
* and a message.
*
*/ */
public class Validation { public class ValidationRemark {
private int todoReviewWholeClass;
public static Validation CONSIDER(String table, String type, String message) {
return new Validation(Level.CONSIDER, table, type, message);
}
public static Validation CONSIDER(String table, ColumnInspector col, String message) {
return new Validation(Level.CONSIDER, table, col, message);
}
public static Validation WARN(String table, ColumnInspector col, String message) {
return new Validation(Level.WARN, table, col, message);
}
public static Validation WARN(String table, String type, String message) {
return new Validation(Level.WARN, table, type, message);
}
public static Validation ERROR(String table, ColumnInspector col, String message) {
return new Validation(Level.ERROR, table, col, message);
}
public static Validation ERROR(String table, String type, String message) {
return new Validation(Level.ERROR, table, type, message);
}
public static Validation ERROR(String table, FieldDefinition field, String message) {
return new Validation(Level.ERROR, table, field, message);
}
/**
* The validation message level.
*/
public static enum Level { public static enum Level {
CONSIDER, WARN, ERROR; CONSIDER, WARN, ERROR;
} }
Level level; private Level level;
String table; private String table;
String fieldType; private String fieldType;
String fieldName; private String fieldName;
String message; private String message;
private Validation(Level level, String table, String type, String message) { private ValidationRemark(Level level, String table, String type, String message) {
this.level = level; this.level = level;
this.table = table; this.table = table;
this.fieldType = type; this.fieldType = type;
...@@ -67,7 +38,7 @@ public class Validation { ...@@ -67,7 +38,7 @@ public class Validation {
this.message = message; this.message = message;
} }
private Validation(Level level, String table, FieldDefinition field, String message) { private ValidationRemark(Level level, String table, FieldDefinition field, String message) {
this.level = level; this.level = level;
this.table = table; this.table = table;
this.fieldType = field.dataType; this.fieldType = field.dataType;
...@@ -75,7 +46,7 @@ public class Validation { ...@@ -75,7 +46,7 @@ public class Validation {
this.message = message; this.message = message;
} }
private Validation(Level level, String table, ColumnInspector col, String message) { private ValidationRemark(Level level, String table, ColumnInspector col, String message) {
this.level = level; this.level = level;
this.table = table; this.table = table;
this.fieldType = col.type; this.fieldType = col.type;
...@@ -83,9 +54,38 @@ public class Validation { ...@@ -83,9 +54,38 @@ public class Validation {
this.message = message; this.message = message;
} }
public Validation throwError(boolean throwOnError) { public static ValidationRemark consider(String table, String type, String message) {
if (throwOnError && isError()) return new ValidationRemark(Level.CONSIDER, table, type, message);
}
public static ValidationRemark consider(String table, ColumnInspector col, String message) {
return new ValidationRemark(Level.CONSIDER, table, col, message);
}
public static ValidationRemark warn(String table, ColumnInspector col, String message) {
return new ValidationRemark(Level.WARN, table, col, message);
}
public static ValidationRemark warn(String table, String type, String message) {
return new ValidationRemark(Level.WARN, table, type, message);
}
public static ValidationRemark error(String table, ColumnInspector col, String message) {
return new ValidationRemark(Level.ERROR, table, col, message);
}
public static ValidationRemark error(String table, String type, String message) {
return new ValidationRemark(Level.ERROR, table, type, message);
}
public static ValidationRemark error(String table, FieldDefinition field, String message) {
return new ValidationRemark(Level.ERROR, table, field, message);
}
public ValidationRemark throwError(boolean throwOnError) {
if (throwOnError && isError()) {
throw new RuntimeException(toString()); throw new RuntimeException(toString());
}
return this; return this;
} }
...@@ -112,4 +112,5 @@ public class Validation { ...@@ -112,4 +112,5 @@ public class Validation {
sb.append(message); sb.append(message);
return sb.toString(); return sb.toString();
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论