use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class API method getTotalOf.
/**
* Get the total count for a stat of a specific player on a specific world.
* <br>The worldName can also be omitted to get the total of all worlds.
* <br><br><b>This method will return null if Statz does not have info about this player regarding the specific stat.</b>
* @param statType the {@link PlayerStat} to get info of.
* @param uuid The UUID of the Player.
* @param worldName Name of the world to get the info from, can also be null to find the total on all worlds.
* @return the total count of a stat. E.g. the total amount of killed players on a world (or on all worlds).
*/
public Double getTotalOf(final PlayerStat statType, final UUID uuid, final String worldName) {
PlayerInfo info = plugin.getDataManager().getPlayerInfo(uuid, statType);
double value = 0;
List<Query> results = info.getDataOfPlayerStat(statType);
if (results == null || results.isEmpty())
return value;
if (worldName != null) {
// Add every value that is in the proper world
for (Query result : results) {
if (result.getValue("world") != null && result.getValue("world").toString().equalsIgnoreCase(worldName)) {
value += Double.parseDouble(result.getValue("value").toString());
}
}
} else {
// Add every value regardless of the world
for (Query result : results) {
value += Double.parseDouble(result.getValue("value").toString());
}
}
return value;
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class UpdatePlayerCacheTask method run.
@Override
public void run() {
PlayerInfo cachedData = new PlayerInfo(uuid);
for (PlayerStat statType : PlayerStat.values()) {
if (statType.equals(PlayerStat.PLAYERS)) {
continue;
}
PlayerInfo databaseInfo = plugin.getDataManager().getFreshPlayerInfo(uuid, statType);
// User is not loaded, or there is no cache so we don't bother overwriting the cache.
if (databaseInfo == null) {
continue;
}
// Remove all 'id' columns to prevent leakage from the database.
for (Query query : databaseInfo.getRows()) {
query.removeColumn("id");
}
cachedData = cachedData.resolveConflicts(databaseInfo);
}
plugin.debugMessage("Updated cache of " + uuid);
// Store into cache.
plugin.getCachingManager().registerCachedData(uuid, cachedData);
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class MySQLConnector method getObjects.
@Override
public List<Query> getObjects(Table table, RowRequirement... requirements) {
PreparedStatement ps = null;
ResultSet rs = null;
final List<Query> results = new ArrayList<>();
if (table == null) {
plugin.debugMessage("Tried to get data from a null table! This means some tables are not setup");
return results;
}
try {
connection = getConnection();
// Create SQL query to retrieve data
if (requirements == null || requirements.length == 0) {
// No requirements, so we can grab all data in the table.
ps = connection.prepareStatement("SELECT * FROM " + table.getTableName());
} else {
// We have requirements, so we need to filter the data using WHERE clause of SQL.
StringBuilder builder = new StringBuilder(String.format("SELECT * FROM %s WHERE ", table.getTableName()));
// Create a SQL WHERE string.
for (int i = 0; i < requirements.length; i++) {
RowRequirement requirement = requirements[i];
if (i == requirements.length - 1) {
builder.append(String.format("%s = '%s';", requirement.getColumnName(), requirement.getColumnValue()));
} else {
builder.append(String.format("%s = '%s' AND ", requirement.getColumnName(), requirement.getColumnValue()));
}
}
ps = connection.prepareStatement(builder.toString());
}
rs = ps.executeQuery();
while (rs.next()) {
final HashMap<String, String> result = new HashMap<>();
// Populate hashmap
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
final String columnName = rs.getMetaData().getColumnName(i + 1);
final String value = rs.getObject(i + 1).toString();
// Put value in hashmap if not null, otherwise just put
// empty string
result.put(columnName, (value != null ? value : ""));
}
results.add(new Query(result));
}
} catch (final SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement:", ex);
return results;
} finally {
try {
if (ps != null)
ps.close();
// if (conn != null)
// conn.close();
} catch (final SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
}
}
return results;
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class SQLiteConnector method setBatchObjects.
@Override
public void setBatchObjects(final Table table, final List<Query> queries, int mode) {
// Run SQLite query async to not disturb the main Server thread
Connection conn = getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
for (Query query : queries) {
StringBuilder columnNames = new StringBuilder("(");
StringBuilder resultNames = new StringBuilder("(");
StringBuilder updateWhere = new StringBuilder("");
for (final Entry<String, String> result : query.getEntrySet()) {
columnNames.append(result.getKey() + ",");
// DO NOT add for value column
if (!result.getKey().equalsIgnoreCase("value")) {
updateWhere.append(result.getKey() + "=");
}
try {
// Try to check if it is an integer
Double.parseDouble(result.getValue());
resultNames.append(result.getValue() + ",");
if (!result.getKey().equalsIgnoreCase("value")) {
updateWhere.append(result.getValue() + " AND ");
}
} catch (final NumberFormatException e) {
resultNames.append("'" + result.getValue().replace("'", "''") + "',");
if (!result.getKey().equalsIgnoreCase("value")) {
updateWhere.append("'" + result.getValue().replace("'", "''") + "' AND ");
}
}
}
// Remove last comma
columnNames = new StringBuilder(columnNames.substring(0, columnNames.lastIndexOf(",")) + ")");
resultNames = new StringBuilder(resultNames.substring(0, resultNames.lastIndexOf(",")) + ")");
updateWhere = new StringBuilder(updateWhere.substring(0, updateWhere.lastIndexOf("AND")));
String update;
String updateTwo = null;
if (mode == 1 || !query.hasColumn("value")) {
// Override value
update = "INSERT OR REPLACE INTO " + table.getTableName() + " " + columnNames.toString() + " VALUES " + resultNames;
} else {
// Add value
update = "UPDATE " + table.getTableName() + " SET value=value + " + query.getValue() + " WHERE " + updateWhere.toString() + ";";
updateTwo = "INSERT OR IGNORE INTO " + table.getTableName() + " " + columnNames.toString() + " VALUES " + resultNames + ";";
}
stmt.addBatch(update);
if (updateTwo != null) {
stmt.addBatch(updateTwo);
}
}
@SuppressWarnings("unused") int[] updateCounts = stmt.executeBatch();
if (!conn.getAutoCommit()) {
conn.commit();
}
} catch (BatchUpdateException b) {
plugin.getLogger().log(Level.SEVERE, "Couldn't execute SQLite statement:", b);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Couldn't execute SQLite statement:", ex);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class DataManager method getPlayerInfo.
/**
* Get all known data of a player for a given statistic. This is different from {@link #getPlayerInfo(UUID)} as
* that method grabs all data of a player, while this method only retrieves data about a given statistic.
*
* @param uuid UUID of the player
* @param statType Type of statistic to get data of
*
* @return PlayerInfo object with data of the requested player and the given statistic
*
* @throws IllegalArgumentException if the given uuid is null.
*/
public PlayerInfo getPlayerInfo(UUID uuid, PlayerStat statType) throws IllegalArgumentException {
if (uuid == null) {
throw new IllegalArgumentException("UUID cannot be null.");
}
if (!this.isPlayerLoaded(uuid, statType)) {
return null;
}
PlayerInfo info = this.getPlayerInfo(uuid);
if (info == null) {
return null;
}
// Create a new PlayerInfo object that only has the data of a given statistic.
PlayerInfo newInfo = new PlayerInfo(uuid);
List<Query> queriesStored = info.getDataOfPlayerStat(statType);
// Don't store data that is null or empty.
if (queriesStored != null && !queriesStored.isEmpty()) {
newInfo.setData(statType, info.getDataOfPlayerStat(statType));
}
return newInfo;
}
Aggregations