Added more checks for failed database connections.

Should prevent unnecessary null pointer exceptions if anything goes wrong.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-03-07 20:39:27 -05:00
parent 8c24ef97c7
commit c6041d8cba
3 changed files with 92 additions and 36 deletions

View File

@ -20,6 +20,10 @@ package itdelatrisu.opsu.db;
import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.ErrorHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** /**
* Database controller. * Database controller.
*/ */
@ -50,4 +54,19 @@ public class DBController {
OsuDB.closeConnection(); OsuDB.closeConnection();
ScoreDB.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;
}
}
} }

View File

@ -25,7 +25,6 @@ import itdelatrisu.opsu.OsuParser;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -58,12 +57,9 @@ public class OsuDB {
*/ */
public static void init() { public static void init() {
// create a database connection // create a database connection
try { connection = DBController.createConnection(Options.OSU_DB.getPath());
connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.OSU_DB.getPath())); if (connection == null)
} catch (SQLException e) { return;
// 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);
}
// create the database // create the database
createDatabase(); createDatabase();
@ -124,6 +120,9 @@ public class OsuDB {
* from the current version, then updates the version field. * from the current version, then updates the version field.
*/ */
private static void checkVersion() { private static void checkVersion() {
if (connection == null)
return;
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
// get the stored version // get the stored version
String sql = "SELECT value FROM info WHERE key = 'version'"; String sql = "SELECT value FROM info WHERE key = 'version'";
@ -149,6 +148,9 @@ public class OsuDB {
* Clears the database. * Clears the database.
*/ */
public static void clearDatabase() { public static void clearDatabase() {
if (connection == null)
return;
// drop the table, then recreate it // drop the table, then recreate it
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
String sql = "DROP TABLE beatmaps"; String sql = "DROP TABLE beatmaps";
@ -164,6 +166,9 @@ public class OsuDB {
* @param osu the OsuFile object * @param osu the OsuFile object
*/ */
public static void insert(OsuFile osu) { public static void insert(OsuFile osu) {
if (connection == null)
return;
try { try {
setStatementFields(insertStmt, osu); setStatementFields(insertStmt, osu);
insertStmt.executeUpdate(); insertStmt.executeUpdate();
@ -177,6 +182,9 @@ public class OsuDB {
* @param batch a list of OsuFile objects * @param batch a list of OsuFile objects
*/ */
public static void insert(List<OsuFile> batch) { public static void insert(List<OsuFile> batch) {
if (connection == null)
return;
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
// turn off auto-commit mode // turn off auto-commit mode
boolean autoCommit = connection.getAutoCommit(); boolean autoCommit = connection.getAutoCommit();
@ -258,8 +266,12 @@ public class OsuDB {
* Returns an OsuFile from the database, or null if any error occurred. * Returns an OsuFile from the database, or null if any error occurred.
* @param dir the directory * @param dir the directory
* @param file the file * @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) { public static OsuFile getOsuFile(File dir, File file) {
if (connection == null)
return null;
try { try {
OsuFile osu = new OsuFile(file); OsuFile osu = new OsuFile(file);
selectStmt.setString(1, dir.getName()); selectStmt.setString(1, dir.getName());
@ -316,6 +328,9 @@ public class OsuDB {
* null if any error occurred. * null if any error occurred.
*/ */
public static Map<String, Long> getLastModifiedMap() { public static Map<String, Long> getLastModifiedMap() {
if (connection == null)
return null;
try { try {
Map<String, Long> map = new HashMap<String, Long>(); Map<String, Long> map = new HashMap<String, Long>();
ResultSet rs = lastModStmt.executeQuery(); ResultSet rs = lastModStmt.executeQuery();
@ -338,6 +353,9 @@ public class OsuDB {
* @param file the file * @param file the file
*/ */
public static void delete(String dir, String file) { public static void delete(String dir, String file) {
if (connection == null)
return;
try { try {
deleteMapStmt.setString(1, dir); deleteMapStmt.setString(1, dir);
deleteMapStmt.setString(2, file); deleteMapStmt.setString(2, file);
@ -352,6 +370,9 @@ public class OsuDB {
* @param dir the directory * @param dir the directory
*/ */
public static void delete(String dir) { public static void delete(String dir) {
if (connection == null)
return;
try { try {
deleteGroupStmt.setString(1, dir); deleteGroupStmt.setString(1, dir);
deleteGroupStmt.executeUpdate(); deleteGroupStmt.executeUpdate();
@ -364,7 +385,9 @@ public class OsuDB {
* Closes the connection to the database. * Closes the connection to the database.
*/ */
public static void closeConnection() { public static void closeConnection() {
if (connection != null) { if (connection == null)
return;
try { try {
insertStmt.close(); insertStmt.close();
lastModStmt.close(); lastModStmt.close();
@ -372,9 +395,9 @@ public class OsuDB {
deleteMapStmt.close(); deleteMapStmt.close();
deleteGroupStmt.close(); deleteGroupStmt.close();
connection.close(); connection.close();
connection = null;
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to close beatmap database.", e, true); ErrorHandler.error("Failed to close beatmap database.", e, true);
} }
} }
} }
}

View File

@ -24,7 +24,6 @@ import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.ScoreData; import itdelatrisu.opsu.ScoreData;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -60,12 +59,9 @@ public class ScoreDB {
*/ */
public static void init() { public static void init() {
// create a database connection // create a database connection
try { connection = DBController.createConnection(Options.SCORE_DB.getPath());
connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s", Options.SCORE_DB.getPath())); if (connection == null)
} catch (SQLException e) { return;
// 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);
}
// create the database // create the database
createDatabase(); createDatabase();
@ -127,6 +123,9 @@ public class ScoreDB {
* @param data the GameData object * @param data the GameData object
*/ */
public static void addScore(ScoreData data) { public static void addScore(ScoreData data) {
if (connection == null)
return;
try { try {
setStatementFields(insertStmt, data); setStatementFields(insertStmt, data);
insertStmt.executeUpdate(); insertStmt.executeUpdate();
@ -140,6 +139,9 @@ public class ScoreDB {
* @param data the score to delete * @param data the score to delete
*/ */
public static void deleteScore(ScoreData data) { public static void deleteScore(ScoreData data) {
if (connection == null)
return;
try { try {
setStatementFields(deleteScoreStmt, data); setStatementFields(deleteScoreStmt, data);
deleteScoreStmt.executeUpdate(); deleteScoreStmt.executeUpdate();
@ -153,6 +155,9 @@ public class ScoreDB {
* @param osu the OsuFile object * @param osu the OsuFile object
*/ */
public static void deleteScore(OsuFile osu) { public static void deleteScore(OsuFile osu) {
if (connection == null)
return;
try { try {
deleteSongStmt.setInt(1, osu.beatmapID); deleteSongStmt.setInt(1, osu.beatmapID);
deleteSongStmt.setString(2, osu.title); deleteSongStmt.setString(2, osu.title);
@ -195,9 +200,12 @@ public class ScoreDB {
/** /**
* Retrieves the game scores for an OsuFile map. * Retrieves the game scores for an OsuFile map.
* @param osu the OsuFile * @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) { public static ScoreData[] getMapScores(OsuFile osu) {
if (connection == null)
return null;
List<ScoreData> list = new ArrayList<ScoreData>(); List<ScoreData> list = new ArrayList<ScoreData>();
try { try {
selectMapStmt.setInt(1, osu.beatmapID); selectMapStmt.setInt(1, osu.beatmapID);
@ -221,9 +229,13 @@ public class ScoreDB {
/** /**
* Retrieves the game scores for an OsuFile map set. * Retrieves the game scores for an OsuFile map set.
* @param osu the OsuFile * @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<String, ScoreData[]> getMapSetScores(OsuFile osu) { public static Map<String, ScoreData[]> getMapSetScores(OsuFile osu) {
if (connection == null)
return null;
Map<String, ScoreData[]> map = new HashMap<String, ScoreData[]>(); Map<String, ScoreData[]> map = new HashMap<String, ScoreData[]>();
try { try {
selectMapSetStmt.setInt(1, osu.beatmapSetID); selectMapSetStmt.setInt(1, osu.beatmapSetID);
@ -267,15 +279,17 @@ public class ScoreDB {
* Closes the connection to the database. * Closes the connection to the database.
*/ */
public static void closeConnection() { public static void closeConnection() {
if (connection != null) { if (connection == null)
return;
try { try {
insertStmt.close(); insertStmt.close();
selectMapStmt.close(); selectMapStmt.close();
selectMapSetStmt.close(); selectMapSetStmt.close();
connection.close(); connection.close();
connection = null;
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to close score database.", e, true); ErrorHandler.error("Failed to close score database.", e, true);
} }
} }
} }
}