/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see .
*/
package com.djrapitops.plan.storage.database.queries.objects;
import com.djrapitops.plan.delivery.domain.DateHolder;
import com.djrapitops.plan.delivery.domain.PlayerName;
import com.djrapitops.plan.delivery.domain.ServerIdentifier;
import com.djrapitops.plan.delivery.domain.ServerName;
import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator;
import com.djrapitops.plan.gathering.domain.*;
import com.djrapitops.plan.gathering.domain.event.JoinAddress;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.storage.database.queries.QueryAllStatement;
import com.djrapitops.plan.storage.database.queries.QueryStatement;
import com.djrapitops.plan.storage.database.sql.building.Sql;
import com.djrapitops.plan.storage.database.sql.tables.*;
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
import com.djrapitops.plan.utilities.java.Maps;
import org.apache.commons.text.TextStringBuilder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
/**
* Queries for {@link FinishedSession} objects.
*
* @author AuroraLS3
*/
public class SessionQueries {
private SessionQueries() {
/* Static method class */
}
private static final String SELECT_SESSIONS_STATEMENT = SELECT +
"s." + SessionsTable.ID + ',' +
"u." + UsersTable.USER_NAME + " as name," +
"u." + UsersTable.USER_UUID + ',' +
"u_info." + UserInfoTable.REGISTERED + " as registered," +
"server." + ServerTable.NAME + " as server_name," +
"server." + ServerTable.ID + " as server_id," +
"server." + ServerTable.SERVER_UUID + " as server_uuid," +
SessionsTable.SESSION_START + ',' +
SessionsTable.SESSION_END + ',' +
SessionsTable.MOB_KILLS + ',' +
SessionsTable.DEATHS + ',' +
SessionsTable.AFK_TIME + ',' +
WorldTimesTable.SURVIVAL + ',' +
WorldTimesTable.CREATIVE + ',' +
WorldTimesTable.ADVENTURE + ',' +
WorldTimesTable.SPECTATOR + ',' +
WorldTable.NAME + ',' +
"j." + JoinAddressTable.JOIN_ADDRESS + " as join_address," +
KillsTable.KILLER_UUID + ',' +
KillsTable.VICTIM_UUID + ',' +
"v." + UsersTable.USER_NAME + " as victim_name, " +
"v." + UsersTable.REGISTERED + " as victim_" + UsersTable.REGISTERED + ", " +
"k." + UsersTable.USER_NAME + " as killer_name, " +
KillsTable.DATE + ',' +
KillsTable.WEAPON +
FROM + SessionsTable.TABLE_NAME + " s" +
INNER_JOIN + JoinAddressTable.TABLE_NAME + " j on s." + SessionsTable.JOIN_ADDRESS_ID + "=j." + JoinAddressTable.ID +
INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.ID + "=s." + SessionsTable.USER_ID +
INNER_JOIN + ServerTable.TABLE_NAME + " server on server." + ServerTable.ID + "=s." + SessionsTable.SERVER_ID +
LEFT_JOIN + UserInfoTable.TABLE_NAME + " u_info on (u_info." + UserInfoTable.USER_ID + "=s." + SessionsTable.USER_ID + AND + "u_info." + UserInfoTable.SERVER_ID + "=s." + SessionsTable.SERVER_ID + ')' +
LEFT_JOIN + KillsTable.TABLE_NAME + " ON " + "s." + SessionsTable.ID + '=' + KillsTable.TABLE_NAME + '.' + KillsTable.SESSION_ID +
LEFT_JOIN + UsersTable.TABLE_NAME + " v on v." + UsersTable.USER_UUID + '=' + KillsTable.VICTIM_UUID +
LEFT_JOIN + UsersTable.TABLE_NAME + " k on k." + UsersTable.USER_UUID + '=' + KillsTable.KILLER_UUID +
INNER_JOIN + WorldTimesTable.TABLE_NAME + " ON s." + SessionsTable.ID + '=' + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.SESSION_ID +
INNER_JOIN + WorldTable.TABLE_NAME + " ON " + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.WORLD_ID + '=' + WorldTable.TABLE_NAME + '.' + WorldTable.ID;
private static final String ORDER_BY_SESSION_START_DESC = ORDER_BY + SessionsTable.SESSION_START + " DESC";
/**
* Query the database for Session data with kill, death or world data.
*
* @return List of sessions
*/
public static Query> fetchAllSessions() {
String sql = SELECT_SESSIONS_STATEMENT +
ORDER_BY_SESSION_START_DESC;
return new QueryAllStatement<>(sql, 50000) {
@Override
public List processResults(ResultSet set) throws SQLException {
return extractDataFromSessionSelectStatement(set);
}
};
}
/**
* Query the database for Session data of a player with kill and world data.
*
* @param playerUUID UUID of the Player.
* @return Map: Server UUID - List of sessions on the server.
*/
public static Query