提交 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;
import org.h2.jaqu.DbUpgrader;
import org.h2.jaqu.DbVersion;
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.jaqu.SupportedTypes.SupportedTypes2;
......@@ -65,15 +65,15 @@ public class ModelsTest extends TestBase {
}
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) {
trace("Validation remarks for " + o.getClass().getName());
for (Validation remark : remarks) {
for (ValidationRemark remark : remarks) {
trace(remark.toString());
}
trace("");
}
for (Validation remark : remarks) {
for (ValidationRemark remark : remarks) {
assertFalse(remark.toString(), remark.isError());
}
}
......
......@@ -246,11 +246,10 @@ public class Db {
}
public synchronized void setDbUpgrader(DbUpgrader upgrader) {
if (upgrader == null)
throw new RuntimeException("DbUpgrader may not be NULL!");
if (!upgrader.getClass().isAnnotationPresent(JQDatabase.class))
if (!upgrader.getClass().isAnnotationPresent(JQDatabase.class)) {
throw new RuntimeException("DbUpgrader must be annotated with "
+ JQDatabase.class.getSimpleName() + "!");
+ JQDatabase.class.getSimpleName());
}
this.dbUpgrader = upgrader;
upgradeChecked.clear();
}
......
......@@ -82,7 +82,7 @@ public class DbInspector {
* @param throwOnError
* @return
*/
public <T> List<Validation> validateModel(T model, boolean throwOnError) {
public <T> List<ValidationRemark> validateModel(T model, boolean throwOnError) {
try {
TableInspector inspector = findTable(model);
inspector.read(metaData);
......
......@@ -10,7 +10,7 @@ import org.h2.jaqu.Table.JQColumn;
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)
public class DbVersion {
......@@ -24,8 +24,6 @@ public class DbVersion {
@JQColumn(name = "version")
Integer version;
private int todoReviewWholeClass;
public DbVersion() {
// nothing to do
}
......
......@@ -132,8 +132,9 @@ public class Query<T> {
}
public int update() {
if (declarations.size() == 0)
throw new RuntimeException("Please specify SET or INCREMENT before calling Update!");
if (declarations.size() == 0) {
throw new RuntimeException("Missing set or increment call.");
}
SQLStatement stat = new SQLStatement(db);
stat.appendSQL("UPDATE ");
from.appendSQL(stat);
......@@ -201,8 +202,9 @@ public class Query<T> {
int convertHereIsProbablyWrong;
if (Clob.class.isAssignableFrom(o.getClass())) {
value = (X) Utils.convert(o, String.class);
} else
} else {
value = (X) o;
}
result.add(value);
} catch (Exception e) {
throw new RuntimeException(e);
......@@ -328,7 +330,7 @@ public class Query<T> {
}
void addDeclarationToken(Declaration declaration) {
int todoWhatIsDeclaration;
int todoWhatIsDeclaration;
declarations.add(declaration);
}
......@@ -380,10 +382,12 @@ public class Query<T> {
stat.appendSQL(" ");
}
}
if (limit > 0)
if (limit > 0) {
db.getDialect().appendLimit(stat, limit);
if (offset > 0)
}
if (offset > 0) {
db.getDialect().appendOffset(stat, offset);
}
StatementLogger.select(stat.getSQL());
return stat;
}
......
......@@ -274,21 +274,24 @@ class TableDefinition<T> {
}
}
// Optionally truncates strings to maxLength
/**
* Optionally truncates strings to maxLength
*/
private Object getValue(Object obj, FieldDefinition field) {
Object value = field.getValue(obj);
if (field.trimString && field.maxLength > 0) {
if (value instanceof String) {
// Clip Strings
String s = (String) value;
if (s.length() > field.maxLength)
if (s.length() > field.maxLength) {
return s.substring(0, field.maxLength);
}
return s;
}
return value;
} else
// Standard JaQu behavior
return value;
}
// standard behavior
return value;
}
long insert(Db db, Object obj, boolean returnKey) {
......@@ -310,8 +313,9 @@ class TableDefinition<T> {
buff.append(')');
stat.setSQL(buff.toString());
StatementLogger.insert(stat.getSQL());
if (returnKey)
if (returnKey) {
return stat.executeInsert();
}
return stat.executeUpdate();
}
......@@ -434,10 +438,11 @@ class TableDefinition<T> {
StatementBuilder buff;
if (memoryTable &&
db.getConnection().getClass()
.getCanonicalName().equals("org.h2.jdbc.JdbcConnection"))
.getCanonicalName().equals("org.h2.jdbc.JdbcConnection")) {
buff = new StatementBuilder("CREATE MEMORY TABLE IF NOT EXISTS ");
else
} else {
buff = new StatementBuilder("CREATE TABLE IF NOT EXISTS ");
}
int todoChangeToGetTableNameChangeAllMethodsInDialectInterface;
buff.append(db.getDialect().tableName(schemaName, tableName)).append('(');
......@@ -503,17 +508,26 @@ class TableDefinition<T> {
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) {
List<String> cols = Utils.newArrayList();
if (index == null || index.length() == 0)
if (index == null || index.length() == 0) {
return null;
}
String[] cs = index.split("(,|\\s)");
for (String c : cs)
if (c != null && c.trim().length() > 0)
for (String c : cs) {
if (c != null && c.trim().length() > 0) {
cols.add(c.trim());
if (cols.size() == 0)
}
}
if (cols.size() == 0) {
return null;
}
return cols;
}
......@@ -523,29 +537,33 @@ class TableDefinition<T> {
if (clazz.isAnnotationPresent(JQSchema.class)) {
JQSchema schemaAnnotation = clazz.getAnnotation(JQSchema.class);
// Setup Schema name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(schemaAnnotation.name()))
// setup schema name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(schemaAnnotation.name())) {
schemaName = schemaAnnotation.name();
}
}
if (clazz.isAnnotationPresent(JQTable.class)) {
JQTable tableAnnotation = clazz.getAnnotation(JQTable.class);
// Setup Table name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(tableAnnotation.name()))
// setup table name mapping, if properly annotated
if (!StringUtils.isNullOrEmpty(tableAnnotation.name())) {
tableName = tableAnnotation.name();
}
// Allow control over createTableIfRequired()
// allow control over createTableIfRequired()
createTableIfRequired = tableAnnotation.createIfRequired();
// Model Version
if (tableAnnotation.version() > 0)
// model version
if (tableAnnotation.version() > 0) {
tableVersion = tableAnnotation.version();
}
// Setup the Primary Index, if properly annotated
// setup the primary index, if properly annotated
List<String> primaryKey = getColumns(tableAnnotation.primaryKey());
if (primaryKey != null)
if (primaryKey != null) {
setPrimaryKey(primaryKey);
}
}
if (clazz.isAnnotationPresent(JQIndex.class)) {
......@@ -562,17 +580,20 @@ class TableDefinition<T> {
void addIndexes(IndexType type, String [] indexes) {
for (String index:indexes) {
List<String> validatedColumns = getColumns(index);
if (validatedColumns == null)
if (validatedColumns == null) {
return;
}
addIndex(type, validatedColumns);
}
}
List<IndexDefinition> getIndexes(IndexType type) {
List<IndexDefinition> list = Utils.newArrayList();
for (IndexDefinition def:indexes)
if (def.type.equals(type))
for (IndexDefinition def:indexes) {
if (def.type.equals(type)) {
list.add(def);
}
}
return list;
}
......
......@@ -11,55 +11,26 @@ import org.h2.jaqu.TableInspector.ColumnInspector;
import org.h2.jaqu.util.StringUtils;
/**
* A Validation Remark is a result of running a model validation.
* <p>
* Each remark has a level, associated component (schema, table, column, index),
* and a message.
*
* A validation remark is a result of running a model validation. Each remark
* has a level, associated component (schema, table, column, index), and a
* message.
*/
public class Validation {
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);
}
public class ValidationRemark {
/**
* The validation message level.
*/
public static enum Level {
CONSIDER, WARN, ERROR;
}
Level level;
String table;
String fieldType;
String fieldName;
String message;
private Level level;
private String table;
private String fieldType;
private String fieldName;
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.table = table;
this.fieldType = type;
......@@ -67,7 +38,7 @@ public class Validation {
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.table = table;
this.fieldType = field.dataType;
......@@ -75,7 +46,7 @@ public class Validation {
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.table = table;
this.fieldType = col.type;
......@@ -83,9 +54,38 @@ public class Validation {
this.message = message;
}
public Validation throwError(boolean throwOnError) {
if (throwOnError && isError())
public static ValidationRemark consider(String table, String type, String message) {
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());
}
return this;
}
......@@ -112,4 +112,5 @@ public class Validation {
sb.append(message);
return sb.toString();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论