Added basic database manager

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-27 16:22:23 +01:00
parent 45bf4b1eee
commit 8d7ca0ad33

View File

@ -0,0 +1,158 @@
package xyz.vallat.louis.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.MovieQuoteBot;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public final class DBManager {
private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName());
private static final String DRIVER_CLASS = "org.posgresql.Driver";
private static final String CONNECTION_PREFIX = "postgresql";
private static String username;
private static String password;
private static String database;
private static int port = 5432;
private static String host;
private DBManager() {
}
public static void setLoginInformation(String host, String database, String username, String password) {
DBManager.host = host;
DBManager.database = database;
DBManager.username = username;
DBManager.password = password;
}
public static void setLoginInformation(String host, int port, String database, String username, String password) {
DBManager.port = port;
setLoginInformation(host, database, username, password);
}
// TODO: EXIT CODES AS ENUM
private static Connection getConnection() {
try {
Class.forName(DRIVER_CLASS);
return DriverManager.getConnection("jdbc:" + CONNECTION_PREFIX + "://" + host + ":" + port + "/"
+ database, username, password);
} catch (ClassNotFoundException | SQLException e) {
logger.error("Could not connect to database. Reason: {}.", e.getMessage());
System.exit(4);
}
return null;
}
public static void testConnection() {
logger.debug("Testing database connection.");
getConnection();
logger.info("Database connection OK.");
}
public static void initDatabase() {
logger.debug("Initializing database if not done yet.");
Connection connection = getConnection();
try {
initializeApplication(connection);
initializeFilm(connection);
initializeSubtitle(connection);
initializeSubtitleLine(connection);
initializeLanguages(connection);
} catch (SQLException e) {
logger.error("An error happened while initializing the database. Reason: {}", e.getMessage());
System.exit(5);
}
}
private static void initializeApplication(Connection connection) throws SQLException {
logger.debug("Creating application table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS application\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" app_key text NOT NULL,\n" +
" app_value text NOT NULL,\n" +
" PRIMARY KEY (id),\n" +
" UNIQUE (app_key)\n" +
");";
stmt.executeUpdate(query);
}
}
private static void initializeFilm(Connection connection) throws SQLException {
logger.debug("Creating film table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS film\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" imdb_id varchar(10) NOT NULL,\n" +
" title text NOT NULL,\n" +
" release_date date,\n" +
" poster_link text,\n" +
" PRIMARY KEY (id)\n" +
");";
stmt.executeUpdate(query);
}
}
private static void initializeSubtitle(Connection connection) throws SQLException {
logger.debug("Creating subtitle table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS subtitle\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" film_id int NOT NULL,\n" +
" language_id int NOT NULL,\n" +
" importer varchar(37),\n" +
" imported_date timestamptz NOT NULL DEFAULT now(),\n" +
" UNIQUE (film_id, language_id),\n" +
" PRIMARY KEY (id),\n" +
" FOREIGN KEY (film_id)\n" +
" REFERENCES Film (id),\n" +
" FOREIGN KEY (language_id)\n" +
" REFERENCES language (id)\n" +
");";
stmt.executeUpdate(query);
}
}
private static void initializeSubtitleLine(Connection connection) throws SQLException {
logger.debug("Creating subtitle_line table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS subtitle_line\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" subtitle_id int NOT NULL,\n" +
" dialog_line text NOT NULL,\n" +
" time_code text NOT NULL,\n" +
" PRIMARY KEY (id),\n" +
" FOREIGN KEY (subtitle_id)\n" +
" REFERENCES subtitle (id)\n" +
");";
stmt.executeUpdate(query);
}
}
private static void initializeLanguages(Connection connection) throws SQLException {
logger.debug("Creating languages table.");
try (Statement stmt = connection.createStatement()) {
String query = "CREATE TABLE IF NOT EXISTS language\n" +
"(\n" +
" id int GENERATED ALWAYS AS IDENTITY,\n" +
" alpha3_b char(3) NOT NULL,\n" +
" alpha3_t char(3),\n" +
" alpha2 char(2),\n" +
" english text NOT NULL,\n" +
" french text NOT NULL,\n" +
" PRIMARY KEY (id)\n" +
");";
stmt.executeUpdate(query);
}
}
}