Class DatabaseService


  • public final class DatabaseService
    extends Object
    Database backed by JDBI with reactive capabilities.
    • Method Detail

      • create

        public static DatabaseService create​(org.jdbi.v3.core.Jdbi jdbi)
        Creates a new database backed by the given Jdbi instance.

        The given JDBI instance will be enriched as follows:

        • SqlObjectPlugin will be installed
        • Column and argument mappers will be added for the Snowflake type
        • SerializableTransactionRunner will be set as transaction handler
        Parameters:
        jdbi - the Jdbi instance backing the database
        Returns:
        a new DatabaseService
      • configureGuild

        public reactor.core.publisher.Flux<GuildConfigurator<?>> configureGuild​(Translator tr,
                                                                                discord4j.common.util.Snowflake guildId)
        Retrieves all registered configurators for the given guild referenced by its ID. Empty configuration data will be inserted in database for this specific guild if it doesn't exist yet. Configurators are emitted in the alphabetical order of the class name of their associated data object.
        Parameters:
        tr - the translator to use to translate strings necessary for the configuration
        guildId - the guild ID
        Returns:
        a Flux emitting all configurators for the guild
      • configureJdbi

        public void configureJdbi​(Consumer<org.jdbi.v3.core.Jdbi> jdbiConsumer)
        Allows to access the backing Jdbi instance to enrich its configuration, such as registering mappers. Note that SqlObjectPlugin is already installed by default.

        This method MUST NOT attempt to open any connection to the database. Database operations should be made using the other methods of this DatabaseService class.

        Parameters:
        jdbiConsumer - the consumer that mutates the backing Jdbi instance
      • useHandle

        public reactor.core.publisher.Mono<Void> useHandle​(org.jdbi.v3.core.HandleConsumer<?> handleConsumer)
        Acquires a Handle to perform actions on the database. When the consumer returns, the handle is closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations don't need to return a result. If you need to return a result, use withHandle(HandleCallback) instead.

        Parameters:
        handleConsumer - a Consumer using the handle
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • withHandle

        public <T> reactor.core.publisher.Mono<T> withHandle​(org.jdbi.v3.core.HandleCallback<T,​?> handleCallback)
        Acquires a Handle to perform actions on the database. When the callback returns, the handle is closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations need to return a result. If you don't need to return a result, use useHandle(HandleConsumer) instead.

        Type Parameters:
        T - the type of the desired return value
        Parameters:
        handleCallback - a Consumer using the handle
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • useTransaction

        public reactor.core.publisher.Mono<Void> useTransaction​(org.jdbi.v3.core.HandleConsumer<?> txConsumer)
        Performs a database transaction. When the consumer returns, the trasnaction is committed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations don't need to return a result. If you need to return a result, use withHandle(HandleCallback) instead.

        Parameters:
        txConsumer - a Consumer using the transaction
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • inTransaction

        public <T> reactor.core.publisher.Mono<T> inTransaction​(org.jdbi.v3.core.HandleCallback<T,​?> txCallback)
        Performs a database transaction. When the callback returns, the transaction is committed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations need to return a result. If you don't need to return a result, use useHandle(HandleConsumer) instead.

        Type Parameters:
        T - the type of the desired return value
        Parameters:
        txCallback - a Consumer using the transaction
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • useExtension

        public <E> reactor.core.publisher.Mono<Void> useExtension​(Class<E> extensionType,
                                                                  org.jdbi.v3.core.extension.ExtensionConsumer<E,​?> extensionConsumer)
        Uses a JDBI Extension. When the consumer returns, any acquired connections are closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the use of the extension doesn't need to return a result. If you need to return a result, use withExtension(Class, ExtensionCallback) instead.

        Type Parameters:
        E - the extension type
        Parameters:
        extensionType - the type of extension to use
        extensionConsumer - the consumer that uses the extension
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • withExtension

        public <T,​E> reactor.core.publisher.Mono<T> withExtension​(Class<E> extensionType,
                                                                        org.jdbi.v3.core.extension.ExtensionCallback<T,​E,​?> extensionCallback)
        Uses a JDBI Extension. When the callback returns, any acquired connections are closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the use of the extension needs to return a result. If you don't need to return a result, use useExtension(Class, ExtensionConsumer) instead.

        Type Parameters:
        T - the return value type
        E - the extension type
        Parameters:
        extensionType - the type of extension to use
        extensionCallback - the callback that uses the extension
        Returns:
        a Mono that completes when the callback returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause