Package io.github.sigmacasino
Class LocalDatabase
java.lang.Object
io.github.sigmacasino.LocalDatabase
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprivate 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
ConstructorsConstructorDescriptionLocalDatabase
(int port, String user, String password, String database) Creates a new LocalDatabase object and connects to the PostgreSQL database. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
private void
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
getPort()
Gets the port number of the PostgreSQL database.getUser()
Gets the username to connect to the PostgreSQL database.prepareStatement
(String sqlTemplate) Prepares a statement for the given SQL template.void
Runs the given SQL script by splitting it into individual queries and executing them in a batch statement.
-
Field Details
-
connection
The JDBC connection to the PostgreSQL database. -
port
private int portThe port number of the PostgreSQL database. -
user
The username to connect to the PostgreSQL database. -
database
The name of the database to connect to. -
logger
private org.slf4j.Logger loggerThe logger for this class.
-
-
Constructor Details
-
LocalDatabase
Creates a new LocalDatabase object and connects to the PostgreSQL database.- Parameters:
port
- the port number of the PostgreSQL databaseuser
- the username to connect to the PostgreSQL databasepassword
- the password to connect to the PostgreSQL databasedatabase
- the name of the database to connect to
-
-
Method Details
-
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
Gets the username to connect to the PostgreSQL database.- Returns:
- the username to connect to the PostgreSQL database
-
getDatabaseName
Gets the name of the database to connect to.- Returns:
- the name of the database to connect to
-
connect
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
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
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
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-