Java Code Examples for org.skife.jdbi.v2.DBI#registerArgumentFactory()

The following examples show how to use org.skife.jdbi.v2.DBI#registerArgumentFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBIFactory.java    From dropwizard-java8 with Apache License 2.0 6 votes vote down vote up
@Override
public DBI build(Environment environment,
                 PooledDataSourceFactory configuration,
                 ManagedDataSource dataSource,
                 String name) {
    final DBI dbi = super.build(environment, configuration, dataSource, name);

    dbi.registerArgumentFactory(new OptionalArgumentFactory(configuration.getDriverClass()));
    dbi.registerContainerFactory(new OptionalContainerFactory());
    dbi.registerArgumentFactory(new LocalDateArgumentFactory());
    dbi.registerArgumentFactory(new OptionalLocalDateArgumentFactory());
    dbi.registerArgumentFactory(new LocalDateTimeArgumentFactory());
    dbi.registerArgumentFactory(new OptionalLocalDateTimeArgumentFactory());
    dbi.registerMapper(new LocalDateMapper());
    dbi.registerMapper(new LocalDateTimeMapper());

    final Optional<TimeZone> tz = Optional.ofNullable(databaseTimeZone().orNull());
    dbi.registerArgumentFactory(new InstantArgumentFactory(tz));
    dbi.registerArgumentFactory(new OptionalInstantArgumentFactory(tz));
    dbi.registerMapper(new InstantMapper(tz));


    return dbi;
}
 
Example 2
Source File: JdbiStore.java    From breakerbox with Apache License 2.0 6 votes vote down vote up
public JdbiStore(JdbiConfiguration storeConfiguration,
                 Environment environment,
                 DBI database) {
    super(storeConfiguration, environment);
    database.registerArgumentFactory(new DependencyIdArgumentFactory());
    database.registerArgumentFactory(new ServiceIdArgumentFactory());
    database.registerArgumentFactory(new TenacityConfigurationArgumentFactory(environment.getObjectMapper()));
    database.registerArgumentFactory(new DateTimeArgumentFactory());

    dependencyDB = database.onDemand(DependencyDB.class);
    serviceDB = database.onDemand(ServiceDB.class);

    this.configuration = storeConfiguration;

    metricRegistry = environment.metrics();
}
 
Example 3
Source File: AirpalModule.java    From airpal with Apache License 2.0 5 votes vote down vote up
@Singleton
@Provides
public DBI provideDBI(ObjectMapper objectMapper)
        throws ClassNotFoundException
{
    final DBIFactory factory = new DBIFactory();
    final DBI dbi = factory.build(environment, config.getDataSourceFactory(), provideDbType().name());
    dbi.registerMapper(new TableRow.TableRowMapper(objectMapper));
    dbi.registerMapper(new QueryStoreMapper(objectMapper));
    dbi.registerArgumentFactory(new UUIDArgumentFactory());
    dbi.registerArgumentFactory(new URIArgumentFactory());

    return dbi;
}
 
Example 4
Source File: ThreadLocalTransactionManager.java    From digdag with Apache License 2.0 4 votes vote down vote up
@Override
public Handle getHandle(ConfigMapper configMapper)
{
    if (state != State.ACTIVE) {
        throw new IllegalStateException("Transaction is already " + state.name().toLowerCase(ENGLISH));
    }

    if (handle == null) {
        DBI dbi = new DBI(ds);
        ConfigKeyListMapper cklm = new ConfigKeyListMapper();
        dbi.registerMapper(new DatabaseProjectStoreManager.StoredProjectMapper(configMapper));
        dbi.registerMapper(new DatabaseProjectStoreManager.StoredProjectWithRevisionMapper(configMapper));
        dbi.registerMapper(new DatabaseProjectStoreManager.StoredRevisionMapper(configMapper));
        dbi.registerMapper(new DatabaseProjectStoreManager.StoredWorkflowDefinitionMapper(configMapper));
        dbi.registerMapper(new DatabaseProjectStoreManager.StoredWorkflowDefinitionWithProjectMapper(configMapper));
        dbi.registerMapper(new DatabaseProjectStoreManager.WorkflowConfigMapper());
        dbi.registerMapper(new DatabaseProjectStoreManager.IdNameMapper());
        dbi.registerMapper(new DatabaseProjectStoreManager.ScheduleStatusMapper());
        dbi.registerMapper(new DatabaseQueueSettingStoreManager.StoredQueueSettingMapper(configMapper));
        dbi.registerMapper(new DatabaseScheduleStoreManager.StoredScheduleMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredTaskMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.ArchivedTaskMapper(cklm, configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.ResumingTaskMapper(cklm, configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredSessionMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredSessionWithLastAttemptMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredSessionAttemptMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredSessionAttemptWithSessionMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.TaskStateSummaryMapper());
        dbi.registerMapper(new DatabaseSessionStoreManager.TaskAttemptSummaryMapper());
        dbi.registerMapper(new DatabaseSessionStoreManager.SessionAttemptSummaryMapper());
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredSessionMonitorMapper(configMapper));
        dbi.registerMapper(new DatabaseSessionStoreManager.StoredDelayedSessionAttemptMapper());
        dbi.registerMapper(new DatabaseSessionStoreManager.TaskRelationMapper());
        dbi.registerMapper(new DatabaseSessionStoreManager.InstantMapper());
        dbi.registerMapper(new DatabaseSecretStore.ScopedSecretMapper());
        dbi.registerMapper(new DatabaseTaskQueueServer.ImmutableTaskQueueLockMapper());

        dbi.registerArgumentFactory(configMapper.getArgumentFactory());
        handle = dbi.open();

        try {
            handle.getConnection().setAutoCommit(autoAutoCommit);
        }
        catch (SQLException ex) {
            throw new TransactionFailedException("Failed to set auto commit: " + autoAutoCommit, ex);
        }
        if (!autoAutoCommit) {
            handle.begin();
        }
    }
    return handle;
}
 
Example 5
Source File: EtlJdbi.java    From pocket-etl with Apache License 2.0 3 votes vote down vote up
/**
 * Construct a new DBI wrapper for a JDBC datasource using an optional secondary mapper. The secondary mapper comes
 * into play when the default mapper is unable to directly map a value that the SQL query returned to a property in
 * the bean class you have specified. In this case you give it a lambda to call that will insert the value correctly
 * into the bean object.
 * @param dataSource JDBC datasource to be wrapped.
 * @param secondaryMapper A lambda that is invoked when a data element can't be directly mapped to the bean is
 *                        extracted. The lambda takes as its arguments the bean that is being extracted to and
 *                        a Map.Entry that represents the (key, value) pair of the data element that could not be
 *                        directly mapped to the bean. If set to null, unrecognized properties will be ignored.
 * @return A fully constructed DBI object.
 */
public static <T> DBI newDBI(DataSource dataSource, @Nullable BiConsumer<T, Map.Entry<String, String>> secondaryMapper) {
    DBI dbi = new DBI(dataSource);
    dbi.setStatementRewriter(new HashPrefixStatementRewriter());
    dbi.registerMapper(new EtlBeanMapperFactory(secondaryMapper));
    dbi.registerArgumentFactory(new PostgresStringArrayArgumentFactory());

    return dbi;
}