提交 113996ea authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Javadocs
- Work in progress
上级 daa2b0bf
...@@ -19,7 +19,6 @@ import org.h2.jaqu.bytecode.ClassReader; ...@@ -19,7 +19,6 @@ import org.h2.jaqu.bytecode.ClassReader;
import org.h2.jaqu.util.StatementLogger; import org.h2.jaqu.util.StatementLogger;
import org.h2.jaqu.util.JdbcUtils; import org.h2.jaqu.util.JdbcUtils;
import org.h2.jaqu.util.Utils; import org.h2.jaqu.util.Utils;
//import org.h2.util.JdbcUtils;
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
...@@ -38,8 +37,8 @@ public class Query<T> { ...@@ -38,8 +37,8 @@ public class Query<T> {
private final IdentityHashMap<Object, SelectColumn<T>> aliasMap = Utils.newIdentityHashMap(); private final IdentityHashMap<Object, SelectColumn<T>> aliasMap = Utils.newIdentityHashMap();
private ArrayList<OrderExpression<T>> orderByList = Utils.newArrayList(); private ArrayList<OrderExpression<T>> orderByList = Utils.newArrayList();
private Object[] groupByExpressions; private Object[] groupByExpressions;
private long limit = 0; private long limit;
private long offset = 0; private long offset;
Query(Db db) { Query(Db db) {
this.db = db; this.db = db;
...@@ -65,6 +64,7 @@ public class Query<T> { ...@@ -65,6 +64,7 @@ public class Query<T> {
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
int whyTrue;
JdbcUtils.closeSilently(rs, true); JdbcUtils.closeSilently(rs, true);
} }
} }
...@@ -120,24 +120,26 @@ public class Query<T> { ...@@ -120,24 +120,26 @@ public class Query<T> {
StatementLogger.delete(stat.getSQL()); StatementLogger.delete(stat.getSQL());
return stat.executeUpdate(); return stat.executeUpdate();
} }
public <A> SetColumn<T, A> set(A field) { public <A> SetColumn<T, A> set(A field) {
int renameSetColumnClassToUpdateSetColumn;
return new SetColumn<T, A>(this, field); return new SetColumn<T, A>(this, field);
} }
public <A> IncrementColumn<T, A> increment(A field) { public <A> IncrementColumn<T, A> increment(A field) {
int renameIncrementColumnClassToUpdateIncrementColumn;
return new IncrementColumn<T, A>(this, field); return new IncrementColumn<T, A>(this, field);
} }
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("Please specify SET or INCREMENT before calling Update!");
SQLStatement stat = new SQLStatement(db); SQLStatement stat = new SQLStatement(db);
stat.appendSQL("UPDATE "); stat.appendSQL("UPDATE ");
from.appendSQL(stat); from.appendSQL(stat);
stat.appendSQL(" SET "); stat.appendSQL(" SET ");
int i = 0; int i = 0;
for (Declaration declaration:declarations) { for (Declaration declaration : declarations) {
if (i++ > 0) { if (i++ > 0) {
stat.appendSQL(", "); stat.appendSQL(", ");
} }
...@@ -145,7 +147,7 @@ public class Query<T> { ...@@ -145,7 +147,7 @@ public class Query<T> {
} }
appendWhere(stat); appendWhere(stat);
StatementLogger.update(stat.getSQL()); StatementLogger.update(stat.getSQL());
return stat.executeUpdate(); return stat.executeUpdate();
} }
public <X, Z> List<X> selectDistinct(Z x) { public <X, Z> List<X> selectDistinct(Z x) {
...@@ -196,6 +198,7 @@ public class Query<T> { ...@@ -196,6 +198,7 @@ public class Query<T> {
try { try {
X value; X value;
Object o = rs.getObject(1); Object o = rs.getObject(1);
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
...@@ -266,7 +269,7 @@ public class Query<T> { ...@@ -266,7 +269,7 @@ public class Query<T> {
return this; return this;
} }
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
* Order by a number of columns. * Order by a number of columns.
* *
...@@ -323,8 +326,9 @@ public class Query<T> { ...@@ -323,8 +326,9 @@ public class Query<T> {
void addConditionToken(Token condition) { void addConditionToken(Token condition) {
conditions.add(condition); conditions.add(condition);
} }
void addDeclarationToken(Declaration declaration) { void addDeclarationToken(Declaration declaration) {
int todoWhatIsDeclaration;
declarations.add(declaration); declarations.add(declaration);
} }
......
...@@ -33,12 +33,12 @@ public class QueryWhere<T> { ...@@ -33,12 +33,12 @@ public class QueryWhere<T> {
query.addConditionToken(ConditionAndOr.OR); query.addConditionToken(ConditionAndOr.OR);
return new QueryCondition<T, A>(query, x); return new QueryCondition<T, A>(query, x);
} }
public QueryWhere<T> limit(long limit) { public QueryWhere<T> limit(long limit) {
query.limit(limit); query.limit(limit);
return this; return this;
} }
public QueryWhere<T> offset(long offset) { public QueryWhere<T> offset(long offset) {
query.offset(offset); query.offset(offset);
return this; return this;
......
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.jaqu; package org.h2.jaqu;
import org.h2.jaqu.TableDefinition.IndexDefinition; import org.h2.jaqu.TableDefinition.IndexDefinition;
import org.h2.jaqu.util.StatementBuilder; import org.h2.jaqu.util.StatementBuilder;
import org.h2.jaqu.util.StringUtils; import org.h2.jaqu.util.StringUtils;
/** /**
* This interface defines points where JaQu can build different statements * This interface defines points where JaQu can build different statements
* depending on the database used. * depending on the database used.
*/ */
public interface SQLDialect { public interface SQLDialect {
/** /**
* Get the SQL snippet for the table name. * Get the SQL snippet for the table name.
* *
* @param schema the schema name, or null for no schema * @param schema the schema name, or null for no schema
* @param table the table name * @param table the table name
* @return the SQL snippet * @return the SQL snippet
*/ */
String tableName(String schema, String table); String tableName(String schema, String table);
/** /**
* Get the CREATE INDEX statement. * Get the CREATE INDEX statement.
* *
* @param schema the schema name * @param schema the schema name
* @param table the table name * @param table the table name
* @param index the index definition * @param index the index definition
* @return the SQL statement * @return the SQL statement
*/ */
String createIndex(String schema, String table, IndexDefinition index); String createIndex(String schema, String table, IndexDefinition index);
/** /**
* Append "LIMIT limit" to the SQL statement. * Append "LIMIT limit" to the SQL statement.
* *
* @param stat the statement * @param stat the statement
* @param limit the limit * @param limit the limit
*/ */
void appendLimit(SQLStatement stat, long limit); void appendLimit(SQLStatement stat, long limit);
/** /**
* Append "OFFSET offset" to the SQL statement. * Append "OFFSET offset" to the SQL statement.
* *
* @param stat the statement * @param stat the statement
* @param offset the offset * @param offset the offset
*/ */
void appendOffset(SQLStatement stat, long offset); void appendOffset(SQLStatement stat, long offset);
/** /**
* Default implementation of an SQL dialect. * Default implementation of an SQL dialect.
* Designed for an H2 database. May be suitable for others. * Designed for an H2 database. May be suitable for others.
*/ */
public static class DefaultSQLDialect implements SQLDialect { public static class DefaultSQLDialect implements SQLDialect {
public String tableName(String schema, String table) { public String tableName(String schema, String table) {
if (StringUtils.isNullOrEmpty(schema)) { if (StringUtils.isNullOrEmpty(schema)) {
return table; return table;
} }
return schema + "." + table; return schema + "." + table;
} }
public String createIndex(String schema, String table, IndexDefinition index) { public String createIndex(String schema, String table, IndexDefinition index) {
StatementBuilder buff = new StatementBuilder(); StatementBuilder buff = new StatementBuilder();
buff.append("CREATE "); buff.append("CREATE ");
switch(index.type) { switch(index.type) {
case STANDARD: case STANDARD:
break; break;
case UNIQUE: case UNIQUE:
buff.append("UNIQUE "); buff.append("UNIQUE ");
break; break;
case HASH: case HASH:
buff.append("HASH "); buff.append("HASH ");
break; break;
case UNIQUE_HASH: case UNIQUE_HASH:
buff.append("UNIQUE HASH "); buff.append("UNIQUE HASH ");
break; break;
} }
buff.append("INDEX IF NOT EXISTS "); buff.append("INDEX IF NOT EXISTS ");
buff.append(index.indexName); buff.append(index.indexName);
buff.append(" ON "); buff.append(" ON ");
buff.append(table); buff.append(table);
buff.append("("); buff.append("(");
for (String col:index.columnNames) { for (String col:index.columnNames) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(col); buff.append(col);
} }
buff.append(")"); buff.append(")");
return buff.toString(); return buff.toString();
} }
public void appendLimit(SQLStatement stat, long limit) { public void appendLimit(SQLStatement stat, long limit) {
stat.appendSQL(" LIMIT " + limit); stat.appendSQL(" LIMIT " + limit);
} }
public void appendOffset(SQLStatement stat, long offset) { public void appendOffset(SQLStatement stat, long offset) {
stat.appendSQL(" OFFSET " + offset); stat.appendSQL(" OFFSET " + offset);
} }
} }
} }
...@@ -38,7 +38,7 @@ public class SQLStatement { ...@@ -38,7 +38,7 @@ public class SQLStatement {
sql = null; sql = null;
return this; return this;
} }
public SQLStatement appendTable(String schema, String table) { public SQLStatement appendTable(String schema, String table) {
return appendSQL(db.getDialect().tableName(schema, table)); return appendSQL(db.getDialect().tableName(schema, table));
} }
...@@ -54,7 +54,7 @@ public class SQLStatement { ...@@ -54,7 +54,7 @@ public class SQLStatement {
params.add(o); params.add(o);
return this; return this;
} }
ResultSet executeQuery() { ResultSet executeQuery() {
try { try {
return prepare(false).executeQuery(); return prepare(false).executeQuery();
...@@ -74,7 +74,7 @@ public class SQLStatement { ...@@ -74,7 +74,7 @@ public class SQLStatement {
JdbcUtils.closeSilently(ps); JdbcUtils.closeSilently(ps);
} }
} }
long executeInsert() { long executeInsert() {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
...@@ -82,9 +82,10 @@ public class SQLStatement { ...@@ -82,9 +82,10 @@ public class SQLStatement {
ps.executeUpdate(); ps.executeUpdate();
long identity = -1; long identity = -1;
ResultSet rs = ps.getGeneratedKeys(); ResultSet rs = ps.getGeneratedKeys();
if (rs != null && rs.next()) if (rs != null && rs.next()) {
identity = rs.getLong(1); identity = rs.getLong(1);
JdbcUtils.closeSilently(rs); }
JdbcUtils.closeSilently(rs);
return identity; return identity;
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -101,8 +102,8 @@ public class SQLStatement { ...@@ -101,8 +102,8 @@ public class SQLStatement {
} }
} }
private PreparedStatement prepare(boolean returnIdentity) { private PreparedStatement prepare(boolean returnGeneratedKeys) {
PreparedStatement prep = db.prepare(getSQL(), returnIdentity); PreparedStatement prep = db.prepare(getSQL(), returnGeneratedKeys);
for (int i = 0; i < params.size(); i++) { for (int i = 0; i < params.size(); i++) {
Object o = params.get(i); Object o = params.get(i);
setValue(prep, i + 1, o); setValue(prep, i + 1, o);
......
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.jaqu; package org.h2.jaqu;
/** /**
* This class represents "SET column = value" in an UPDATE statement. * This class represents "SET column = value" in an UPDATE statement.
* *
* @param <T> the query type * @param <T> the query type
* @param <A> the new value data type * @param <A> the new value data type
*/ */
//## Java 1.5 begin ## //## Java 1.5 begin ##
public class SetColumn<T, A> implements Declaration { public class SetColumn<T, A> implements Declaration {
private Query<T> query; private Query<T> query;
private A x; private A x;
private A y; private A y;
SetColumn(Query<T> query, A x) { SetColumn(Query<T> query, A x) {
this.query = query; this.query = query;
this.x = x; this.x = x;
} }
public Query<T> to(A y) { public Query<T> to(A y) {
query.addDeclarationToken(this); query.addDeclarationToken(this);
this.y = y; this.y = y;
return query; return query;
} }
public void appendSQL(SQLStatement stat) { public void appendSQL(SQLStatement stat) {
query.appendSQL(stat, x); query.appendSQL(stat, x);
stat.appendSQL("=?"); stat.appendSQL("=?");
stat.addParameter(y); stat.addParameter(y);
} }
} }
//## Java 1.5 end ## //## Java 1.5 end ##
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group * (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/ */
package org.h2.jaqu; package org.h2.jaqu;
...@@ -10,6 +11,9 @@ import java.lang.annotation.Retention; ...@@ -10,6 +11,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* A class that implements this interface can be used as a database table.
*/
/** /**
* A class that implements the JaQu model mapping options. * A class that implements the JaQu model mapping options.
* <p> * <p>
...@@ -112,95 +116,93 @@ import java.lang.annotation.Target; ...@@ -112,95 +116,93 @@ import java.lang.annotation.Target;
* <p> * <p>
* You may automatically generate model classes as strings with the <i>Db</i> * You may automatically generate model classes as strings with the <i>Db</i>
* and <i>DbInspector</i> objects. * and <i>DbInspector</i> objects.
* *
* <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, * List&lt;String&gt; models =
* packageName, annotateSchema, trimStrings); * inspector.generateModel(schema, table, 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
* classes to the filesystem. * classes to the filesystem.
* *
* <pre> * <pre>
* java -cp h2jaqu.jar org.h2.jaqu.util.GenerateModels * java -cp h2jaqu.jar org.h2.jaqu.util.GenerateModels
* -url &quot;jdbc:h2:mem:&quot; * -url &quot;jdbc:h2:mem:&quot;
* -user sa -password sa -schema schemaName -table tableName * -user sa -password sa -schema schemaName -table tableName
* -package packageName -folder destination * -package packageName -folder destination
* -annotateSchema false -trimStrings true * -annotateSchema false -trimStrings true
* </pre> * </pre>
* *
* <b>Model Validation</b> * <b>Model Validation</b>
* <p> * <p>
* You may validate your model class with <i>DbInspector</i> object.<br> * You may validate your model class with <i>DbInspector</i> object.<br>
* The DbInspector will report ERRORS, WARNINGS, and SUGGESTIONS to help you. * The DbInspector will report ERRORS, WARNINGS, and
* * SUGGESTIONS to help you.
*
* <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);
* MyModel model = new MyModel(); * List&lt;Validation&gt; remarks =
* List&lt;Validation&gt; remarks = inspector.validateModel(model, throwOnError); * inspector.validateModel(new MyModel(), throwOnError);
* for (Validation remark : remarks) * for (Validation remark : remarks) {
* System.out.println(remark); * System.out.println(remark);
* }
* </pre> * </pre>
*/ */
public interface Table { public interface Table {
static final int reviewWholeClassAndJavadocs = 0;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface JQDatabase { public @interface JQDatabase {
/** /**
* If <b>version</b> is set to a <i>non-zero</i> value, JaQu will * If version is set to a non-zero value, JaQu will
* maintain a "_jq_versions" table within your database. The * maintain a "_jq_versions" table within your database. The
* <i>version</i> number will be used to call to a registered * version number will be used to call to a registered
* <i>DbUpgrader</i> implementation to perform relevant ALTERs or * DbUpgrader implementation to perform relevant ALTER statements.
* whatever. * Default: 0.
* <p> * You must specify a DbUpgrader on your Db object to
* <b>Default: <i>0</i></b>
* <p>
* <b>NOTE:</b><br>
* You must specify a <i>DbUpgrader</i> on your <i>Db</i> object to
* use this parameter. * use this parameter.
*/ */
int version() default 0; int version() default 0;
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface JQSchema { public @interface JQSchema {
/** /**
* <b>schema</b> may be optionally specified. If it is not specified the * The schema may be optionally specified. If it is not specified the
* schema will be ignored. * schema will be ignored. Default: Unspecified.
* <p>
* <b>Default: <i>Unspecified</i></b>
*/ */
String name() default ""; String name() default "";
} }
/** /**
* Enumeration defining the 4 index types. * Enumeration defining the 4 index types.
*
*/ */
public static enum IndexType { public static enum IndexType {
STANDARD, UNIQUE, HASH, UNIQUE_HASH; STANDARD, UNIQUE, HASH, UNIQUE_HASH;
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface JQIndex { public @interface JQIndex {
/** /**
* <b>standard</b> indexes may be optionally specified. If not specified, * Standard indexes may be optionally specified. If not specified,
* these values will be ignored. * these values will be ignored.
* <ul> * <ul>
* <li>standard = "id, name" * <li>standard = "id, name"
* <li>standard = "id name" * <li>standard = "id name"
* <li>standard = { "id name", "date" } * <li>standard = { "id name", "date" }
* </ul> * </ul>
* Standard indexes may still be added in the <i>define()</i> method if * Standard indexes may still be added in the define() method if
* the model class is not annotated with JQTable. * the model class is not annotated with JQTable.
* <p> * Default: Unspecified.
* <b>Default: <i>Unspecified</i></b>
*/ */
String[] standard() default {}; String[] standard() default {};
...@@ -218,7 +220,7 @@ public interface Table { ...@@ -218,7 +220,7 @@ public interface Table {
* <b>Default: <i>Unspecified</i></b> * <b>Default: <i>Unspecified</i></b>
*/ */
String[] unique() default {}; String[] unique() default {};
/** /**
* <b>hash</b> indexes may be optionally specified. If not specified, * <b>hash</b> indexes may be optionally specified. If not specified,
* these values will be ignored. * these values will be ignored.
...@@ -232,7 +234,7 @@ public interface Table { ...@@ -232,7 +234,7 @@ public interface Table {
* <b>Default: <i>Unspecified</i></b> * <b>Default: <i>Unspecified</i></b>
*/ */
String[] hash() default {}; String[] hash() default {};
/** /**
* <b>uniqueHash</b> indexes may be optionally specified. If not specified, * <b>uniqueHash</b> indexes may be optionally specified. If not specified,
* these values will be ignored. * these values will be ignored.
...@@ -337,8 +339,8 @@ public interface Table { ...@@ -337,8 +339,8 @@ public interface Table {
* If <b>version</b> is set to a <i>non-zero</i> value, JaQu will * If <b>version</b> is set to a <i>non-zero</i> value, JaQu will
* maintain a "_jq_versions" table within your database. The * maintain a "_jq_versions" table within your database. The
* <i>version</i> number will be used to call to a registered * <i>version</i> number will be used to call to a registered
* <i>DbUpgrader</i> implementation to perform relevant ALTERs or * <i>DbUpgrader</i> implementation to perform relevant ALTER
* whatever. * statements.
* <p> * <p>
* <b>Default: <i>0</i></b> * <b>Default: <i>0</i></b>
* <p> * <p>
......
...@@ -227,7 +227,7 @@ class TableDefinition<T> { ...@@ -227,7 +227,7 @@ class TableDefinition<T> {
for (Field f : classFields) { for (Field f : classFields) {
// default to field name // default to field name
String columnName = f.getName(); String columnName = f.getName();
boolean isAutoIncrement = false; boolean isAutoIncrement = false;
boolean isPrimaryKey = false; boolean isPrimaryKey = false;
int maxLength = 0; int maxLength = 0;
......
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.jaqu; package org.h2.jaqu;
import org.h2.jaqu.TableDefinition.FieldDefinition; import org.h2.jaqu.TableDefinition.FieldDefinition;
import org.h2.jaqu.TableInspector.ColumnInspector; 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.
* <p> * <p>
* Each remark has a level, associated component (schema, table, column, index), * Each remark has a level, associated component (schema, table, column, index),
* and a message. * and a message.
* *
*/ */
public class Validation { public class Validation {
public static Validation CONSIDER(String table, String type, String message) { private int todoReviewWholeClass;
return new Validation(Level.CONSIDER, table, type, message);
} 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 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, 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 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, 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, 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 static Validation ERROR(String table, FieldDefinition field, String message) {
return new Validation(Level.ERROR, table, field, message);
public static enum Level { }
CONSIDER, WARN, ERROR;
} public static enum Level {
CONSIDER, WARN, ERROR;
Level level; }
String table;
String fieldType; Level level;
String fieldName; String table;
String message; String fieldType;
String fieldName;
private Validation(Level level, String table, String type, String message) { String message;
this.level = level;
this.table = table; private Validation(Level level, String table, String type, String message) {
this.fieldType = type; this.level = level;
this.fieldName = ""; this.table = table;
this.message = message; this.fieldType = type;
} this.fieldName = "";
this.message = message;
private Validation(Level level, String table, FieldDefinition field, String message) { }
this.level = level;
this.table = table; private Validation(Level level, String table, FieldDefinition field, String message) {
this.fieldType = field.dataType; this.level = level;
this.fieldName = field.columnName; this.table = table;
this.message = message; this.fieldType = field.dataType;
} this.fieldName = field.columnName;
this.message = message;
private Validation(Level level, String table, ColumnInspector col, String message) { }
this.level = level;
this.table = table; private Validation(Level level, String table, ColumnInspector col, String message) {
this.fieldType = col.type; this.level = level;
this.fieldName = col.name; this.table = table;
this.message = message; this.fieldType = col.type;
} this.fieldName = col.name;
this.message = message;
public Validation throwError(boolean throwOnError) { }
if (throwOnError && isError())
throw new RuntimeException(toString()); public Validation throwError(boolean throwOnError) {
return this; if (throwOnError && isError())
} throw new RuntimeException(toString());
return this;
public boolean isError() { }
return level.equals(Level.ERROR);
} public boolean isError() {
return level.equals(Level.ERROR);
public String toString() { }
StringBuilder sb = new StringBuilder();
sb.append(StringUtils.pad(level.name(), 9, " ", true)); public String toString() {
sb.append(StringUtils.pad(table, 25, " ", true)); StringBuilder sb = new StringBuilder();
sb.append(StringUtils.pad(fieldName, 20, " ", true)); sb.append(StringUtils.pad(level.name(), 9, " ", true));
sb.append(' '); sb.append(StringUtils.pad(table, 25, " ", true));
sb.append(message); sb.append(StringUtils.pad(fieldName, 20, " ", true));
return sb.toString(); sb.append(' ');
} sb.append(message);
return sb.toString();
public String toCSVString() { }
StringBuilder sb = new StringBuilder();
sb.append(level.name()).append(','); public String toCSVString() {
sb.append(table).append(','); StringBuilder sb = new StringBuilder();
sb.append(fieldType).append(','); sb.append(level.name()).append(',');
sb.append(fieldName).append(','); sb.append(table).append(',');
sb.append(message); sb.append(fieldType).append(',');
return sb.toString(); sb.append(fieldName).append(',');
} sb.append(message);
} return sb.toString();
}
}
...@@ -13,6 +13,8 @@ package org.h2.jaqu.util; ...@@ -13,6 +13,8 @@ package org.h2.jaqu.util;
*/ */
public class ClassUtils { public class ClassUtils {
int todoDelete;
private ClassUtils() { private ClassUtils() {
// utility class // utility class
} }
...@@ -31,5 +33,7 @@ public class ClassUtils { ...@@ -31,5 +33,7 @@ public class ClassUtils {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
//## Java 1.5 end ## //## Java 1.5 end ##
} }
...@@ -18,6 +18,8 @@ import java.sql.SQLException; ...@@ -18,6 +18,8 @@ import java.sql.SQLException;
*/ */
public class Message { public class Message {
private int todoDelete;
private Message() { private Message() {
// utility class // utility class
} }
......
...@@ -30,6 +30,8 @@ package org.h2.jaqu.util; ...@@ -30,6 +30,8 @@ package org.h2.jaqu.util;
*/ */
public class StatementBuilder { public class StatementBuilder {
private int todoDelete;
private final StringBuilder builder = new StringBuilder(); private final StringBuilder builder = new StringBuilder();
private int index; private int index;
...@@ -113,11 +115,11 @@ public class StatementBuilder { ...@@ -113,11 +115,11 @@ public class StatementBuilder {
builder.append(s); builder.append(s);
} }
} }
public void append(StatementBuilder sb) { public void append(StatementBuilder sb) {
builder.append(sb); builder.append(sb);
} }
public void insert(int offset, char c) { public void insert(int offset, char c) {
builder.insert(offset, c); builder.insert(offset, c);
} }
......
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.jaqu.util; package org.h2.jaqu.util;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* Utility class to optionally log generated statements to an output stream.<br> * Utility class to optionally log generated statements to an output stream.<br>
* Default output stream is System.out.<br> * Default output stream is System.out.<br>
* Statement logging is disabled by default. * Statement logging is disabled by default.
* <p> * <p>
* This class also tracks the counts for generated statements by major type. * This class also tracks the counts for generated statements by major type.
* *
*/ */
public class StatementLogger { public class StatementLogger {
public static boolean logStatements = false; public static boolean logStatements;
private static PrintWriter out = new PrintWriter(System.out);
public static PrintWriter out = new PrintWriter(System.out); private static final AtomicLong SELECT_COUNT = new AtomicLong();
private static final AtomicLong CREATE_COUNT = new AtomicLong();
public final static AtomicLong selectCount = new AtomicLong(0); private static final AtomicLong INSERT_COUNT = new AtomicLong();
private static final AtomicLong UPDATE_COUNT = new AtomicLong();
public final static AtomicLong createCount = new AtomicLong(0); private static final AtomicLong MERGE_COUNT = new AtomicLong();
private static final AtomicLong DELETE_COUNT = new AtomicLong();
public final static AtomicLong insertCount = new AtomicLong(0);
public static void create(String statement) {
public final static AtomicLong updateCount = new AtomicLong(0); CREATE_COUNT.incrementAndGet();
log(statement);
public final static AtomicLong mergeCount = new AtomicLong(0); }
public final static AtomicLong deleteCount = new AtomicLong(0); public static void insert(String statement) {
INSERT_COUNT.incrementAndGet();
public static void create(String statement) { log(statement);
createCount.incrementAndGet(); }
log(statement);
} public static void update(String statement) {
UPDATE_COUNT.incrementAndGet();
public static void insert(String statement) { log(statement);
insertCount.incrementAndGet(); }
log(statement);
} public static void merge(String statement) {
MERGE_COUNT.incrementAndGet();
public static void update(String statement) { log(statement);
updateCount.incrementAndGet(); }
log(statement);
} public static void delete(String statement) {
DELETE_COUNT.incrementAndGet();
public static void merge(String statement) { log(statement);
mergeCount.incrementAndGet(); }
log(statement);
} public static void select(String statement) {
SELECT_COUNT.incrementAndGet();
public static void delete(String statement) { log(statement);
deleteCount.incrementAndGet(); }
log(statement);
} private static void log(String statement) {
if (logStatements) {
public static void select(String statement) { out.println(statement);
selectCount.incrementAndGet(); }
log(statement); }
}
public static void printStats() {
private static void log(String statement) { out.println("JaQu Runtime Statistics");
if (logStatements) out.println("=======================");
out.println(statement); printStat("CREATE", CREATE_COUNT);
} printStat("INSERT", INSERT_COUNT);
printStat("UPDATE", UPDATE_COUNT);
public static void printStats() { printStat("MERGE", MERGE_COUNT);
out.println("JaQu Runtime Stats"); printStat("DELETE", DELETE_COUNT);
out.println("======================="); printStat("SELECT", SELECT_COUNT);
printStat("CREATE", createCount); }
printStat("INSERT", insertCount);
printStat("UPDATE", updateCount); private static void printStat(String name, AtomicLong value) {
printStat("MERGE", mergeCount); if (value.get() > 0) {
printStat("DELETE", deleteCount); DecimalFormat df = new DecimalFormat("###,###,###,###");
printStat("SELECT", selectCount); out.println(name + "=" + df.format(CREATE_COUNT.get()));
} }
}
private static void printStat(String name, AtomicLong value) {
if (value.get() > 0) {
DecimalFormat df = new DecimalFormat("###,###,###,###");
out.println(name + "=" + df.format(createCount.get()));
}
}
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论