mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-07 04:18:26 +02:00
ISO8601 timestamp sql
This commit is contained in:
parent
e51e6f4194
commit
01fb2debb6
@ -44,80 +44,69 @@ public interface Sql {
|
||||
return "varchar(" + length + ')';
|
||||
}
|
||||
|
||||
String dateFromEpochSecond(String sql);
|
||||
String epochSecondToDate(String sql);
|
||||
|
||||
String epochSecondFromDate(String sql);
|
||||
String dateToEpochSecond(String sql);
|
||||
|
||||
String dayOfYear(String sql);
|
||||
|
||||
String year(String sql);
|
||||
String dateToDayStamp(String sql);
|
||||
|
||||
// https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
|
||||
class MySQL implements Sql {
|
||||
|
||||
@Override
|
||||
public String dateFromEpochSecond(String sql) {
|
||||
public String epochSecondToDate(String sql) {
|
||||
return "FROM_UNIXTIME(" + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String epochSecondFromDate(String sql) {
|
||||
public String dateToEpochSecond(String sql) {
|
||||
return "UNIX_TIMESTAMP(" + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dayOfYear(String sql) {
|
||||
return "DAYOFYEAR(" + sql + ')';
|
||||
public String dateToDayStamp(String sql) {
|
||||
return "DATE(" + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String year(String sql) {
|
||||
return "YEAR(" + sql + ')';
|
||||
}
|
||||
}
|
||||
|
||||
// https://h2database.com/html/functions.html
|
||||
class H2 extends MySQL {
|
||||
|
||||
@Override
|
||||
public String dateFromEpochSecond(String sql) {
|
||||
public String epochSecondToDate(String sql) {
|
||||
return "DATEADD('SECOND', " + sql + ", DATE '1970-01-01')";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String epochSecondFromDate(String sql) {
|
||||
public String dateToEpochSecond(String sql) {
|
||||
return "DATEDIFF('SECOND', DATE '1970-01-01', " + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dayOfYear(String sql) {
|
||||
return "DAY_OF_YEAR(" + sql + ')';
|
||||
public String dateToDayStamp(String sql) {
|
||||
return "DATE(" + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String year(String sql) {
|
||||
return "YEAR(" + sql + ')';
|
||||
}
|
||||
}
|
||||
|
||||
// https://sqlite.org/lang_datefunc.html
|
||||
class SQLite implements Sql {
|
||||
|
||||
@Override
|
||||
public String dateFromEpochSecond(String sql) {
|
||||
public String epochSecondToDate(String sql) {
|
||||
return "datetime(" + sql + ", 'unixepoch')";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String epochSecondFromDate(String sql) {
|
||||
public String dateToEpochSecond(String sql) {
|
||||
return "strftime('%s'," + sql + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dayOfYear(String sql) {
|
||||
return "strftime('%j'," + sql + ')';
|
||||
public String dateToDayStamp(String sql) {
|
||||
return "strftime('%Y-%m-%d'," + sql + ')';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String year(String sql) {
|
||||
return "strftime('%Y'" + sql + ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1096,13 +1096,13 @@ public interface DatabaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
default void sqlDateParsingSanityCheck() {
|
||||
default void sqlDateConversionSanityCheck() {
|
||||
Database db = db();
|
||||
|
||||
long expected = System.currentTimeMillis() / 1000;
|
||||
|
||||
Sql sql = db.getType().getSql();
|
||||
String testSQL = SELECT + sql.epochSecondFromDate(sql.dateFromEpochSecond(Long.toString(expected))) + " as ms";
|
||||
String testSQL = SELECT + sql.dateToEpochSecond(sql.epochSecondToDate(Long.toString(expected))) + " as ms";
|
||||
|
||||
long result = db.query(new QueryAllStatement<Long>(testSQL) {
|
||||
@Override
|
||||
@ -1113,6 +1113,26 @@ public interface DatabaseTest {
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
default void sqlDateParsingSanityCheck() {
|
||||
Database db = db();
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
Sql sql = db.getType().getSql();
|
||||
String testSQL = SELECT + sql.dateToDayStamp(sql.epochSecondToDate(Long.toString(time / 1000))) + " as date";
|
||||
|
||||
System.out.println(testSQL);
|
||||
String expected = system().getHtmlUtilities().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);
|
||||
}
|
||||
|
||||
@Test
|
||||
default void extensionPlayerValuesAreStored() {
|
||||
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system().getExtensionService();
|
||||
|
Loading…
Reference in New Issue
Block a user