Skip to content

Real World Examples

Daniel Ennis edited this page Mar 14, 2018 · 1 revision

Real World Examples

        List<Long> empireIds = DB.getFirstColumnResults("SELECT empire_id FROM empire_users WHERE user_id = ?", this.getUser().userId);

        Long packageId = DB.getFirstColumn("SELECT package_id FROM vault WHERE user_id = ? AND vault_id = ?", userId, vault);

        DB.executeUpdateAsync("DELETE FROM mail WHERE user_id = ? AND package_id = ?", user.userId, packageId);

        boolean credit(Long userId, int amount, CurrencyTransType transType, String extraData) {
            return DB.createTransaction(stm -> credit(userId, amount, transType, extraData, stm));
        }

        boolean credit(Long userId, int amount, CurrencyTransType transType, String extraData, DbStatement stm) {
            if (extraData != null && extraData.length() > 250) {
                extraData = extraData.substring(0, 249);
            }
            if (!stm.inTransaction()) {
                throw new IllegalStateException("Currency Operations require a transaction");
            }

            if (amount == 0) {
                return true;
            }

            try {
                String finalExtraData = extraData;
                stm.onCommit(unused -> logCurrency(userId, EmpireServer.getServerId(), amount, userId, transType.id(), finalExtraData));

                return stm.executeUpdateQuery("INSERT INTO currency_table (user_id, balance, last_update) " +
                        "VALUES (?, ?, UNIX_TIMESTAMP()) ON DUPLICATE KEY " +
                        "UPDATE balance = balance + VALUES(balance), last_update = UNIX_TIMESTAMP()", userId, amount) > 0;
            } catch (SQLException e) {
                Log.exception(curType + " credit: " + userId + ": "+ amount, e);
            }
            return false;
        }
You can’t perform that action at this time.