提交 41519da0 authored 作者: james.moger@gmail.com's avatar james.moger@gmail.com

Improved documentation.

Checkstyle formatting.
Build docs (1 remaining lengthy line).
Clarified the multiple addIndex and setPrimaryKey methods.
Restored previous COUNTER increment scheme.
上级 7f521673
...@@ -36,7 +36,7 @@ public class DbInspector { ...@@ -36,7 +36,7 @@ public class DbInspector {
/** /**
* Set the preferred Date class. * Set the preferred Date class.
* Possible values are: java.util.Date (default), java.sql.Date, java.sql.Timestamp. * Possible values are: java.util.Date (default) and java.sql.Timestamp.
* *
* @param dateClass the new date class * @param dateClass the new date class
*/ */
...@@ -45,14 +45,17 @@ public class DbInspector { ...@@ -45,14 +45,17 @@ public class DbInspector {
} }
/** /**
* Generates models class skeletons for schemas and tables. * Generates models class skeletons for schemas and tables. If the table
* name is undefined, models will be generated for every table within the
* specified schema. Additionally, if no schema is defined, models will be
* generated for all schemas and all tables.
* *
* @param schema the schema name (optional) * @param schema the schema name (optional)
* @param table the table name (required) * @param table the table name (optional)
* @param packageName the package name (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 a list of strings, each element a line of source code * @return a list of complete model classes as strings, each element a class
*/ */
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) {
......
...@@ -13,16 +13,16 @@ import org.h2.jaqu.Table.JQTable; ...@@ -13,16 +13,16 @@ import org.h2.jaqu.Table.JQTable;
* Model class for JaQu to track db and table versions. * Model class for JaQu to track db 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 {
@JQColumn(name="schemaName", allowNull=false) @JQColumn(name = "schemaName", allowNull = false)
String schema = ""; String schema;
@JQColumn(name="tableName", allowNull = false) @JQColumn(name = "tableName", allowNull = false)
String table = ""; String table;
@JQColumn(name="version") @JQColumn(name = "version")
Integer version; Integer version;
public DbVersion() { public DbVersion() {
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
package org.h2.jaqu; package org.h2.jaqu;
/** /**
* Classes implementing this interface can be used as a declaration in a statement. * Classes implementing this interface can be used as a declaration in an
* update statement.
*/ */
public interface Declaration { public interface Declaration {
/** /**
......
...@@ -71,11 +71,11 @@ public class ModelUtils { ...@@ -71,11 +71,11 @@ public class ModelUtils {
* Returns the Java class type for a given SQL type. * Returns the Java class type for a given SQL type.
* *
* @param sqlType * @param sqlType
* @param dateClazz the preferred date class (java.util.Date or java.sql.Timestamp) * @param dateClass the preferred date class (java.util.Date or java.sql.Timestamp)
* @return * @return
*/ */
public static Class<?> getClassType(String sqlType, public static Class<?> getClassType(String sqlType,
Class<? extends java.util.Date> dateClazz) { Class<? extends java.util.Date> dateClass) {
sqlType = sqlType.toUpperCase(); sqlType = sqlType.toUpperCase();
// FIXME dropping "UNSIGNED" or parts like that. could be trouble. // FIXME dropping "UNSIGNED" or parts like that. could be trouble.
sqlType = sqlType.split(" ")[0].trim(); sqlType = sqlType.split(" ")[0].trim();
...@@ -83,17 +83,17 @@ public class ModelUtils { ...@@ -83,17 +83,17 @@ public class ModelUtils {
if (sqlTypes.containsKey(sqlType)) if (sqlTypes.containsKey(sqlType))
// Marshall sqlType to a standard type // Marshall sqlType to a standard type
sqlType = sqlTypes.get(sqlType); sqlType = sqlTypes.get(sqlType);
Class<?> mappedClazz = null; Class<?> mappedClass = null;
for (Class<?> clazz : supportedTypes.keySet()) for (Class<?> clazz : supportedTypes.keySet())
if (supportedTypes.get(clazz).equalsIgnoreCase(sqlType)) { if (supportedTypes.get(clazz).equalsIgnoreCase(sqlType)) {
mappedClazz = clazz; mappedClass = clazz;
break; break;
} }
if (mappedClazz != null) { if (mappedClass != null) {
if (mappedClazz.equals(java.util.Date.class) if (mappedClass.equals(java.util.Date.class)
|| mappedClazz.equals(java.sql.Timestamp.class)) || mappedClass.equals(java.sql.Timestamp.class))
return dateClazz; return dateClass;
return mappedClazz; return mappedClass;
} }
return null; return null;
} }
...@@ -294,7 +294,7 @@ public class ModelUtils { ...@@ -294,7 +294,7 @@ public class ModelUtils {
// TIMESTAMPs // TIMESTAMPs
if (modelClazz == java.util.Date.class if (modelClazz == java.util.Date.class
|| modelClazz == java.sql.Timestamp.class){ || modelClazz == java.sql.Timestamp.class) {
// This may be a little loose.... // This may be a little loose....
// 00-00-00 00:00:00 // 00-00-00 00:00:00
// 00/00/00T00:00:00 // 00/00/00T00:00:00
......
...@@ -116,8 +116,8 @@ import java.lang.annotation.Target; ...@@ -116,8 +116,8 @@ import java.lang.annotation.Target;
* <pre> * <pre>
* Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;); * Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;);
* DbInspector inspector = new DbInspector(db); * DbInspector inspector = new DbInspector(db);
* List&lt;String&gt; models = inspector.generateModel(schema, table, packageName, * List&lt;String&gt; models = inspector.generateModel(schema, table,
* annotateSchema, trimStrings) * packageName, annotateSchema, trimStrings);
* </pre> * </pre>
* *
* OR you may use the <i>GenerateModels</i> tool to generate and save your * OR you may use the <i>GenerateModels</i> tool to generate and save your
...@@ -139,7 +139,8 @@ import java.lang.annotation.Target; ...@@ -139,7 +139,8 @@ import java.lang.annotation.Target;
* <pre> * <pre>
* Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;); * Db db = Db.open(&quot;jdbc:h2:mem:&quot;, &quot;sa&quot;, &quot;sa&quot;);
* DbInspector inspector = new DbInspector(db); * DbInspector inspector = new DbInspector(db);
* List&lt;Validation&gt; remarks = inspector.validateModel(new MyModel(), throwOnError); * MyModel model = new MyModel();
* List&lt;Validation&gt; remarks = inspector.validateModel(model, throwOnError);
* for (Validation remark : remarks) * for (Validation remark : remarks)
* System.out.println(remark); * System.out.println(remark);
* </pre> * </pre>
......
...@@ -135,18 +135,23 @@ class TableDefinition<T> { ...@@ -135,18 +135,23 @@ class TableDefinition<T> {
this.tableName = tableName; this.tableName = tableName;
} }
void setPrimaryKey(Object[] primaryKeyColumns) { /**
this.primaryKeyColumnNames = mapColumnNames(primaryKeyColumns); * Define a primary key by the specified model fields.
// set isPrimaryKey flag for all field definitions *
for (FieldDefinition fieldDefinition : fieldMap.values()) { * @param modelFields the ordered list of model fields
fieldDefinition.isPrimaryKey = this.primaryKeyColumnNames */
.contains(fieldDefinition.columnName); void setPrimaryKey(Object[] modelFields) {
} List<String> columnNames = mapColumnNames(modelFields);
setPrimaryKey(columnNames);
} }
void setPrimaryKey(List<String> primaryKeyColumnNames) { /**
int duplicateFunctionIsItRequired; * Define a primary key by the specified column names.
this.primaryKeyColumnNames = Utils.newArrayList(primaryKeyColumnNames); *
* @param columnNames the ordered list of column names
*/
void setPrimaryKey(List<String> columnNames) {
primaryKeyColumnNames = Utils.newArrayList(columnNames);
// 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
...@@ -167,16 +172,24 @@ class TableDefinition<T> { ...@@ -167,16 +172,24 @@ class TableDefinition<T> {
return columnNames; return columnNames;
} }
void addIndex(IndexType type, Object[] columns) { /**
IndexDefinition index = new IndexDefinition(); * Defines an index with the specified model fields.
index.indexName = tableName + "_" + indexes.size(); *
index.columnNames = mapColumnNames(columns); * @param type the index type (STANDARD, HASH, UNIQUE, UNIQUE_HASH)
index.type = type; * @param modelFields the ordered list of model fields
indexes.add(index); */
void addIndex(IndexType type, Object[] modelFields) {
List<String> columnNames = mapColumnNames(modelFields);
addIndex(type, columnNames);
} }
/**
* Defines an index with the specified column names.
*
* @param type the index type (STANDARD, HASH, UNIQUE, UNIQUE_HASH)
* @param columnNames the ordered list of column names
*/
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.newArrayList(columnNames); index.columnNames = Utils.newArrayList(columnNames);
......
...@@ -174,7 +174,7 @@ public class TableInspector { ...@@ -174,7 +174,7 @@ public class TableInspector {
* @param packageName * @param packageName
* @param annotateSchema * @param annotateSchema
* @param trimStrings * @param trimStrings
* @return * @return a complete model (class definition) for this table as a string
*/ */
String generateModel(String packageName, boolean annotateSchema, String generateModel(String packageName, boolean annotateSchema,
boolean trimStrings) { boolean trimStrings) {
...@@ -397,8 +397,8 @@ public class TableInspector { ...@@ -397,8 +397,8 @@ 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.
* <p> * <p>
* Results are returned as a List&lt;Validation&gt; which includes recommendations, * Results are returned as a List&lt;Validation&gt; which includes
* warnings, and errors about the model. * recommendations, warnings, and errors about the model.
* <p> * <p>
* The caller may choose to have validate throw an exception on any validation * The caller may choose to have validate throw an exception on any validation
* ERROR. * ERROR.
......
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
*/ */
package org.h2.jaqu.util; package org.h2.jaqu.util;
/**
* Common string utilities. I expect that this class will be removed.
*
*/
public class StringUtils { public class StringUtils {
/** /**
......
...@@ -70,33 +70,33 @@ public class Utils { ...@@ -70,33 +70,33 @@ public class Utils {
public static <T> T newObject(Class<T> clazz) { public static <T> T newObject(Class<T> clazz) {
// must create new instances // must create new instances
if (clazz == Integer.class) { if (clazz == Integer.class) {
return (T) new Integer((int) COUNTER.incrementAndGet()); return (T) new Integer((int) COUNTER.getAndIncrement());
} else if (clazz == String.class) { } else if (clazz == String.class) {
return (T) ("" + COUNTER.incrementAndGet()); return (T) ("" + COUNTER.getAndIncrement());
} else if (clazz == Long.class) { } else if (clazz == Long.class) {
return (T) new Long(COUNTER.incrementAndGet()); return (T) new Long(COUNTER.getAndIncrement());
} else if (clazz == Short.class) { } else if (clazz == Short.class) {
return (T) new Short((short) COUNTER.incrementAndGet()); return (T) new Short((short) COUNTER.getAndIncrement());
} else if (clazz == Byte.class) { } else if (clazz == Byte.class) {
return (T) new Byte((byte) COUNTER.incrementAndGet()); return (T) new Byte((byte) COUNTER.getAndIncrement());
} else if (clazz == Float.class) { } else if (clazz == Float.class) {
return (T) new Float(COUNTER.incrementAndGet()); return (T) new Float(COUNTER.getAndIncrement());
} else if (clazz == Double.class) { } else if (clazz == Double.class) {
return (T) new Double(COUNTER.incrementAndGet()); return (T) new Double(COUNTER.getAndIncrement());
} else if (clazz == Boolean.class) { } else if (clazz == Boolean.class) {
return (T) new Boolean(false); return (T) new Boolean(false);
} else if (clazz == BigDecimal.class) { } else if (clazz == BigDecimal.class) {
return (T) new BigDecimal(COUNTER.incrementAndGet()); return (T) new BigDecimal(COUNTER.getAndIncrement());
} else if (clazz == BigInteger.class) { } else if (clazz == BigInteger.class) {
return (T) new BigInteger("" + COUNTER.incrementAndGet()); return (T) new BigInteger("" + COUNTER.getAndIncrement());
} else if (clazz == java.sql.Date.class) { } else if (clazz == java.sql.Date.class) {
return (T) new java.sql.Date(COUNTER.incrementAndGet()); return (T) new java.sql.Date(COUNTER.getAndIncrement());
} else if (clazz == java.sql.Time.class) { } else if (clazz == java.sql.Time.class) {
return (T) new java.sql.Time(COUNTER.incrementAndGet()); return (T) new java.sql.Time(COUNTER.getAndIncrement());
} else if (clazz == java.sql.Timestamp.class) { } else if (clazz == java.sql.Timestamp.class) {
return (T) new java.sql.Timestamp(COUNTER.incrementAndGet()); return (T) new java.sql.Timestamp(COUNTER.getAndIncrement());
} else if (clazz == java.util.Date.class) { } else if (clazz == java.util.Date.class) {
return (T) new java.util.Date(COUNTER.incrementAndGet()); return (T) new java.util.Date(COUNTER.getAndIncrement());
} else if (clazz == List.class) { } else if (clazz == List.class) {
return (T) new ArrayList(); return (T) new ArrayList();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论