1
1
package com .genexus .db .cosmosdb ;
2
2
3
3
import com .genexus .CommonUtil ;
4
- import com .genexus .ModelContext ;
5
4
import com .genexus .db .service .IOServiceContext ;
6
5
import com .genexus .db .service .ServiceResultSet ;
7
6
8
7
import com .azure .cosmos .CosmosException ;
9
8
import java .io .InputStream ;
10
9
import java .math .BigDecimal ;
10
+ import java .sql .Date ;
11
11
import java .sql .SQLException ;
12
12
import java .sql .Time ;
13
13
import java .sql .Timestamp ;
14
- import java .time .ZoneOffset ;
15
- import java .time .Instant ;
16
- import java .time .LocalDate ;
17
- import java .time .LocalDateTime ;
14
+ import java .text .DateFormat ;
15
+ import java .text .SimpleDateFormat ;
16
+ import java .time .*;
18
17
import java .time .format .DateTimeFormatter ;
19
18
import java .time .format .DateTimeFormatterBuilder ;
20
19
import java .time .format .DateTimeParseException ;
21
- import java .time .temporal .TemporalAccessor ;
22
- import java .util .TimeZone ;
23
20
import static java .lang .Boolean .valueOf ;
24
21
25
22
public class CosmosDBResultSet extends ServiceResultSet <Object >
@@ -50,7 +47,8 @@ public boolean next()
50
47
}
51
48
52
49
private static final IOServiceContext SERVICE_CONTEXT = null ;
53
-
50
+ private static final Timestamp TIMESTAMP_NULL_VALUE = java .sql .Timestamp .from (CommonUtil .nullDate ().toInstant ());
51
+ private static final java .util .Date DATE_NULL_VALUE = Date .from (CommonUtil .nullDate ().toInstant ());
54
52
private boolean lastWasNull ;
55
53
@ Override
56
54
public boolean wasNull ()
@@ -180,47 +178,23 @@ public BigDecimal getBigDecimal(int columnIndex)
180
178
return BigDecimal .ZERO ;
181
179
}
182
180
183
- private Instant getInstant ( int columnIndex )
184
- {
185
- String value = getString (columnIndex );
186
- if ( value == null || value . trim ().isEmpty ( ))
181
+ @ Override
182
+ public java . sql . Date getDate ( int columnIndex ) throws SQLException {
183
+ Timestamp ts = getTimestamp (columnIndex );
184
+ if (! ts . toString ().equals ( TIMESTAMP_NULL_VALUE . toString () ))
187
185
{
188
- lastWasNull = true ;
189
- return CommonUtil .nullDate ().toInstant ();
186
+ return java .sql .Date .valueOf (ts .toInstant ().atOffset (ZoneOffset .UTC ).toLocalDate ());
190
187
}
191
-
192
- TemporalAccessor accessor = null ;
193
-
194
- try
195
- {
196
- accessor = ISO_DATE_TIME_OR_DATE .parseBest (value , LocalDateTime ::from , LocalDate ::from );
197
- }catch (DateTimeParseException dtpe )
198
- {
199
- for (DateTimeFormatter dateTimeFormatter :DATE_TIME_FORMATTERS )
200
- {
201
- try
202
- {
203
- accessor = dateTimeFormatter .parseBest (value , LocalDateTime ::from , LocalDate ::from );
204
- break ;
205
- }catch (Exception ignored ){ }
188
+ String strDate = getString (columnIndex );
189
+ for (String dateFormatter :DATE_FORMATTERS ) {
190
+ try {
191
+ DateFormat dateFormat = new SimpleDateFormat (dateFormatter );
192
+ java .util .Date date = dateFormat .parse (strDate );
193
+ return new java .sql .Date (date .getTime ());
194
+ } catch (Exception ignored ) {
206
195
}
207
- if (accessor == null )
208
- {
209
- return CommonUtil .resetTime (CommonUtil .nullDate ()).toInstant ();
210
- }
211
- }
212
- if (accessor instanceof LocalDateTime )
213
- {
214
- ModelContext ctx = ModelContext .getModelContext ();
215
- TimeZone tz = ctx != null ? ctx .getClientTimeZone () : TimeZone .getDefault ();
216
- return ((LocalDateTime ) accessor ).atZone (tz .toZoneId ()).toInstant ();
217
196
}
218
- else return LocalDate .from (accessor ).atStartOfDay ().toInstant (ZoneOffset .UTC );
219
- }
220
- @ Override
221
- public java .sql .Date getDate (int columnIndex )
222
- {
223
- return java .sql .Date .valueOf (getTimestamp (columnIndex ).toInstant ().atOffset (ZoneOffset .UTC ).toLocalDate ());
197
+ return new java .sql .Date (DATE_NULL_VALUE .getTime ());
224
198
}
225
199
226
200
@ Override
@@ -232,7 +206,33 @@ public Time getTime(int columnIndex)
232
206
@ Override
233
207
public Timestamp getTimestamp (int columnIndex )
234
208
{
235
- return java .sql .Timestamp .from (getInstant (columnIndex ));
209
+ String datetimeString = getString (columnIndex );
210
+ if (datetimeString == null || datetimeString .trim ().isEmpty ())
211
+ {
212
+ lastWasNull = true ;
213
+ return TIMESTAMP_NULL_VALUE ;
214
+ }
215
+ LocalDateTime localDateTime = null ;
216
+ try {
217
+ localDateTime = LocalDateTime .parse (datetimeString , ISO_DATE_TIME_OR_DATE );
218
+ ZonedDateTime zonedDateTime = ZonedDateTime .of (localDateTime , ZoneOffset .UTC );
219
+ return Timestamp .valueOf (zonedDateTime .toLocalDateTime ());
220
+ }
221
+ catch (DateTimeParseException dateTimeParseException )
222
+ {
223
+ for (DateTimeFormatter dateTimeFormatter :DATE_TIME_FORMATTERS ) {
224
+ try {
225
+ localDateTime = LocalDateTime .parse (datetimeString , dateTimeFormatter );
226
+ ZonedDateTime zonedDateTime = ZonedDateTime .of (localDateTime , ZoneOffset .UTC );
227
+ return Timestamp .valueOf (zonedDateTime .toLocalDateTime ());
228
+ }
229
+ catch (Exception ignored )
230
+ {}
231
+ }
232
+ if (localDateTime == null )
233
+ {return TIMESTAMP_NULL_VALUE ;}
234
+ }
235
+ return TIMESTAMP_NULL_VALUE ;
236
236
}
237
237
238
238
@ Override
@@ -280,4 +280,27 @@ public <T> T getObject(String columnLabel, Class<T> type)
280
280
DateTimeFormatter .ofPattern ("yyyy-M-d[ H:mm:ss.S]" ),
281
281
DateTimeFormatter .ofPattern ("yyyy-M-d[ H:mm:ss.S a]" )
282
282
};
283
+ private static final String [] DATE_FORMATTERS = new String []
284
+ {
285
+ "yyyy-MM-dd" ,
286
+ "yyyy-dd-MM" ,
287
+ "dd-MM-yyyy" ,
288
+ "MM/dd/yyyy" ,
289
+ "dd/MM/yyyy" ,
290
+ "yyyy/MM/dd" ,
291
+ "EEE, dd MMM yyyy" ,
292
+ "EEEE, dd MMMM yyyy" ,
293
+ "Month D, Yr" ,
294
+ "Yr, Month D" ,
295
+ "D Month, Yr" ,
296
+ "M/D/YY" ,
297
+ "D/M/YY" ,
298
+ "YY/M/D" ,
299
+ "Mon-DD-YYYY" ,
300
+ "DD-Mon-YYYY" ,
301
+ "YYYYY-Mon-DD" ,
302
+ "Mon DD, YYYY" ,
303
+ "DD Mon, YYYY" ,
304
+ "YYYY, Mon DD"
305
+ };
283
306
}
0 commit comments