More suggested improvements; commented out getEntries method for now.

This commit is contained in:
Peter Miller 2014-06-11 02:35:44 -04:00
parent 7bdaaa8ae0
commit 01fbda81ba
2 changed files with 87 additions and 5 deletions

View File

@ -23,13 +23,14 @@ public class SimpleStats extends MiniPlugin
_repository.initialize();
}
/*
public NautHashMap<String, String> getEntries()
{
synchronized (_transferLock)
{
return _entries;
}
}
}*/
@EventHandler
public void storeStatsUpdate(final UpdateEvent updateEvent)
@ -65,4 +66,22 @@ public class SimpleStats extends MiniPlugin
}
});
}
public NautHashMap<String, String> getStat(String statName)
{
final String statNameFinal = statName;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
synchronized (_transferLock)
{
_entries = _repository.retrieveStat(statNameFinal);
}
}
});
return _entries;
}
}

View File

@ -17,8 +17,9 @@ public class SimpleStatsRepository
private String _password = "tAbechAk3wR7tuTh"; //try to obfuscate this in the future!
private static String CREATE_STATS_TABLE = "CREATE TABLE IF NOT EXISTS simpleStats (id INT NOT NULL AUTO_INCREMENT, statName VARCHAR(64), statValue VARCHAR(64), PRIMARY KEY (id));";
private static String RETRIEVE_STATS_RECORDS = "SELECT statName.*, statValue.* FROM simpleStats;";
private static String STORE_STATS_RECORD = "INSERT INTO simpleStats (statName,statValue) VALUES(";
private static String RETRIEVE_STATS_RECORDS = "SELECT simpleStats.statName, simpleStats.statValue FROM simpleStats;";
private static String STORE_STATS_RECORD = "INSERT INTO simpleStats (statName,statValue) VALUES(?,?);";
private static String RETRIEVE_STAT_RECORD = "SELECT simpleStats.statName, simpleStats.statValue FROM simpleStats WHERE statName = '?';";
private Connection _connection = null;
@ -127,8 +128,10 @@ public class SimpleStatsRepository
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(STORE_STATS_RECORD + statName + "," + statValue + ");");
preparedStatement = _connection.prepareStatement(STORE_STATS_RECORD);
preparedStatement.setString(1, statName);
preparedStatement.setString(2, statValue);
preparedStatement.executeUpdate();
}
@ -152,4 +155,64 @@ public class SimpleStatsRepository
}
}
}
public NautHashMap<String, String> retrieveStat(String statName)
{
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
try
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_STAT_RECORD);
preparedStatement.setString(1, statName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return statRecords;
}
}