Class Sql

java.lang.Object
dev.demeng.pluginbase.sql.Sql
All Implemented Interfaces:
ISql, dev.demeng.pluginbase.terminable.Terminable, AutoCloseable

public class Sql extends Object implements ISql
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 Details

  • 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 URL
      credentials - The database credentials
  • Method Details

    • getHikari

      @NotNull public @NotNull com.zaxxer.hikari.HikariDataSource getHikari()
      Description copied from interface: ISql
      Gets the Hikari instance backing the datasource.
      Specified by:
      getHikari in interface ISql
      Returns:
      The hikari instance
    • getConnection

      @NotNull public @NotNull Connection getConnection() throws SQLException
      Description copied from interface: ISql
      Gets a connection from the datasource.

      The connection should be returned once it has been used.

      Specified by:
      getConnection in interface ISql
      Returns:
      A connection
      Throws:
      SQLException
    • stream

      @NotNull public @NotNull be.bendem.sqlstreams.SqlStream stream()
      Description copied from interface: ISql
      Gets a SqlStream instance for this ISql.
      Specified by:
      stream in interface ISql
      Returns:
      An instance of the stream library for this connection
    • execute

      public void execute(@Language("SQL") @NotNull @NotNull String statement, @NotNull @NotNull be.bendem.sqlstreams.util.SqlConsumer<PreparedStatement> preparer)
      Description copied from interface: ISql
      Executes a database statement with preparation.

      This will be executed on whichever thread it's called from.

      Specified by:
      execute in interface ISql
      Parameters:
      statement - The statement to be executed
      preparer - The preparation used for this statement
      See Also:
    • 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: ISql
      Executes a database query with preparation.

      This will be executed on whichever thread it's called from.

      In the case of a SQLException or in the case of no data being returned, or the handler evaluating to null, this method will return an Optional.empty() object.

      Specified by:
      query in interface ISql
      Type Parameters:
      R - The returned type
      Parameters:
      query - The query to be executed
      preparer - The preparation used for this statement
      handler - The handler for the data returned by the query
      Returns:
      The results of the database query
      See Also:
    • executeBatch

      public void executeBatch(@NotNull @NotNull BatchBuilder builder)
      Description copied from interface: ISql
      Executes 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 BatchBuilder is actually worth of being a batched statement. For instance, a BatchBuilder with only one handler can safely be referred to ISql.execute(String, SqlConsumer)

      Specified by:
      executeBatch in interface ISql
      Parameters:
      builder - The builder to be used.
      See Also:
    • batch

      @NotNull public @NotNull BatchBuilder batch(@Language("SQL") @NotNull @NotNull String statement)
      Description copied from interface: ISql
      Gets a BatchBuilder for the provided statement.
      Specified by:
      batch in interface ISql
      Parameters:
      statement - the statement to prepare for batching.
      Returns:
      a BatchBuilder
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface dev.demeng.pluginbase.terminable.Terminable