Class LocalDatabase

java.lang.Object
io.github.sigmacasino.LocalDatabase
All Implemented Interfaces:
AutoCloseable

public class LocalDatabase extends Object implements AutoCloseable
A simple wrapper around a PostgreSQL database connection. It is used to connect to a local database and run SQL scripts/queries.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Connection
    The JDBC connection to the PostgreSQL database.
    private String
    The name of the database to connect to.
    private org.slf4j.Logger
    The logger for this class.
    private int
    The port number of the PostgreSQL database.
    private String
    The username to connect to the PostgreSQL database.
  • Constructor Summary

    Constructors
    Constructor
    Description
    LocalDatabase(int port, String user, String password, String database)
    Creates a new LocalDatabase object and connects to the PostgreSQL database.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    private void
    connect(String password)
    Connects to the PostgreSQL database using the given password via JDBC.
    Gets the raw connection to the PostgreSQL database.
    Gets the name of the database to connect to.
    int
    Gets the port number of the PostgreSQL database.
    Gets the username to connect to the PostgreSQL database.
    prepareStatement(String sqlTemplate)
    Prepares a statement for the given SQL template.
    void
    runScript(String script)
    Runs the given SQL script by splitting it into individual queries and executing them in a batch statement.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • connection

      private Connection connection
      The JDBC connection to the PostgreSQL database.
    • port

      private int port
      The port number of the PostgreSQL database.
    • user

      private String user
      The username to connect to the PostgreSQL database.
    • database

      private String database
      The name of the database to connect to.
    • logger

      private org.slf4j.Logger logger
      The logger for this class.
  • Constructor Details

    • LocalDatabase

      public LocalDatabase(int port, String user, String password, String database)
      Creates a new LocalDatabase object and connects to the PostgreSQL database.
      Parameters:
      port - the port number of the PostgreSQL database
      user - the username to connect to the PostgreSQL database
      password - the password to connect to the PostgreSQL database
      database - the name of the database to connect to
  • Method Details

    • getConnection

      public Connection getConnection()
      Gets the raw connection to the PostgreSQL database.
      Returns:
      the connection to the PostgreSQL database
    • getPort

      public int getPort()
      Gets the port number of the PostgreSQL database.
      Returns:
      the port number of the PostgreSQL database
    • getUser

      public String getUser()
      Gets the username to connect to the PostgreSQL database.
      Returns:
      the username to connect to the PostgreSQL database
    • getDatabaseName

      public String getDatabaseName()
      Gets the name of the database to connect to.
      Returns:
      the name of the database to connect to
    • connect

      private void connect(String password)
      Connects to the PostgreSQL database using the given password via JDBC. If the connection fails, it will retry 5 times with an exponential backoff, starting from 1s and doubling for each failure. If the connection is successful, it will log the success.
      Parameters:
      password - the password to connect to the PostgreSQL database
    • prepareStatement

      public PreparedStatement prepareStatement(String sqlTemplate)
      Prepares a statement for the given SQL template.
      Parameters:
      sqlTemplate - the SQL template to prepare a statement for
      Returns:
      the prepared statement for the given SQL template
      See Also:
    • runScript

      public void runScript(String script)
      Runs the given SQL script by splitting it into individual queries and executing them in a batch statement. It filters out comments and empty lines.
      Parameters:
      script - the SQL script contents
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception