Search in sources :

Example 1 with RowRequirement

use of me.staartvin.statz.database.datatype.RowRequirement 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;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Example 2 with RowRequirement

use of me.staartvin.statz.database.datatype.RowRequirement in project Statz by Staartvin.

the class PlayerInfo method getTotalValue.

/**
 * Get the sum of all values in the 'value' column of each row that meets the given RowRequirements.
 * See the {@link RowRequirement} class for more info about requirements and some examples.
 * @param statType Type of statistics to get data for.
 * @param reqs A list of requirements that need to be met before adding the value to the sum.
 * @return the sum of the values in the rows that meet the given requirement or 0 if results were invalid or non-existent.
 */
public double getTotalValue(PlayerStat statType, RowRequirement... reqs) {
    // Check if we have any requirements - if not, just return double value.
    if (reqs == null || reqs.length == 0) {
        return this.getTotalValue(statType);
    }
    double value = 0;
    List<Query> rows = this.getDataOfPlayerStat(statType);
    if (rows.isEmpty() || !this.isValid())
        return value;
    for (Query row : rows) {
        boolean isValid = true;
        for (RowRequirement req : reqs) {
            // Check if each condition that was given is true.
            if (row.getValue(req.getColumnName()) == null || !row.getValue(req.getColumnName()).toString().equalsIgnoreCase(req.getColumnValue())) {
                isValid = false;
                break;
            }
        }
        // All conditions were met, so we add this value.
        if (isValid) {
            value += row.getDoubleValue("value");
        }
    }
    return value;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Example 3 with RowRequirement

use of me.staartvin.statz.database.datatype.RowRequirement in project Statz by Staartvin.

the class SQLiteConnector method getObjects.

@Override
public List<Query> getObjects(Table table, RowRequirement... requirements) {
    PreparedStatement ps = null;
    ResultSet rs = null;
    final List<Query> results = new ArrayList<>();
    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 SQLite statement:", ex);
        return results;
    } finally {
        try {
            if (ps != null)
                ps.close();
        } catch (final SQLException ex) {
            plugin.getLogger().log(Level.SEVERE, "Failed to close SQLite connection: ", ex);
        }
    }
    return results;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Example 4 with RowRequirement

use of me.staartvin.statz.database.datatype.RowRequirement in project Statz by Staartvin.

the class DataManager method getFreshPlayerInfo.

/**
 * Get data of a player for a given statistic. This method will obtain 'fresh' data from the database, meaning
 * that it will ignore cached data. Hence, this method will block the thread it is ran on. It is therefore
 * advised to run this method asynchronously.
 * <br>
 * <br>
 * It is recommended to use another method to obtain the data of a player when it is not loaded into the cache
 * yet: using {@link #loadPlayerData(UUID, PlayerStat)}, the retrieved data will also be stored in the cache, so
 * you can retrieve it the next time without making an expensive call to the database.
 *
 * @param uuid     UUID of the player.
 * @param statType Type of statistic.
 * @return fresh player data in the form of a {@link PlayerInfo} object.
 * @throws IllegalArgumentException if the given uuid is null.
 */
public PlayerInfo getFreshPlayerInfo(UUID uuid, PlayerStat statType) throws IllegalArgumentException {
    if (uuid == null) {
        throw new IllegalArgumentException("UUID cannot be null.");
    }
    Table table = DatabaseConnector.getTable(statType);
    List<Query> databaseRows = plugin.getDatabaseConnector().getObjects(table, new RowRequirement("uuid", uuid.toString()));
    PlayerInfo info = new PlayerInfo(uuid);
    info.setData(statType, databaseRows);
    return info;
}
Also used : Table(me.staartvin.statz.database.datatype.Table) Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Aggregations

Query (me.staartvin.statz.database.datatype.Query)4 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Table (me.staartvin.statz.database.datatype.Table)1 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)1