Search in sources :

Example 11 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner in project Core by iConomy.

the class MoneyComparator method topAccounts.

static List<Account> topAccounts(int amount) {
    Accounts Accounts = new Accounts();
    List<Account> accounts = new ArrayList<Account>();
    List<Account> finals = new ArrayList<Account>();
    List<String> total = new ArrayList<String>();
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        if (useInventoryDB())
            total.addAll(inventory.getAllPlayers());
        if (useOrbDB())
            for (Player p : iConomy.Server.getOnlinePlayers()) total.add(p.getName());
        total.addAll(database.getIndices().keySet());
    } else {
        try {
            QueryRunner run = new QueryRunner();
            Connection c = iConomy.Database.getConnection();
            try {
                String t = Constants.Nodes.DatabaseTable.toString();
                total = run.query(c, "SELECT username FROM " + t + " WHERE status <> 1 ORDER BY balance DESC LIMIT " + amount, returnList);
            } catch (SQLException ex) {
                System.out.println("[iConomy] Error issueing SQL query: " + ex);
            } finally {
                DbUtils.close(c);
            }
        } catch (SQLException ex) {
            System.out.println("[iConomy] Database Error: " + ex);
        }
    }
    for (Iterator<String> it = total.iterator(); it.hasNext(); ) {
        String player = it.next();
        if (useMiniDB() || useInventoryDB() || useOrbDB()) {
            accounts.add(Accounts.get(player));
        } else {
            finals.add(new Account(player));
        }
    }
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        Collections.sort(accounts, new MoneyComparator());
        if (amount > accounts.size())
            amount = accounts.size();
        for (int i = 0; i < amount; i++) {
            if (accounts.get(i).getStatus() == 1) {
                i--;
                continue;
            }
            finals.add(accounts.get(i));
        }
    }
    return finals;
}
Also used : Player(org.bukkit.entity.Player) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 12 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner in project Core by iConomy.

the class MoneyComparator method getBalance.

static double getBalance(String name) {
    Double balance = Constants.Nodes.Balance.getDouble();
    if (!hasAccount(name))
        return balance;
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        if (useInventoryDB())
            if (inventory.dataExists(name))
                return inventory.getBalance(name);
        if (useOrbDB())
            if (iConomy.Server.getPlayer(name) != null)
                return iConomy.Server.getPlayer(name).getTotalExperience();
        if (database.hasIndex(name))
            return database.getArguments(name).getDouble("balance");
        return balance;
    }
    try {
        QueryRunner run = new QueryRunner();
        Connection c = iConomy.Database.getConnection();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            balance = run.query(c, "SELECT balance FROM " + t + " WHERE username=?", returnBalance, name.toLowerCase());
        } catch (SQLException ex) {
            System.out.println("[iConomy] Error issueing SQL query: " + ex);
        } finally {
            DbUtils.close(c);
        }
    } catch (SQLException ex) {
        System.out.println("[iConomy] Database Error: " + ex);
    }
    return balance;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 13 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner in project Core by iConomy.

the class MoneyComparator method hasAccount.

static boolean hasAccount(String name) {
    Boolean exists = false;
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        if (useInventoryDB())
            if (inventory.dataExists(name))
                return true;
        if (useOrbDB())
            if (iConomy.Server.getPlayer(name) != null)
                return true;
        return database.hasIndex(name);
    }
    try {
        QueryRunner run = new QueryRunner();
        Connection c = iConomy.Database.getConnection();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            exists = run.query(c, "SELECT id FROM " + t + " WHERE username=?", returnBoolean, name.toLowerCase());
        } catch (SQLException ex) {
            System.out.println("[iConomy] Error issueing SQL query: " + ex);
        } finally {
            DbUtils.close(c);
        }
    } catch (SQLException ex) {
        System.out.println("[iConomy] Database Error: " + ex);
    }
    return exists;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Aggregations

QueryRunner (com.iCo6.util.org.apache.commons.dbutils.QueryRunner)13 Connection (java.sql.Connection)13 SQLException (java.sql.SQLException)13 Player (org.bukkit.entity.Player)4 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Drivers (com.iCo6.Constants.Drivers)1 Database (com.iCo6.IO.Database)1 Type (com.iCo6.IO.Database.Type)1 MissingDriver (com.iCo6.IO.exceptions.MissingDriver)1 Arguments (com.iCo6.IO.mini.Arguments)1 com.iCo6.listeners.players (com.iCo6.listeners.players)1 Account (com.iCo6.system.Account)1 Interest (com.iCo6.system.Interest)1 Template (com.iCo6.util.Template)1 ResultSetHandler (com.iCo6.util.org.apache.commons.dbutils.ResultSetHandler)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Timer (java.util.Timer)1