|
7 | 7 | import json.org.json.JSONException;
|
8 | 8 | import json.org.json.JSONObject;
|
9 | 9 | import java.sql.SQLException;
|
| 10 | +import java.time.format.DateTimeFormatter; |
10 | 11 | import java.util.*;
|
11 | 12 | import java.sql.Timestamp;
|
12 | 13 | import java.text.SimpleDateFormat;
|
|
17 | 18 | public class CosmosDBHelper {
|
18 | 19 |
|
19 | 20 | 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'"; |
21 | 23 | public static boolean formattedAsStringGXType(GXType gXType)
|
22 | 24 | {
|
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); |
24 | 30 | }
|
25 | 31 | private static String setupQuery(String projectionList, String filterExpression, String tableName, String orderbys) throws Exception {
|
26 | 32 | String sqlSelect = "";
|
@@ -91,9 +97,26 @@ public static String createCosmosQuery(CosmosDBQuery query, ServiceCursorBase cu
|
91 | 97 | varValuestr = '"' + entryValue.value.toString() + '"';
|
92 | 98 | else
|
93 | 99 | {
|
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 | + } |
97 | 120 | }
|
98 | 121 | filterProcess = filterProcess.replace(entryValue.name + ":", varValuestr);
|
99 | 122 | }
|
@@ -163,7 +186,11 @@ private static boolean tryConvertToDateISOFormat(VarValue parm, String[] dateStr
|
163 | 186 | return true;
|
164 | 187 | }
|
165 | 188 | 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); |
167 | 194 | return true;
|
168 | 195 | }
|
169 | 196 | return false;
|
|
0 commit comments