use of com.querydsl.sql.codegen.support.InverseForeignKeyData in project querydsl by querydsl.
the class KeyDataFactoryTest method test.
@Test
public void test() throws SQLException {
statement.execute("drop table employee if exists");
statement.execute("drop table survey if exists");
statement.execute("drop table date_test if exists");
statement.execute("drop table date_time_test if exists");
statement.execute("create table survey (id int, name varchar(30), " + "CONSTRAINT PK_survey PRIMARY KEY (id, name))");
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_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);
DatabaseMetaData md = connection.getMetaData();
// EMPLOYEE
// primary key
Map<String, PrimaryKeyData> primaryKeys = keyDataFactory.getPrimaryKeys(md, null, null, "EMPLOYEE");
assertFalse(primaryKeys.isEmpty());
// inverse foreign keys sorted in abc
Map<String, InverseForeignKeyData> exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "EMPLOYEE");
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");
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
// primary key
primaryKeys = keyDataFactory.getPrimaryKeys(md, null, null, "SURVEY");
assertFalse(primaryKeys.isEmpty());
// inverse foreign keys
exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "SURVEY");
assertFalse(exportedKeys.isEmpty());
assertTrue(exportedKeys.containsKey("FK_SURVEY"));
// foreign keys
importedKeys = keyDataFactory.getImportedKeys(md, null, null, "SURVEY");
assertTrue(importedKeys.isEmpty());
}
use of com.querydsl.sql.codegen.support.InverseForeignKeyData in project querydsl by querydsl.
the class KeyDataFactory method getExportedKeys.
public Map<String, InverseForeignKeyData> getExportedKeys(DatabaseMetaData md, String catalog, String schema, String tableName) throws SQLException {
try (ResultSet foreignKeys = md.getExportedKeys(catalog, schema, tableName)) {
Map<String, InverseForeignKeyData> inverseForeignKeyData = new TreeMap<String, InverseForeignKeyData>();
while (foreignKeys.next()) {
String name = foreignKeys.getString(FK_NAME);
String parentColumnName = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_PARENT_COLUMN_NAME));
String foreignSchemaName = namingStrategy.normalizeSchemaName(foreignKeys.getString(FK_FOREIGN_SCHEMA_NAME));
String foreignTableName = namingStrategy.normalizeTableName(foreignKeys.getString(FK_FOREIGN_TABLE_NAME));
String foreignColumn = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_FOREIGN_COLUMN_NAME));
if (name == null || name.isEmpty()) {
name = tableName + "_" + foreignTableName + "_IFK";
}
InverseForeignKeyData data = inverseForeignKeyData.get(name);
if (data == null) {
data = new InverseForeignKeyData(name, foreignSchemaName, foreignTableName, createType(foreignSchemaName, foreignTableName));
inverseForeignKeyData.put(name, data);
}
data.add(parentColumnName, foreignColumn);
}
return inverseForeignKeyData;
}
}
use of com.querydsl.sql.codegen.support.InverseForeignKeyData in project querydsl by querydsl.
the class MetaDataSerializer method serializeProperties.
@SuppressWarnings("unchecked")
@Override
protected void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
if (innerClassesForKeys) {
Type primaryKeyType = new SimpleType(namingStrategy.getPrimaryKeysClassName());
Type foreignKeysType = new SimpleType(namingStrategy.getForeignKeysClassName());
// primary keys
if (primaryKeys != null) {
writer.beginClass(primaryKeyType);
serializePrimaryKeys(model, writer, primaryKeys);
writer.end();
}
// foreign keys
if (foreignKeys != null || inverseForeignKeys != null) {
writer.beginClass(foreignKeysType);
if (foreignKeys != null) {
serializeForeignKeys(model, writer, foreignKeys, false);
}
// inverse foreign keys
if (inverseForeignKeys != null) {
serializeForeignKeys(model, writer, inverseForeignKeys, true);
}
writer.end();
}
super.serializeProperties(model, config, writer);
if (primaryKeys != null) {
writer.publicFinal(primaryKeyType, namingStrategy.getPrimaryKeysVariable(model), "new " + primaryKeyType.getSimpleName() + "()");
}
if (foreignKeys != null || inverseForeignKeys != null) {
writer.publicFinal(foreignKeysType, namingStrategy.getForeignKeysVariable(model), "new " + foreignKeysType.getSimpleName() + "()");
}
} else {
super.serializeProperties(model, config, writer);
// primary keys
if (primaryKeys != null) {
serializePrimaryKeys(model, writer, primaryKeys);
}
// foreign keys
if (foreignKeys != null) {
serializeForeignKeys(model, writer, foreignKeys, false);
}
// inverse foreign keys
if (inverseForeignKeys != null) {
serializeForeignKeys(model, writer, inverseForeignKeys, true);
}
}
}
use of com.querydsl.sql.codegen.support.InverseForeignKeyData in project querydsl by querydsl.
the class MetaDataExporter method handleTable.
private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLException {
String catalog = tables.getString("TABLE_CAT");
String schema = tables.getString("TABLE_SCHEM");
String schemaName = normalize(tables.getString("TABLE_SCHEM"));
String tableName = normalize(tables.getString("TABLE_NAME"));
String normalizedSchemaName = namingStrategy.normalizeSchemaName(schemaName);
String normalizedTableName = namingStrategy.normalizeTableName(tableName);
SchemaAndTable schemaAndTable = new SchemaAndTable(normalizedSchemaName, normalizedTableName);
if (!namingStrategy.shouldGenerateClass(schemaAndTable)) {
return;
}
String className = namingStrategy.getClassName(schemaAndTable);
EntityType classModel = createEntityType(schemaAndTable, className);
if (exportPrimaryKeys) {
// collect primary keys
Map<String, PrimaryKeyData> primaryKeyData = keyDataFactory.getPrimaryKeys(md, catalog, schema, tableName);
if (!primaryKeyData.isEmpty()) {
classModel.getData().put(PrimaryKeyData.class, primaryKeyData.values());
}
}
if (exportForeignKeys) {
if (exportDirectForeignKeys) {
// collect foreign keys
Map<String, ForeignKeyData> foreignKeyData = keyDataFactory.getImportedKeys(md, catalog, schema, tableName);
if (!foreignKeyData.isEmpty()) {
Collection<ForeignKeyData> foreignKeysToGenerate = new LinkedHashSet<ForeignKeyData>();
for (ForeignKeyData fkd : foreignKeyData.values()) {
if (namingStrategy.shouldGenerateForeignKey(schemaAndTable, fkd)) {
foreignKeysToGenerate.add(fkd);
}
}
if (!foreignKeysToGenerate.isEmpty()) {
classModel.getData().put(ForeignKeyData.class, foreignKeysToGenerate);
}
}
}
if (exportInverseForeignKeys) {
// collect inverse foreign keys
Map<String, InverseForeignKeyData> inverseForeignKeyData = keyDataFactory.getExportedKeys(md, catalog, schema, tableName);
if (!inverseForeignKeyData.isEmpty()) {
classModel.getData().put(InverseForeignKeyData.class, inverseForeignKeyData.values());
}
}
}
// collect columns
try (ResultSet columns = md.getColumns(catalog, schema, tableName.replace("/", "//"), null)) {
while (columns.next()) {
handleColumn(classModel, tableName, columns);
}
}
// serialize model
serialize(classModel, schemaAndTable);
logger.info("Exported " + tableName + " successfully");
}
use of com.querydsl.sql.codegen.support.InverseForeignKeyData in project querydsl by querydsl.
the class MetaDataSerializer method introImports.
@SuppressWarnings("unchecked")
@Override
protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
super.introImports(writer, config, model);
Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
boolean addJavaUtilImport = false;
if (foreignKeys != null) {
for (ForeignKeyData keyData : foreignKeys) {
if (keyData.getForeignColumns().size() > 1) {
addJavaUtilImport = true;
break;
}
}
}
if (inverseForeignKeys != null) {
for (InverseForeignKeyData keyData : inverseForeignKeys) {
if (keyData.getForeignColumns().size() > 1) {
addJavaUtilImport = true;
break;
}
}
}
if (addJavaUtilImport) {
writer.imports(List.class.getPackage());
}
writer.imports(ColumnMetadata.class, java.sql.Types.class);
if (!entityPathType.getPackage().equals(ColumnMetadata.class.getPackage())) {
writer.imports(entityPathType);
}
writeUserImports(writer);
}
Aggregations