Search in sources :

Example 6 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner 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 QueryRunner

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

the class Database method tableExists.

public boolean tableExists(String table) {
    boolean exists = false;
    try {
        Connection conn = getConnection();
        QueryRunner run = new QueryRunner();
        try {
            String t = Constants.Nodes.DatabaseTable.toString();
            exists = run.query(conn, "SELECT id FROM " + table, returnBoolean);
        } finally {
            DbUtils.close(conn);
        }
    } catch (SQLException e) {
        exists = false;
    }
    return exists;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(com.iCo6.util.org.apache.commons.dbutils.QueryRunner)

Example 8 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner 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 9 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner 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 10 with QueryRunner

use of com.iCo6.util.org.apache.commons.dbutils.QueryRunner 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)

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