All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bendb.dropwizard.jooq.JooqFactory Maven / Gradle / Ivy

There is a newer version: 3.0.0-0
Show newest version
package com.bendb.dropwizard.jooq;

import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Optional;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.ManagedDataSource;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.setup.Environment;
import org.jooq.Configuration;
import org.jooq.ConnectionProvider;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.conf.*;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.jooq.tools.jdbc.JDBCUtils;

import javax.validation.constraints.NotNull;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * A factory for jOOQ {@link org.jooq.Configuration} objects.
 * 

* Configuration Parameters *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
NameDefaultDescription
dialect{@code null} * Inferred from the JDBC url if absent. If present, any name * from the {@link org.jooq.SQLDialect} enumeration. *
logExecutedSql{@code false}Whether to log generated SQL statements as they are executed.
renderSchema{@code true}Whether any schema is rendered at all; disable for single-schema environments.
renderNameStyleQUOTED * Whether rendered schema, table, column, etc. names should be quoted in rendered SQL, * or transformed in any way. Valid values are QUOTED, UPPER, LOWER, and AS_IS. *
renderKeywordStyleUPPERWhether SQL keywords should be rendered in upper or lower case.
renderFormatted{@code false}Whether generated SQL should be pretty-printed.
paramTypeINDEXED * How rendered bind values should be rendered. Valid values are: *
    *
  • INDEXED
  • *
  • NAMED
  • *
  • INLINED
  • *
*
statementTypePREPARED_STATEMENT * Whether to use prepared statements with bind values, or static SQL. * Valid values are PREPARED_STATEMENT and STATIC_STATEMENT. *
executeLogging{@code false}Whether to enable jOOQ's built-in logging.
executeWithOptimisticLocking{@code false} * Whether {@code store()} and {@code delete()} should be executed * with optimistic locking. *
attachRecords{@code true}Whether fetched records should be attached to the fetching configuration.
updatablePrimaryKeys{@code false} * Whether primary-key values are deemed "updatable" in jOOQ. * Setting this to {@code true} will allow for updating primary keys * through {@link org.jooq.UpdatableRecord#store()} and {@link org.jooq.UpdatableRecord#update()}. *
*/ public class JooqFactory { private static final String DEFAULT_NAME = "jooq"; @NotNull private Optional dialect = Optional.absent(); private boolean logExecutedSql = false; private boolean renderSchema = true; @NotNull private RenderNameStyle renderNameStyle = RenderNameStyle.QUOTED; @NotNull private RenderKeywordStyle renderKeywordStyle = RenderKeywordStyle.UPPER; private boolean renderFormatted = false; @NotNull private ParamType paramType = ParamType.INDEXED; @NotNull private StatementType statementType = StatementType.PREPARED_STATEMENT; private boolean executeLogging = false; private boolean executeWithOptimisticLocking = false; private boolean attachRecords = true; private boolean updatablePrimaryKeys = false; private boolean fetchWarnings = true; public Optional getDialect() { return dialect; } public void setDialect(Optional dialect) { this.dialect = dialect; } public boolean isLogExecutedSql() { return logExecutedSql; } public void setLogExecutedSql(boolean logExecutedSql) { this.logExecutedSql = logExecutedSql; } public boolean isRenderSchema() { return renderSchema; } public void setRenderSchema(boolean renderSchema) { this.renderSchema = renderSchema; } public RenderNameStyle getRenderNameStyle() { return renderNameStyle; } public void setRenderNameStyle(RenderNameStyle renderNameStyle) { this.renderNameStyle = renderNameStyle; } public RenderKeywordStyle getRenderKeywordStyle() { return renderKeywordStyle; } public void setRenderKeywordStyle(RenderKeywordStyle renderKeywordStyle) { this.renderKeywordStyle = renderKeywordStyle; } public boolean isRenderFormatted() { return renderFormatted; } public void setRenderFormatted(boolean renderFormatted) { this.renderFormatted = renderFormatted; } public ParamType getParamType() { return paramType; } public void setParamType(ParamType paramType) { this.paramType = paramType; } public StatementType getStatementType() { return statementType; } public void setStatementType(StatementType statementType) { this.statementType = statementType; } public boolean isExecuteLogging() { return executeLogging; } public void setExecuteLogging(boolean executeLogging) { this.executeLogging = executeLogging; } public boolean isExecuteWithOptimisticLocking() { return executeWithOptimisticLocking; } public void setExecuteWithOptimisticLocking(boolean executeWithOptimisticLocking) { this.executeWithOptimisticLocking = executeWithOptimisticLocking; } public boolean isAttachRecords() { return attachRecords; } public void setAttachRecords(boolean attachRecords) { this.attachRecords = attachRecords; } public boolean isUpdatablePrimaryKeys() { return updatablePrimaryKeys; } public void setUpdatablePrimaryKeys(boolean updatablePrimaryKeys) { this.updatablePrimaryKeys = updatablePrimaryKeys; } public boolean isFetchWarnings() { return fetchWarnings; } public void setFetchWarnings(boolean fetchWarnings) { this.fetchWarnings = fetchWarnings; } public Configuration build(Environment environment, DataSourceFactory factory) throws ClassNotFoundException { return build(environment, factory, DEFAULT_NAME); } public Configuration build(Environment environment, PooledDataSourceFactory factory, String name) throws ClassNotFoundException { final Settings settings = buildSettings(); final ManagedDataSource dataSource = factory.build(environment.metrics(), name); final SQLDialect dialect = determineDialect(factory, dataSource); final ConnectionProvider connectionProvider = new DataSourceConnectionProvider(dataSource); final Configuration config = new DefaultConfiguration(); config.set(settings); config.set(dialect); config.set(connectionProvider); if (logExecutedSql && !executeLogging) { final Settings loggerSettings = (Settings) settings.clone(); loggerSettings.setRenderFormatted(true); final DSLContext loggerContext = DSL.using(dialect, loggerSettings); final LoggingExecutionListener listener = new LoggingExecutionListener(loggerContext); config.set(new DefaultExecuteListenerProvider(listener)); } environment.lifecycle().manage(dataSource); return config; } private SQLDialect determineDialect(PooledDataSourceFactory dataSourceFactory, ManagedDataSource dataSource) { // If a dialect was specified, great! if (getDialect().isPresent()) { return dialect.get(); } // TODO: When DW >= 0.9.2 ships, revert this // return JDBCUtils.dialect(dataSourceFactory.getUrl()); // We need to use the JDBC url to determine which SQL dialect to use. // PooledDataSourceFactory doesn't expose the connection's url, but // the sole implementation of this interface does - use that. if (dataSourceFactory instanceof DataSourceFactory) { String url = ((DataSourceFactory) dataSourceFactory).getUrl(); return JDBCUtils.dialect(url); } // If we have a custom implementation, no choice but to establish a // connection and get the url that way. try (Connection connection = dataSource.getConnection()) { DatabaseMetaData metaData = connection.getMetaData(); String url = metaData.getURL(); return JDBCUtils.dialect(url); } catch (SQLException e) { // If all else fails - fail. throw new IllegalStateException( "Could not determine which SQL dialect to use - please configure 'jooq.dialect'!", e); } } private Settings buildSettings() { final Settings settings = new Settings(); settings.setRenderSchema(renderSchema); settings.setRenderNameStyle(renderNameStyle); settings.setRenderKeywordStyle(renderKeywordStyle); settings.setRenderFormatted(renderFormatted); settings.setParamType(paramType); settings.setStatementType(statementType); settings.setExecuteLogging(executeLogging); settings.setExecuteWithOptimisticLocking(executeWithOptimisticLocking); settings.setAttachRecords(attachRecords); settings.setUpdatablePrimaryKeys(updatablePrimaryKeys); settings.setFetchWarnings(fetchWarnings); return settings; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy