/*
* 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
* 1st of January 1970 (Epoch) is Thursday (-2). * * @param day 1 = Sunday, 2 = Monday etc.. 7 = Saturday * @return Milliseconds since epoch for this day to be given by {@link java.text.SimpleDateFormat} "EEEE" */ public static long getDayEpochMs(int day) { return TimeUnit.DAYS.toMillis(day + 2L); } public static void setStringOrNull(PreparedStatement statement, int index, String value) throws SQLException { if (value != null) { statement.setString(index, value); } else { statement.setNull(index, Types.VARCHAR); } } public static String concat(DBType dbType, String one, String two) { if (dbType == DBType.MYSQL) { return "CONCAT(" + one + ',' + two + ")"; } else if (dbType == DBType.SQLITE) { return one + " || " + two; } return one + two; } public abstract String epochSecondToDate(String sql); public abstract String dateToEpochSecond(String sql); public abstract String dateToDayStamp(String sql); public abstract String dateToHourStamp(String sql); public abstract String dateToDayOfWeek(String sql); public abstract String dateToHour(String sql); // https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html public static class MySQL extends Sql { @Override public String epochSecondToDate(String sql) { return "FROM_UNIXTIME(" + sql + ')'; } @Override public String dateToEpochSecond(String sql) { return "UNIX_TIMESTAMP(" + sql + ')'; } @Override public String dateToDayStamp(String sql) { return "DATE(" + sql + ')'; } @Override public String dateToHourStamp(String sql) { return "DATE_FORMAT(" + sql + ",'%Y-%m-%d %H:00:00')"; } @Override public String dateToDayOfWeek(String sql) { return "DAYOFWEEK(" + sql + ')'; } @Override public String dateToHour(String sql) { return "HOUR(" + sql + ") % 24"; } } // https://sqlite.org/lang_datefunc.html public static class SQLite extends Sql { @Override public String epochSecondToDate(String sql) { return "datetime(" + sql + ", 'unixepoch')"; } @Override public String dateToEpochSecond(String sql) { return "strftime('%s'," + sql + ")"; } @Override public String dateToDayStamp(String sql) { return "strftime('%Y-%m-%d'," + sql + ')'; } @Override public String dateToHourStamp(String sql) { return "strftime('%Y-%m-%d %H:00:00'," + sql + ')'; } @Override public String dateToDayOfWeek(String sql) { return "strftime('%w'," + sql + ")+1"; } @Override public String dateToHour(String sql) { return "strftime('%H'," + sql + ')'; } } }