diff --git a/src/itdelatrisu/opsu/db/DBController.java b/src/itdelatrisu/opsu/db/DBController.java index f87bca6b..05841b6c 100644 --- a/src/itdelatrisu/opsu/db/DBController.java +++ b/src/itdelatrisu/opsu/db/DBController.java @@ -20,6 +20,10 @@ package itdelatrisu.opsu.db; import itdelatrisu.opsu.ErrorHandler; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + /** * Database controller. */ @@ -50,4 +54,19 @@ public class DBController { OsuDB.closeConnection(); ScoreDB.closeConnection(); } + + /** + * Establishes a connection to the database given by the path string. + * @param path the database path + * @return the Connection, or null if a connection could not be established + */ + public static Connection createConnection(String path) { + try { + return DriverManager.getConnection(String.format("jdbc:sqlite:%s", path)); + } catch (SQLException e) { + // if the error message is "out of memory", it probably means no database file is found + ErrorHandler.error(String.format("Could not connect to database: '%s'.", path), e, true); + return null; + } + } } diff --git a/src/itdelatrisu/opsu/db/OsuDB.java b/src/itdelatrisu/opsu/db/OsuDB.java index 3ff3ccc7..1e5e085e 100644 --- a/src/itdelatrisu/opsu/db/OsuDB.java +++ b/src/itdelatrisu/opsu/db/OsuDB.java @@ -25,7 +25,6 @@ import itdelatrisu.opsu.OsuParser; import java.io.File; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -58,12 +57,9 @@ public class OsuDB { */ public static void init() { // create a database connection - try { - connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.OSU_DB.getPath())); - } catch (SQLException e) { - // if the error message is "out of memory", it probably means no database file is found - ErrorHandler.error("Could not connect to beatmap database.", e, true); - } + connection = DBController.createConnection(Options.OSU_DB.getPath()); + if (connection == null) + return; // create the database createDatabase(); @@ -124,6 +120,9 @@ public class OsuDB { * from the current version, then updates the version field. */ private static void checkVersion() { + if (connection == null) + return; + try (Statement stmt = connection.createStatement()) { // get the stored version String sql = "SELECT value FROM info WHERE key = 'version'"; @@ -149,6 +148,9 @@ public class OsuDB { * Clears the database. */ public static void clearDatabase() { + if (connection == null) + return; + // drop the table, then recreate it try (Statement stmt = connection.createStatement()) { String sql = "DROP TABLE beatmaps"; @@ -164,6 +166,9 @@ public class OsuDB { * @param osu the OsuFile object */ public static void insert(OsuFile osu) { + if (connection == null) + return; + try { setStatementFields(insertStmt, osu); insertStmt.executeUpdate(); @@ -177,6 +182,9 @@ public class OsuDB { * @param batch a list of OsuFile objects */ public static void insert(List batch) { + if (connection == null) + return; + try (Statement stmt = connection.createStatement()) { // turn off auto-commit mode boolean autoCommit = connection.getAutoCommit(); @@ -258,8 +266,12 @@ public class OsuDB { * Returns an OsuFile from the database, or null if any error occurred. * @param dir the directory * @param file the file + * @return the OsuFile with all fields filled, or null if any error occurred */ public static OsuFile getOsuFile(File dir, File file) { + if (connection == null) + return null; + try { OsuFile osu = new OsuFile(file); selectStmt.setString(1, dir.getName()); @@ -316,6 +328,9 @@ public class OsuDB { * null if any error occurred. */ public static Map getLastModifiedMap() { + if (connection == null) + return null; + try { Map map = new HashMap(); ResultSet rs = lastModStmt.executeQuery(); @@ -338,6 +353,9 @@ public class OsuDB { * @param file the file */ public static void delete(String dir, String file) { + if (connection == null) + return; + try { deleteMapStmt.setString(1, dir); deleteMapStmt.setString(2, file); @@ -352,6 +370,9 @@ public class OsuDB { * @param dir the directory */ public static void delete(String dir) { + if (connection == null) + return; + try { deleteGroupStmt.setString(1, dir); deleteGroupStmt.executeUpdate(); @@ -364,17 +385,19 @@ public class OsuDB { * Closes the connection to the database. */ public static void closeConnection() { - if (connection != null) { - try { - insertStmt.close(); - lastModStmt.close(); - selectStmt.close(); - deleteMapStmt.close(); - deleteGroupStmt.close(); - connection.close(); - } catch (SQLException e) { - ErrorHandler.error("Failed to close beatmap database.", e, true); - } + if (connection == null) + return; + + try { + insertStmt.close(); + lastModStmt.close(); + selectStmt.close(); + deleteMapStmt.close(); + deleteGroupStmt.close(); + connection.close(); + connection = null; + } catch (SQLException e) { + ErrorHandler.error("Failed to close beatmap database.", e, true); } } } diff --git a/src/itdelatrisu/opsu/db/ScoreDB.java b/src/itdelatrisu/opsu/db/ScoreDB.java index 00f77579..25976bb9 100644 --- a/src/itdelatrisu/opsu/db/ScoreDB.java +++ b/src/itdelatrisu/opsu/db/ScoreDB.java @@ -24,7 +24,6 @@ import itdelatrisu.opsu.OsuFile; import itdelatrisu.opsu.ScoreData; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -60,12 +59,9 @@ public class ScoreDB { */ public static void init() { // create a database connection - try { - connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.SCORE_DB.getPath())); - } catch (SQLException e) { - // if the error message is "out of memory", it probably means no database file is found - ErrorHandler.error("Could not connect to score database.", e, true); - } + connection = DBController.createConnection(Options.SCORE_DB.getPath()); + if (connection == null) + return; // create the database createDatabase(); @@ -127,6 +123,9 @@ public class ScoreDB { * @param data the GameData object */ public static void addScore(ScoreData data) { + if (connection == null) + return; + try { setStatementFields(insertStmt, data); insertStmt.executeUpdate(); @@ -140,6 +139,9 @@ public class ScoreDB { * @param data the score to delete */ public static void deleteScore(ScoreData data) { + if (connection == null) + return; + try { setStatementFields(deleteScoreStmt, data); deleteScoreStmt.executeUpdate(); @@ -153,6 +155,9 @@ public class ScoreDB { * @param osu the OsuFile object */ public static void deleteScore(OsuFile osu) { + if (connection == null) + return; + try { deleteSongStmt.setInt(1, osu.beatmapID); deleteSongStmt.setString(2, osu.title); @@ -195,9 +200,12 @@ public class ScoreDB { /** * Retrieves the game scores for an OsuFile map. * @param osu the OsuFile - * @return all scores for the beatmap + * @return all scores for the beatmap, or null if any error occurred */ public static ScoreData[] getMapScores(OsuFile osu) { + if (connection == null) + return null; + List list = new ArrayList(); try { selectMapStmt.setInt(1, osu.beatmapID); @@ -221,9 +229,13 @@ public class ScoreDB { /** * Retrieves the game scores for an OsuFile map set. * @param osu the OsuFile - * @return all scores for the beatmap set (Version, ScoreData[]) + * @return all scores for the beatmap set (Version, ScoreData[]), + * or null if any error occurred */ public static Map getMapSetScores(OsuFile osu) { + if (connection == null) + return null; + Map map = new HashMap(); try { selectMapSetStmt.setInt(1, osu.beatmapSetID); @@ -267,15 +279,17 @@ public class ScoreDB { * Closes the connection to the database. */ public static void closeConnection() { - if (connection != null) { - try { - insertStmt.close(); - selectMapStmt.close(); - selectMapSetStmt.close(); - connection.close(); - } catch (SQLException e) { - ErrorHandler.error("Failed to close score database.", e, true); - } + if (connection == null) + return; + + try { + insertStmt.close(); + selectMapStmt.close(); + selectMapSetStmt.close(); + connection.close(); + connection = null; + } catch (SQLException e) { + ErrorHandler.error("Failed to close score database.", e, true); } } }