Skip to content

Commit de984f0

Browse files
committed
[NOID] moved JdbcUtil and LoadJdbcConfig from core to full
1 parent 7883388 commit de984f0

File tree

6 files changed

+131
-70
lines changed

6 files changed

+131
-70
lines changed

full/src/main/java/apoc/load/Jdbc.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ private Object convert(Object value, int sqlType) {
281281
ZoneId zoneId = config.getZoneId();
282282
if (Types.TIMESTAMP == sqlType) {
283283
if (zoneId != null) {
284-
return ((java.sql.Timestamp)value).toInstant()
284+
return ((java.sql.Timestamp) value)
285+
.toInstant()
285286
.atZone(zoneId)
286287
.toOffsetDateTime();
287288
} else {
@@ -290,7 +291,8 @@ private Object convert(Object value, int sqlType) {
290291
}
291292
if (Types.TIMESTAMP_WITH_TIMEZONE == sqlType) {
292293
if (zoneId != null) {
293-
return ((java.sql.Timestamp)value).toInstant()
294+
return ((java.sql.Timestamp) value)
295+
.toInstant()
294296
.atZone(zoneId)
295297
.toOffsetDateTime();
296298
} else {

core/src/main/java/apoc/load/util/JdbcUtil.java renamed to full/src/main/java/apoc/load/util/JdbcUtil.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,18 @@
1818
*/
1919
package apoc.load.util;
2020

21-
import us.fatehi.utility.datasource.DatabaseConnectionSource;
22-
import us.fatehi.utility.datasource.DatabaseConnectionSources;
23-
import us.fatehi.utility.datasource.MultiUseUserCredentials;
24-
25-
import javax.security.auth.Subject;
26-
import javax.security.auth.callback.Callback;
27-
import javax.security.auth.callback.NameCallback;
28-
import javax.security.auth.callback.PasswordCallback;
29-
import javax.security.auth.login.LoginContext;
3021
import apoc.util.Util;
3122
import java.net.URI;
3223
import java.security.PrivilegedActionException;
3324
import java.security.PrivilegedExceptionAction;
34-
import java.sql.Connection;
35-
import java.sql.DriverManager;
3625
import javax.security.auth.Subject;
3726
import javax.security.auth.callback.Callback;
3827
import javax.security.auth.callback.NameCallback;
3928
import javax.security.auth.callback.PasswordCallback;
4029
import javax.security.auth.login.LoginContext;
30+
import us.fatehi.utility.datasource.DatabaseConnectionSource;
31+
import us.fatehi.utility.datasource.DatabaseConnectionSources;
32+
import us.fatehi.utility.datasource.MultiUseUserCredentials;
4133

4234
public class JdbcUtil {
4335

@@ -47,8 +39,11 @@ public class JdbcUtil {
4739
private JdbcUtil() {}
4840

4941
public static DatabaseConnectionSource getConnection(String jdbcUrl, LoadJdbcConfig config) throws Exception {
50-
if(config.hasCredentials()) {
51-
return createConnection(jdbcUrl, config.getCredentials().getUser(), config.getCredentials().getPassword());
42+
if (config.hasCredentials()) {
43+
return createConnection(
44+
jdbcUrl,
45+
config.getCredentials().getUser(),
46+
config.getCredentials().getPassword());
5247
} else {
5348
URI uri = new URI(jdbcUrl.substring("jdbc:".length()));
5449
String userInfo = uri.getUserInfo();
@@ -58,11 +53,12 @@ public static DatabaseConnectionSource getConnection(String jdbcUrl, LoadJdbcCon
5853
String[] user = userInfo.split(":");
5954
return createConnection(cleanUrl, user[0], user[1]);
6055
}
61-
return DriverManager.getConnection(jdbcUrl);
56+
return DatabaseConnectionSources.newDatabaseConnectionSource(jdbcUrl, new MultiUseUserCredentials());
6257
}
6358
}
6459

65-
private static Connection createConnection(String jdbcUrl, String userName, String password) throws Exception {
60+
private static DatabaseConnectionSource createConnection(String jdbcUrl, String userName, String password)
61+
throws Exception {
6662
if (jdbcUrl.contains(";auth=kerberos")) {
6763
String client = System.getProperty("java.security.auth.login.config.client", "KerberosClient");
6864
LoginContext lc = new LoginContext(client, callbacks -> {
@@ -74,12 +70,15 @@ private static Connection createConnection(String jdbcUrl, String userName, Stri
7470
lc.login();
7571
Subject subject = lc.getSubject();
7672
try {
77-
return Subject.doAs(subject, (PrivilegedExceptionAction<DatabaseConnectionSource>) () -> DatabaseConnectionSources.newDatabaseConnectionSource(jdbcUrl, new MultiUseUserCredentials(userName, password)));
73+
return Subject.doAs(subject, (PrivilegedExceptionAction<DatabaseConnectionSource>)
74+
() -> DatabaseConnectionSources.newDatabaseConnectionSource(
75+
jdbcUrl, new MultiUseUserCredentials(userName, password)));
7876
} catch (PrivilegedActionException pae) {
7977
throw pae.getException();
8078
}
8179
} else {
82-
return DriverManager.getConnection(jdbcUrl, userName, password);
80+
return DatabaseConnectionSources.newDatabaseConnectionSource(
81+
jdbcUrl, new MultiUseUserCredentials(userName, password));
8382
}
8483
}
8584

File renamed without changes.

full/src/main/java/apoc/model/Model.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
import schemacrawler.schema.*;
3737
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
3838
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
39-
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
40-
import schemacrawler.utility.SchemaCrawlerUtility;
39+
import schemacrawler.tools.utility.SchemaCrawlerUtility;
4140

4241
@Extended
4342
public class Model {

full/src/test/java/apoc/load/MySQLJdbcTest.java

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import static apoc.util.TestUtil.testCall;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
56

67
import apoc.util.MySQLContainerExtension;
78
import apoc.util.TestUtil;
89
import apoc.util.Util;
10+
import java.time.LocalDate;
11+
import java.time.LocalDateTime;
12+
import java.time.LocalTime;
13+
import java.time.ZonedDateTime;
914
import java.util.Map;
1015
import org.junit.AfterClass;
1116
import org.junit.BeforeClass;
@@ -16,37 +21,29 @@
1621
import org.neo4j.test.rule.DbmsRule;
1722
import org.neo4j.test.rule.ImpermanentDbmsRule;
1823

19-
import java.time.LocalDate;
20-
import java.time.LocalDateTime;
21-
import java.time.LocalTime;
22-
import java.time.ZonedDateTime;
23-
import java.util.Map;
24-
25-
import static apoc.util.TestUtil.testCall;
26-
import static org.junit.Assert.assertEquals;
27-
import static org.junit.Assert.assertTrue;
28-
2924
@RunWith(Enclosed.class)
3025
public class MySQLJdbcTest extends AbstractJdbcTest {
31-
26+
3227
public static class MySQLJdbcLatestVersionTest {
33-
28+
3429
@ClassRule
3530
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:8.0.31");
3631

3732
@ClassRule
3833
public static DbmsRule db = new ImpermanentDbmsRule();
39-
34+
4035
@BeforeClass
4136
public static void setUpContainer() {
4237
mysql.start();
4338
TestUtil.registerProcedure(db, Jdbc.class);
4439
}
40+
4541
@AfterClass
4642
public static void tearDown() {
4743
mysql.stop();
4844
db.shutdown();
4945
}
46+
5047
@Test
5148
public void testLoadJdbc() {
5249
MySQLJdbcTest.testLoadJdbc(db, mysql);
@@ -57,9 +54,9 @@ public void testIssue3496() {
5754
MySQLJdbcTest.testIssue3496(db, mysql);
5855
}
5956
}
60-
57+
6158
public static class MySQLJdbcFiveVersionTest {
62-
59+
6360
@ClassRule
6461
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:5.7");
6562

@@ -90,34 +87,58 @@ public void testIssue3496() {
9087
}
9188

9289
private static void testLoadJdbc(DbmsRule db, MySQLContainerExtension mysql) {
93-
// with the config {timezone: 'UTC'} and `preserveInstants=true&connectionTimeZone=SERVER` to make the result deterministic,
94-
// since `TIMESTAMP` values are automatically converted from the session time zone to UTC for storage, and vice versa.
95-
testCall(db, "CALL apoc.load.jdbc($url, $table, [], {timezone: 'UTC'})",
90+
// with the config {timezone: 'UTC'} and `preserveInstants=true&connectionTimeZone=SERVER` to make the result
91+
// deterministic,
92+
// since `TIMESTAMP` values are automatically converted from the session time zone to UTC for storage, and vice
93+
// versa.
94+
testCall(
95+
db,
96+
"CALL apoc.load.jdbc($url, $table, [], {timezone: 'UTC'})",
9697
Util.map(
97-
"url", mysql.getJdbcUrl() + "&preserveInstants=true&connectionTimeZone=SERVER",
98-
"table", "country"),
98+
"url",
99+
mysql.getJdbcUrl() + "&preserveInstants=true&connectionTimeZone=SERVER",
100+
"table",
101+
"country"),
99102
row -> {
100103
Map<String, Object> expected = Util.map(
101-
"Code", "NLD",
102-
"Name", "Netherlands",
103-
"Continent", "Europe",
104-
"Region", "Western Europe",
105-
"SurfaceArea", 41526f,
106-
"IndepYear", 1581,
107-
"Population", 15864000,
108-
"LifeExpectancy", 78.3f,
109-
"GNP", 371362f,
110-
"GNPOld", 360478f,
111-
"LocalName", "Nederland",
112-
"GovernmentForm", "Constitutional Monarchy",
113-
"HeadOfState", "Beatrix",
114-
"Capital", 5,
115-
"Code2", "NL",
116-
"myTime", LocalTime.of(1, 0, 0),
117-
"myTimeStamp", ZonedDateTime.parse("2003-01-01T01:00Z"),
118-
"myDate", LocalDate.parse("2003-01-01"),
119-
"myYear", LocalDate.parse("2003-01-01")
120-
);
104+
"Code",
105+
"NLD",
106+
"Name",
107+
"Netherlands",
108+
"Continent",
109+
"Europe",
110+
"Region",
111+
"Western Europe",
112+
"SurfaceArea",
113+
41526f,
114+
"IndepYear",
115+
1581,
116+
"Population",
117+
15864000,
118+
"LifeExpectancy",
119+
78.3f,
120+
"GNP",
121+
371362f,
122+
"GNPOld",
123+
360478f,
124+
"LocalName",
125+
"Nederland",
126+
"GovernmentForm",
127+
"Constitutional Monarchy",
128+
"HeadOfState",
129+
"Beatrix",
130+
"Capital",
131+
5,
132+
"Code2",
133+
"NL",
134+
"myTime",
135+
LocalTime.of(1, 0, 0),
136+
"myTimeStamp",
137+
ZonedDateTime.parse("2003-01-01T01:00Z"),
138+
"myDate",
139+
LocalDate.parse("2003-01-01"),
140+
"myYear",
141+
LocalDate.parse("2003-01-01"));
121142
Map actual = (Map) row.get("row");
122143
Object myDateTime = actual.remove("myDateTime");
123144
assertTrue(myDateTime instanceof LocalDateTime);
@@ -126,22 +147,24 @@ private static void testLoadJdbc(DbmsRule db, MySQLContainerExtension mysql) {
126147
}
127148

128149
private static void testIssue3496(DbmsRule db, MySQLContainerExtension mysql) {
129-
testCall(db, "CALL apoc.load.jdbc($url,'SELECT DATE(NOW()), NOW(), CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), DATE(UTC_TIMESTAMP());')",
150+
testCall(
151+
db,
152+
"CALL apoc.load.jdbc($url,'SELECT DATE(NOW()), NOW(), CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), DATE(UTC_TIMESTAMP());')",
130153
Util.map("url", mysql.getJdbcUrl()),
131154
r -> {
132155
Map row = (Map) r.get("row");
133156
assertEquals(8, row.size());
134-
157+
135158
assertTrue(row.get("UTC_DATE()") instanceof LocalDate);
136159
assertTrue(row.get("CURDATE()") instanceof LocalDate);
137-
160+
138161
assertTrue(row.get("UTC_TIMESTAMP()") instanceof LocalDateTime);
139162
assertTrue(row.get("NOW()") instanceof LocalDateTime);
140163
assertTrue(row.get("DATE(UTC_TIMESTAMP())") instanceof LocalDate);
141164
assertTrue(row.get("DATE(NOW())") instanceof LocalDate);
142-
165+
143166
assertTrue(row.get("CURTIME()") instanceof LocalTime);
144167
assertTrue(row.get("UTC_TIME()") instanceof LocalTime);
145168
});
146169
}
147-
}
170+
}

full/src/test/java/apoc/model/ModelTest.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,27 @@ public void testLoadJdbcSchema() {
126126
assertEquals(29, columns.size());
127127

128128
List<String> countryNodes = filterColumnsByTableName(columns, "country");
129-
List<String> expectedCountryCols = Arrays.asList("Code", "Name", "Continent", "Region", "SurfaceArea", "IndepYear", "Population", "LifeExpectancy", "GNP", "GNPOld", "LocalName", "GovernmentForm", "HeadOfState", "Capital", "Code2",
130-
"myTime", "myDateTime", "myTimeStamp", "myDate", "myYear");
129+
List<String> expectedCountryCols = Arrays.asList(
130+
"Code",
131+
"Name",
132+
"Continent",
133+
"Region",
134+
"SurfaceArea",
135+
"IndepYear",
136+
"Population",
137+
"LifeExpectancy",
138+
"GNP",
139+
"GNPOld",
140+
"LocalName",
141+
"GovernmentForm",
142+
"HeadOfState",
143+
"Capital",
144+
"Code2",
145+
"myTime",
146+
"myDateTime",
147+
"myTimeStamp",
148+
"myDate",
149+
"myYear");
131150
assertEquals(expectedCountryCols, countryNodes);
132151

133152
List<String> cityNodes = filterColumnsByTableName(columns, "city");
@@ -195,8 +214,27 @@ public void testLoadJdbcSchemaWithWriteOperation() {
195214
assertEquals(29, columns.size());
196215

197216
List<String> countryNodes = filterColumnsByTableName(columns, "country");
198-
List<String> expectedCountryCols = Arrays.asList("Code", "Name", "Continent", "Region", "SurfaceArea", "IndepYear", "Population", "LifeExpectancy", "GNP", "GNPOld", "LocalName", "GovernmentForm", "HeadOfState", "Capital", "Code2",
199-
"myTime", "myDateTime", "myTimeStamp", "myDate", "myYear");
217+
List<String> expectedCountryCols = Arrays.asList(
218+
"Code",
219+
"Name",
220+
"Continent",
221+
"Region",
222+
"SurfaceArea",
223+
"IndepYear",
224+
"Population",
225+
"LifeExpectancy",
226+
"GNP",
227+
"GNPOld",
228+
"LocalName",
229+
"GovernmentForm",
230+
"HeadOfState",
231+
"Capital",
232+
"Code2",
233+
"myTime",
234+
"myDateTime",
235+
"myTimeStamp",
236+
"myDate",
237+
"myYear");
200238
assertEquals(expectedCountryCols, countryNodes);
201239

202240
List<String> cityNodes = filterColumnsByTableName(columns, "city");

0 commit comments

Comments
 (0)