Search in sources :

Example 1 with SqlLogger

use of org.jdbi.v3.core.statement.SqlLogger in project ADP-Core by AlessioDP.

the class SQLDispatcher method init.

@Override
public void init() {
    // Initialize DAO
    connectionFactory = initConnectionFactory();
    // Check if initialized
    if (connectionFactory != null) {
        connectionFactory.init();
        // Check for failures
        if (!connectionFactory.isFailed()) {
            // Setup logger
            connectionFactory.getJdbi().setSqlLogger(new SqlLogger() {

                @Override
                public void logBeforeExecution(StatementContext context) {
                    plugin.getLoggerManager().logDebug(String.format(Constants.DEBUG_DB_QUERY_EXECUTE, context.getRenderedSql()), true);
                }
            });
            try {
                MigratorConfiguration migratorConfiguration = prepareMigrator();
                migrateTables(migratorConfiguration.load(plugin, this));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
Also used : SqlLogger(org.jdbi.v3.core.statement.SqlLogger) MigratorConfiguration(com.alessiodp.core.common.storage.sql.migrator.MigratorConfiguration) StatementContext(org.jdbi.v3.core.statement.StatementContext)

Example 2 with SqlLogger

use of org.jdbi.v3.core.statement.SqlLogger in project nia-patient-switching-standard-adaptor by NHSDigital.

the class DbConnectorConfiguration method jdbi.

@Bean
public Jdbi jdbi(DataSource ds, List<JdbiPlugin> jdbiPlugins, List<RowMapper<?>> rowMappers) {
    TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds);
    Jdbi jdbi = Jdbi.create(proxy);
    SqlLogger sqlLogger = new SqlLogger() {

        @Override
        public void logAfterExecution(StatementContext context) {
            LOGGER.debug("sql {}, parameters {}, timeTaken {} ms", context.getRenderedSql(), context.getBinding().toString(), context.getElapsedTime(ChronoUnit.MILLIS));
        }
    };
    jdbi.setSqlLogger(sqlLogger);
    jdbiPlugins.forEach(jdbi::installPlugin);
    rowMappers.forEach(jdbi::registerRowMapper);
    return jdbi;
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) SqlLogger(org.jdbi.v3.core.statement.SqlLogger) StatementContext(org.jdbi.v3.core.statement.StatementContext) Bean(org.springframework.context.annotation.Bean)

Example 3 with SqlLogger

use of org.jdbi.v3.core.statement.SqlLogger in project jdbi by jdbi.

the class TestTimestamped method before.

@BeforeEach
public void before() {
    TimestampedFactory.setTimeSource(clock::withZone);
    final Jdbi db = h2Extension.getJdbi();
    db.getConfig(TimestampedConfig.class).setTimezone(GMT_PLUS_2);
    db.setSqlLogger(new SqlLogger() {

        @Override
        public void logBeforeExecution(StatementContext ctx) {
            String name = logNext.get();
            if (name != null) {
                String toString = ctx.getBinding().findForName(name, ctx).orElseThrow(AssertionError::new).toString();
                insertedTimestamp.set(OffsetDateTime.parse(toString));
                logNext.set(null);
            }
        }
    });
    personDAO = db.onDemand(PersonDAO.class);
    personDAO.createTable();
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) TimestampedConfig(org.jdbi.v3.sqlobject.customizer.TimestampedConfig) SqlLogger(org.jdbi.v3.core.statement.SqlLogger) StatementContext(org.jdbi.v3.core.statement.StatementContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with SqlLogger

use of org.jdbi.v3.core.statement.SqlLogger in project marquez by MarquezProject.

the class MarquezApp method registerResources.

public void registerResources(@NonNull MarquezConfig config, @NonNull Environment env, @NonNull DataSource source) {
    final JdbiFactory factory = new JdbiFactory();
    final Jdbi jdbi = factory.build(env, config.getDataSourceFactory(), (ManagedDataSource) source, DB_POSTGRES).installPlugin(new SqlObjectPlugin()).installPlugin(new PostgresPlugin());
    SqlLogger sqlLogger = new InstrumentedSqlLogger(env.metrics());
    if (isSentryEnabled(config)) {
        sqlLogger = new TracingSQLLogger(sqlLogger);
    }
    jdbi.setSqlLogger(sqlLogger);
    final MarquezContext context = MarquezContext.builder().jdbi(jdbi).tags(config.getTags()).build();
    if (config.getGraphql().isEnabled()) {
        env.servlets().addServlet("api/v1-beta/graphql", context.getGraphqlServlet()).addMapping("/api/v1-beta/graphql", "/api/v1/schema.json");
    }
    log.debug("Registering resources...");
    for (final Object resource : context.getResources()) {
        env.jersey().register(resource);
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) JdbiFactory(io.dropwizard.jdbi3.JdbiFactory) TracingSQLLogger(marquez.tracing.TracingSQLLogger) InstrumentedSqlLogger(com.codahale.metrics.jdbi3.InstrumentedSqlLogger) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) SqlLogger(org.jdbi.v3.core.statement.SqlLogger) InstrumentedSqlLogger(com.codahale.metrics.jdbi3.InstrumentedSqlLogger) PostgresPlugin(org.jdbi.v3.postgres.PostgresPlugin)

Example 5 with SqlLogger

use of org.jdbi.v3.core.statement.SqlLogger in project Hangar by HangarMC.

the class JDBIConfig method jdbi.

@Bean
public Jdbi jdbi(DataSource dataSource, List<JdbiPlugin> jdbiPlugins, List<RowMapper<?>> rowMappers, List<RowMapperFactory> rowMapperFactories, List<ColumnMapper<?>> columnMappers) {
    SqlLogger myLogger = new SqlLogger() {

        @Override
        public void logException(StatementContext context, SQLException ex) {
            Logger.getLogger("sql").info("sql: " + context.getRenderedSql());
        }

        @Override
        public void logAfterExecution(StatementContext context) {
            Logger.getLogger("sql").info("sql ae: " + context.getRenderedSql());
        }
    };
    TransactionAwareDataSourceProxy dataSourceProxy = new TransactionAwareDataSourceProxy(dataSource);
    Jdbi jdbi = Jdbi.create(dataSourceProxy);
    // jdbi.setSqlLogger(myLogger); // for debugging sql statements
    PostgresTypes config = jdbi.getConfig(PostgresTypes.class);
    jdbiPlugins.forEach(jdbi::installPlugin);
    rowMappers.forEach(jdbi::registerRowMapper);
    rowMapperFactories.forEach(jdbi::registerRowMapper);
    columnMappers.forEach(jdbi::registerColumnMapper);
    config.registerCustomType(PGLoggedAction.class, "logged_action_type");
    config.registerCustomType(RoleCategory.class, "role_category");
    config.registerCustomType(JobState.class, "job_state");
    config.registerCustomType(JSONB.class, "jsonb");
    return jdbi;
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SQLException(java.sql.SQLException) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) PostgresTypes(org.jdbi.v3.postgres.PostgresTypes) SqlLogger(org.jdbi.v3.core.statement.SqlLogger) StatementContext(org.jdbi.v3.core.statement.StatementContext) Bean(org.springframework.context.annotation.Bean)

Aggregations

SqlLogger (org.jdbi.v3.core.statement.SqlLogger)5 Jdbi (org.jdbi.v3.core.Jdbi)4 StatementContext (org.jdbi.v3.core.statement.StatementContext)4 Bean (org.springframework.context.annotation.Bean)2 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)2 MigratorConfiguration (com.alessiodp.core.common.storage.sql.migrator.MigratorConfiguration)1 InstrumentedSqlLogger (com.codahale.metrics.jdbi3.InstrumentedSqlLogger)1 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)1 SQLException (java.sql.SQLException)1 TracingSQLLogger (marquez.tracing.TracingSQLLogger)1 PostgresPlugin (org.jdbi.v3.postgres.PostgresPlugin)1 PostgresTypes (org.jdbi.v3.postgres.PostgresTypes)1 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)1 TimestampedConfig (org.jdbi.v3.sqlobject.customizer.TimestampedConfig)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1