Fixed 3 H2 SQL Errors

Three different SQL queries were causing errors on a H2 database.

Found causes:
- [42001-199] Extra parentheses around subquery: '... FROM (subquery)'
- [HY004 not found] H2 performs query type checks at compile time and
  thus parametized  variables inside SELECT need to be implicitly cast
  to a type

Fixes:
- Removed extra parentheses around 2 queries
- Added a cast to CHAR to a query

Affects issues:
- Fixed #1183
This commit is contained in:
Rsl1122 2019-10-19 12:38:24 +03:00
parent ce8a5d331f
commit c46eac3121
3 changed files with 8 additions and 8 deletions

View File

@ -143,17 +143,18 @@ public class ActivityIndexQueries {
public static Query<Map<String, Integer>> fetchActivityIndexGroupingsOn(long date, UUID serverUUID, long threshold) {
String selectActivityIndex = selectActivityIndexSQL();
String selectIndexes = SELECT + "? as activity_group, activity_index" +
// ? inside SELECT needs to be implicitly cast to a type because of H2 compile time type checks.
String selectIndexes = SELECT + "CAST(? as CHAR) as activity_group, activity_index" +
FROM + UserInfoTable.TABLE_NAME + " u" +
LEFT_JOIN + '(' + selectActivityIndex + ") s on s." + SessionsTable.USER_UUID + "=u." + UserInfoTable.USER_UUID +
WHERE + "u." + UserInfoTable.SERVER_UUID + "=?" +
AND + "u." + UserInfoTable.REGISTERED + "<=?";
String selectCount = SELECT + "activity_group, COUNT(1) as count" + FROM +
String selectCount = SELECT + "indexes.activity_group, COUNT(1) as count" + FROM +
'(' + selectIndexes + ") indexes" +
WHERE + "COALESCE(indexes.activity_index,0)>=?" +
AND + "COALESCE(indexes.activity_index,0)<?" +
GROUP_BY + "activity_group";
GROUP_BY + "indexes.activity_group";
String selectMultipleCounts = SELECT + '*' + FROM +
'(' + selectCount +

View File

@ -171,10 +171,10 @@ public class GeoInfoQueries {
"MAX(" + GeoInfoTable.LAST_USED + ") as m" +
FROM + GeoInfoTable.TABLE_NAME +
GROUP_BY + GeoInfoTable.USER_UUID;
String sql = SELECT + GeoInfoTable.GEOLOCATION + ", COUNT(1) as c FROM (" +
String sql = SELECT + GeoInfoTable.GEOLOCATION + ", COUNT(1) as c FROM " +
"(" + selectGeolocations + ") AS q1" +
INNER_JOIN + "(" + selectLatestGeolocationDate + ") AS q2 ON q1.uuid = q2.uuid" +
INNER_JOIN + UserInfoTable.TABLE_NAME + " u on u." + UserInfoTable.USER_UUID + "=q1.uuid)" +
INNER_JOIN + UserInfoTable.TABLE_NAME + " u on u." + UserInfoTable.USER_UUID + "=q1.uuid" +
WHERE + GeoInfoTable.LAST_USED + "=m" +
AND + "u." + UserInfoTable.SERVER_UUID + "=?" +
GROUP_BY + GeoInfoTable.GEOLOCATION;

View File

@ -210,10 +210,9 @@ public class PingQueries {
", MIN(" + PingTable.MIN_PING + ") as minPing" +
", MAX(" + PingTable.MAX_PING + ") as maxPing" +
", AVG(" + PingTable.AVG_PING + ") as avgPing" +
FROM + "(" +
"(" + selectGeolocations + ") AS q1" +
FROM + "(" + selectGeolocations + ") AS q1" +
INNER_JOIN + "(" + selectLatestGeolocationDate + ") AS q2 ON q1.uuid = q2.uuid" +
INNER_JOIN + '(' + selectPingOfServer + ") sp on sp." + PingTable.USER_UUID + "=q1.uuid)" +
INNER_JOIN + '(' + selectPingOfServer + ") sp on sp." + PingTable.USER_UUID + "=q1.uuid" +
WHERE + GeoInfoTable.LAST_USED + "=m" +
AND + "sp." + PingTable.SERVER_UUID + "=?" +
GROUP_BY + GeoInfoTable.GEOLOCATION;