提交 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;
} }
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
package org.h2.jaqu; package org.h2.jaqu;
import static java.text.MessageFormat.format; import static java.text.MessageFormat.format;
import static org.h2.jaqu.Validation.CONSIDER; import static org.h2.jaqu.ValidationRemark.consider;
import static org.h2.jaqu.Validation.ERROR; import static org.h2.jaqu.ValidationRemark.error;
import static org.h2.jaqu.Validation.WARN; import static org.h2.jaqu.ValidationRemark.warn;
import static org.h2.jaqu.util.JdbcUtils.closeSilently; import static org.h2.jaqu.util.JdbcUtils.closeSilently;
import static org.h2.jaqu.util.StringUtils.isNullOrEmpty; import static org.h2.jaqu.util.StringUtils.isNullOrEmpty;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
...@@ -147,7 +147,7 @@ public class TableInspector { ...@@ -147,7 +147,7 @@ public class TableInspector {
* and allowNull information. * and allowNull information.
* <p> * <p>
* The caller may optionally set a destination package name, whether or not * The caller may optionally set a destination package name, whether or not
* ot include the schema name (setting schema can be a problem when using * to include the schema name (setting schema can be a problem when using
* the model between databases), and if to automatically trim strings for * the model between databases), and if to automatically trim strings for
* those that have a maximum length. * those that have a maximum length.
* <p> * <p>
...@@ -274,13 +274,7 @@ public class TableInspector { ...@@ -274,13 +274,7 @@ public class TableInspector {
} }
/** private List<IndexInspector> getIndexes(IndexType type) {
* Returns indexes of a specific type from the map.
* <p>
* @param type
* @return
*/
List<IndexInspector> getIndexes(IndexType type) {
List<IndexInspector> list = Utils.newArrayList(); List<IndexInspector> list = Utils.newArrayList();
for (IndexInspector index:indexes.values()) { for (IndexInspector index:indexes.values()) {
if (index.type.equals(type)) { if (index.type.equals(type)) {
...@@ -290,16 +284,7 @@ public class TableInspector { ...@@ -290,16 +284,7 @@ public class TableInspector {
return list; return list;
} }
private StatementBuilder generateColumn(Set<String> imports, ColumnInspector col,
/**
* Generates a column field definition with annotations.
* <p>
* @param imports
* @param col
* @param trimStrings
* @return
*/
StatementBuilder generateColumn(Set<String> imports, ColumnInspector col,
boolean trimStrings) { boolean trimStrings) {
StatementBuilder sb = new StatementBuilder(); StatementBuilder sb = new StatementBuilder();
Class<?> clazz = col.clazz; Class<?> clazz = col.clazz;
...@@ -375,27 +360,24 @@ public class TableInspector { ...@@ -375,27 +360,24 @@ public class TableInspector {
/** /**
* Validates that a table definition (annotated, interface, or both) matches * Validates that a table definition (annotated, interface, or both) matches
* the current state of the table and indexes in the database. * the current state of the table and indexes in the database. Results are
* <p> * returned as a list of validation remarks which includes recommendations,
* Results are returned as a List&lt;Validation&gt; which includes * warnings, and errors about the model. The caller may choose to have
* recommendations, warnings, and errors about the model. * validate throw an exception on any validation ERROR.
* <p>
* The caller may choose to have validate throw an exception on any
* validation ERROR.
* <p>
* *
* @param <T> * @param <T>
* @param def * @param def
* @param throwError * @param throwError whether or not to throw an exception if an error was
* @return List&lt;Validation&gt; * found
* @return a list if validation remarks
*/ */
<T> List<Validation> validate(TableDefinition<T> def, <T> List<ValidationRemark> validate(TableDefinition<T> def,
boolean throwError) { boolean throwError) {
List<Validation> remarks = Utils.newArrayList(); List<ValidationRemark> remarks = Utils.newArrayList();
// model class definition validation // model class definition validation
if (!Modifier.isPublic(def.getModelClass().getModifiers())) { if (!Modifier.isPublic(def.getModelClass().getModifiers())) {
remarks.add(ERROR(table, "SCHEMA", remarks.add(error(table, "SCHEMA",
format("Class {0} MUST BE PUBLIC!", format("Class {0} MUST BE PUBLIC!",
def.getModelClass().getCanonicalName())).throwError(throwError)); def.getModelClass().getCanonicalName())).throwError(throwError));
} }
...@@ -403,11 +385,11 @@ public class TableInspector { ...@@ -403,11 +385,11 @@ public class TableInspector {
// Schema Validation // Schema Validation
if (!isNullOrEmpty(schema)) { if (!isNullOrEmpty(schema)) {
if (isNullOrEmpty(def.schemaName)) { if (isNullOrEmpty(def.schemaName)) {
remarks.add(CONSIDER(table, "SCHEMA", remarks.add(consider(table, "SCHEMA",
format("@{0}(name={1})", format("@{0}(name={1})",
JQSchema.class.getSimpleName(), schema))); JQSchema.class.getSimpleName(), schema)));
} else if (!schema.equalsIgnoreCase(def.schemaName)) { } else if (!schema.equalsIgnoreCase(def.schemaName)) {
remarks.add(ERROR(table, "SCHEMA", remarks.add(error(table, "SCHEMA",
format("@{0}(name={1}) != {2}", format("@{0}(name={1}) != {2}",
JQSchema.class.getSimpleName(), def.schemaName, JQSchema.class.getSimpleName(), def.schemaName,
schema)).throwError(throwError)); schema)).throwError(throwError));
...@@ -429,46 +411,33 @@ public class TableInspector { ...@@ -429,46 +411,33 @@ public class TableInspector {
/** /**
* Validates an inspected index from the database against the IndexDefinition * Validates an inspected index from the database against the IndexDefinition
* within the TableDefinition. * within the TableDefinition.
* <p>
* <b>TODO</b>: Complete index validation
* <p>
* @param <T>
* @param remarks
* @param def
* @param index
* @param throwError
*/ */
<T> void validate(List<Validation> remarks, TableDefinition<T> def, private <T> void validate(List<ValidationRemark> remarks, TableDefinition<T> def,
IndexInspector index, boolean throwError) { IndexInspector index, boolean throwError) {
List<IndexDefinition> defIndexes = def.getIndexes(IndexType.STANDARD); List<IndexDefinition> defIndexes = def.getIndexes(IndexType.STANDARD);
List<IndexInspector> dbIndexes = getIndexes(IndexType.STANDARD); List<IndexInspector> dbIndexes = getIndexes(IndexType.STANDARD);
if (defIndexes.size() > dbIndexes.size()) { if (defIndexes.size() > dbIndexes.size()) {
remarks.add(WARN(table, IndexType.STANDARD.name(), "# of Model Indexes > DB Indexes!")); remarks.add(warn(table, IndexType.STANDARD.name(), "More model indexes than database indexes"));
} else if (defIndexes.size() < dbIndexes.size()) { } else if (defIndexes.size() < dbIndexes.size()) {
remarks.add(WARN(table, IndexType.STANDARD.name(), "Model class is missing indexes!")); remarks.add(warn(table, IndexType.STANDARD.name(), "Model class is missing indexes"));
} }
// TODO complete index validation. // TODO complete index validation.
// Need to actually compare index types and columns within each index. // need to actually compare index types and columns within each index.
// At this point my head was starting to hurt.
} }
/** /**
* Validates a column against the model's field definition. Checks for * Validates a column against the model's field definition. Checks for
* existence, supported type, type mapping, default value, defined lengths, * existence, supported type, type mapping, default value, defined lengths,
* primary key, autoincrement. * primary key, autoincrement.
* <p>
* @param remarks
* @param fieldDef
* @param throwError
*/ */
void validate(List<Validation> remarks, FieldDefinition fieldDef, private void validate(List<ValidationRemark> remarks, FieldDefinition fieldDef,
boolean throwError) { boolean throwError) {
// unknown field // unknown field
String field = forceUpperCase ? String field = forceUpperCase ?
fieldDef.columnName.toUpperCase() : fieldDef.columnName; fieldDef.columnName.toUpperCase() : fieldDef.columnName;
if (!columns.containsKey(field)) { if (!columns.containsKey(field)) {
// unknown column mapping // unknown column mapping
remarks.add(ERROR(table, fieldDef, remarks.add(error(table, fieldDef,
"Does not exist in database!").throwError(throwError)); "Does not exist in database!").throwError(throwError));
return; return;
} }
...@@ -480,19 +449,19 @@ public class TableInspector { ...@@ -480,19 +449,19 @@ public class TableInspector {
// JaQu maps to VARCHAR for unsupported types. // JaQu maps to VARCHAR for unsupported types.
if (fieldDef.dataType.equals("VARCHAR") if (fieldDef.dataType.equals("VARCHAR")
&& (fieldClazz != String.class)) { && (fieldClazz != String.class)) {
remarks.add(ERROR(table, fieldDef, remarks.add(error(table, fieldDef,
"JaQu does not currently implement support for " "JaQu does not currently implement support for "
+ fieldClazz.getName()).throwError(throwError)); + fieldClazz.getName()).throwError(throwError));
} }
// number types // number types
if (!fieldClazz.equals(jdbcClazz)) { if (!fieldClazz.equals(jdbcClazz)) {
if (Number.class.isAssignableFrom(fieldClazz)) { if (Number.class.isAssignableFrom(fieldClazz)) {
remarks.add(WARN(table, col, remarks.add(warn(table, col,
format("Precision Mismatch: ModelObject={0}, ColumnObject={1}", format("Precision mismatch: ModelObject={0}, ColumnObject={1}",
fieldClazz.getSimpleName(), jdbcClazz.getSimpleName()))); fieldClazz.getSimpleName(), jdbcClazz.getSimpleName())));
} else { } else {
if (!Date.class.isAssignableFrom(jdbcClazz)) { if (!Date.class.isAssignableFrom(jdbcClazz)) {
remarks.add(WARN(table, col, remarks.add(warn(table, col,
format("Object Mismatch: ModelObject={0}, ColumnObject={1}", format("Object Mismatch: ModelObject={0}, ColumnObject={1}",
fieldClazz.getSimpleName(), jdbcClazz.getSimpleName()))); fieldClazz.getSimpleName(), jdbcClazz.getSimpleName())));
} }
...@@ -503,13 +472,13 @@ public class TableInspector { ...@@ -503,13 +472,13 @@ public class TableInspector {
if (fieldClazz == String.class) { if (fieldClazz == String.class) {
if ((fieldDef.maxLength != col.size) if ((fieldDef.maxLength != col.size)
&& (col.size < Integer.MAX_VALUE)) { && (col.size < Integer.MAX_VALUE)) {
remarks.add(WARN(table, col, remarks.add(warn(table, col,
format("{0}.maxLength={1}, ColumnMaxLength={2}", format("{0}.maxLength={1}, ColumnMaxLength={2}",
JQColumn.class.getSimpleName(), JQColumn.class.getSimpleName(),
fieldDef.maxLength, col.size))); fieldDef.maxLength, col.size)));
} }
if (fieldDef.maxLength > 0 && !fieldDef.trimString) { if (fieldDef.maxLength > 0 && !fieldDef.trimString) {
remarks.add(CONSIDER(table, col, remarks.add(consider(table, col,
format("{0}.truncateToMaxLength=true" format("{0}.truncateToMaxLength=true"
+ " will prevent RuntimeExceptions on" + " will prevent RuntimeExceptions on"
+ " INSERT or UPDATE, but will clip data!", + " INSERT or UPDATE, but will clip data!",
...@@ -519,7 +488,7 @@ public class TableInspector { ...@@ -519,7 +488,7 @@ public class TableInspector {
// numeric autoIncrement // numeric autoIncrement
if (fieldDef.isAutoIncrement != col.isAutoIncrement) { if (fieldDef.isAutoIncrement != col.isAutoIncrement) {
remarks.add(WARN(table, col, format("{0}.isAutoIncrement={1}" remarks.add(warn(table, col, format("{0}.isAutoIncrement={1}"
+ " while Column autoIncrement={2}", + " while Column autoIncrement={2}",
JQColumn.class.getSimpleName(), fieldDef.isAutoIncrement, JQColumn.class.getSimpleName(), fieldDef.isAutoIncrement,
col.isAutoIncrement))); col.isAutoIncrement)));
...@@ -529,7 +498,7 @@ public class TableInspector { ...@@ -529,7 +498,7 @@ public class TableInspector {
if (!col.isAutoIncrement && !col.isPrimaryKey) { if (!col.isAutoIncrement && !col.isPrimaryKey) {
// check Model.defaultValue format // check Model.defaultValue format
if (!ModelUtils.isProperlyFormattedDefaultValue(fieldDef.defaultValue)) { if (!ModelUtils.isProperlyFormattedDefaultValue(fieldDef.defaultValue)) {
remarks.add(ERROR(table, col, format("{0}.defaultValue=\"{1}\"" remarks.add(error(table, col, format("{0}.defaultValue=\"{1}\""
+ " is improperly formatted!", + " is improperly formatted!",
JQColumn.class.getSimpleName(), JQColumn.class.getSimpleName(),
fieldDef.defaultValue)).throwError(throwError)); fieldDef.defaultValue)).throwError(throwError));
...@@ -540,20 +509,20 @@ public class TableInspector { ...@@ -540,20 +509,20 @@ public class TableInspector {
if (isNullOrEmpty(fieldDef.defaultValue) if (isNullOrEmpty(fieldDef.defaultValue)
&& !isNullOrEmpty(col.defaultValue)) { && !isNullOrEmpty(col.defaultValue)) {
// Model.defaultValue is NULL, Column.defaultValue is NOT NULL // Model.defaultValue is NULL, Column.defaultValue is NOT NULL
remarks.add(WARN(table, col, format("{0}.defaultValue=\"\"" remarks.add(warn(table, col, format("{0}.defaultValue=\"\""
+ " while Column default=\"{1}\"", + " while Column default=\"{1}\"",
JQColumn.class.getSimpleName(), col.defaultValue))); JQColumn.class.getSimpleName(), col.defaultValue)));
} else if (!isNullOrEmpty(fieldDef.defaultValue) } else if (!isNullOrEmpty(fieldDef.defaultValue)
&& isNullOrEmpty(col.defaultValue)) { && isNullOrEmpty(col.defaultValue)) {
// Column.defaultValue is NULL, Model.defaultValue is NOT NULL // Column.defaultValue is NULL, Model.defaultValue is NOT NULL
remarks.add(WARN(table, col, format("{0}.defaultValue=\"{1}\"" remarks.add(warn(table, col, format("{0}.defaultValue=\"{1}\""
+ " while Column default=\"\"", + " while Column default=\"\"",
JQColumn.class.getSimpleName(), fieldDef.defaultValue))); JQColumn.class.getSimpleName(), fieldDef.defaultValue)));
} else if (!isNullOrEmpty(fieldDef.defaultValue) } else if (!isNullOrEmpty(fieldDef.defaultValue)
&& !isNullOrEmpty(col.defaultValue)) { && !isNullOrEmpty(col.defaultValue)) {
if (!fieldDef.defaultValue.equals(col.defaultValue)) { if (!fieldDef.defaultValue.equals(col.defaultValue)) {
// Model.defaultValue != Column.defaultValue // Model.defaultValue != Column.defaultValue
remarks.add(WARN(table, col, format("{0}.defaultValue=\"{1}\"" remarks.add(warn(table, col, format("{0}.defaultValue=\"{1}\""
+ " while Column default=\"{2}\"", + " while Column default=\"{2}\"",
JQColumn.class.getSimpleName(), fieldDef.defaultValue, JQColumn.class.getSimpleName(), fieldDef.defaultValue,
col.defaultValue))); col.defaultValue)));
...@@ -563,7 +532,7 @@ public class TableInspector { ...@@ -563,7 +532,7 @@ public class TableInspector {
// sanity check Model.defaultValue literal value // sanity check Model.defaultValue literal value
if (!ModelUtils.isValidDefaultValue(fieldDef.field.getType(), if (!ModelUtils.isValidDefaultValue(fieldDef.field.getType(),
fieldDef.defaultValue)) { fieldDef.defaultValue)) {
remarks.add(ERROR(table, col, remarks.add(error(table, col,
format("{0}.defaultValue=\"{1}\" is invalid!!", format("{0}.defaultValue=\"{1}\" is invalid!!",
JQColumn.class.getSimpleName(), JQColumn.class.getSimpleName(),
fieldDef.defaultValue))); fieldDef.defaultValue)));
...@@ -574,11 +543,10 @@ public class TableInspector { ...@@ -574,11 +543,10 @@ public class TableInspector {
/** /**
* Represents an index as it exists in the database. * Represents an index as it exists in the database.
*/ */
public static class IndexInspector { private static class IndexInspector {
String name;
String name;
IndexType type; IndexType type;
private List<String> columns = new ArrayList<String>(); private List<String> columns = new ArrayList<String>();
public IndexInspector(ResultSet rs) throws SQLException { public IndexInspector(ResultSet rs) throws SQLException {
...@@ -616,9 +584,8 @@ public class TableInspector { ...@@ -616,9 +584,8 @@ public class TableInspector {
/** /**
* Represents a column as it exists in the database. * Represents a column as it exists in the database.
*
*/ */
public static class ColumnInspector implements Comparable<ColumnInspector> { static class ColumnInspector implements Comparable<ColumnInspector> {
String name; String name;
String type; String type;
int size; int size;
...@@ -648,7 +615,6 @@ public class TableInspector { ...@@ -648,7 +615,6 @@ public class TableInspector {
/** /**
* Convenience class based on StatementBuilder for creating the * Convenience class based on StatementBuilder for creating the
* annotation parameter list. * annotation parameter list.
*
*/ */
private static class AnnotationBuilder extends StatementBuilder { private static class AnnotationBuilder extends StatementBuilder {
AnnotationBuilder() { AnnotationBuilder() {
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论