Merge pull request #2583 from balazs-zsoldos/i2582_keys_should_be_sorted_in_metadata_classes

#2582 keys should be sorted in metadata classes
This commit is contained in:
John Tims 2020-05-27 06:10:54 -06:00 committed by GitHub
commit 7336708cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 13 deletions

View File

@ -16,8 +16,8 @@ package com.querydsl.sql.codegen;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nullable;
@ -72,7 +72,7 @@ public class KeyDataFactory {
public Map<String, InverseForeignKeyData> getExportedKeys(DatabaseMetaData md,
String catalog, String schema, String tableName) throws SQLException {
ResultSet foreignKeys = md.getExportedKeys(catalog, schema, tableName);
Map<String,InverseForeignKeyData> inverseForeignKeyData = new HashMap<String,InverseForeignKeyData>();
Map<String,InverseForeignKeyData> inverseForeignKeyData = new TreeMap<String,InverseForeignKeyData>();
try {
while (foreignKeys.next()) {
String name = foreignKeys.getString(FK_NAME);
@ -101,7 +101,7 @@ public class KeyDataFactory {
public Map<String, ForeignKeyData> getImportedKeys(DatabaseMetaData md,
String catalog, String schema, String tableName) throws SQLException {
ResultSet foreignKeys = md.getImportedKeys(catalog, schema, tableName);
Map<String,ForeignKeyData> foreignKeyData = new HashMap<String,ForeignKeyData>();
Map<String,ForeignKeyData> foreignKeyData = new TreeMap<String,ForeignKeyData>();
try {
while (foreignKeys.next()) {
String name = foreignKeys.getString(FK_NAME);
@ -130,7 +130,7 @@ public class KeyDataFactory {
public Map<String, PrimaryKeyData> getPrimaryKeys(DatabaseMetaData md,
String catalog, String schema, String tableName) throws SQLException {
ResultSet primaryKeys = md.getPrimaryKeys(catalog, schema, tableName);
Map<String,PrimaryKeyData> primaryKeyData = new HashMap<String,PrimaryKeyData>();
Map<String,PrimaryKeyData> primaryKeyData = new TreeMap<String,PrimaryKeyData>();
try {
while (primaryKeys.next()) {
String name = primaryKeys.getString(PK_NAME);

View File

@ -356,7 +356,7 @@ public class MetaDataExporter {
Map<String,ForeignKeyData> foreignKeyData = keyDataFactory
.getImportedKeys(md, catalog, schema, tableName);
if (!foreignKeyData.isEmpty()) {
Collection<ForeignKeyData> foreignKeysToGenerate = new HashSet<ForeignKeyData>();
Collection<ForeignKeyData> foreignKeysToGenerate = new LinkedHashSet<ForeignKeyData>();
for (ForeignKeyData fkd : foreignKeyData.values()) {
if (namingStrategy.shouldGenerateForeignKey(schemaAndTable, fkd)) {
foreignKeysToGenerate.add(fkd);

View File

@ -13,11 +13,13 @@
*/
package com.querydsl.sql.codegen;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import org.junit.Test;
@ -43,11 +45,13 @@ public class KeyDataFactoryTest extends AbstractJDBCTest {
statement.execute("create table employee("
+ "id INT, "
+ "superior_id int, "
+ "superior_id2 int, "
+ "survey_id int, "
+ "survey_name varchar(30), "
+ "CONSTRAINT PK_employee PRIMARY KEY (id), "
+ "CONSTRAINT FK_survey FOREIGN KEY (survey_id, survey_name) REFERENCES survey(id,name), "
+ "CONSTRAINT FK_superior FOREIGN KEY (superior_id) REFERENCES employee(id))");
+ "CONSTRAINT FK_superior2 FOREIGN KEY (superior_id) REFERENCES employee(id), "
+ "CONSTRAINT FK_superior1 FOREIGN KEY (superior_id2) REFERENCES employee(id))");
KeyDataFactory keyDataFactory = new KeyDataFactory(new DefaultNamingStrategy(), "Q","","test", false);
@ -58,15 +62,19 @@ public class KeyDataFactoryTest extends AbstractJDBCTest {
// primary key
Map<String, PrimaryKeyData> primaryKeys = keyDataFactory.getPrimaryKeys(md, null, null, "EMPLOYEE");
assertFalse(primaryKeys.isEmpty());
// inverse foreign keys
// inverse foreign keys sorted in abc
Map<String, InverseForeignKeyData> exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "EMPLOYEE");
assertFalse(exportedKeys.isEmpty());
assertTrue(exportedKeys.containsKey("FK_SUPERIOR"));
// foreign keys
assertEquals(2, exportedKeys.size());
Iterator<String> exportedKeysIterator = exportedKeys.keySet().iterator();
assertEquals("FK_SUPERIOR1", exportedKeysIterator.next());
assertEquals("FK_SUPERIOR2", exportedKeysIterator.next());
// foreign keys sorted in abc
Map<String, ForeignKeyData> importedKeys = keyDataFactory.getImportedKeys(md, null, null, "EMPLOYEE");
assertFalse(importedKeys.isEmpty());
assertTrue(importedKeys.containsKey("FK_SUPERIOR"));
assertTrue(importedKeys.containsKey("FK_SURVEY"));
assertEquals(3, importedKeys.size());
Iterator<String> importedKeysIterator = importedKeys.keySet().iterator();
assertEquals("FK_SUPERIOR1", importedKeysIterator.next());
assertEquals("FK_SUPERIOR2", importedKeysIterator.next());
assertEquals("FK_SURVEY", importedKeysIterator.next());
// SURVEY