Skip to content

Commit 03c8f6b

Browse files
authored
Datetime variable with servernow() initial value causes NullPointerException (#731)
Issue: 103191
1 parent 499dc46 commit 03c8f6b

File tree

6 files changed

+77
-5
lines changed

6 files changed

+77
-5
lines changed

java/client.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ MAX_CURSOR=100
174174
INITIALIZE_NEW=1
175175
ISOLATION_LEVEL=CR
176176
XBASE_TINT=1
177-
DBMS=sqlserver
177+
DBMS=sqlite
178178
UnlimitedRWPool=1
179179
PoolRWEnabled=1
180180
RecycleRW=1

java/src/main/java/com/genexus/GXutil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.*;
99

1010
import com.genexus.common.interfaces.SpecificImplementation;
11+
import com.genexus.db.DataStoreProvider;
1112
import com.genexus.internet.HttpContext;
1213
import com.genexus.internet.StringCollection;
1314
import com.genexus.platform.INativeFunctions;
@@ -974,16 +975,22 @@ public static Date serverNow(ModelContext context, int handle, String dataSource
974975

975976
public static Date serverNow(ModelContext context, int handle, com.genexus.db.IDataStoreProvider dataStore)
976977
{
978+
if (dataStore == null)
979+
dataStore = new DataStoreProvider(context, handle);
977980
return serverNow( context, handle, dataStore, false);
978981
}
979982

980983
public static Date serverNowMs(ModelContext context, int handle, com.genexus.db.IDataStoreProvider dataStore)
981984
{
985+
if (dataStore == null)
986+
dataStore = new DataStoreProvider(context, handle);
982987
return serverNow( context, handle, dataStore, true);
983988
}
984989

985990
public static Date serverNow(ModelContext context, int handle, com.genexus.db.IDataStoreProvider dataStore, boolean millisecond)
986991
{
992+
if (dataStore == null)
993+
dataStore = new DataStoreProvider(context, handle);
987994
return SpecificImplementation.GXutil.serverNow(context, handle, dataStore, millisecond);
988995
}
989996

java/src/main/java/com/genexus/db/DataStoreProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class DataStoreProvider extends DataStoreProviderBase implements
3737

3838
private static AtomicBoolean firstTime = new AtomicBoolean(true);
3939

40+
public DataStoreProvider(ModelContext context, int remoteHandle)
41+
{
42+
super(context, remoteHandle);
43+
}
44+
4045
public DataStoreProvider(ModelContext context, int remoteHandle, ILocalDataStoreHelper helper, Object[] buffers)
4146
{
4247
super(context, remoteHandle);

java/src/main/java/com/genexus/db/DataStoreProviderBase.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ protected DataStoreProviderBase(ModelContext context, int remoteHandle)
3232

3333
public GXConnection getConnection() throws SQLException
3434
{
35+
if (getHelper() == null)
36+
return getConnection("DEFAULT", false);
37+
3538
return getConnection(getHelper().getDataStoreName(), getHelper().needsReadOnlyConnection());
3639
}
3740
protected GXConnection getConnection(String dataStoreName, boolean needsReadOnlyConnection) throws SQLException
3841
{
3942
if (connectionProvider == null)
4043
{
41-
connectionProvider = getHelper().getConnectionProvider();
44+
if (getHelper() == null)
45+
connectionProvider = new DefaultConnectionProvider();
46+
else
47+
connectionProvider = getHelper().getConnectionProvider();
4248
}
4349

4450
con = (GXConnection) connectionProvider.getConnection(context, remoteHandle, dataStoreName, needsReadOnlyConnection, true);

java/src/main/java/com/genexus/db/driver/GXDBMSsqlite.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.genexus.db.driver;
22

33
import java.sql.*;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
46
import java.util.Date;
57

68
import com.genexus.CommonUtil;
@@ -120,13 +122,24 @@ public void onConnection(GXConnection con) throws SQLException
120122

121123
public java.util.Date serverDateTime(GXConnection con) throws SQLException
122124
{
123-
ResultSet rslt = con.getStatement("_ServerDT_", "SELECT GETDATE()", false).executeQuery();
125+
ResultSet rslt = con.getStatement("_ServerDT_", "SELECT datetime('now')", false).executeQuery();
124126

125127
rslt.next();
126-
Date value = rslt.getTimestamp(1);
128+
String value = rslt.getString(1);
127129
rslt.close();
128130

129-
return value;
131+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
132+
Date dateValue;
133+
try
134+
{
135+
dateValue = sdf.parse(value);
136+
}
137+
catch (ParseException e)
138+
{
139+
dateValue = nullDate();
140+
}
141+
142+
return dateValue;
130143
}
131144

132145
public String serverVersion(GXConnection con) throws SQLException
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.genexus.db.driver;
2+
3+
import com.genexus.Application;
4+
import com.genexus.ClientContext;
5+
import com.genexus.GXObjectHelper;
6+
import com.genexus.GXutil;
7+
import com.genexus.db.IDataStoreProvider;
8+
import com.genexus.db.UserInformation;
9+
import com.genexus.sampleapp.GXcfg;
10+
import com.genexus.specific.java.Connect;
11+
import org.junit.Assert;
12+
import org.junit.Test;
13+
14+
import java.util.Calendar;
15+
16+
public class TestServerNowNullProvider {
17+
18+
@Test
19+
public void testGetProviderNormalizedUrl() {
20+
Connect.init();
21+
Application.init(GXcfg.class);
22+
int remoteHandle = ((UserInformation) GXObjectHelper.getUserInformation(ClientContext.getModelContext(), -1)).getHandle();
23+
24+
java.util.Date serverDate = GXutil.serverNow(ClientContext.getModelContext(), remoteHandle, (IDataStoreProvider)null) ;
25+
Calendar calendar = Calendar.getInstance();
26+
calendar.setTime(serverDate);
27+
int century = getCentury(calendar);
28+
Assert.assertEquals(century, 21);
29+
30+
serverDate = GXutil.serverNowMs(ClientContext.getModelContext(), remoteHandle, null) ;
31+
calendar.setTime(serverDate);
32+
century = getCentury(calendar);
33+
Assert.assertEquals(century, 21);
34+
35+
}
36+
37+
private int getCentury(Calendar calendar) {
38+
int year = calendar.get(Calendar.YEAR) - 1;
39+
return (year / 100) + 1;
40+
}
41+
}

0 commit comments

Comments
 (0)