Permalink
Browse files

new option --smart2Inheritance

This code has been funded by the Canton of Zug (Switzerland)
  • Loading branch information...
claeis committed May 30, 2016
1 parent 3e387e9 commit fb309f2a6b42d71663e78a525d39c65ce291e98d
@@ -15,6 +15,8 @@ ideas/open issues/questions
ili2gpkg/ili2pg 3.1.0 (2016-05-xx)
----------------------------------
- added column datasetName to table T_ILI2DB_DATASET to make a dataset identifiable from the outside
- new option --smart2Inheritance
- --smartInheritance renamed to --smart1Inheritance

ili2gpkg/ili2pg 3.0.4 (2016-05-26)
----------------------------------

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -173,9 +173,12 @@ public void domain(String args[]){
config.setMultiSurfaceTrafo(null);
config.setMultilingualTrafo(null);
config.setInheritanceTrafo(null);
}else if(arg.equals("--smartInheritance")){
}else if(arg.equals("--smart1Inheritance")){
argi++;
config.setInheritanceTrafo(config.INHERITANCE_TRAFO_SMART1);
}else if(arg.equals("--smart2Inheritance")){
argi++;
config.setInheritanceTrafo(config.INHERITANCE_TRAFO_SMART2);
}else if(arg.equals("--coalesceCatalogueRef")){
argi++;
config.setCatalogueRefTrafo(config.CATALOGUE_REF_TRAFO_COALESCE);
@@ -268,7 +271,8 @@ public void domain(String args[]){
System.err.println("--createscript filename Generate a sql script that creates the db schema.");
System.err.println("--dropscript filename Generate a sql script that drops the generated db schema.");
System.err.println("--noSmartMapping disable all smart mappings");
System.err.println("--smartInheritance enable smart mapping of class/structure inheritance");
System.err.println("--smart1Inheritance enable smart1 mapping of class/structure inheritance");
System.err.println("--smart2Inheritance enable smart2 mapping of class/structure inheritance");
System.err.println("--coalesceCatalogueRef enable smart mapping of CHBase:CatalogueReference");
System.err.println("--coalesceMultiSurface enable smart mapping of CHBase:MultiSurface");
System.err.println("--expandMultilingual enable smart mapping of CHBase:MultilingualText");
@@ -1,6 +1,7 @@
package ch.ehi.ili2db.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

@@ -11,6 +12,7 @@
import ch.ehi.ili2db.base.Ili2dbException;
import ch.ehi.ili2db.fromili.CustomMapping;
import ch.ehi.ili2db.mapping.TrafoConfig;
import ch.ehi.ili2db.mapping.TrafoConfigNames;
import ch.ehi.ili2db.mapping.Viewable2TableMapping;
import ch.ehi.ili2db.mapping.ViewableWrapper;
import ch.ehi.sqlgen.repository.DbColBoolean;
@@ -168,10 +170,38 @@ public void addIliTidCol(DbTable dbTable,Domain oidDomain) {
dbTable.addColumn(dbColIliTid);
}
}

protected String getSqlRoleName(RoleDef def,String ownerSqlTableName,String targetSqlTableName){
return ili2sqlName.mapIliRoleDef(def,ownerSqlTableName,targetSqlTableName);
}
private HashMap<AbstractClassDef,ArrayList<ViewableWrapper>> targetTablesPool=new HashMap<AbstractClassDef,ArrayList<ViewableWrapper>>();
protected ArrayList<ViewableWrapper> getTargetTables(AbstractClassDef destination) {
if(targetTablesPool.containsKey(destination)){
return targetTablesPool.get(destination);
}
ArrayList<ViewableWrapper> ret=new ArrayList<ViewableWrapper>();
ArrayList<AbstractClassDef> candids=new ArrayList<AbstractClassDef>();
candids.add(destination);
while(!candids.isEmpty()){
AbstractClassDef candid=candids.remove(0);
String inheritanceStrategy = trafoConfig.getViewableConfig(candid, TrafoConfigNames.INHERITANCE_TRAFO);
if(TrafoConfigNames.INHERITANCE_TRAFO_SUPERCLASS.equals(inheritanceStrategy)){
ret.add(class2wrapper.get(candid)); // ViewableWrapper of base
}else if(TrafoConfigNames.INHERITANCE_TRAFO_SUBCLASS.equals(inheritanceStrategy)){
// visit all sub classes
candids.addAll(candid.getDirectExtensions());
}else if(TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(inheritanceStrategy)){
ret.add(class2wrapper.get(candid));
}else if(TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(inheritanceStrategy)){
ret.add(class2wrapper.get(candid));
// visit all sub classes
candids.addAll(candid.getDirectExtensions());
}else{
throw new IllegalStateException("unexpected mapping strategy <"+inheritanceStrategy+">");
}
}
// buffer result, so that later calls return exactly the same ordering of targetTables
// first call: build sql statement
// second++ calls: set/get sql parameters
targetTablesPool.put(destination, ret);
return ret;
}
protected String getSqlAttrName(AttributeDef def,String ownerSqlTableName,String targetSqlTableName){
return ili2sqlName.mapIliAttributeDef(def,ownerSqlTableName,targetSqlTableName);
}
@@ -190,7 +190,7 @@ public void generateTable(ViewableWrapper def,int pass)
}else{
// add reference to parent for each structAttr when generating structAttr
}
// add seqeunce attr
// add sequence attr
DbColId dbSeq=new DbColId();
dbSeq.setName(DbNames.T_SEQ_COL);
//dbSeq.setNotNull(true); // must be optional for cases where struct is exdended by a class
@@ -234,58 +234,65 @@ public void generateTable(ViewableWrapper def,int pass)
if(role.getExtending()==null){
// not an embedded role and roledef not defined in a lightweight association?
if (!obj.embedded && !def.isAssocLightweight()){
dbColId=new DbColId();
DbTableName targetSqlTableName=class2wrapper.get(role.getDestination()).getSqlTable();
String roleSqlName=getSqlRoleName(role,sqlName.getName(),targetSqlTableName.getName());
dbColId.setName(roleSqlName);
dbColId.setNotNull(true);
dbColId.setPrimaryKey(false);
if(createFk){
dbColId.setReferencedTable(targetSqlTableName);
}
if(createFkIdx){
dbColId.setIndex(true);
}
dbTable.addColumn(dbColId);
// handle ordered
if(role.isOrdered()){
// add seqeunce attr
DbColId dbSeq=new DbColId();
dbSeq.setName(roleSqlName+"_"+DbNames.T_SEQ_COL);
dbSeq.setNotNull(true);
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}
}
// a role of an embedded association?
if(obj.embedded){
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if(roleOwner.getDerivedFrom()==null){
// role is oppend;
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for(ViewableWrapper targetTable : targetTables){
dbColId=new DbColId();
DbTableName targetSqlTableName=class2wrapper.get(role.getDestination()).getSqlTable();
String roleSqlName=getSqlRoleName(role,sqlName.getName(),targetSqlTableName.getName());
DbTableName targetSqlTableName=targetTable.getSqlTable();
String roleSqlName=ili2sqlName.mapIliRoleDef(role,sqlName.getName(),targetSqlTableName.getName(),targetTables.size()>1);
dbColId.setName(roleSqlName);
boolean notNull=false;
dbColId.setNotNull(notNull);
dbColId.setNotNull(true);
dbColId.setPrimaryKey(false);
if(createFk){
dbColId.setReferencedTable(targetSqlTableName);
}
if(createFkIdx){
dbColId.setIndex(true);
}
customMapping.fixupEmbeddedLink(dbTable,dbColId,roleOwner,role,targetSqlTableName,colT_ID);
dbTable.addColumn(dbColId);
// handle ordered
if(role.getOppEnd().isOrdered()){
if(role.isOrdered()){
// add seqeunce attr
DbColId dbSeq=new DbColId();
dbSeq.setName(roleSqlName+"_"+DbNames.T_SEQ_COL);
dbSeq.setNotNull(notNull);
dbSeq.setNotNull(true);
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}
}
}
// a role of an embedded association?
if(obj.embedded){
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if(roleOwner.getDerivedFrom()==null){
// role is oppend;
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for(ViewableWrapper targetTable:targetTables){
dbColId=new DbColId();
DbTableName targetSqlTableName=targetTable.getSqlTable();
String roleSqlName=ili2sqlName.mapIliRoleDef(role,sqlName.getName(),targetSqlTableName.getName(),targetTables.size()>1);
dbColId.setName(roleSqlName);
boolean notNull=false;
dbColId.setNotNull(notNull);
dbColId.setPrimaryKey(false);
if(createFk){
dbColId.setReferencedTable(targetSqlTableName);
}
if(createFkIdx){
dbColId.setIndex(true);
}
customMapping.fixupEmbeddedLink(dbTable,dbColId,roleOwner,role,targetSqlTableName,colT_ID);
dbTable.addColumn(dbColId);
// handle ordered
if(role.getOppEnd().isOrdered()){
// add seqeunce attr
DbColId dbSeq=new DbColId();
dbSeq.setName(roleSqlName+"_"+DbNames.T_SEQ_COL);
dbSeq.setNotNull(notNull);
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}

}
}
}
}
@@ -316,7 +323,7 @@ public void generateTable(ViewableWrapper def,int pass)
}else if(attro instanceof RoleDef){
RoleDef role=(RoleDef) attro;
DbTableName targetSqlTableName=getSqlType(role.getDestination());
attrSqlName=getSqlRoleName(role,def.getSqlTablename(),targetSqlTableName.getName());
attrSqlName=ili2sqlName.mapIliRoleDef(role,def.getSqlTablename(),targetSqlTableName.getName());
}else{
throw new IllegalStateException("unexpected attr "+attro);
}
@@ -334,6 +341,7 @@ public void generateTable(ViewableWrapper def,int pass)
}

}

private HashSet getWrapperCols(List<ViewableTransferElement> attrv) {
HashSet ret=new HashSet();
for(ViewableTransferElement attr:attrv){
@@ -505,16 +513,21 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
dbCol=null;
}
}else if (type instanceof ReferenceType){
DbColId ret=new DbColId();
ret.setNotNull(false);
ret.setPrimaryKey(false);
if(createFk){
ret.setReferencedTable(getSqlType(((ReferenceType)type).getReferred()));
}
if(createFkIdx){
ret.setIndex(true);
ArrayList<ViewableWrapper> targetTables = getTargetTables(((ReferenceType)type).getReferred());
for(ViewableWrapper targetTable:targetTables)
{
DbColId ret=new DbColId();
ret.setName(ili2sqlName.mapIliAttributeDef(attr,dbTable.getName().getName(),targetTable.getSqlTablename(),targetTables.size()>1));
ret.setNotNull(false);
ret.setPrimaryKey(false);
if(createFk){
ret.setReferencedTable(targetTable.getSqlTable());
}
if(createFkIdx){
ret.setIndex(true);
}
dbColExts.add(ret);
}
dbCol=ret;
}else if (type instanceof BasketType){
// skip it; type no longer exists in ili 2.3
dbCol=null;
@@ -170,6 +170,8 @@ public void writeRecord(int basketSqlId, IomObject iomObj,Viewable iomClass,
RoleDef role = (RoleDef) obj.obj;
if(role.getExtending()==null){
if(attrs.contains(role)){

String refoid=null;
String roleName=role.getName();
// a role of an embedded association?
if(obj.embedded){
@@ -182,7 +184,7 @@ public void writeRecord(int basketSqlId, IomObject iomObj,Viewable iomClass,
// TODO handle attributes of link
}
if(structvalue!=null){
String refoid=structvalue.getobjectrefoid();
refoid=structvalue.getobjectrefoid();
long orderPos=structvalue.getobjectreforderpos();
if(orderPos!=0){
// refoid,orderPos
@@ -192,16 +194,13 @@ public void writeRecord(int basketSqlId, IomObject iomObj,Viewable iomClass,
// refoid
//ret.setStringAttribute(roleName, refoid);
}
int refsqlId=oidPool.getObjSqlId(refoid);
ps.setInt(valuei, refsqlId);
}else{
ps.setNull(valuei, Types.INTEGER);
refoid=null;
}
valuei++;
}
}else{
IomObject structvalue=iomObj.getattrobj(roleName,0);
String refoid=structvalue.getobjectrefoid();
refoid=structvalue.getobjectrefoid();
long orderPos=structvalue.getobjectreforderpos();
if(orderPos!=0){
// refoid,orderPos
@@ -211,10 +210,26 @@ public void writeRecord(int basketSqlId, IomObject iomObj,Viewable iomClass,
// refoid
//ret.setStringAttribute(roleName, refoid);
}
int refsqlId=oidPool.getObjSqlId(refoid);
ps.setInt(valuei, refsqlId);
valuei++;
}
String targetClass=null;
if(refoid!=null){
targetClass=oidPool.getXtfObjTag(refoid);
if(targetClass==null){
// should not happen, because objects with forward references are buffered,
// until all other objects are processed
throw new IllegalStateException("unknown class of TID "+refoid);
}
}
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for(ViewableWrapper targetTable : targetTables){
if(targetTable.getViewable()==tag2class.get(targetClass) && refoid!=null){
int refsqlId=oidPool.getObjSqlId(refoid);
ps.setInt(valuei, refsqlId);
}else{
ps.setNull(valuei, Types.INTEGER);
}
valuei++;
}
}
}
}
@@ -440,32 +455,35 @@ public String createInsertStmt(boolean isUpdate,Viewable iomClass,DbTableName sq
RoleDef role = (RoleDef) obj.obj;
if(role.getExtending()==null){
if(attrs.contains(role)){
String roleName=ili2sqlName.mapIliRoleDef(role,sqlTableName.getName(),class2wrapper.get(role.getDestination()).getSqlTablename());
// a role of an embedded association?
if(obj.embedded){
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if(roleOwner.getDerivedFrom()==null){
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if(isUpdate){
ret.append("=?");
}else{
values.append(",?");
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for(ViewableWrapper targetTable : targetTables){
String roleName=ili2sqlName.mapIliRoleDef(role,sqlTableName.getName(),targetTable.getSqlTablename(),targetTables.size()>1);
// a role of an embedded association?
if(obj.embedded){
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if(roleOwner.getDerivedFrom()==null){
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if(isUpdate){
ret.append("=?");
}else{
values.append(",?");
}
sep=",";
}
sep=",";
}
}else{
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if(isUpdate){
ret.append("=?");
}else{
values.append(",?");
}
sep=",";
}
}else{
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if(isUpdate){
ret.append("=?");
}else{
values.append(",?");
}
sep=",";
}
}

}
}
@@ -645,6 +663,20 @@ public String addAttrToInsertStmt(boolean isUpdate,
}
sep=",";
}
}if(type instanceof ReferenceType){
ArrayList<ViewableWrapper> targetTables = getTargetTables(((ReferenceType)type).getReferred());
for(ViewableWrapper targetTable:targetTables)
{
attrSqlName=ili2sqlName.mapIliAttributeDef(attr,sqlTableName,targetTable.getSqlTablename(),targetTables.size()>1);
ret.append(sep);
ret.append(attrSqlName);
if(isUpdate){
ret.append("=?");
}else{
values.append(",?");
}
sep=",";
}
}else{
ret.append(sep);
ret.append(attrSqlName);
Oops, something went wrong.

0 comments on commit fb309f2

Please sign in to comment.