Search in sources :

Example 1 with Sql

use of com.djrapitops.plan.storage.database.sql.building.Sql in project Plan by plan-player-analytics.

the class DatabaseTest method sqlDateParsingSanitySQLDoesNotApplyTimezone.

@Test
@Disabled
default void sqlDateParsingSanitySQLDoesNotApplyTimezone() {
    Database db = db();
    List<org.junit.jupiter.api.function.Executable> assertions = new ArrayList<>();
    long now = System.currentTimeMillis();
    for (int i = 0; i < 24; i++) {
        long hourChange = TimeUnit.HOURS.toMillis(i);
        assertions.add(() -> {
            long time = now + hourChange;
            int offset = 0;
            Sql sql = db.getType().getSql();
            String testSQL = SELECT + sql.dateToDayStamp(sql.epochSecondToDate(Long.toString((time + offset) / 1000))) + " as date";
            String expected = deliveryUtilities().getFormatters().iso8601NoClockLong().apply(time);
            String result = db.query(new QueryAllStatement<String>(testSQL) {

                @Override
                public String processResults(ResultSet set) throws SQLException {
                    return set.next() ? set.getString("date") : null;
                }
            });
            assertEquals(expected, result, () -> "Expected <" + expected + "> but was: <" + result + "> for query <" + testSQL + ">");
        });
    }
    assertAll(assertions);
}
Also used : SQLException(java.sql.SQLException) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with Sql

use of com.djrapitops.plan.storage.database.sql.building.Sql in project Plan by plan-player-analytics.

the class DatabaseTest method sqlDateConversionSanityCheck.

@Test
default void sqlDateConversionSanityCheck() {
    Database db = db();
    long expected = System.currentTimeMillis() / 1000;
    Sql sql = db.getType().getSql();
    String testSQL = SELECT + sql.dateToEpochSecond(sql.epochSecondToDate(Long.toString(expected))) + " as ms";
    long result = db.query(new QueryAllStatement<Long>(testSQL) {

        @Override
        public Long processResults(ResultSet set) throws SQLException {
            return set.next() ? set.getLong("ms") : -1L;
        }
    });
    assertEquals(expected, result);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) Test(org.junit.jupiter.api.Test)

Example 3 with Sql

use of com.djrapitops.plan.storage.database.sql.building.Sql in project Plan by plan-player-analytics.

the class WorldObj method updateWorldTimesTableWorldIDs.

private void updateWorldTimesTableWorldIDs() {
    List<WorldObj> worldObjects = getWorldObjects();
    Map<WorldObj, List<WorldObj>> oldToNewMap = worldObjects.stream().filter(worldObj -> worldObj.serverId == 0).collect(Collectors.toMap(Function.identity(), oldWorld -> worldObjects.stream().filter(worldObj -> worldObj.serverId != 0).filter(worldObj -> worldObj.equals(oldWorld)).collect(Collectors.toList())));
    String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " + WorldTimesTable.WORLD_ID + "=?" + WHERE + WorldTimesTable.WORLD_ID + "=?" + AND + "server_id=?";
    execute(new ExecBatchStatement(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {
                WorldObj old = entry.getKey();
                for (WorldObj newWorld : entry.getValue()) {
                    statement.setInt(1, newWorld.id);
                    statement.setInt(2, old.id);
                    statement.setInt(3, newWorld.serverId);
                    statement.addBatch();
                }
            }
        }
    });
}
Also used : WorldTable(com.djrapitops.plan.storage.database.sql.tables.WorldTable) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement) java.util(java.util) LargeStoreQueries(com.djrapitops.plan.storage.database.queries.LargeStoreQueries) SessionsTable(com.djrapitops.plan.storage.database.sql.tables.SessionsTable) ServerUUID(com.djrapitops.plan.identification.ServerUUID) QueryAllStatement(com.djrapitops.plan.storage.database.queries.QueryAllStatement) PreparedStatement(java.sql.PreparedStatement) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) SQLException(java.sql.SQLException) WorldTimesTable(com.djrapitops.plan.storage.database.sql.tables.WorldTimesTable) ResultSet(java.sql.ResultSet) ServerQueries(com.djrapitops.plan.storage.database.queries.objects.ServerQueries) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ExecBatchStatement(com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)

Example 4 with Sql

use of com.djrapitops.plan.storage.database.sql.building.Sql in project Plan by plan-player-analytics.

the class DatabaseTest method sqlDateParsingSanityCheck.

@Test
default void sqlDateParsingSanityCheck() {
    Database db = db();
    long time = System.currentTimeMillis();
    int offset = TimeZone.getDefault().getOffset(time);
    Sql sql = db.getType().getSql();
    String testSQL = SELECT + sql.dateToDayStamp(sql.epochSecondToDate(Long.toString((time + offset) / 1000))) + " as date";
    String expected = deliveryUtilities().getFormatters().iso8601NoClockLong().apply(time);
    String result = db.query(new QueryAllStatement<String>(testSQL) {

        @Override
        public String processResults(ResultSet set) throws SQLException {
            return set.next() ? set.getString("date") : null;
        }
    });
    assertEquals(expected, result, () -> "Expected <" + expected + "> but was: <" + result + "> for query <" + testSQL + ">");
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) Test(org.junit.jupiter.api.Test)

Aggregations

Sql (com.djrapitops.plan.storage.database.sql.building.Sql)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Test (org.junit.jupiter.api.Test)3 ServerUUID (com.djrapitops.plan.identification.ServerUUID)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 SessionsTable (com.djrapitops.plan.storage.database.sql.tables.SessionsTable)1 WorldTable (com.djrapitops.plan.storage.database.sql.tables.WorldTable)1 WorldTimesTable (com.djrapitops.plan.storage.database.sql.tables.WorldTimesTable)1 ExecBatchStatement (com.djrapitops.plan.storage.database.transactions.ExecBatchStatement)1 PreparedStatement (java.sql.PreparedStatement)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Disabled (org.junit.jupiter.api.Disabled)1