Search in sources :

Example 1 with ExecBatchStatement

use of com.djrapitops.plan.storage.database.transactions.ExecBatchStatement in project Plan by plan-player-analytics.

the class LargeStoreQueries method storePerServerUserInformation.

/**
 * Execute a big batch of Per server UserInfo insert statements.
 *
 * @param ofServers Map: Server UUID - List of user information
 * @return Executable, use inside a {@link com.djrapitops.plan.storage.database.transactions.Transaction}
 */
public static Executable storePerServerUserInformation(Map<ServerUUID, List<UserInfo>> ofServers) {
    if (ofServers == null || ofServers.isEmpty())
        return Executable.empty();
    return new ExecBatchStatement(UserInfoTable.INSERT_STATEMENT) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            // Every Server
            for (Map.Entry<ServerUUID, List<UserInfo>> entry : ofServers.entrySet()) {
                ServerUUID serverUUID = entry.getKey();
                // Every User
                for (UserInfo user : entry.getValue()) {
                    statement.setString(1, user.getPlayerUuid().toString());
                    statement.setLong(2, user.getRegistered());
                    statement.setString(3, serverUUID.toString());
                    statement.setBoolean(4, user.isBanned());
                    statement.setString(5, user.getJoinAddress());
                    statement.setBoolean(6, user.isOperator());
                    statement.addBatch();
                }
            }
        }
    };
}
Also used : ServerUUID(com.djrapitops.plan.identification.ServerUUID) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Example 2 with ExecBatchStatement

use of com.djrapitops.plan.storage.database.transactions.ExecBatchStatement in project Plan by plan-player-analytics.

the class StoreServerTableResultTransaction method insertNewRows.

private void insertNewRows(Integer tableID, Integer afterRow, List<Object[]> rows) {
    String sql = "INSERT INTO " + TABLE_NAME + '(' + TABLE_ID + ',' + SERVER_UUID + ',' + VALUE_1 + ',' + VALUE_2 + ',' + VALUE_3 + ',' + VALUE_4 + ',' + VALUE_5 + ',' + TABLE_ROW + ") VALUES (?,?,?,?,?,?,?,?)";
    execute(new ExecBatchStatement(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            // Limit to maximum 5 columns, or how many column names there are.
            int maxColumnSize = Math.min(table.getMaxColumnSize(), 5);
            for (int rowNumber = afterRow; rowNumber < rows.size(); rowNumber++) {
                Object[] row = rows.get(rowNumber);
                statement.setInt(1, tableID);
                statement.setString(2, serverUUID.toString());
                for (int i = 0; i < maxColumnSize; i++) {
                    Object value = row[i];
                    setStringOrNull(statement, 3 + i, value != null ? StringUtils.truncate(value.toString(), 250) : null);
                }
                // Rest are set null if not 5 columns wide.
                for (int i = maxColumnSize; i < 5; i++) {
                    statement.setNull(3 + i, Types.VARCHAR);
                }
                statement.setInt(8, rowNumber);
                statement.addBatch();
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Example 3 with ExecBatchStatement

use of com.djrapitops.plan.storage.database.transactions.ExecBatchStatement in project Plan by plan-player-analytics.

the class NicknameLastSeenPatch method updateLastUsed.

private void updateLastUsed(Map<ServerUUID, Integer> serverIDsByUUID, Map<Integer, Set<Nickname>> nicknames) {
    String updateSQL = "UPDATE " + NicknamesTable.TABLE_NAME + " SET " + NicknamesTable.LAST_USED + "=?" + WHERE + NicknamesTable.NICKNAME + "=?" + AND + "user_id=?" + AND + "server_id=?";
    execute(new ExecBatchStatement(updateSQL) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {
                Integer userId = entry.getKey();
                Set<Nickname> nicks = entry.getValue();
                for (Nickname nick : nicks) {
                    Integer serverID = serverIDsByUUID.get(nick.getServerUUID());
                    statement.setLong(1, nick.getDate());
                    statement.setString(2, nick.getName());
                    statement.setInt(3, userId);
                    statement.setInt(4, serverID);
                    statement.addBatch();
                }
            }
        }
    });
}
Also used : Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement) Nickname(com.djrapitops.plan.delivery.domain.Nickname)

Example 4 with ExecBatchStatement

use of com.djrapitops.plan.storage.database.transactions.ExecBatchStatement in project Plan by plan-player-analytics.

the class LinkUsersToPlayersSecurityTablePatch method applyPatch.

@Override
protected void applyPatch() {
    String querySQL = SELECT + UsersTable.USER_UUID + ',' + SecurityTable.USERNAME + FROM + SecurityTable.TABLE_NAME + LEFT_JOIN + UsersTable.TABLE_NAME + " on " + UsersTable.USER_NAME + "=" + SecurityTable.USERNAME + WHERE + SecurityTable.LINKED_TO + "=''";
    String sql = "UPDATE " + SecurityTable.TABLE_NAME + " SET " + SecurityTable.LINKED_TO + "=?" + WHERE + SecurityTable.USERNAME + "=?";
    Map<String, String> byUsername = query(new QueryAllStatement<Map<String, String>>(querySQL) {

        @Override
        public Map<String, String> processResults(ResultSet set) throws SQLException {
            Map<String, String> byUsername = new HashMap<>();
            while (set.next()) {
                byUsername.put(set.getString(SecurityTable.USERNAME), set.getString(UsersTable.USER_UUID));
            }
            return byUsername;
        }
    });
    execute(new ExecBatchStatement(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            for (Map.Entry<String, String> usernameUUIDPair : byUsername.entrySet()) {
                Sql.setStringOrNull(statement, 1, usernameUUIDPair.getValue());
                statement.setString(2, usernameUUIDPair.getKey());
                statement.addBatch();
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) HashMap(java.util.HashMap) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Example 5 with ExecBatchStatement

use of com.djrapitops.plan.storage.database.transactions.ExecBatchStatement in project Plan by plan-player-analytics.

the class WorldTimesSeverIDPatch method applyPatch.

@Override
protected void applyPatch() {
    Map<Integer, Integer> sessionIDServerIDRelation = query(new SessionIDServerIDRelationQuery());
    String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " + "server_id=?" + WHERE + WorldTimesTable.SESSION_ID + "=?";
    execute(new ExecBatchStatement(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {
                Integer sessionID = entry.getKey();
                Integer serverID = entry.getValue();
                statement.setInt(1, serverID);
                statement.setInt(2, sessionID);
                statement.addBatch();
            }
        }
    });
}
Also used : SessionIDServerIDRelationQuery(com.djrapitops.plan.storage.database.queries.schema.SessionIDServerIDRelationQuery) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Aggregations

ExecBatchStatement (com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)21 PreparedStatement (java.sql.PreparedStatement)21 SQLException (java.sql.SQLException)13 ServerUUID (com.djrapitops.plan.identification.ServerUUID)9 Nickname (com.djrapitops.plan.delivery.domain.Nickname)3 ResultSet (java.sql.ResultSet)3 User (com.djrapitops.plan.delivery.domain.auth.User)2 Server (com.djrapitops.plan.identification.Server)2 SessionIDServerIDRelationQuery (com.djrapitops.plan.storage.database.queries.schema.SessionIDServerIDRelationQuery)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 World (com.djrapitops.plan.delivery.domain.World)1 com.djrapitops.plan.gathering.domain (com.djrapitops.plan.gathering.domain)1 LargeStoreQueries (com.djrapitops.plan.storage.database.queries.LargeStoreQueries)1 QueryAllStatement (com.djrapitops.plan.storage.database.queries.QueryAllStatement)1 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)1 ServerQueries (com.djrapitops.plan.storage.database.queries.objects.ServerQueries)1 WorldTimesQueries (com.djrapitops.plan.storage.database.queries.objects.WorldTimesQueries)1 Sql (com.djrapitops.plan.storage.database.sql.building.Sql)1 com.djrapitops.plan.storage.database.sql.tables (com.djrapitops.plan.storage.database.sql.tables)1