Skip to content

Commit c2030e8

Browse files
authored
CosmosDB Fix filter by dates at the server (#699)
1 parent e467e94 commit c2030e8

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

gxcosmosdb/src/main/java/com/genexus/db/cosmosdb/CosmosDBHelper.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json.org.json.JSONException;
88
import json.org.json.JSONObject;
99
import java.sql.SQLException;
10+
import java.time.format.DateTimeFormatter;
1011
import java.util.*;
1112
import java.sql.Timestamp;
1213
import java.text.SimpleDateFormat;
@@ -17,10 +18,15 @@
1718
public class CosmosDBHelper {
1819

1920
private static final String TABLE_ALIAS = "t";
20-
private static final SimpleDateFormat ISO_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
21+
private static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
22+
private static final String ISO_DATE_FORMAT = "yyyy-MM-dd'T'00:00:00.000'Z'";
2123
public static boolean formattedAsStringGXType(GXType gXType)
2224
{
23-
return (gXType == GXType.Date || gXType == GXType.DateTime || gXType == GXType.DateTime2 || gXType == GXType.VarChar || gXType == GXType.DateAsChar || gXType == GXType.NVarChar || gXType == GXType.LongVarChar || gXType == GXType.NChar || gXType == GXType.Char || gXType == GXType.Text || gXType == GXType.NText);
25+
return (gXType == GXType.VarChar || gXType == GXType.DateAsChar || gXType == GXType.NVarChar || gXType == GXType.LongVarChar || gXType == GXType.NChar || gXType == GXType.Char || gXType == GXType.Text || gXType == GXType.NText);
26+
}
27+
public static boolean formattedAsStringDateGXType(GXType gXType)
28+
{
29+
return (gXType == GXType.Date || gXType == GXType.DateTime || gXType == GXType.DateTime2);
2430
}
2531
private static String setupQuery(String projectionList, String filterExpression, String tableName, String orderbys) throws Exception {
2632
String sqlSelect = "";
@@ -91,9 +97,26 @@ public static String createCosmosQuery(CosmosDBQuery query, ServiceCursorBase cu
9197
varValuestr = '"' + entryValue.value.toString() + '"';
9298
else
9399
{
94-
varValuestr = entryValue.value.toString();
95-
varValuestr = varValuestr.equals("True") ? "true" : varValuestr;
96-
varValuestr = varValuestr.equals("False") ? "false" : varValuestr;
100+
if (formattedAsStringDateGXType(entryValue.type))
101+
{
102+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(ISO_DATETIME_FORMAT);
103+
try {
104+
java.sql.Timestamp ts = (java.sql.Timestamp)entryValue.value;
105+
varValuestr = '"' + ts.toLocalDateTime().format(dtf) + '"';
106+
}
107+
catch (Exception ex)
108+
{
109+
java.sql.Date sqlDate = (java.sql.Date)entryValue.value;
110+
SimpleDateFormat outputDateFormat = new SimpleDateFormat(ISO_DATE_FORMAT);
111+
outputDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
112+
varValuestr = '"' + outputDateFormat.format(sqlDate) + '"';
113+
}
114+
}
115+
else {
116+
varValuestr = entryValue.value.toString();
117+
varValuestr = varValuestr.equals("True") ? "true" : varValuestr;
118+
varValuestr = varValuestr.equals("False") ? "false" : varValuestr;
119+
}
97120
}
98121
filterProcess = filterProcess.replace(entryValue.name + ":", varValuestr);
99122
}
@@ -163,7 +186,11 @@ private static boolean tryConvertToDateISOFormat(VarValue parm, String[] dateStr
163186
return true;
164187
}
165188
else if (parm.type == GXType.Date) {
166-
dateStr[0] = ISO_DATE_FORMATTER.format(value);
189+
190+
java.sql.Date sqlDate = (java.sql.Date)value;
191+
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'00:00:00.000'Z'");
192+
outputDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
193+
dateStr[0] = outputDateFormat.format(sqlDate);
167194
return true;
168195
}
169196
return false;

0 commit comments

Comments
 (0)