Class Database

java.lang.Object
com.github.tadukoo.database.mysql.Database

public class Database extends Object
A class used to connect to a MySQL database and make queries, updates, etc. to it.
Since:
Alpha v.0.1
Version:
Alpha v.0.3
Author:
Logan Ferree (Tadukoo)
  • Field Details

    • logger

      private final com.github.tadukoo.util.logger.EasyLogger logger
      The logger to use for logging
    • host

      private final String host
      The MySQL host url
    • port

      private final int port
      The MySQL host port
    • databaseName

      private final String databaseName
      The MySQL host database name
    • username

      private final String username
      The MySQL username for connecting to the database
    • password

      private final String password
      The MySQL password for connecting to the database
    • maxAttempts

      private final int maxAttempts
      The maximum number of attempts to try a SQL transaction before giving up
  • Constructor Details

    • Database

      private Database(com.github.tadukoo.util.logger.EasyLogger logger, String host, int port, String databaseName, String username, String password, int maxAttempts)
      Constructs a new Database with the given parameters
      Parameters:
      logger - The logger to use for logging
      host - The MySQL host url
      port - The MySQL host port
      databaseName - The MySQL host database name
      username - The MySQL username for connecting to the database
      password - The MySQL password for connecting to the database
      maxAttempts - The maximum number of attempts to try a SQL transaction before giving up
  • Method Details

    • builder

      public static Database.Logger builder()
      Returns:
      A new builder to use to make a Database
    • getLogger

      public com.github.tadukoo.util.logger.EasyLogger getLogger()
      Returns:
      The logger to use for logging
    • getConnectionURL

      public String getConnectionURL()
      Returns:
      The connection URL (includes host, port, databaseName, but not login credentials)
    • getMaxAttempts

      public int getMaxAttempts()
      Returns:
      The maximum number of attempts to try a SQL transaction before giving up
    • connect

      private Connection connect() throws SQLException
      Creates a Connection to a MySQL database with the url and login information that was set in the constructor of this Database class.
      Returns:
      The Connection that's been created
      Throws:
      SQLException - If anything goes wrong
    • executeTransaction

      public <ResultType> ResultType executeTransaction(SQLTransaction<ResultType> transaction) throws SQLException
      Runs a SQL transaction. Will attempt maxAttempts times until it works, before throwing a SQLException if it doesn't work in that many attempts.
      Type Parameters:
      ResultType - The type of result to be returned
      Parameters:
      transaction - The SQLTransaction to run
      Returns:
      The result from the transaction
      Throws:
      SQLException - If anything goes wrong
    • executeQuery

      public <ResultType> ResultType executeQuery(String name, String sql, com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet,ResultType,SQLException> convertFromResultSet) throws SQLException
      Executes a sql query after building a Query object for it from the given pieces. Returns the result of the query.
      Type Parameters:
      ResultType - The type of result to be returned
      Parameters:
      name - The name to use for the query (for debugging purposes - may be null)
      sql - The sql query string to run
      convertFromResultSet - The ThrowingFunction to use to run the query
      Returns:
      The result from the query
      Throws:
      SQLException - If anything goes wrong
    • executeUpdates

      public boolean executeUpdates(String transactionName, List<String> names, List<String> sqls) throws SQLException
      Executes sql updates and returns if they were a success. This version builds the Updates object using the given parameters.
      Parameters:
      transactionName - The name for the overall transaction
      names - The names to use for the updates (optional - used for debugging)
      sqls - The sql update statements to run
      Returns:
      If it succeeded or not
      Throws:
      SQLException - If anything goes wrong
    • executeUpdate

      public boolean executeUpdate(String name, String sql) throws SQLException
      Executes a single sql update and returns if it was a success.

      This version sends the name and statement to the plural version to create the Updates object.
      Parameters:
      name - The name to use for the update (optional - used for debugging)
      sql - The sql update statement to run
      Returns:
      If it succeeded or not
      Throws:
      SQLException - If anything goes wrong
    • insert

      public void insert(String table, Collection<String> cols, Collection<Object> values) throws SQLException
      Executes a single sql insert statement.

      This sends the table, cols, and values to SQLSyntaxUtil.formatInsertStatement(String, Collection, Collection) to create the insert statement and then uses executeUpdate(String, String) to run it
      Parameters:
      table - The name of the table to insert into
      cols - The names of the columns to insert into
      values - The values to be inserted
      Throws:
      SQLException - If anything goes wrong
    • insertAndGetID

      public Integer insertAndGetID(String table, String idColumnName, Collection<String> cols, Collection<Object> values) throws SQLException
      Executes a sql insert statement and then performs a query to retrieve an id (useful if the ID is auto-incremented).

      To do this, a InsertAndGetID transaction object is created using the given parameters
      Parameters:
      table - The name of the table to insert into
      idColumnName - The column name for the ID column
      cols - The column names for the columns to use in the insert
      values - The values to be inserted
      Returns:
      The ID found from the newly inserted data
      Throws:
      SQLException - If anything goes wrong
    • update

      public void update(String table, Collection<String> cols, Collection<Object> values, Collection<String> whereCols, Collection<Object> whereValues) throws SQLException
      Executes a single sql update statement

      This sends the table, cols, values, whereCols, and whereValues to SQLSyntaxUtil.formatUpdateStatement(String, Collection, Collection, Collection, Collection) to create the update statement and then uses executeUpdate(String, String) to run it
      Parameters:
      table - The name of the table to be updated
      cols - The names of the columns to be updated
      values - The values to use in the update
      whereCols - The names of the columns for the where statement (can be empty/null)
      whereValues - The values to use for the where statement (can be empty/null)
      Throws:
      SQLException - If anything goes wrong