Search in sources :

Example 6 with Database

use of com.iCo6.IO.Database in project Core by iConomy.

the class MoneyComparator method accountList.

static List<String> accountList() {
    List<String> accounts = new ArrayList<String>();
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        if (useInventoryDB())
            accounts.addAll(inventory.getAllPlayers());
        if (useOrbDB())
            for (Player p : iConomy.Server.getOnlinePlayers()) accounts.add(p.getName());
        accounts.addAll(database.getIndices().keySet());
        return accounts;
    }
    try {
        QueryRunner run = new QueryRunner();
        Connection c = iConomy.Database.getConnection();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            accounts = run.query(c, "SELECT username FROM " + t, 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);
    }
    return accounts;
}
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 7 with Database

use of com.iCo6.IO.Database in project Core by iConomy.

the class MoneyComparator method doInterest.

static void doInterest(final String query, LinkedHashMap<String, HashMap<String, Object>> queries) {
    final Object[][] parameters = new Object[queries.size()][2];
    int i = 0;
    for (String name : queries.keySet()) {
        double balance = (Double) queries.get(name).get("balance");
        double original = 0.0, gain = 0.0, loss = 0.0;
        if (Constants.Nodes.Logging.getBoolean()) {
            original = getBalance(name);
            gain = balance - original;
            loss = original - balance;
        }
        // We are using a query for MySQL
        if (!useInventoryDB() && !useMiniDB() && !useOrbDB()) {
            parameters[i][0] = balance;
            parameters[i][1] = name;
            i++;
        } else if (useMiniDB()) {
            if (!hasAccount(name))
                continue;
            database.setArgument(name, "balance", balance);
            database.update();
        } else if (useInventoryDB()) {
            if (inventory.dataExists(name))
                inventory.setBalance(name, balance);
            else if (database.hasIndex(name)) {
                database.setArgument(name, "balance", balance);
                database.update();
            }
        } else if (useOrbDB()) {
            if (!hasAccount(name))
                continue;
            Player gainer = iConomy.Server.getPlayer(name);
            if (gainer != null)
                setBalance(name, balance);
        }
        if (Constants.Nodes.Logging.getBoolean()) {
            if (gain < 0.0)
                gain = 0.0;
            if (loss < 0.0)
                loss = 0.0;
            Transactions.insert(new Transaction("Interest", "System", name).setFromBalance(original).setToBalance(balance).setGain(gain).setLoss(loss).setSet(balance));
        }
    }
    if (!useInventoryDB() && !useMiniDB() && !useOrbDB())
        Thrun.init(new Runnable() {

            public void run() {
                try {
                    QueryRunner run = new QueryRunner();
                    Connection c = iConomy.Database.getConnection();
                    try {
                        run.batch(c, query, parameters);
                    } catch (SQLException ex) {
                        System.out.println("[iConomy] Error with batching: " + ex);
                    } finally {
                        DbUtils.close(c);
                    }
                } catch (SQLException ex) {
                    System.out.println("[iConomy] Database Error: " + ex);
                }
            }
        });
}
Also used : Player(org.bukkit.entity.Player) SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 8 with Database

use of com.iCo6.IO.Database in project Core by iConomy.

the class MoneyComparator method removeAccount.

static boolean removeAccount(String name) {
    Boolean removed = false;
    if (useMiniDB() || useInventoryDB() || useOrbDB()) {
        if (database.hasIndex(name)) {
            database.removeIndex(name);
            database.update();
            return true;
        }
        return false;
    }
    try {
        QueryRunner run = new QueryRunner();
        Connection c = iConomy.Database.getConnection();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            Integer amount = run.update(c, "DELETE FROM " + t + " WHERE username=?", name.toLowerCase());
            if (amount > 0)
                removed = true;
        } 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 removed;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 9 with Database

use of com.iCo6.IO.Database in project Core by iConomy.

the class MoneyComparator method getStatus.

static Integer getStatus(String name) {
    int status = 0;
    if (!hasAccount(name))
        return -1;
    if (useMiniDB())
        return database.getArguments(name).getInteger("status");
    if (useInventoryDB())
        return (inventory.dataExists(name)) ? 1 : (database.hasIndex(name)) ? database.getArguments(name).getInteger("status") : 0;
    if (useOrbDB())
        return (iConomy.Server.getPlayer(name) != null) ? 1 : (database.hasIndex(name)) ? database.getArguments(name).getInteger("status") : 0;
    try {
        QueryRunner run = new QueryRunner();
        Connection c = iConomy.Database.getConnection();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            status = run.query(c, "SELECT status FROM " + t + " WHERE username=?", returnStatus, 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 status;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 10 with Database

use of com.iCo6.IO.Database 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)

Aggregations

QueryRunner (com.iCo6.util.org.apache.commons.dbutils.QueryRunner)12 Connection (java.sql.Connection)12 SQLException (java.sql.SQLException)12 Player (org.bukkit.entity.Player)4 Arguments (com.iCo6.IO.mini.Arguments)2 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 Mini (com.iCo6.IO.mini.Mini)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