Skip to content

Commit 7fe7e51

Browse files
SYM-7186: Fixed NullPointerException when resolving a unique key violation on a table that is referenced by a table in different schema #481 (#483)
Co-authored-by: evan-miller-jumpmind <[email protected]>
1 parent cb34fab commit 7fe7e51

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

symmetric-jdbc/src/main/java/org/jumpmind/db/platform/AbstractJdbcDdlReader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,11 @@ protected void readExportedKey(DatabaseMetaDataWrapper metaData, Map<String, Obj
11911191
fk = new ForeignKey(fkName);
11921192
fk.setForeignTableName((String) values.get(getName("FKTABLE_NAME")));
11931193
try {
1194-
fk.setForeignTableCatalog((String) values.get(getName("FKTABLE_CAT")));
1194+
fk.setForeignTableCatalog((String) values.getOrDefault(getName("FKTABLE_CAT"), values.get(getName("fktable_cat"))));
11951195
} catch (Exception e) {
11961196
}
11971197
try {
1198-
fk.setForeignTableSchema((String) values.get(getName("FKTABLE_SCHEM")));
1198+
fk.setForeignTableSchema((String) values.getOrDefault(getName("FKTABLE_SCHEM"), values.get(getName("fktable_schem"))));
11991199
} catch (Exception e) {
12001200
}
12011201
knownFks.put(fkName, fk);
@@ -1652,7 +1652,7 @@ public List<TableRow> getExportedForeignTableRows(ISqlTransaction transaction, L
16521652
Collection<ForeignKey> exportedKeys = getExportedKeys(tableRow.getTable());
16531653
if (exportedKeys != null) {
16541654
for (ForeignKey fk : exportedKeys) {
1655-
Table foreignTable = lookupForeignTable(platform, fk, tableRow, false);
1655+
Table foreignTable = lookupForeignTable(platform, fk, false);
16561656
if (foreignTable != null) {
16571657
// Get the column names used by the foreign table and the values to bind to them from the primary table
16581658
Row selectRow = new Row(fk.getReferenceCount());
@@ -1729,7 +1729,7 @@ protected List<TableRow> getImportedForeignTableRows(List<TableRow> tableRows, S
17291729
for (TableRow tableRow : tableRows) {
17301730
if (visited.add(tableRow)) {
17311731
for (ForeignKey fk : tableRow.getTable().getForeignKeys()) {
1732-
Table foreignTable = lookupForeignTable(platform, fk, tableRow, true);
1732+
Table foreignTable = lookupForeignTable(platform, fk, true);
17331733
if (foreignTable != null) {
17341734
Row whereRow = new Row(fk.getReferenceCount());
17351735
String referenceColumnName = null;
@@ -1787,9 +1787,9 @@ protected List<TableRow> getImportedForeignTableRows(List<TableRow> tableRows, S
17871787
return fkDepList;
17881788
}
17891789

1790-
private Table lookupForeignTable(IDatabasePlatform platform, ForeignKey fk, TableRow tableRow, boolean clearPrimaryKeys) {
1790+
private Table lookupForeignTable(IDatabasePlatform platform, ForeignKey fk, boolean clearPrimaryKeys) {
17911791
Table foreignTable = null;
1792-
Table table = platform.getTableFromCache(tableRow.getTable().getCatalog(), tableRow.getTable().getSchema(), fk.getForeignTableName(), false);
1792+
Table table = platform.getTableFromCache(fk.getForeignTableCatalog(), fk.getForeignTableSchema(), fk.getForeignTableName(), false);
17931793
if (table == null) {
17941794
table = fk.getForeignTable();
17951795
if (table == null) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Licensed to JumpMind Inc under one or more contributor
3+
* license agreements. See the NOTICE file distributed
4+
* with this work for additional information regarding
5+
* copyright ownership. JumpMind Inc licenses this file
6+
* to you under the GNU General Public License, version 3.0 (GPLv3)
7+
* (the "License"); you may not use this file except in compliance
8+
* with the License.
9+
*
10+
* You should have received a copy of the GNU General Public License,
11+
* version 3.0 (GPLv3) along with this library; if not, see
12+
* <http://www.gnu.org/licenses/>.
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
package org.jumpmind.db.platform;
22+
23+
import static org.junit.Assert.assertEquals;
24+
import static org.mockito.Mockito.mock;
25+
26+
import java.sql.SQLException;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
import org.jumpmind.db.model.ForeignKey;
31+
import org.jumpmind.db.model.Reference;
32+
import org.junit.jupiter.api.Test;
33+
import org.mockito.Answers;
34+
35+
public class AbstractJdbcDdlReaderTest {
36+
@Test
37+
public void testReadExportedKey() throws SQLException {
38+
AbstractJdbcDdlReader ddlReader = mock(AbstractJdbcDdlReader.class, Answers.CALLS_REAL_METHODS);
39+
Map<String, Object> metadataMap = new HashMap<String, Object>();
40+
metadataMap.put("FK_NAME", "test_fk");
41+
metadataMap.put("FKTABLE_NAME", "test_table");
42+
metadataMap.put("fktable_cat", "test_catalog_lowercase");
43+
metadataMap.put("fktable_schem", "test_schema_lowercase");
44+
metadataMap.put("FKCOLUMN_NAME", "foreign_col");
45+
metadataMap.put("PKCOLUMN_NAME", "local_col");
46+
metadataMap.put("KEY_SEQ", (short) 123);
47+
Map<String, ForeignKey> fkMap = new HashMap<String, ForeignKey>();
48+
ddlReader.readExportedKey(null, metadataMap, fkMap);
49+
assertEquals(1, fkMap.size());
50+
ForeignKey fk = fkMap.get("test_fk");
51+
assertEquals("test_fk", fk.getName());
52+
assertEquals("test_table", fk.getForeignTableName());
53+
assertEquals("test_catalog_lowercase", fk.getForeignTableCatalog());
54+
assertEquals("test_schema_lowercase", fk.getForeignTableSchema());
55+
assertEquals(1, fk.getReferenceCount());
56+
Reference reference = fk.getFirstReference();
57+
assertEquals("foreign_col", reference.getForeignColumnName());
58+
assertEquals("local_col", reference.getLocalColumnName());
59+
assertEquals(123, reference.getSequenceValue());
60+
metadataMap.put("FKTABLE_CAT", "TEST_CATALOG_UPPERCASE");
61+
metadataMap.put("FKTABLE_SCHEM", "TEST_SCHEMA_UPPERCASE");
62+
fkMap.clear();
63+
ddlReader.readExportedKey(null, metadataMap, fkMap);
64+
assertEquals(1, fkMap.size());
65+
fk = fkMap.get("test_fk");
66+
assertEquals("TEST_CATALOG_UPPERCASE", fk.getForeignTableCatalog());
67+
assertEquals("TEST_SCHEMA_UPPERCASE", fk.getForeignTableSchema());
68+
ddlReader.readExportedKey(null, metadataMap, fkMap);
69+
assertEquals(1, fkMap.size());
70+
}
71+
}

0 commit comments

Comments
 (0)