Class AutoCatchingSQLConnection<T>

java.lang.Object
com.marcpg.libpg.data.database.sql.SQLConnection<T>
com.marcpg.libpg.data.database.sql.AutoCatchingSQLConnection<T>
Type Parameters:
T - The primary key's type.
Direct Known Subclasses:
DummySQLConnection

public class AutoCatchingSQLConnection<T> extends SQLConnection<T>
Modified version of the SQLConnection, but with the key difference of throwing no exceptions and instead handling them automatically inside the methods, using a code snipped provided in the constructor or optionally modified using changeExceptionHandling(Consumer). Can break if the code if the way of handling the exceptions is invalid.
Since:
0.0.6
  • Constructor Details

    • AutoCatchingSQLConnection

      public AutoCatchingSQLConnection(SQLConnection.DatabaseType type, String url, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) throws SQLException, ClassNotFoundException
      Creates a connection to a SQL compatible database, that automatically catches SQL exceptions.
      Parameters:
      type - The database type.
      url - The URL to the database (jdbc:type://ip:port/name)
      username - Username of the account to access the database with.
      password - Password of the account to access the database with.
      table - The table in the database that will accessed. Can be changed later on using SQLConnection.changeTable(String).
      primaryKeyName - The primary key's name.
      exceptionHandling - What should be done with exceptions, in the rare case of them occurring.
      Throws:
      SQLException - if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.
      ClassNotFoundException - if the dependency of the database type is missing.
      See Also:
    • AutoCatchingSQLConnection

      public AutoCatchingSQLConnection(SQLConnection.DatabaseType type, String ip, int port, String databaseName, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) throws SQLException, ClassNotFoundException
      Creates a connection to a SQL compatible database, that automatically catches SQL exceptions.
      Parameters:
      type - The database type.
      ip - The IP of the database. Can be localhost.
      port - The port that the database runs on. If set to 0, will use the database type's default port.
      databaseName - The database to name, this should connect to.
      username - Username of the account to access the database with.
      password - Password of the account to access the database with.
      table - The table in the database that will accessed. Can be changed later on using SQLConnection.changeTable(String).
      primaryKeyName - The primary key's name.
      exceptionHandling - What should be done with exceptions, in the rare case of them occurring.
      Throws:
      SQLException - if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.
      ClassNotFoundException - if the dependency of the database type is missing.
      See Also:
  • Method Details

    • closeConnection

      public void closeConnection()
      Description copied from class: SQLConnection
      Closes the connection to the database. Recommended to use this before shutting down the program, to not cause any connection issues with the database.
      Overrides:
      closeConnection in class SQLConnection<T>
    • createNewStatement

      public void createNewStatement()
      Description copied from class: SQLConnection
      Generates a new statement to use for queries. Can be useful for clearing some memory.
      Overrides:
      createNewStatement in class SQLConnection<T>
    • changeExceptionHandling

      public void changeExceptionHandling(Consumer<SQLException> exceptionHandling)
      Changes what should be done with the SQLExceptions, when they are caught. No usage case for normal users.
      Parameters:
      exceptionHandling - The new way of handling the SQLExceptions.
    • executeQuery

      public <T2> T2 executeQuery(String sql, Object @NotNull ... params)
      Description copied from class: SQLConnection
      Executes a parameterized SQL query and returns the result. When the result has multiple values, it will choose the first column index.
      Overrides:
      executeQuery in class SQLConnection<T>
      Type Parameters:
      T2 - The type of the result to be returned.
      Parameters:
      sql - The parameterized query to be executed.
      params - The parameters that should be set into the sql query input.
      Returns:
      The result of the query execution, or null if it's empty.
    • getRowArray

      public Object[] getRowArray(T primaryKey)
      Description copied from class: SQLConnection
      Get a whole row from the table based on the primary key.
      Overrides:
      getRowArray in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the row from.
      Returns:
      All objects of the row as an array.
    • getRowMap

      public Map<String,Object> getRowMap(T primaryKey)
      Description copied from class: SQLConnection
      Get a whole row with column names and values from the table based on the primary key.
      Overrides:
      getRowMap in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the row from.
      Returns:
      All objects of the row as a map.
    • getRow

      public ResultSet getRow(T primaryKey)
      Description copied from class: SQLConnection
      Get the result of getting a row based on the primary key.
      Overrides:
      getRow in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the row from.
      Returns:
      The result of the query.
    • get

      public Object get(T primaryKey, String column)
      Description copied from class: SQLConnection
      Get a specified column, based on the primary key and the column name.
      Overrides:
      get in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the value from.
      column - The column/value's name.
      Returns:
      The value in the specified field.
    • get

      public Object get(T primaryKey, int column)
      Description copied from class: SQLConnection
      Get a specified column, based on the primary key and the column index.
      Overrides:
      get in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the value from.
      column - The column/value's index (starting at 1).
      Returns:
      The value in the specified field.
    • set

      public void set(T primaryKey, String column, Object newValue)
      Description copied from class: SQLConnection
      Set a value of a specified row to a new value, based on the primary key and the column name.
      Overrides:
      set in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the value from.
      column - The column/value's name.
      newValue - The new value for the specified field.
    • set

      public void set(T primaryKey, int column, Object newValue)
      Description copied from class: SQLConnection
      Set a value of a specified row to a new value, based on the primary key and the column index.
      Overrides:
      set in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to get the value from.
      column - The column/value's index (starting at 1).
      newValue - The new value for the specified field.
    • add

      public void add(@NotNull @NotNull Map<String,Object> values)
      Description copied from class: SQLConnection
      Inserts a new row with the specified values into the table.
      Overrides:
      add in class SQLConnection<T>
      Parameters:
      values - All values for the new row. The keys are the column names and the values are the values. Has to be valid according to database settings, otherwise throws an Exception.
    • remove

      public void remove(T primaryKey)
      Description copied from class: SQLConnection
      Removed a row based on the primary key.
      Overrides:
      remove in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to remove.
    • contains

      public boolean contains(T primaryKey)
      Description copied from class: SQLConnection
      Checks whether the table contains an entry/row with the primary key or not.
      Overrides:
      contains in class SQLConnection<T>
      Parameters:
      primaryKey - The primary key to check for.
      Returns:
      true if the table contains the primary key, false otherwise.
    • getAllRowArrays

      public List<Object[]> getAllRowArrays()
      Description copied from class: SQLConnection
      Get all rows of the table, where the list represents the rows and the arrays are each row's columns.
      Overrides:
      getAllRowArrays in class SQLConnection<T>
      Returns:
      A list containing all columns as an array.
    • getAllRowMaps

      public List<Map<String,Object>> getAllRowMaps()
      Description copied from class: SQLConnection
      Get all rows of the table, where the list represents the rows and the maps are each row's columns with their name first and then their value.
      Overrides:
      getAllRowMaps in class SQLConnection<T>
      Returns:
      A map containing all columns with their names and values.
    • getRowArraysContaining

      public List<Object[]> getRowArraysContaining(Object object, String... checkedColumns)
      Description copied from class: SQLConnection
      Get all rows of the table containing a specific value in any specified column as a List of arrays, where the list represents the rows and the arrays are each row's columns.
      Overrides:
      getRowArraysContaining in class SQLConnection<T>
      Parameters:
      object - What object must be contained in at least one of the rows.
      checkedColumns - All columns that are checked for the object.
      Returns:
      A list containing all columns as an array.
    • getRowMapsContaining

      public List<Map<String,Object>> getRowMapsContaining(Object object, String... checkedColumns)
      Description copied from class: SQLConnection
      Get all rows of the table containing a specific value in any specified column as a List of maps, where the list represents the rows and the maps are each row's columns with their name first and then their value.
      Overrides:
      getRowMapsContaining in class SQLConnection<T>
      Parameters:
      object - What object must be contained in at least one of the rows.
      checkedColumns - All columns that are checked for the object.
      Returns:
      A map containing all columns with their names and values.
    • getRowArraysMatching

      public Collection<Object[]> getRowArraysMatching(String wherePredicate, Object @NotNull ... replacements)
      Description copied from class: SQLConnection
      Get all rows matching a specified WHERE predicate as a Collection of arrays, where the list represents the rows and the arrays are each row's columns with their name first and then their value.
      Overrides:
      getRowArraysMatching in class SQLConnection<T>
      Parameters:
      wherePredicate - The SQL WHERE predicate, like column = ? AND another_column = ?. This is inserted right after the WHERE.
      replacements - What objects the question marks should be replaced with.
      Returns:
      All rows that match the WHERE predicate as arrays.
    • getRowMapsMatching

      public Collection<Map<String,Object>> getRowMapsMatching(String wherePredicate, Object @NotNull ... replacements)
      Description copied from class: SQLConnection
      Get all rows matching a specified WHERE predicate as a Collection of arrays, where the collection represents the rows and the arrays are each row's columns with their name first and then their value.
      Overrides:
      getRowMapsMatching in class SQLConnection<T>
      Parameters:
      wherePredicate - The SQL WHERE predicate, like column = ? AND another_column = ?. This is inserted right after the WHERE.
      replacements - What objects the question marks should be replaced with.
      Returns:
      All rows that match the WHERE predicate as arrays.