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

Cleanup:

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