提交 e857a250 authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Let it compile with JDK 5
上级 dd7cdf0d
......@@ -14,6 +14,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
......@@ -27,6 +28,7 @@ import org.h2.jaqu.util.JdbcUtils;
import org.h2.jaqu.util.StringUtils;
import org.h2.jaqu.util.Utils;
import org.h2.jaqu.util.WeakIdentityHashMap;
//## Java 1.5 end ##
/**
* This class represents a connection to a database.
......@@ -48,14 +50,17 @@ public class Db {
Utils.newHashMap();
private final SQLDialect dialect;
private DbUpgrader dbUpgrader = new DefaultDbUpgrader();
private final Set<Class<?>> upgradeChecked = Utils.newConcurrentHashSet();
private final Set<Class<?>> upgradeChecked = Collections.synchronizedSet(new HashSet<Class<?>>());
private int todoDocumentNewFeaturesInHtmlFile;
public Db(Connection conn) {
this.conn = conn;
dialect = getDialect(conn.getClass().getCanonicalName());
}
SQLDialect getDialect(String clazz) {
int todo;
// TODO add special cases here
return new DefaultSQLDialect();
}
......@@ -119,6 +124,7 @@ public class Db {
public <T> void insert(T t) {
Class<?> clazz = t.getClass();
int upgradeDbSoundsWrongHere;
upgradeDb().define(clazz).createTableIfRequired(this).insert(this, t, false);
}
......@@ -141,13 +147,13 @@ public class Db {
Class<?> clazz = t.getClass();
upgradeDb().define(clazz).createTableIfRequired(this).delete(this, t);
}
public <T extends Object> Query<T> from(T alias) {
Class<?> clazz = alias.getClass();
upgradeDb().define(clazz).createTableIfRequired(this);
return Query.from(this, alias);
}
Db upgradeDb() {
if (!upgradeChecked.contains(dbUpgrader.getClass())) {
// Flag as checked immediately because calls are nested.
......@@ -156,7 +162,7 @@ public class Db {
JQDatabase model = dbUpgrader.getClass().getAnnotation(JQDatabase.class);
if (model.version() > 0) {
DbVersion v = new DbVersion();
DbVersion dbVersion =
DbVersion dbVersion =
// (SCHEMA="" && TABLE="") == DATABASE
from(v).where(v.schema).is("").and(v.table).is("").selectFirst();
if (dbVersion == null) {
......@@ -167,7 +173,7 @@ public class Db {
} else {
// Database has a version registration,
// check to see if upgrade is required.
if ((model.version() > dbVersion.version)
if ((model.version() > dbVersion.version)
&& (dbUpgrader != null)) {
// Database is an older version than model.
boolean success = dbUpgrader.upgradeDatabase(this,
......@@ -182,7 +188,7 @@ public class Db {
}
return this;
}
<T> void upgradeTable(TableDefinition<T> model) {
if (!upgradeChecked.contains(model.getModelClass())) {
// Flag as checked immediately because calls are nested.
......@@ -192,7 +198,7 @@ public class Db {
// Table is using JaQu version tracking.
DbVersion v = new DbVersion();
String schema = StringUtils.isNullOrEmpty(model.schemaName) ? "" : model.schemaName;
DbVersion dbVersion =
DbVersion dbVersion =
from(v).where(v.schema).like(schema).and(v.table)
.like(model.tableName).selectFirst();
if (dbVersion == null) {
......@@ -205,7 +211,7 @@ public class Db {
} else {
// Table has a version registration.
// Check to see if upgrade is required.
if ((model.tableVersion > dbVersion.version)
if ((model.tableVersion > dbVersion.version)
&& (dbUpgrader != null)) {
// Table is an older version than model.
boolean success = dbUpgrader.upgradeTable(this, schema,
......@@ -233,12 +239,12 @@ public class Db {
} else if (clazz.isAnnotationPresent(JQTable.class)) {
// Annotated Class skips Define().define() static initializer
T t = instance(clazz);
def.mapObject(t);
def.mapObject(t);
}
}
return def;
}
public synchronized void setDbUpgrader(DbUpgrader upgrader) {
if (upgrader == null)
throw new RuntimeException("DbUpgrader may not be NULL!");
......@@ -248,11 +254,11 @@ public class Db {
this.dbUpgrader = upgrader;
upgradeChecked.clear();
}
SQLDialect getDialect() {
return dialect;
}
public Connection getConnection() {
return conn;
}
......@@ -295,10 +301,11 @@ public class Db {
}
}
PreparedStatement prepare(String sql, boolean returnKey) {
PreparedStatement prepare(String sql, boolean returnGeneratedKeys) {
try {
if (returnKey)
return conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
if (returnGeneratedKeys) {
return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
return conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
......
......@@ -21,10 +21,10 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.h2.jaqu.Db;
import org.h2.jaqu.DbInspector;
import org.h2.message.DbException;
/**
* Generates JaQu models.
*
*/
public class GenerateModels {
......@@ -32,7 +32,7 @@ public class GenerateModels {
* The output stream where this tool writes to.
*/
protected PrintStream out = System.out;
public static void main(String... args) throws SQLException {
new GenerateModels().runTool(args);
}
......@@ -82,13 +82,13 @@ public class GenerateModels {
if (url == null) {
throw new SQLException("URL not set");
}
execute(url, user, password, schema, table, packageName, folder,
execute(url, user, password, schema, table, packageName, folder,
annotateSchema, trimStrings);
}
/**
* Generates models from the database.
*
*
* @param url the database URL
* @param user the user name
* @param password the password
......@@ -99,8 +99,8 @@ public class GenerateModels {
* @param annotateSchema includes the schema in the table model annotations
* @param trimStrings automatically trim strings that exceed maxLength
*/
public static void execute(String url, String user, String password,
String schema, String table, String packageName, String folder,
public static void execute(String url, String user, String password,
String schema, String table, String packageName, String folder,
boolean annotateSchema, boolean trimStrings)
throws SQLException {
Connection conn = null;
......@@ -109,7 +109,7 @@ public class GenerateModels {
conn = DriverManager.getConnection(url, user, password);
Db db = Db.open(url, user, password.toCharArray());
DbInspector inspector = new DbInspector(db);
List<String> models = inspector.generateModel(schema, table,
List<String> models = inspector.generateModel(schema, table,
packageName, annotateSchema, trimStrings);
File parentFile;
if (StringUtils.isNullOrEmpty(folder))
......@@ -130,15 +130,13 @@ public class GenerateModels {
System.out.println("Generated " + classFile.getAbsolutePath());
}
}
} catch (SQLException s) {
throw s;
} catch (IOException i) {
throw new SQLException(i);
} catch (IOException io) {
throw DbException.convertIOException(io, "could not generate model").getSQLException();
} finally {
JdbcUtils.closeSilently(conn);
}
}
/**
* Throw a SQLException saying this command line option is not supported.
*
......@@ -149,7 +147,7 @@ public class GenerateModels {
showUsage();
throw new SQLException("Unsupported option: " + option);
}
protected void showUsage() {
out.println("GenerateModels");
out.println("Usage: java "+getClass().getName());
......
......@@ -22,8 +22,6 @@ import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
//## Java 1.5 end ##
......@@ -55,10 +53,6 @@ public class Utils {
return new HashSet<T>(list);
}
public static <T> Set<T> newConcurrentHashSet() {
return Collections.newSetFromMap(new ConcurrentHashMap<T, Boolean>());
}
public static <A, B> HashMap<A, B> newHashMap() {
return new HashMap<A, B>();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论