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