[#3346] Allow for omitting the JDBC driver property in the code generator

This commit is contained in:
Lukas Eder 2014-06-23 15:15:28 +02:00
parent 2b137aebfb
commit 1027d8734a
4 changed files with 66 additions and 1 deletions

View File

@ -191,7 +191,7 @@ public class GenerationTool {
// ---------------------
if (connection == null) {
errorIfNull(j, "The <jdbc/> tag is mandatory.");
loadClass(j.getDriver());
loadClass(driverClass(j));
Properties properties = new Properties();
for (Property p : j.getProperties()) {
@ -384,6 +384,17 @@ public class GenerationTool {
}
}
private String driverClass(Jdbc j) {
String result = j.getDriver();
if (result == null) {
result = JDBCUtils.driver(j.getUrl());
log.info("Database", "Inferring driver " + result + " from URL " + j.getUrl());
}
return result;
}
private Class<? extends Database> databaseClass(Jdbc j) {
SQLDialect dialect = JDBCUtils.dialect(j.getUrl());
Class<? extends Database> result = JDBCDatabase.class;

View File

@ -101,4 +101,8 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
cp.release(c);
}
}
public void testDriverGuessing() throws Exception {
Class.forName(JDBCUtils.driver(getConnection().getMetaData().getURL()));
}
}

View File

@ -2967,6 +2967,11 @@ public abstract class jOOQAbstractTest<
new JDBCTests(this).testDialectGuessing();
}
@Test
public void testDriverGuessing() throws Exception {
new JDBCTests(this).testDriverGuessing();
}
@Test
public void testTransactionsWithJDBCSimple() throws Exception {
new TransactionTests(this).testTransactionsWithJDBCSimple();

View File

@ -179,6 +179,51 @@ public class JDBCUtils {
return SQLDialect.SQL99;
}
/**
* "Guess" the JDBC driver from a connection URL.
*/
public static final String driver(String url) {
switch (dialect(url).family()) {
case CUBRID:
return "cubrid.jdbc.driver.CUBRIDDriver";
case DERBY:
return "org.apache.derby.jdbc.ClientDriver";
case FIREBIRD:
return "org.firebirdsql.jdbc.FBDriver";
case H2:
return "org.h2.Driver";
case HSQLDB:
return "org.hsqldb.jdbcDriver";
case MARIADB:
return "org.mariadb.jdbc.Driver";
case MYSQL:
return "com.mysql.jdbc.Driver";
case POSTGRES:
return "org.postgresql.Driver";
case SQLITE:
return "org.sqlite.JDBC";
/* [pro] xx
xxxx xxxxxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxxxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxxxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxxxxxxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxxxxx
xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx [/pro] */
}
return "java.sql.Driver";
}
/**
* Safely close a connection.
* <p>