Class DatabaseService
- java.lang.Object
-
- com.github.alex1304.ultimategdbot.api.database.DatabaseService
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <D extends GuildConfigData<D>>
voidaddGuildConfigurator(Class<? extends GuildConfigDao<D>> dao, BiFunction<? super D,? super Translator,GuildConfigurator<D>> configuratorFactory)
Registers a guild configurator to this database.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.void
configureJdbi(Consumer<org.jdbi.v3.core.Jdbi> jdbiConsumer)
Allows to access the backingJdbi
instance to enrich its configuration, such as registering mappers.static DatabaseService
create(org.jdbi.v3.core.Jdbi jdbi)
Creates a new database backed by the givenJdbi
instance.<T> reactor.core.publisher.Mono<T>
inTransaction(org.jdbi.v3.core.HandleCallback<T,?> txCallback)
Performs a database transaction.<E> reactor.core.publisher.Mono<Void>
useExtension(Class<E> extensionType, org.jdbi.v3.core.extension.ExtensionConsumer<E,?> extensionConsumer)
Uses a JDBI Extension.reactor.core.publisher.Mono<Void>
useHandle(org.jdbi.v3.core.HandleConsumer<?> handleConsumer)
Acquires aHandle
to perform actions on the database.reactor.core.publisher.Mono<Void>
useTransaction(org.jdbi.v3.core.HandleConsumer<?> txConsumer)
Performs a database transaction.<T,E>
reactor.core.publisher.Mono<T>withExtension(Class<E> extensionType, org.jdbi.v3.core.extension.ExtensionCallback<T,E,?> extensionCallback)
Uses a JDBI Extension.<T> reactor.core.publisher.Mono<T>
withHandle(org.jdbi.v3.core.HandleCallback<T,?> handleCallback)
Acquires aHandle
to perform actions on the database.
-
-
-
Method Detail
-
create
public static DatabaseService create(org.jdbi.v3.core.Jdbi jdbi)
Creates a new database backed by the givenJdbi
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
- theJdbi
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 configurationguildId
- the guild ID- Returns:
- a Flux emitting all configurators for the guild
-
addGuildConfigurator
public <D extends GuildConfigData<D>> void addGuildConfigurator(Class<? extends GuildConfigDao<D>> dao, BiFunction<? super D,? super Translator,GuildConfigurator<D>> configuratorFactory)
Registers a guild configurator to this database. This allows to retrieve all configuration data via theconfigureGuild(Translator, Snowflake)
method.- Type Parameters:
D
- the type of data object- Parameters:
dao
- the DAO to retrieve configuration dataconfiguratorFactory
- a function which produces aGuildConfigurator
, given the data and a translator
-
configureJdbi
public void configureJdbi(Consumer<org.jdbi.v3.core.Jdbi> jdbiConsumer)
Allows to access the backingJdbi
instance to enrich its configuration, such as registering mappers. Note thatSqlObjectPlugin
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 backingJdbi
instance
-
useHandle
public reactor.core.publisher.Mono<Void> useHandle(org.jdbi.v3.core.HandleConsumer<?> handleConsumer)
Acquires aHandle
to perform actions on the database. When the consumer returns, the handle is closed and the returnedMono
completes. The task is executed onSchedulers.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
- aConsumer
using the handle- Returns:
- a Mono that completes when the consumer returns. If an error is
received, the
Mono
will emitDatabaseException
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 aHandle
to perform actions on the database. When the callback returns, the handle is closed and the returnedMono
completes. The task is executed onSchedulers.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
- aConsumer
using the handle- Returns:
- a Mono that completes when the consumer returns. If an error is
received, the
Mono
will emitDatabaseException
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 returnedMono
completes. The task is executed onSchedulers.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
- aConsumer
using the transaction- Returns:
- a Mono that completes when the consumer returns. If an error is
received, the
Mono
will emitDatabaseException
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 returnedMono
completes. The task is executed onSchedulers.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
- aConsumer
using the transaction- Returns:
- a Mono that completes when the consumer returns. If an error is
received, the
Mono
will emitDatabaseException
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 returnedMono
completes. The task is executed onSchedulers.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 useextensionConsumer
- the consumer that uses the extension- Returns:
- a Mono that completes when the consumer returns. If an error is
received, the
Mono
will emitDatabaseException
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 returnedMono
completes. The task is executed onSchedulers.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 typeE
- the extension type- Parameters:
extensionType
- the type of extension to useextensionCallback
- the callback that uses the extension- Returns:
- a Mono that completes when the callback returns. If an error is
received, the
Mono
will emitDatabaseException
with the underlying JDBI exception as the cause
-
-