提交 82ecbf33 authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Typos in Javadocs (./build.sh spellcheck finds this)
- Use private fields where possible
- Fix Javadoc with something that isn't a sentence
- Fix Javadoc without an actual comment
- The workaround 'clazz' instead of 'class' is not needed for 'dateClazz'
- Javadoc for parameters is inconsistent / incorrect
- Javadoc: undocumented return
- Javadoc comments are usually lowercase except for sentences
上级 c6e9d93c
...@@ -18,49 +18,49 @@ import org.h2.jaqu.util.StringUtils; ...@@ -18,49 +18,49 @@ import org.h2.jaqu.util.StringUtils;
import org.h2.jaqu.util.Utils; import org.h2.jaqu.util.Utils;
/** /**
* Class to inspect a model and a database for the purposes of model validation * Class to inspect a model and a database for the purposes of model validation
* and automatic model generation. This class finds the svailable schemas and * and automatic model generation. This class finds the available schemas and
* tables and serves as the entry point for model generation and validation. * tables and serves as the entry point for model generation and validation.
*
*/ */
public class DbInspector { public class DbInspector {
Db db; private Db db;
DatabaseMetaData metadata; private DatabaseMetaData metaData;
Class<? extends java.util.Date> dateClazz = java.util.Date.class; private Class<? extends java.util.Date> dateClass = java.util.Date.class;
private int todoReviewWholeClass;
public DbInspector(Db db) { public DbInspector(Db db) {
this.db = db; this.db = db;
} }
/** /**
* Set the preferred Date class. * Set the preferred Date class.
* java.util.Date (default) * Possible values are: java.util.Date (default), java.sql.Date, java.sql.Timestamp.
* java.sql.Timestamp *
* * @param dateClass the new date class
* @param dateClazz
*/ */
public void setPreferredDateClass(Class<? extends java.util.Date> dateClazz) { public void setPreferredDateClass(Class<? extends java.util.Date> dateClass) {
this.dateClazz = dateClazz; this.dateClass = dateClass;
} }
/** /**
* Generates models class skeletons for schemas and tables. * Generates models class skeletons for schemas and tables.
* *
* @param schema (optional) * @param schema the schema name (optional)
* @param table (required) * @param table the table name (required)
* @param packageName (optional) * @param packageName the package name (optional)
* @param annotateSchema (includes schema name in annotation) * @param annotateSchema (includes schema name in annotation)
* @param trimStrings (trims strings to maxLength of column) * @param trimStrings (trims strings to maxLength of column)
* @return List<String> source code models as strings * @return a list of strings, each element a line of source code
*/ */
public List<String> generateModel(String schema, String table, public List<String> generateModel(String schema, String table,
String packageName, boolean annotateSchema, boolean trimStrings) { String packageName, boolean annotateSchema, boolean trimStrings) {
try { try {
List<String> models = Utils.newArrayList(); List<String> models = Utils.newArrayList();
List<TableInspector> tables = findTables(schema, table); List<TableInspector> tables = findTables(schema, table);
for (TableInspector t : tables) { for (TableInspector t : tables) {
t.read(metadata); t.read(metaData);
String model = t.generateModel(packageName, annotateSchema, String model = t.generateModel(packageName, annotateSchema,
trimStrings); trimStrings);
models.add(model); models.add(model);
...@@ -73,17 +73,18 @@ public class DbInspector { ...@@ -73,17 +73,18 @@ public class DbInspector {
/** /**
* Validates a model. * Validates a model.
* *
* @param <T> type of model * @param <T> the model class
* @param model class * @param model and instance of the model class
* @param throwOnError * @param throwOnError
* @return * @return
*/ */
public <T> List<Validation> validateModel(T model, boolean throwOnError) { public <T> List<Validation> validateModel(T model, boolean throwOnError) {
try { try {
TableInspector inspector = findTable(model); TableInspector inspector = findTable(model);
inspector.read(metadata); inspector.read(metaData);
Class clazz = model.getClass(); @SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) model.getClass();
TableDefinition<T> def = db.define(clazz); TableDefinition<T> def = db.define(clazz);
return inspector.validate(def, throwOnError); return inspector.validate(def, throwOnError);
} catch (SQLException s) { } catch (SQLException s) {
...@@ -91,24 +92,26 @@ public class DbInspector { ...@@ -91,24 +92,26 @@ public class DbInspector {
} }
} }
private DatabaseMetaData metadata() throws SQLException { private DatabaseMetaData getMetaData() throws SQLException {
if (metadata == null) if (metaData == null) {
metadata = db.getConnection().getMetaData(); metaData = db.getConnection().getMetaData();
return metadata; }
return metaData;
} }
/** /**
* Attempts to find a table in the database based on the model definition. * Attempts to find a table in the database based on the model definition.
* *
* @param <T> * @param <T>
* @param model * @param model
* @return * @return
* @throws SQLException * @throws SQLException
*/ */
private <T> TableInspector findTable(T model) throws SQLException { private <T> TableInspector findTable(T model) throws SQLException {
Class clazz = model.getClass(); @SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) model.getClass();
TableDefinition<T> def = db.define(clazz); TableDefinition<T> def = db.define(clazz);
boolean forceUpperCase = metadata().storesUpperCaseIdentifiers(); boolean forceUpperCase = getMetaData().storesUpperCaseIdentifiers();
String sname = (forceUpperCase && def.schemaName != null) ? String sname = (forceUpperCase && def.schemaName != null) ?
def.schemaName.toUpperCase() : def.schemaName; def.schemaName.toUpperCase() : def.schemaName;
String tname = forceUpperCase ? def.tableName.toUpperCase() : def.tableName; String tname = forceUpperCase ? def.tableName.toUpperCase() : def.tableName;
...@@ -118,54 +121,57 @@ public class DbInspector { ...@@ -118,54 +121,57 @@ public class DbInspector {
/** /**
* Returns a list of tables * Returns a list of tables
* *
* @param schema * @param schema the schema name
* @param table * @param table the table name
* @return * @return a list of table inspectors
* @throws SQLException
*/ */
private List<TableInspector> findTables(String schema, String table) throws SQLException { private List<TableInspector> findTables(String schema, String table) throws SQLException {
ResultSet rs = null; ResultSet rs = null;
try { try {
rs = metadata().getSchemas(); rs = getMetaData().getSchemas();
ArrayList<String> schemaList = Utils.newArrayList(); ArrayList<String> schemaList = Utils.newArrayList();
while (rs.next()) while (rs.next()) {
schemaList.add(rs.getString("TABLE_SCHEM")); schemaList.add(rs.getString("TABLE_SCHEM"));
}
JdbcUtils.closeSilently(rs); JdbcUtils.closeSilently(rs);
// Get JaQu Tables table name. // get JaQu Tables table name.
String jaquTables = DbVersion.class.getAnnotation(JQTable.class).name(); String jaquTables = DbVersion.class.getAnnotation(JQTable.class).name();
List<TableInspector> tables = Utils.newArrayList(); List<TableInspector> tables = Utils.newArrayList();
if (schemaList.size() == 0) if (schemaList.size() == 0) {
schemaList.add(null); schemaList.add(null);
}
for (String s : schemaList) { for (String s : schemaList) {
rs = metadata().getTables(null, s, null, new String[] { "TABLE" }); rs = getMetaData().getTables(null, s, null, new String[] { "TABLE" });
while (rs.next()) { while (rs.next()) {
String t = rs.getString("TABLE_NAME"); String t = rs.getString("TABLE_NAME");
if (!t.equalsIgnoreCase(jaquTables)) if (!t.equalsIgnoreCase(jaquTables)) {
// Ignore JaQu versions table // Ignore JaQu versions table
tables.add(new TableInspector(s, t, tables.add(new TableInspector(s, t,
metadata().storesUpperCaseIdentifiers(), dateClazz)); getMetaData().storesUpperCaseIdentifiers(), dateClass));
}
} }
} }
if (StringUtils.isNullOrEmpty(schema) && StringUtils.isNullOrEmpty(table)) { if (StringUtils.isNullOrEmpty(schema) && StringUtils.isNullOrEmpty(table)) {
// All schemas and tables // all schemas and tables
return tables; return tables;
} else { }
// schema subset OR table subset OR exact match // schema subset OR table subset OR exact match
List<TableInspector> matches = Utils.newArrayList(); List<TableInspector> matches = Utils.newArrayList();
for (TableInspector t : tables) { for (TableInspector t : tables) {
if (t.matches(schema, table)) if (t.matches(schema, table)) {
matches.add(t); matches.add(t);
} }
if (matches.size() == 0)
throw new RuntimeException(
MessageFormat.format("Failed to find schema={0} table={1}",
schema == null ? "" : schema, table == null ? "" : table));
return matches;
} }
if (matches.size() == 0) {
throw new RuntimeException(
MessageFormat.format("Failed to find schema={0} table={1}",
schema == null ? "" : schema, table == null ? "" : table));
}
return matches;
} finally { } finally {
JdbcUtils.closeSilently(rs); JdbcUtils.closeSilently(rs);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论