Class Database

java.lang.Object
com.ptsmods.mysqlw.Database

public class Database extends Object
  • Method Details

    • loadConnector

      public static void loadConnector(Database.RDBMS type, @Nullable @Nullable String version, File file, boolean useCache) throws IOException
      Downloads the latest version of the connector for the given type and attempts to add it to the classpath.
      It is not recommended you rely on this, but if, for example, you offer your users a choice whether to use MySQL or SQLite and you do not want to make your jar file huge, there is always this option.
      Parameters:
      type - The type of the connector to download.
      version - The version of the connector to download. If null, automatically downloads the latest one. In case of MySQL, this version should correspond with the version of the server you're trying to connect to.
      file - The file to download to.
      useCache - Whether to use a cached file if the given file already exists. If the given file does not appear to be a connector of the given type, a new version will be downloaded nonetheless.
      Throws:
      IllegalArgumentException - If the given type is Database.RDBMS.UNKNOWN.
      IOException - If anything went wrong while downloading the file.
    • connect

      public static Database connect(String host, int port, String name, String username, String password) throws SQLException
      Makes a connection to a MySQL database.
      Parameters:
      host - The hostname of this database. Often localhost
      port - The port this dataserver runs on. Often 3306
      name - The name of this database. An attempt to create this database will be made if it does not yet exist.
      username - The username to log in with.
      password - The password that goes with the username. Can be null if there isn't one.
      Returns:
      A Database with which you can do anything.
      Throws:
      SQLException - If an error occurred while either connecting or creating the database.
    • connect

      public static Database connect(File file) throws SQLException
      Makes a new connection to an SQLite database or creates it if it does not yet exist.
      Parameters:
      file - The database file to connect to.
      Returns:
      A Database with which you can do anything.
      Throws:
      SQLException - If an error occurred while either connecting or creating the database.
    • connect

      public static Database connect(Connection connection)
      Wraps an SQL connection in a Database. Allows you to connect to any type of database.

      THIS FEATURE IS UNSUPPORTED.

      Parameters:
      connection - The connection to wrap.
      Returns:
      A new, probably unstable, Database.
      See Also:
    • getDatabase

      @Nullable public static @Nullable Database getDatabase(Connection connection)
      Gets the Database that wraps this connection.
      Parameters:
      connection - The connection
      Returns:
      The Database that wraps this connection or null.
    • getDatabase

      public static Database getDatabase(ResultSet set)
    • getLog

      public Logger getLog()
    • doLog

      public boolean doLog()
    • setLogging

      public void setLogging(boolean doLog)
      Sets whether exceptions should be logged or thrown.
      When this is set to false, all SQLExceptions will be thrown wrapped in a SilentSQLException.
      Parameters:
      doLog - Whether to log or throw exceptions.
    • logOrThrow

      public void logOrThrow(String msg, SQLException e) throws SilentSQLException
      Throws:
      SilentSQLException
    • getExecutor

      public Executor getExecutor()
      Returns:
      The Executor used to run tasks asynchronously.
    • setExecutor

      public void setExecutor(Executor executor)
      Sets the Executor used to run tasks asynchronously.
      Parameters:
      executor - The new default executor to use
      See Also:
    • getErrorHandler

      public Consumer<Throwable> getErrorHandler()
      Returns:
      The default error handler used whenever an asynchronous call throws an error.
    • setErrorHandler

      public void setErrorHandler(Consumer<Throwable> errorHandler)
      Sets the error handler used whenever an asynchronous call throws an error.
      Parameters:
      errorHandler - The error handler to use
    • getType

      public Database.RDBMS getType()
    • getName

      public String getName() throws SilentSQLException
      Attempts to get the name of the database currently in use.
      Returns:
      The name of the currently in use database, or the cached name if it could not be gotten.
      Throws:
      SilentSQLException
    • getConnection

      public Connection getConnection()
      Returns the connection to the dataserver.
      Returns:
      The connection to the dataserver.
    • createStatement

      public Statement createStatement() throws SilentSQLException
      Creates a new statement.

      DO NOT FORGET TO CLOSE THIS.

      Returns:
      A new statement which must be closed once finished.
      Throws:
      SilentSQLException
    • prepareStatement

      public PreparedStatement prepareStatement(String query)
      Prepares a new statement.
      Parameters:
      query - The query to use in this statement. Use question marks as argument placeholders.
      Returns:
      A prepared statement which can be used to easily insert or update data.
    • runAsync

      public <T> CompletableFuture<T> runAsync(Supplier<T> sup)
      Runs the given supplier on the set executor using CompletableFutures.
      Type Parameters:
      T - The type the given supplier returns.
      Parameters:
      sup - The supplier to run.
      Returns:
      A invalid @link CompletableFuture.
    • runAsync

      public CompletableFuture<Void> runAsync(Runnable run)
      Runs the given runnable on the set executor using CompletableFutures.
      Parameters:
      run - The runnable to run.
      Returns:
      A invalid @link CompletableFuture.
    • count

      public int count(String table, String what) throws SilentSQLException
      Counts columns in a table.
      Parameters:
      table - The table to count them in.
      what - What columns to count.
      Returns:
      The amount of results found or -1 if an error occurred.
      Throws:
      SilentSQLException
      See Also:
    • count

      public int count(String table, String what, QueryCondition condition) throws SilentSQLException
      Counts columns in a table.
      Parameters:
      table - The table to count them in.
      what - What columns to count.
      condition - The condition the row must meet to be counted.
      Returns:
      The amount of results found or -1 if an error occurred.
      Throws:
      SilentSQLException
      See Also:
    • countAsync

      public CompletableFuture<Integer> countAsync(String table, String what)
      Counts columns in a table asynchronously.
      Parameters:
      table - The table to count them in.
      what - What columns to count.
      Returns:
      The amount of results found or -1 if an error occurred.
      See Also:
    • countAsync

      public CompletableFuture<Integer> countAsync(String table, String what, QueryCondition condition)
      Counts columns in a table asynchronously.
      Parameters:
      table - The table to count them in.
      what - What columns to count.
      condition - The condition the row must meet to be counted.
      Returns:
      The amount of results found or -1 if an error occurred.
      See Also:
    • truncate

      public void truncate(String table)
      Truncates (clears) a table.
      Parameters:
      table - The table to truncate.
      See Also:
    • truncateAsync

      public CompletableFuture<Void> truncateAsync(String table)
      Truncates (clears) a table asynchronously.
      Parameters:
      table - The table to truncate.
      See Also:
    • delete

      public int delete(String table, QueryCondition condition)
      Deletes rows matching the given condition or all when no condition given.
      Parameters:
      table - The table to delete rows from
      condition - The condition rows must meet in order to be deleted
      Returns:
      The amount of rows affected
      See Also:
    • delete

      public int delete(String table, QueryCondition condition, int limit)
      Deletes rows matching the given condition or all when no condition given.
      Parameters:
      table - The table to delete rows from
      condition - The condition rows must meet in order to be deleted
      limit - The maximum amount of rows to delete
      Returns:
      The amount of rows affected.
      Throws:
      IllegalStateException - When limit > 0 and type is SQLite as SQLite does not support delete limits.
      See Also:
    • deleteAsync

      public CompletableFuture<Integer> deleteAsync(String table, QueryCondition condition)
      Deletes rows matching the given condition or all when no condition given asynchronously.
      Parameters:
      table - The table to delete rows from
      condition - The condition rows must meet in order to be deleted
      Returns:
      The amount of rows affected.
      See Also:
    • deleteAsync

      public CompletableFuture<Integer> deleteAsync(String table, QueryCondition condition, int limit)
      Deletes rows matching the given condition or all when no condition given asynchronously.
      Parameters:
      table - The table to delete rows from
      condition - The condition rows must meet in order to be deleted
      limit - The maximum amount of rows to delete
      Returns:
      The amount of rows affected.
      See Also:
    • selectBuilder

      public SelectBuilder selectBuilder(String table)
      Creates a new SelectBuilder to use for selecting data.
      Parameters:
      table - The table to select from.
      Returns:
      A new SelectBuilder.
    • selectBuilder

      public SelectBuilder selectBuilder(SelectBuilder select)
      Creates a new SelectBuilder to use for selecting data.
      Parameters:
      select - The select query to select from.
      Returns:
      A new SelectBuilder.
    • selectVariable

      public Object selectVariable(String variable)
      Selects a single variable stored in the RDBMS.
      Parameters:
      variable - The name of the variable
      Returns:
      The value of the variable
      See Also:
    • selectVariableAsync

      public CompletableFuture<Object> selectVariableAsync(String variable)
      Asynchronously selects a single variable stored in the RDBMS.
      Parameters:
      variable - The name of the variable
      Returns:
      The value of the variable
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence column)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence column)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence column, QueryCondition condition)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence column, QueryCondition condition)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence[] columns)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence[] columns)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence[] columns, QueryCondition condition)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence[] columns, QueryCondition condition)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRaw

      public ResultSet selectRaw(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns the raw output. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • selectRawAsync

      public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns the raw output asynchronously. The statement used for this query is

      not closed

      so make sure to close it with set.getStatement().close().
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      A raw ResultSet that must be closed after use.
      See Also:
    • select

      public SelectResults select(String table, CharSequence column)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence column)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      column - The column to select.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • select

      public SelectResults select(String table, CharSequence column, QueryCondition condition)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence column, QueryCondition condition)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • select

      public SelectResults select(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      column - The column to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • select

      public SelectResults select(String table, CharSequence[] columns)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence[] columns)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • select

      public SelectResults select(String table, CharSequence[] columns, QueryCondition condition)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence[] columns, QueryCondition condition)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • select

      public SelectResults select(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns parsed output.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • selectAsync

      public CompletableFuture<SelectResults> selectAsync(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit)
      Runs a select query and returns parsed output asynchronously.
      Parameters:
      table - The table to select from.
      columns - The columns to select.
      condition - The condition rows must meet in order to be selected.
      order - What column to order by and in what direction.
      limit - The limit of rows returned, including the offset at which these rows are selected from the entire result.
      Returns:
      Parsed data in the form of SelectResults.
      See Also:
    • insertBuilder

      public InsertBuilder insertBuilder(String table, String... columns)
      Creates a new InsertBuilder to build insert queries with.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into.
      Returns:
      A new InsertBuilder.
    • insert

      public int insert(String table, String column, Object value)
      Inserts new data into the table.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into.
      value - The value to insert into the column.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insertAsync

      public CompletableFuture<Integer> insertAsync(String table, String column, Object value)
      Inserts new data into the table asynchronously.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into.
      value - The value to insert into the column.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insert

      public int insert(String table, String[] columns, Object[] values)
      Inserts new data into the table.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into.
      values - The values to insert into the columns.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insertAsync

      public CompletableFuture<Integer> insertAsync(String table, String[] columns, Object[] values)
      Inserts new data into the table asynchronously.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into.
      values - The values to insert into the columns.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insert

      public int insert(String table, String[] columns, List<Object[]> values)
      Inserts new data into the table.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into.
      values - The values to insert into the columns. Each array in this list is a new row to be inserted.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insertAsync

      public CompletableFuture<Integer> insertAsync(String table, String[] columns, List<Object[]> values)
      Inserts new data into the table asynchronously.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into.
      values - The values to insert into the columns. Each array in this list is a new row to be inserted.
      Returns:
      The amount of rows affected (added).
      See Also:
    • insertUpdate

      public int insertUpdate(String table, String column, Object value, Object duplicateValue)
      Inserts new data or edits old data when a row with the given value for the given column already exists. It's like replace, but only replaces a column instead of the whole row.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into.
      value - The value to insert.
      duplicateValue - The value to insert when the column already has this value.
      Returns:
      The amount of rows affected.
      See Also:
    • insertUpdateAsync

      public CompletableFuture<Integer> insertUpdateAsync(String table, String column, Object value, Object duplicateValue)
      Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously. It's like replace, but only replaces a column instead of the whole row.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into.
      value - The value to insert.
      duplicateValue - The value to insert when the column already has this value.
      Returns:
      The amount of rows affected.
      See Also:
    • insertUpdate

      public int insertUpdate(String table, String[] columns, Object[] values, Map<String,Object> duplicateValues, String keyColumn) throws SilentSQLException
      Inserts new data or edits old data when a row with the given key already exists. It's like replace, but only replaces a couple columns instead of the whole row.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into, one of these should be a PRIMARY KEY column.
      values - The values to insert into the columns.
      duplicateValues - The columns to update and the values to update them with when a row with the given key already exists.
      keyColumn - The name of the PRIMARY KEY column. Only has to be set when the type of this Database is SQLite, can be null otherwise.
      Returns:
      The amount of rows affected.
      Throws:
      SilentSQLException
      See Also:
    • insertUpdateAsync

      public CompletableFuture<Integer> insertUpdateAsync(String table, String[] columns, Object[] values, Map<String,Object> duplicateValues, String keyColumn) throws SilentSQLException
      Inserts new data or edits old data when a row with the given key already exists asynchronously. It's like replace, but only replaces a couple columns instead of the whole row.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into, one of these should be a PRIMARY KEY column.
      values - The values to insert into the columns.
      duplicateValues - The columns to update and the values to update them with when a row with the given key already exists.
      keyColumn - The name of the PRIMARY KEY column. Only has to be set when the type of this Database is SQLite, can be null otherwise.
      Returns:
      The amount of rows affected.
      Throws:
      SilentSQLException
      See Also:
    • insertIgnore

      public int insertIgnore(String table, String column, Object value)
      Inserts new data or edits old data when a row with the given value for the given column already exists. It's like replace, but only replaces a column instead of the whole row.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into. This must be a PRIMARY KEY column.
      value - The value to insert.
      Returns:
      The amount of rows affected.
      See Also:
    • insertIgnoreAsync

      public CompletableFuture<Integer> insertIgnoreAsync(String table, String column, Object value)
      Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously. It's like replace, but only replaces a column instead of the whole row.
      Parameters:
      table - The table to insert into.
      column - The column to insert a value into. This must be a PRIMARY KEY column.
      value - The value to insert.
      Returns:
      The amount of rows affected.
      See Also:
    • insertIgnore

      public int insertIgnore(String table, String[] columns, Object[] values, String keyColumn)
      Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into, one of these should be a PRIMARY KEY column.
      values - The values to insert into the columns.
      keyColumn - The PRIMARY KEY column that's used to determine whether to ignore the insertion. This column should also be present in columns and a value for it should be present in values.
      Returns:
      The amount of rows affected.
      See Also:
    • insertIgnoreAsync

      public CompletableFuture<Integer> insertIgnoreAsync(String table, String[] columns, Object[] values, String keyColumn)
      Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists asynchronously.
      Parameters:
      table - The table to insert into.
      columns - The columns to insert values into, one of these should be a PRIMARY KEY column.
      values - The values to insert into the columns.
      keyColumn - The PRIMARY KEY column that's used to determine whether to ignore the insertion. This column should also be present in columns and a value for it should be present in values.
      Returns:
      The amount of rows affected.
      See Also:
    • update

      public int update(String table, String column, Object value, QueryCondition condition)
      Updates data in a table.
      Parameters:
      table - The table to update.
      column - The column to update
      value - The new value of the column.
      condition - The condition rows must meet in order to be updated.
      Returns:
      The amount of rows affected.
      See Also:
    • updateAsync

      public CompletableFuture<Integer> updateAsync(String table, String column, Object value, QueryCondition condition)
      Updates data in a table asynchronously.
      Parameters:
      table - The table to update.
      column - The column to update
      value - The new value of the column.
      condition - The condition rows must meet in order to be updated.
      Returns:
      The amount of rows affected.
      See Also:
    • update

      public int update(String table, Map<String,Object> updates, QueryCondition condition)
      Updates data in a table.
      Parameters:
      table - The table to update.
      updates - The columns and their corresponding values.
      condition - The condition rows must meet in order to be updated.
      Returns:
      The amount of rows affected.
      See Also:
    • updateAsync

      public CompletableFuture<Integer> updateAsync(String table, Map<String,Object> updates, QueryCondition condition)
      Updates data in a table asynchronously.
      Parameters:
      table - The table to update.
      updates - The columns and their corresponding values.
      condition - The condition rows must meet in order to be updated.
      Returns:
      The amount of rows affected.
      See Also:
    • replace

      public int replace(String table, String column, Object value)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists.
      Parameters:
      table - The table to replace rows in.
      column - The column to replace.
      value - The value to update.
      Returns:
      The amount of rows affected.
      See Also:
    • replaceAsync

      public CompletableFuture<Integer> replaceAsync(String table, String column, Object value)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.
      Parameters:
      table - The table to replace rows in.
      column - The column to replace.
      value - The value to update.
      Returns:
      The amount of rows affected.
      See Also:
    • replace

      public int replace(String table, String[] columns, Object[] values)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists.
      Parameters:
      table - The table to replace rows in.
      columns - The columns to replace.
      values - The values to update.
      Returns:
      The amount of rows affected.
      See Also:
    • replaceAsync

      public CompletableFuture<Integer> replaceAsync(String table, String[] columns, Object[] values)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.
      Parameters:
      table - The table to replace rows in.
      columns - The columns to replace.
      values - The values to update.
      Returns:
      The amount of rows affected.
      See Also:
    • replace

      public int replace(String table, String[] columns, List<Object[]> values)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists.
      Parameters:
      table - The table to replace rows in.
      columns - The columns to replace.
      values - The values to update.
      Returns:
      The amount of rows affected.
      See Also:
    • replaceAsync

      public CompletableFuture<Integer> replaceAsync(String table, String[] columns, List<Object[]> values)
      Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.
      Parameters:
      table - The table to replace rows in.
      columns - The columns to replace.
      values - The values to update.
      Returns:
      The amount of rows affected.
      See Also:
    • drop

      public void drop(String table)
      Drops (completely removes from existence) a table from this database.
      Parameters:
      table - The name of the table to drop.
      See Also:
    • dropAsync

      public CompletableFuture<Void> dropAsync(String table)
      Drops (completely removes from existence) a table from this database asynchronously.
      Parameters:
      table - The name of the table to drop.
      See Also:
    • execute

      public boolean execute(String query) throws SilentSQLException
      Executes a query and returns a boolean value which can mean anything. No need to close any statements here.
      Parameters:
      query - The query to execute.
      Returns:
      A boolean value which can mean anything.
      Throws:
      SilentSQLException
      See Also:
    • executeAsync

      public CompletableFuture<Boolean> executeAsync(String query)
      Executes a query and returns a boolean value which can mean anything asynchronously. No need to close any statements here.
      Parameters:
      query - The query to execute.
      Returns:
      A boolean value which can mean anything.
      See Also:
    • executeUpdate

      public int executeUpdate(String query) throws SilentSQLException
      Executes a query and returns an integer value which often denotes the amount of rows affected.
      Parameters:
      query - The query to execute.
      Returns:
      An integer value often denoting the amount of rows affected.
      Throws:
      SilentSQLException
      See Also:
    • executeUpdateAsync

      public CompletableFuture<Integer> executeUpdateAsync(String query)
      Executes a query and returns an integer value which often denotes the amount of rows affected asynchronously.
      Parameters:
      query - The query to execute.
      Returns:
      An integer value often denoting the amount of rows affected.
      See Also:
    • executeQuery

      public ResultSet executeQuery(String query) throws SilentSQLException
      Executes a query and returns a ResultSet. Most often used with the SELECT query.

      DO NOT FORGET TO CLOSE THE STATEMENT.

      This can be done with set.getStatement().close(). Not doing so will eventually result in memory leaks.
      Parameters:
      query - The query to execute.
      Returns:
      The ResultSet containing all the data this query returned.
      Throws:
      SilentSQLException
      See Also:
    • executeQueryAsync

      public CompletableFuture<ResultSet> executeQueryAsync(String query)
      Executes a query and returns a ResultSet asynchronously. Most often used with the SELECT query.

      DO NOT FORGET TO CLOSE THE STATEMENT.

      This can be done with set.getStatement().close(). Not doing so will eventually result in memory leaks.
      Parameters:
      query - The query to execute.
      Returns:
      The ResultSet containing all the data this query returned.
      See Also:
    • createTable

      public void createTable(TablePreset preset)
      Creates a table from a preset.
      Parameters:
      preset - The preset to build.
      See Also:
    • createTableAsync

      public CompletableFuture<Void> createTableAsync(TablePreset preset)
      Creates a table from a preset asynchronously.
      Parameters:
      preset - The preset to build.
      See Also:
    • tableExists

      public boolean tableExists(String name) throws SilentSQLException
      Checks if a table exists.
      Parameters:
      name - The name of the table.
      Returns:
      Whether a table by the given name exists.
      Throws:
      SilentSQLException
      See Also:
    • tableExistsAsync

      public CompletableFuture<Boolean> tableExistsAsync(String name)
      Checks if a table exists asynchronously.
      Parameters:
      name - The name of the table.
      Returns:
      Whether a table by the given name exists.
      See Also:
    • getCreateQuery

      public String getCreateQuery(String table)
      Parameters:
      table - The table to get the creation query of.
      Returns:
      The query used to create this table.
      See Also:
    • getCreateQueryAsync

      public CompletableFuture<String> getCreateQueryAsync(String table)
      Parameters:
      table - The table to get the creation query of.
      Returns:
      The query used to create this table asynchronously.
      See Also:
    • createIndex

      public void createIndex(String table, TableIndex index)
      Create a new index on an existing column in an existing table. This is the only way to create indices on SQLite.
      Parameters:
      table - The table to create the index on.
      index - The index to create.
      See Also:
    • createIndexAsync

      public CompletableFuture<Void> createIndexAsync(String table, TableIndex index)
      Create a new index on an existing column in an existing table asynchronously. This is the only way to create indices on SQLite.
      Parameters:
      table - The table to create the index on.
      index - The index to create.
      See Also:
    • addColumn

      public void addColumn(String table, String name, ColumnStructure<?> structure, @Nullable @Nullable String after)
      Adds a new column to the given table.
      Parameters:
      table - The table to add the column to
      name - The name of the new column
      structure - The structure of the new column
      after - The column after which to add this column or null to add it to the beginning.
      See Also:
    • addColumnAsync

      public CompletableFuture<Void> addColumnAsync(String table, String name, ColumnStructure<?> structure, @Nullable @Nullable String after)
      Adds a new column to the given table asynchronously.
      Parameters:
      table - The table to add the column to
      name - The name of the new column
      structure - The structure of the new column
      after - The column after which to add this column or null to add it to the beginning.
      See Also:
    • modifyColumn

      public void modifyColumn(String table, String name, ColumnStructure<?> structure)
      Modifies a column's definition.
      Parameters:
      table - The table to modify the column on
      name - The name of the column to modify
      structure - The new structure of the column
      See Also:
    • modifyColumnAsync

      public CompletableFuture<Void> modifyColumnAsync(String table, String name, ColumnStructure<?> structure)
      Modifies a column's definition asynchronously.
      Parameters:
      table - The table to modify the column on
      name - The name of the column to modify
      structure - The new structure of the column
      See Also:
    • dropColumn

      public void dropColumn(String table, String column)
      Drops a column.
      Parameters:
      table - The table to drop the column from
      column - The column to drop
      See Also:
    • dropColumnAsync

      public CompletableFuture<Void> dropColumnAsync(String table, String column)
      Drops a column asynchronously
      Parameters:
      table - The table to drop the column from
      column - The column to drop
      See Also:
    • createTrigger

      public void createTrigger(String name, String table, TriggeringEvent event, BlockBuilder trigger)
      Creates a new trigger with the given statements.
      Parameters:
      name - The name of the trigger
      table - The table to create the trigger on
      event - The event which will trigger this trigger
      trigger - The statements to execute when this trigger is triggered
      See Also:
    • createTriggerAsync

      public CompletableFuture<Void> createTriggerAsync(String name, String table, TriggeringEvent event, BlockBuilder trigger)
      Asynchronously creates a new trigger with the given statements.
      Parameters:
      name - The name of the trigger
      table - The table to create the trigger on
      event - The event which will trigger this trigger
      trigger - The statements to execute when this trigger is triggered
      Returns:
      A CompletableFuture
      See Also:
    • createProcedure

      public void createProcedure(String name, ProcedureParameter[] parameters, BlockBuilder procedure)
      Creates a new procedure with the given statements.
      Parameters:
      name - The name of the procedure
      parameters - The parameters of the procedure
      procedure - The statements to execute when this procedure is called
      See Also:
    • createProcedureAsync

      public CompletableFuture<Void> createProcedureAsync(String name, ProcedureParameter[] parameters, BlockBuilder procedure)
      Asynchronously creates a new procedure with the given statements.
      Parameters:
      name - The name of the procedure
      parameters - The parameters of the procedure
      procedure - The statements to execute when this procedure is called
      Returns:
      A CompletableFuture
      See Also:
    • call

      public void call(String procedure, Object... parameters)
      Calls a procedure with the given parameters.
      Parameters:
      procedure - The name of the procedure to call
      parameters - The parameters to pass when calling the procedure
      See Also:
    • callAsync

      public CompletableFuture<Void> callAsync(String procedure, Object... parameters)
      Asynchronously calls a procedure with the given parameters.
      Parameters:
      procedure - The name of the procedure to call
      parameters - The parameters to pass when calling the procedure
      Returns:
      A CompletableFuture
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • enquote

      public static String enquote(String s)
      Puts the given String in quotes and escapes any quotes in it to avoid SQL injection.
      Parameters:
      s - The String to enquote.
      Returns:
      The given String surrounded by quotes.
    • escapeQuotes

      public static String escapeQuotes(String s)
      Replaces all single quotes in the string with two single quotes to have MySQL read it as a single quote rather than a string end.
      Parameters:
      s - The String to escape.
      Returns:
      A String in which all single quotes are now two single quotes.
    • engrave

      public static String engrave(String s)
    • escapeQuotes

      public static String[] escapeQuotes(String[] sa)
      The same as escapeQuotes(String) but does it for a whole array of Strings.
      Parameters:
      sa - The String array.
      Returns:
      The same array given except all Strings have been escaped. Null values in the array are retained.
    • getAsString

      public static String getAsString(CharSequence seq)
      Gets a CharSequence as a String, in case of a QueryFunction this returns its function, in case of an asterisk this returns an asterisk, in all other cases this returns the given CharSequence but surrounded by graves.
      Parameters:
      seq - The sequence to get as String.
      Returns:
      A String, either a QueryFunction's function or the given CharSequence surrounded by graves.
    • getAsString

      public static String getAsString(Object o)
      Converts an Object to a String to be used in queries. The default cases are as follows:
      Parameters:
      o - The object to convert.
      Returns:
      A String representation of the given object.
    • getFromString

      public static <T> T getFromString(String s, Class<T> clazz)
      Converts a String
      Type Parameters:
      T - The generic type of the object.
      Parameters:
      s - The String to parse.
      clazz - The type of the object you wish to parse.
      Returns:
      An object
    • registerTypeConverter

      public static <T> void registerTypeConverter(Class<T> clazz, Function<T,String> converterTo, Function<String,T> converterFrom)
      Register a type converterTo used to determine how to convert an object of the given Class to a String which can be used in MySQL queries.
      Type Parameters:
      T - The type of objects to accept.
      Parameters:
      clazz - The type of objects this converterTo can accept, objects extending this class must be registered separately.
      converterTo - The function accepting the given type and outputting its String representation.
    • readQuotedString

      public static String readQuotedString(String s)
      Reads a String starting with a single quote until the next quote.
      Parameters:
      s - The String to read.
      Returns:
      The String between the first set of quotes found.
    • singletonMap

      public static <K, V> Map<K,V> singletonMap(K key, V value)
    • checkNotNull

      public static <T> T checkNotNull(T reference, Object errorMessage)