Skip to content

Commit

Permalink
[#3745] NullPointerException when <jdbc/> is not provided and
Browse files Browse the repository at this point in the history
<database/> <name/> isn't provided either
  • Loading branch information
lukaseder committed Jan 5, 2015
1 parent 6c845e9 commit fad02d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
29 changes: 24 additions & 5 deletions jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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<? extends Database> databaseClass = isBlank(databaseName)
? databaseClass(j)
: (Class<? extends Database>) loadClass(databaseName);
Class<? extends Database> databaseClass = !isBlank(databaseName)
? (Class<? extends Database>) loadClass(databaseName)
: connection != null
? databaseClass(connection)
: databaseClass(j);
Database database = databaseClass.newInstance();
database.setProperties(properties(d.getProperties()));

Expand Down Expand Up @@ -442,8 +445,24 @@ private String driverClass(Jdbc j) {
}

private Class<? extends Database> databaseClass(Jdbc j) {
Class<? extends Database> result = Databases.databaseClass(JDBCUtils.dialect(j.getUrl()));
log.info("Database", "Inferring database " + result.getName() + " from URL " + j.getUrl());
return databaseClass(j.getUrl());
}

private Class<? extends Database> databaseClass(Connection c) {
try {
return databaseClass(c.getMetaData().getURL());
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}

private Class<? extends Database> databaseClass(String url) {
if (isBlank(url))
throw new RuntimeException("No JDBC URL configured.");

Class<? extends Database> result = Databases.databaseClass(JDBCUtils.dialect(url));
log.info("Database", "Inferring database " + result.getName() + " from URL " + url);
return result;
}

Expand Down
29 changes: 17 additions & 12 deletions jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -185,7 +190,7 @@ else if (url.startsWith("jdbc:sqlite:")) {
x
xx [/pro] */

return SQLDialect.DEFAULT;
return DEFAULT;
}

/**
Expand Down

0 comments on commit fad02d2

Please sign in to comment.