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 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;
}
}
}

View File

@ -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<OsuFile> 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<String, Long> getLastModifiedMap() {
if (connection == null)
return null;
try {
Map<String, Long> map = new HashMap<String, Long>();
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);
}
}
}

View File

@ -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<ScoreData> list = new ArrayList<ScoreData>();
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<String, ScoreData[]> getMapSetScores(OsuFile osu) {
if (connection == null)
return null;
Map<String, ScoreData[]> map = new HashMap<String, ScoreData[]>();
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);
}
}
}