Package dev.demeng.pluginbase.sql
Class Sql
java.lang.Object
dev.demeng.pluginbase.sql.Sql
- All Implemented Interfaces:
ISql,dev.demeng.pluginbase.terminable.Terminable,AutoCloseable
Represents an SQL database. Contains several useful utilities such as quickly querying and
executing (prepared) statements as well as initializing the database with optimal settings.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringFields inherited from interface dev.demeng.pluginbase.terminable.Terminable
EMPTY -
Constructor Summary
ConstructorsConstructorDescriptionSql(@Nullable String driverClass, @Nullable String jdbcUrl, @NotNull SqlCredentials credentials) Creates a new SQL database manager instance with the optimized settings. -
Method Summary
Modifier and TypeMethodDescription@NotNull BatchBuilderGets aBatchBuilderfor the provided statement.voidclose()voidexecute(@NotNull String statement, @NotNull be.bendem.sqlstreams.util.SqlConsumer<PreparedStatement> preparer) Executes a database statement with preparation.voidexecuteBatch(@NotNull BatchBuilder builder) Executes a batched database execution.@NotNull ConnectionGets a connection from the datasource.@NotNull com.zaxxer.hikari.HikariDataSourceGets the Hikari instance backing the datasource.<R> @NotNull Optional<R>query(@NotNull String query, @NotNull be.bendem.sqlstreams.util.SqlConsumer<PreparedStatement> preparer, @NotNull be.bendem.sqlstreams.util.SqlFunction<ResultSet, R> handler) Executes a database query with preparation.@NotNull be.bendem.sqlstreams.SqlStreamstream()Gets aSqlStreaminstance for thisISql.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.demeng.pluginbase.sql.ISql
execute, executeAsync, executeAsync, executeBatchAsync, query, queryAsync, queryAsyncMethods inherited from interface dev.demeng.pluginbase.terminable.Terminable
bindWith, closeAndReportException, closeSilently, isClosed
-
Field Details
-
MYSQL_DRIVER
- See Also:
-
MYSQL_LEGACY_DRIVER
- See Also:
-
DEFAULT_JDBC_URL_HEAD
- See Also:
-
DEFAULT_JDBC_URL_TAIL
- See Also:
-
DEFAULT_JDBC_URL
- See Also:
-
-
Constructor Details
-
Sql
public Sql(@Nullable @Nullable String driverClass, @Nullable @Nullable String jdbcUrl, @NotNull @NotNull SqlCredentials credentials) Creates a new SQL database manager instance with the optimized settings. Initializes a new Hikari data source and SQL stream.- Parameters:
driverClass- The driver class name (ex. com.mysql.cj.jdbc.Driver)jdbcUrl- The JDBC URL, or null for the default URLcredentials- The database credentials
-
-
Method Details
-
getHikari
@NotNull public @NotNull com.zaxxer.hikari.HikariDataSource getHikari()Description copied from interface:ISqlGets the Hikari instance backing the datasource. -
getConnection
Description copied from interface:ISqlGets a connection from the datasource.The connection should be returned once it has been used.
- Specified by:
getConnectionin interfaceISql- Returns:
- A connection
- Throws:
SQLException
-
stream
@NotNull public @NotNull be.bendem.sqlstreams.SqlStream stream()Description copied from interface:ISqlGets aSqlStreaminstance for thisISql. -
execute
public void execute(@Language("SQL") @NotNull @NotNull String statement, @NotNull @NotNull be.bendem.sqlstreams.util.SqlConsumer<PreparedStatement> preparer) Description copied from interface:ISqlExecutes a database statement with preparation.This will be executed on whichever thread it's called from.
-
query
@NotNull public <R> @NotNull Optional<R> query(@Language("SQL") @NotNull @NotNull String query, @NotNull @NotNull be.bendem.sqlstreams.util.SqlConsumer<PreparedStatement> preparer, @NotNull @NotNull be.bendem.sqlstreams.util.SqlFunction<ResultSet, R> handler) Description copied from interface:ISqlExecutes a database query with preparation.This will be executed on whichever thread it's called from.
In the case of a
SQLExceptionor in the case of no data being returned, or the handler evaluating to null, this method will return anOptional.empty()object. -
executeBatch
Description copied from interface:ISqlExecutes a batched database execution.This will be executed on whichever thread it's called from.
Note that proper implementations of this method should determine if the provided
BatchBuilderis actually worth of being a batched statement. For instance, a BatchBuilder with only one handler can safely be referred toISql.execute(String, SqlConsumer)- Specified by:
executeBatchin interfaceISql- Parameters:
builder- The builder to be used.- See Also:
-
batch
Description copied from interface:ISqlGets aBatchBuilderfor the provided statement. -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacedev.demeng.pluginbase.terminable.Terminable
-