[#2446] Add JDBCUtils.dialect(Connection) to "guess" the jOOQ SQLDialect
from a JDBC Connection
This commit is contained in:
parent
ce52beb115
commit
a1f558a69b
95
jOOQ-test/src/org/jooq/test/_/testcases/JDBCTests.java
Normal file
95
jOOQ-test/src/org/jooq/test/_/testcases/JDBCTests.java
Normal file
@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._.testcases;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
|
||||
import org.jooq.ConnectionProvider;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record2;
|
||||
import org.jooq.Record3;
|
||||
import org.jooq.Record6;
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.test.BaseTest;
|
||||
import org.jooq.test.jOOQAbstractTest;
|
||||
import org.jooq.tools.jdbc.JDBCUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JDBCTests<
|
||||
A extends UpdatableRecord<A> & Record6<Integer, String, String, Date, Integer, ?>,
|
||||
AP,
|
||||
B extends UpdatableRecord<B>,
|
||||
S extends UpdatableRecord<S> & Record1<String>,
|
||||
B2S extends UpdatableRecord<B2S> & Record3<String, Integer, Integer>,
|
||||
BS extends UpdatableRecord<BS>,
|
||||
L extends TableRecord<L> & Record2<String, String>,
|
||||
X extends TableRecord<X>,
|
||||
DATE extends UpdatableRecord<DATE>,
|
||||
BOOL extends UpdatableRecord<BOOL>,
|
||||
D extends UpdatableRecord<D>,
|
||||
T extends UpdatableRecord<T>,
|
||||
U extends TableRecord<U>,
|
||||
UU extends UpdatableRecord<UU>,
|
||||
I extends TableRecord<I>,
|
||||
IPK extends UpdatableRecord<IPK>,
|
||||
T725 extends UpdatableRecord<T725>,
|
||||
T639 extends UpdatableRecord<T639>,
|
||||
T785 extends TableRecord<T785>>
|
||||
extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785> {
|
||||
|
||||
public JDBCTests(jOOQAbstractTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785> delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDialectGuessing() throws Exception {
|
||||
ConnectionProvider cp = create().configuration().connectionProvider();
|
||||
Connection c = cp.acquire();
|
||||
|
||||
try {
|
||||
assertEquals(create().configuration().dialect(), JDBCUtils.dialect(getConnection()));
|
||||
assertEquals(create().configuration().dialect(), JDBCUtils.dialect(c));
|
||||
}
|
||||
finally {
|
||||
cp.release(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -119,6 +119,7 @@ import org.jooq.test._.testcases.FunctionTests;
|
||||
import org.jooq.test._.testcases.GeneralTests;
|
||||
import org.jooq.test._.testcases.GroupByTests;
|
||||
import org.jooq.test._.testcases.InsertUpdateTests;
|
||||
import org.jooq.test._.testcases.JDBCTests;
|
||||
import org.jooq.test._.testcases.JoinTests;
|
||||
import org.jooq.test._.testcases.LoaderTests;
|
||||
import org.jooq.test._.testcases.MetaDataTests;
|
||||
@ -2208,4 +2209,9 @@ public abstract class jOOQAbstractTest<
|
||||
public void testCancelStatement() throws Exception {
|
||||
new StatementTests(this).testCancelStatement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDialectGuessing() throws Exception {
|
||||
new JDBCTests(this).testDialectGuessing();
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,15 +35,34 @@
|
||||
*/
|
||||
package org.jooq.tools.jdbc;
|
||||
|
||||
import static org.jooq.SQLDialect.ASE;
|
||||
import static org.jooq.SQLDialect.CUBRID;
|
||||
import static org.jooq.SQLDialect.DB2;
|
||||
import static org.jooq.SQLDialect.DERBY;
|
||||
import static org.jooq.SQLDialect.FIREBIRD;
|
||||
import static org.jooq.SQLDialect.H2;
|
||||
import static org.jooq.SQLDialect.HSQLDB;
|
||||
import static org.jooq.SQLDialect.INGRES;
|
||||
import static org.jooq.SQLDialect.MYSQL;
|
||||
import static org.jooq.SQLDialect.ORACLE;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLITE;
|
||||
import static org.jooq.SQLDialect.SQLSERVER;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
|
||||
import java.sql.Blob;
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Clob;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLInput;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.jooq.SQLDialect;
|
||||
|
||||
/**
|
||||
* JDBC-related utility methods
|
||||
*
|
||||
@ -51,6 +70,94 @@ import java.sql.Statement;
|
||||
*/
|
||||
public class JDBCUtils {
|
||||
|
||||
/**
|
||||
* "Guess" the {@link SQLDialect} from a {@link Connection} instance.
|
||||
* <p>
|
||||
* This method tries to guess the <code>SQLDialect</code> of a connection
|
||||
* from the its connection URL as obtained by
|
||||
* {@link DatabaseMetaData#getURL()}. If the dialect cannot be guessed from
|
||||
* the URL (e.g. when using an JDBC-ODBC bridge), further actions may be
|
||||
* implemented in the future.
|
||||
*
|
||||
* @see #dialect(String)
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static final SQLDialect dialect(Connection connection) {
|
||||
SQLDialect result = SQLDialect.SQL99;
|
||||
|
||||
try {
|
||||
String url = connection.getMetaData().getURL();
|
||||
result = dialect(url);
|
||||
}
|
||||
catch (SQLException ignore) {}
|
||||
|
||||
if (result == SQLDialect.SQL99) {
|
||||
// If the dialect cannot be guessed from the URL, take some other
|
||||
// measures, e.g. by querying DatabaseMetaData.getDatabaseProductName()
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* "Guess" the {@link SQLDialect} from a connection URL.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static final SQLDialect dialect(String url) {
|
||||
|
||||
// The below list might not be accurate or complete. Feel free to
|
||||
// contribute fixes related to new / different JDBC driver configuraitons
|
||||
if (url.startsWith("jdbc:jtds:sybase:")) {
|
||||
return ASE;
|
||||
}
|
||||
else if (url.startsWith("jdbc:cubrid:")) {
|
||||
return CUBRID;
|
||||
}
|
||||
else if (url.startsWith("jdbc:db2:")) {
|
||||
return DB2;
|
||||
}
|
||||
else if (url.startsWith("jdbc:derby:")) {
|
||||
return DERBY;
|
||||
}
|
||||
else if (url.startsWith("jdbc:firebirdsql:")) {
|
||||
return FIREBIRD;
|
||||
}
|
||||
else if (url.startsWith("jdbc:h2:")) {
|
||||
return H2;
|
||||
}
|
||||
else if (url.startsWith("jdbc:hsqldb:")) {
|
||||
return HSQLDB;
|
||||
}
|
||||
else if (url.startsWith("jdbc:ingres:")) {
|
||||
return INGRES;
|
||||
}
|
||||
else if (url.startsWith("jdbc:mysql:")
|
||||
|| url.startsWith("jdbc:google:")) {
|
||||
return MYSQL;
|
||||
}
|
||||
else if (url.startsWith("jdbc:oracle:")
|
||||
|| url.startsWith("jdbc:oracle:oci")) {
|
||||
return ORACLE;
|
||||
}
|
||||
else if (url.startsWith("jdbc:postgresql:")) {
|
||||
return POSTGRES;
|
||||
}
|
||||
else if (url.startsWith("jdbc:sqlite:")) {
|
||||
return SQLITE;
|
||||
}
|
||||
else if (url.startsWith("jdbc:sqlserver:")
|
||||
|| url.startsWith("jdbc:jtds:sqlserver:")
|
||||
|| url.startsWith("jdbc:microsoft:sqlserver:")
|
||||
|| url.contains(":mssql")) {
|
||||
return SQLSERVER;
|
||||
}
|
||||
else if (url.startsWith("jdbc:sybase:")) {
|
||||
return SYBASE;
|
||||
}
|
||||
|
||||
return SQLDialect.SQL99;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely close a statement
|
||||
* <p>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user