Search in sources :

Example 11 with Settings

use of org.jooq.conf.Settings in project OpenAttestation by OpenAttestation.

the class TagJdbi method jooq.

/**
     * CODE QUALITY: All usage of this method should be in the following form:
     * <pre>
     * try(JooqContainer jc = TagJdbi.jooq()) {
     * DSLContext jooq = jc.getDslContext();
     * // code
     * }
     * </pre>
     *
     * This ensures the jooq database connection is automatically released
     * at the end of the block (either closed or returned to the pool)
     * 
     * @return
     * @throws SQLException
     * @throws IOException
     */
public static JooqContainer jooq() throws SQLException, IOException {
    // omits the schema name from generated sql ; when we connect to the database we already specify a schema so this settings avoid 
    // redundancy in the sql and allows the administrator to change the database name without breaking the application
    Settings settings = new Settings().withRenderSchema(false).withRenderNameStyle(RenderNameStyle.LOWER);
    SQLDialect dbDialect = getSqlDialect();
    // throws SQLException; Note that the DSLContext doesn't close the connection. We'll have to do that ourselves.
    Connection connection = TagJdbi.getDataSource().getConnection();
    DSLContext jooq = DSL.using(connection, dbDialect, settings);
    return new JooqContainer(jooq, connection);
}
Also used : JooqContainer(com.intel.mtwilson.jooq.util.JooqContainer) SQLDialect(org.jooq.SQLDialect) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) Settings(org.jooq.conf.Settings)

Example 12 with Settings

use of org.jooq.conf.Settings in project steve by RWTH-i5-IDSG.

the class BeanConfiguration method dslContext.

/**
 * Can we re-use DSLContext as a Spring bean (singleton)? Yes, the Spring tutorial of
 * Jooq also does it that way, but only if we do not change anything about the
 * config after the init (which we don't do anyways) and if the ConnectionProvider
 * does not store any shared state (we use DataSourceConnectionProvider of Jooq, so no problem).
 *
 * Some sources and discussion:
 * - http://www.jooq.org/doc/3.6/manual/getting-started/tutorials/jooq-with-spring/
 * - http://jooq-user.narkive.com/2fvuLodn/dslcontext-and-threads
 * - https://groups.google.com/forum/#!topic/jooq-user/VK7KQcjj3Co
 * - http://stackoverflow.com/questions/32848865/jooq-dslcontext-correct-autowiring-with-spring
 */
@Bean
public DSLContext dslContext() {
    initDataSource();
    Settings settings = new Settings().withAttachRecords(false).withExecuteLogging(CONFIG.getDb().isSqlLogging());
    // Configuration for JOOQ
    org.jooq.Configuration conf = new DefaultConfiguration().set(SQLDialect.MYSQL).set(new DataSourceConnectionProvider(dataSource)).set(settings);
    return DSL.using(conf);
}
Also used : DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Settings(org.jooq.conf.Settings) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 13 with Settings

use of org.jooq.conf.Settings in project waltz by khartec.

the class DIBaseConfiguration method dsl.

@Bean
@Autowired
public DSLContext dsl(DataSource dataSource) {
    try {
        SQLDialect.valueOf(dialect);
    } catch (IllegalArgumentException iae) {
        System.err.println("Cannot parse sql dialect: " + dialect);
        throw iae;
    }
    // TODO: remove sql server setting, see #4553
    Settings dslSettings = new Settings().withRenderOutputForSQLServerReturningClause(false);
    if ("true".equals(System.getProperty(JOOQ_DEBUG_PROPERTY))) {
        dslSettings.withRenderFormatted(true).withExecuteLogging(true);
    }
    org.jooq.Configuration configuration = new DefaultConfiguration().set(dataSource).set(SQLDialect.valueOf(dialect)).set(dslSettings).set(new SlowQueryListener(databasePerformanceQuerySlowThreshold)).set(new SpringExceptionTranslationExecuteListener(new SQLStateSQLExceptionTranslator()));
    return DSL.using(configuration);
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) Settings(org.jooq.conf.Settings) Autowired(org.springframework.beans.factory.annotation.Autowired) Bean(org.springframework.context.annotation.Bean)

Example 14 with Settings

use of org.jooq.conf.Settings in project waltz by khartec.

the class SlowQueryListener method executeEnd.

@Override
public void executeEnd(ExecuteContext ctx) {
    super.executeEnd(ctx);
    long split = stopWatch.split();
    if (split > slowQueryThresholdInNanos) {
        DSLContext context = DSL.using(ctx.dialect(), // ... and the flag for pretty-printing
        new Settings().withRenderFormatted(true));
        LOG.info(String.format("Slow SQL executed in %d seconds", TimeUnit.NANOSECONDS.toSeconds(split)), new SQLPerformanceWarning(context.renderInlined(ctx.query())));
    }
}
Also used : DSLContext(org.jooq.DSLContext) Settings(org.jooq.conf.Settings)

Example 15 with Settings

use of org.jooq.conf.Settings in project jOOQ by jOOQ.

the class DDLDatabase method export.

@Override
protected void export() throws Exception {
    Settings defaultSettings = new Settings();
    String scripts = getProperties().getProperty("scripts");
    String encoding = getProperties().getProperty("encoding", "UTF-8");
    String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
    final String defaultNameCase = getProperties().getProperty("defaultNameCase", "as_is").toUpperCase();
    boolean parseIgnoreComments = !"false".equalsIgnoreCase(getProperties().getProperty("parseIgnoreComments"));
    String parseIgnoreCommentStart = getProperties().getProperty("parseIgnoreCommentStart", defaultSettings.getParseIgnoreCommentStart());
    String parseIgnoreCommentStop = getProperties().getProperty("parseIgnoreCommentStop", defaultSettings.getParseIgnoreCommentStop());
    logExecutedQueries = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutedQueries"));
    logExecutionResults = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutionResults"));
    if (isBlank(scripts)) {
        scripts = "";
        log.warn("No scripts defined", "It is recommended that you provide an explicit script directory to scan");
    }
    try {
        final DSLContext ctx = DSL.using(connection(), new Settings().withParseIgnoreComments(parseIgnoreComments).withParseIgnoreCommentStart(parseIgnoreCommentStart).withParseIgnoreCommentStop(parseIgnoreCommentStop).withParseUnknownFunctions(ParseUnknownFunctions.IGNORE));
        // [#7771] [#8011] Ignore all parsed storage clauses when executing the statements
        ctx.data("org.jooq.ddl.ignore-storage-clauses", true);
        // [#8910] Parse things a bit differently for use with the DDLDatabase
        ctx.data("org.jooq.ddl.parse-for-ddldatabase", true);
        if (!"AS_IS".equals(defaultNameCase)) {
            ctx.configuration().set(new DefaultVisitListener() {

                @Override
                public void visitStart(VisitContext vc) {
                    if (vc.queryPart() instanceof Name) {
                        Name n = (Name) vc.queryPart();
                        Name[] parts = n.parts();
                        boolean changed = false;
                        for (int i = 0; i < parts.length; i++) {
                            // flag for DSL.systemName() names
                            if (parts[i].quoted() == Quoted.UNQUOTED) {
                                parts[i] = DSL.quotedName("UPPER".equals(defaultNameCase) ? parts[i].first().toUpperCase(renderLocale(ctx.settings())) : parts[i].first().toLowerCase(renderLocale(ctx.settings())));
                                changed = true;
                            }
                        }
                        if (changed)
                            vc.queryPart(DSL.name(parts));
                    }
                }
            });
        }
        new FilePattern().encoding(encoding).basedir(new File(getBasedir())).pattern(scripts).sort(Sort.of(sort)).load(source -> DDLDatabase.this.load(ctx, source));
    } catch (ParserException e) {
        log.error("An exception occurred while parsing script source : " + scripts + ". Please report this error to https://github.com/jOOQ/jOOQ/issues/new", e);
        throw e;
    }
}
Also used : ParserException(org.jooq.impl.ParserException) VisitContext(org.jooq.VisitContext) DSLContext(org.jooq.DSLContext) FilePattern(org.jooq.FilePattern) DefaultVisitListener(org.jooq.impl.DefaultVisitListener) File(java.io.File) Settings(org.jooq.conf.Settings) Name(org.jooq.Name)

Aggregations

Settings (org.jooq.conf.Settings)22 DSLContext (org.jooq.DSLContext)10 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)7 Connection (java.sql.Connection)5 List (java.util.List)5 Configuration (org.jooq.Configuration)5 SQLDialect (org.jooq.SQLDialect)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MappedSchema (org.jooq.conf.MappedSchema)2 MappedTable (org.jooq.conf.MappedTable)2 RenderMapping (org.jooq.conf.RenderMapping)2 DataAccessException (org.jooq.exception.DataAccessException)2 DSL (org.jooq.impl.DSL)2 DataSourceConnectionProvider (org.jooq.impl.DataSourceConnectionProvider)2