diff --git a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java index 9092caee90..f732efc395 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java @@ -55,6 +55,7 @@ import java.io.StringReader; import java.sql.Connection; import java.sql.Driver; +import java.sql.SQLException; import java.util.List; import java.util.Properties; @@ -237,9 +238,11 @@ public void run(Configuration configuration) throws Exception { org.jooq.util.jaxb.Database d = defaultIfNull(g.getDatabase(), new org.jooq.util.jaxb.Database()); String databaseName = trim(d.getName()); - Class databaseClass = isBlank(databaseName) - ? databaseClass(j) - : (Class) loadClass(databaseName); + Class databaseClass = !isBlank(databaseName) + ? (Class) loadClass(databaseName) + : connection != null + ? databaseClass(connection) + : databaseClass(j); Database database = databaseClass.newInstance(); database.setProperties(properties(d.getProperties())); @@ -442,8 +445,24 @@ private String driverClass(Jdbc j) { } private Class databaseClass(Jdbc j) { - Class result = Databases.databaseClass(JDBCUtils.dialect(j.getUrl())); - log.info("Database", "Inferring database " + result.getName() + " from URL " + j.getUrl()); + return databaseClass(j.getUrl()); + } + + private Class databaseClass(Connection c) { + try { + return databaseClass(c.getMetaData().getURL()); + } + catch (SQLException e) { + throw new RuntimeException(e); + } + } + + private Class databaseClass(String url) { + if (isBlank(url)) + throw new RuntimeException("No JDBC URL configured."); + + Class result = Databases.databaseClass(JDBCUtils.dialect(url)); + log.info("Database", "Inferring database " + result.getName() + " from URL " + url); return result; } diff --git a/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java index 7c151de90e..073ad031b2 100644 --- a/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java @@ -43,6 +43,7 @@ // ... import static org.jooq.SQLDialect.CUBRID; // ... +import static org.jooq.SQLDialect.DEFAULT; import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.FIREBIRD; import static org.jooq.SQLDialect.H2; @@ -94,20 +95,22 @@ public class JDBCUtils { public static final SQLDialect dialect(Connection connection) { SQLDialect result = SQLDialect.DEFAULT; - try { - DatabaseMetaData m = connection.getMetaData(); + if (connection != null) { + try { + DatabaseMetaData m = connection.getMetaData(); - /* [pro] xx - xxxxxx xxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxx - xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x - xxxxxx xxxxxxxxxxxxxxxxxx - x - xx [/pro] */ + /* [pro] xx + xxxxxx xxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxx + xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x + xxxxxx xxxxxxxxxxxxxxxxxx + x + xx [/pro] */ - String url = m.getURL(); - result = dialect(url); + String url = m.getURL(); + result = dialect(url); + } + catch (SQLException ignore) {} } - catch (SQLException ignore) {} if (result == SQLDialect.DEFAULT) { // If the dialect cannot be guessed from the URL, take some other @@ -121,6 +124,8 @@ public static final SQLDialect dialect(Connection connection) { * "Guess" the {@link SQLDialect} from a connection URL. */ public static final SQLDialect dialect(String url) { + if (url == null) + return DEFAULT; // The below list might not be accurate or complete. Feel free to // contribute fixes related to new / different JDBC driver configuraitons @@ -185,7 +190,7 @@ else if (url.startsWith("jdbc:sqlite:")) { x xx [/pro] */ - return SQLDialect.DEFAULT; + return DEFAULT; } /**