Search in sources :

Example 1 with ObjectType

use of ch.interlis.ili2c.metamodel.ObjectType in project ili2db by claeis.

the class ToXtfRecordConverter method createQueryStmt.

/**
 * creates sql query statement for a class.
 * @param aclass type of objects to build query for
 * @param wrapper not null, if building query for struct values
 * @return SQL-Query statement
 */
public String createQueryStmt(Viewable aclass1, Long basketSqlId, StructWrapper structWrapper) {
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    ViewableWrapper rootWrapper = aclass.getWrappers().get(0);
    StringBuffer ret = new StringBuffer();
    ret.append("SELECT r0." + colT_ID);
    if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
        ret.append(", r0." + DbNames.T_TYPE_COL);
    }
    if (!aclass.isStructure()) {
        if (createIliTidCol || aclass.getOid() != null) {
            ret.append(", r0." + DbNames.T_ILI_TID_COL);
        }
    }
    if (structWrapper != null) {
        if (createGenericStructRef) {
            ret.append(", r0." + DbNames.T_PARENT_ID_COL);
            ret.append(", r0." + DbNames.T_PARENT_TYPE_COL);
            ret.append(", r0." + DbNames.T_PARENT_ATTR_COL);
        } else {
            ret.append(", r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()));
        }
        ret.append(", r0." + DbNames.T_SEQ_COL);
    }
    String sep = ",";
    int tableAliasIdx = 0;
    HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
    for (ViewableWrapper table : aclass.getWrappers()) {
        String tableAlias = "r" + tableAliasIdx;
        String sqlTableName = table.getSqlTablename();
        Iterator iter = table.getAttrIterator();
        while (iter.hasNext()) {
            ViewableTransferElement obj = (ViewableTransferElement) iter.next();
            if (obj.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) obj.obj;
                AttributeDef baseAttr = attr;
                while (true) {
                    AttributeDef baseAttr1 = (AttributeDef) baseAttr.getExtending();
                    if (baseAttr1 == null) {
                        break;
                    }
                    baseAttr = baseAttr1;
                }
                if (!visitedAttrs.contains(baseAttr)) {
                    visitedAttrs.add(baseAttr);
                    if (!baseAttr.isTransient()) {
                        Type proxyType = baseAttr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            sep = addAttrToQueryStmt(ret, sep, tableAlias, baseAttr, sqlTableName);
                        }
                    }
                }
            }
            if (obj.obj instanceof RoleDef) {
                RoleDef role = (RoleDef) obj.obj;
                if (role.getExtending() == null) {
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    for (ViewableWrapper targetTable : targetTables) {
                        String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlTableName, 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);
                                sep = ",";
                                ret.append(makeColumnRef(tableAlias, roleSqlName));
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            ret.append(sep);
                            sep = ",";
                            ret.append(makeColumnRef(tableAlias, roleSqlName));
                        }
                    }
                }
            }
        }
        // next table alias
        tableAliasIdx++;
    }
    // stdcols
    if (createStdCols) {
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_LAST_CHANGE_COL);
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_CREATE_DATE_COL);
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_USER_COL);
    }
    ret.append(" FROM ");
    ArrayList<ViewableWrapper> tablev = new ArrayList<ViewableWrapper>(10);
    tablev.addAll(aclass.getWrappers());
    sep = "";
    int tablec = tablev.size();
    if (isMsAccess) {
        for (int i = 0; i < tablec; i++) {
            ret.append("(");
        }
    }
    for (int i = 0; i < tablec; i++) {
        ret.append(sep);
        ret.append(tablev.get(i).getSqlTableQName());
        ret.append(" r" + Integer.toString(i));
        if (i > 0) {
            ret.append(" ON r0." + colT_ID + "=r" + Integer.toString(i) + "." + colT_ID);
        }
        if (isMsAccess) {
            ret.append(")");
        }
        sep = " LEFT JOIN ";
    }
    sep = " WHERE";
    if (createTypeDiscriminator || rootWrapper.includesMultipleTypes()) {
        ret.append(sep + " r0." + DbNames.T_TYPE_COL + "='" + getSqlType(aclass1).getName() + "'");
        sep = " AND";
    }
    if (structWrapper != null) {
        if (createGenericStructRef) {
            ret.append(sep + " r0." + DbNames.T_PARENT_ID_COL + "=? AND r0." + DbNames.T_PARENT_ATTR_COL + "=?");
        } else {
            ret.append(sep + " r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()) + "=?");
        }
        sep = " AND";
    }
    if (basketSqlId != null) {
        ret.append(sep + " r0." + DbNames.T_BASKET_COL + "=?");
    }
    if (structWrapper != null) {
        ret.append(" ORDER BY r0." + DbNames.T_SEQ_COL + " ASC");
    }
    return ret.toString();
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 2 with ObjectType

use of ch.interlis.ili2c.metamodel.ObjectType in project ili2db by claeis.

the class FromXtfRecordConverter method createInsertStmt.

/**
 * creates an insert statement for a given viewable.
 * @param sqlTableName table name of viewable
 * @param aclass viewable
 * @return insert statement
 */
public String createInsertStmt(boolean isUpdate, Viewable iomClass, DbTableName sqlTableName, ViewableWrapper aclass, StructWrapper structEle) {
    StringBuffer ret = new StringBuffer();
    StringBuffer values = new StringBuffer();
    // WHERE some_column=some_value;
    if (isUpdate) {
        ret.append("UPDATE ");
    } else {
        ret.append("INSERT INTO ");
    }
    ret.append(sqlTableName.getQName());
    String sep = null;
    if (isUpdate) {
        sep = " SET ";
    } else {
        sep = " (";
    }
    // add T_Id
    if (!isUpdate) {
        ret.append(sep);
        ret.append(colT_ID);
        values.append("?");
        sep = ",";
    }
    // add T_basket
    if (createBasketCol) {
        ret.append(sep);
        ret.append(DbNames.T_BASKET_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (createDatasetCol) {
        ret.append(sep);
        ret.append(DbNames.T_DATASET_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (!aclass.isSecondaryTable()) {
        // if root, add type
        if (aclass.getExtending() == null) {
            if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
                ret.append(sep);
                ret.append(DbNames.T_TYPE_COL);
                if (isUpdate) {
                    ret.append("=?");
                } else {
                    values.append(",?");
                }
                sep = ",";
            }
            // if Class
            if (!aclass.isStructure()) {
                if (!isUpdate) {
                    if (createIliTidCol || aclass.getOid() != null) {
                        ret.append(sep);
                        ret.append(DbNames.T_ILI_TID_COL);
                        values.append(",?");
                        sep = ",";
                    }
                }
            }
            // if STRUCTURE, add ref to parent
            if (aclass.isStructure()) {
                if (structEle == null) {
                // struct is extended by a class and current object is an instance of the class
                } else {
                    // current object is an instance of the structure
                    if (createGenericStructRef) {
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_ID_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_TYPE_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                        // attribute name in parent class
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_ATTR_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                    } else {
                        ret.append(sep);
                        Viewable parentViewable = getViewable(structEle.getParentSqlType());
                        ViewableWrapper parentTable = getViewableWrapperOfAbstractClass((Viewable) structEle.getParentAttr().getContainer(), parentViewable);
                        ret.append(ili2sqlName.mapIliAttributeDefReverse(structEle.getParentAttr(), sqlTableName.getName(), parentTable.getSqlTablename()));
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                    }
                    // seqeunce (not null if LIST)
                    ret.append(sep);
                    ret.append(DbNames.T_SEQ_COL);
                    if (isUpdate) {
                        ret.append("=?");
                    } else {
                        values.append(",?");
                    }
                    sep = ",";
                }
            }
        }
    }
    HashSet attrs = getIomObjectAttrs(iomClass);
    Iterator iter = aclass.getAttrIterator();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attrs.contains(attr)) {
                if (!attr.isTransient()) {
                    Type proxyType = attr.getDomain();
                    if (proxyType != null && (proxyType instanceof ObjectType)) {
                    // skip implicit particles (base-viewables) of views
                    } else {
                        sep = addAttrToInsertStmt(isUpdate, ret, values, sep, attr, sqlTableName.getName());
                    }
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                if (attrs.contains(role)) {
                    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 = ",";
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            ret.append(sep);
                            ret.append(roleName);
                            if (isUpdate) {
                                ret.append("=?");
                            } else {
                                values.append(",?");
                            }
                            sep = ",";
                        }
                    }
                }
            }
        }
    }
    // stdcols
    if (createStdCols) {
        ret.append(sep);
        ret.append(DbNames.T_LAST_CHANGE_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
        if (!isUpdate) {
            ret.append(sep);
            ret.append(DbNames.T_CREATE_DATE_COL);
            values.append(",?");
            sep = ",";
        }
        ret.append(sep);
        ret.append(DbNames.T_USER_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (isUpdate) {
        // WHERE some_column=some_value;
        // add T_Id
        ret.append(" WHERE ");
        ret.append(colT_ID);
        ret.append("=?");
    } else {
        ret.append(") VALUES (");
        ret.append(values);
        ret.append(")");
    }
    return ret.toString();
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) NumericalType(ch.interlis.ili2c.metamodel.NumericalType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) LineType(ch.interlis.ili2c.metamodel.LineType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 3 with ObjectType

use of ch.interlis.ili2c.metamodel.ObjectType in project ili2db by claeis.

the class TransferFromXtf method allReferencesKnownHelper.

private void allReferencesKnownHelper(IomObject iomObj, FixIomObjectExtRefs extref) {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        return;
    }
    // ASSERT: an ordinary class/table
    Viewable aclass = (Viewable) modelele;
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (!attr.isTransient()) {
                Type proxyType = attr.getDomain();
                if (proxyType != null && (proxyType instanceof ObjectType)) {
                // skip implicit particles (base-viewables) of views
                } else {
                    allReferencesKnownHelper(iomObj, attr, extref);
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                String roleName = role.getName();
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // not just a link?
                        IomObject structvalue = iomObj.getattrobj(roleName, 0);
                        if (roleOwner.getAttributes().hasNext() || roleOwner.getLightweightAssociations().iterator().hasNext()) {
                        // TODO handle attributes of link
                        }
                        if (structvalue != null) {
                            String refoid = structvalue.getobjectrefoid();
                            Viewable targetClass = role.getDestination();
                            if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                                extref.addFix(structvalue, targetClass);
                            }
                        }
                    }
                } else {
                    IomObject structvalue = iomObj.getattrobj(roleName, 0);
                    String refoid = structvalue.getobjectrefoid();
                    Viewable targetClass = role.getDestination();
                    if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                        extref.addFix(structvalue, targetClass);
                    }
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 4 with ObjectType

use of ch.interlis.ili2c.metamodel.ObjectType in project ili2db by claeis.

the class FromIliRecordConverter method generateTable.

public void generateTable(ViewableWrapper def, int pass) throws Ili2dbException {
    if (!def.isSecondaryTable()) {
        surfaceAttrs = new ArrayList<AttributeDef>();
    }
    // EhiLogger.debug("viewable "+def);
    if (pass == 1) {
        DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
        DbTable dbTable = new DbTable();
        dbTable.setName(sqlName);
        schema.addTable(dbTable);
        return;
    }
    // second pass; add columns
    DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
    DbTable dbTable = schema.findTable(sqlName);
    ViewableWrapper base = def.getExtending();
    {
        StringBuffer cmt = new StringBuffer();
        String cmtSep = "";
        if (!def.isSecondaryTable()) {
            dbTable.setIliName(def.getViewable().getScopedName(null));
            if (def.getViewable().getDocumentation() != null) {
                cmt.append(cmtSep + def.getViewable().getDocumentation());
                cmtSep = nl;
            }
            cmt.append(cmtSep + "@iliname " + def.getViewable().getScopedName(null));
            cmtSep = nl;
        }
        if (cmt.length() > 0) {
            dbTable.setComment(cmt.toString());
        }
    }
    if (deleteExistingData) {
        dbTable.setDeleteDataIfTableExists(true);
    }
    if (base == null && !def.isSecondaryTable()) {
        dbTable.setRequiresSequence(true);
    }
    String baseRef = "";
    DbColId dbColId = addKeyCol(dbTable);
    if (base != null) {
        dbColId.setScriptComment("REFERENCES " + base.getViewable().getScopedName(null));
        if (createFk) {
            dbColId.setReferencedTable(getSqlType(base.getViewable()));
        }
    } else if (def.isSecondaryTable()) {
        if (createFk) {
            dbColId.setReferencedTable(new DbTableName(schema.getName(), def.getMainTable().getSqlTablename()));
        }
    }
    if (createBasketCol) {
        // add basketCol
        DbColId t_basket = new DbColId();
        t_basket.setName(DbNames.T_BASKET_COL);
        t_basket.setNotNull(true);
        t_basket.setScriptComment("REFERENCES " + DbNames.BASKETS_TAB);
        if (createFk) {
            t_basket.setReferencedTable(new DbTableName(schema.getName(), DbNames.BASKETS_TAB));
        }
        if (createFkIdx) {
            t_basket.setIndex(true);
        }
        dbTable.addColumn(t_basket);
    }
    if (createDatasetCol) {
        DbColVarchar t_dsName = new DbColVarchar();
        t_dsName.setName(DbNames.T_DATASET_COL);
        t_dsName.setSize(DbNames.DATASETNAME_COL_SIZE);
        t_dsName.setNotNull(true);
        t_dsName.setIndex(true);
        dbTable.addColumn(t_dsName);
    }
    DbColumn dbCol;
    if (base == null && !def.isSecondaryTable()) {
        if (createTypeDiscriminator || def.includesMultipleTypes()) {
            dbCol = createSqlTypeCol(DbNames.T_TYPE_COL);
            dbTable.addColumn(dbCol);
        }
        // if CLASS
        if (!def.isStructure()) {
            if (createIliTidCol || def.getOid() != null) {
                addIliTidCol(dbTable, def.getOid());
            }
        }
        // if STRUCTURE, add ref to parent
        if (def.isStructure()) {
            if (createGenericStructRef) {
                // add parentid
                DbColId dbParentId = new DbColId();
                dbParentId.setName(DbNames.T_PARENT_ID_COL);
                dbParentId.setNotNull(true);
                dbParentId.setPrimaryKey(false);
                dbTable.addColumn(dbParentId);
                // add parent_type
                dbCol = createSqlTypeCol(DbNames.T_PARENT_TYPE_COL);
                dbTable.addColumn(dbCol);
                // add parent_attr
                dbCol = createSqlTypeCol(DbNames.T_PARENT_ATTR_COL);
                dbTable.addColumn(dbCol);
            } else {
            // add reference to parent for each structAttr when generating structAttr
            }
            // 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
            dbSeq.setPrimaryKey(false);
            dbTable.addColumn(dbSeq);
        }
    }
    // body
    Iterator<ViewableTransferElement> iter = def.getAttrIterator();
    while (iter.hasNext()) {
        ViewableTransferElement obj = iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attr.getExtending() == null) {
                try {
                    if (createItfLineTables && attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
                        surfaceAttrs.add(attr);
                    }
                    if (!attr.isTransient()) {
                        Type proxyType = attr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            generateAttr(dbTable, def.getViewable(), attr);
                        }
                    }
                } catch (Exception ex) {
                    throw new Ili2dbException(attr.getContainer().getScopedName(null) + "." + attr.getName(), ex);
                }
            } else {
                if (attr.isDomainBoolean()) {
                } else if (createEnumColAsItfCode && attr.getDomainResolvingAll() instanceof EnumerationType) {
                    throw new Ili2dbException("EXTENDED attributes with type enumeration not supported");
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                // not an embedded role and roledef not defined in a lightweight association?
                if (!obj.embedded && !def.isAssocLightweight()) {
                    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;
                        if (!sqlEnableNull) {
                            if (targetTables.size() > 1) {
                                // multiple alternative FK columns
                                notNull = false;
                            } else {
                                notNull = true;
                            }
                        }
                        dbColId.setNotNull(notNull);
                        dbColId.setPrimaryKey(false);
                        if (createFk) {
                            dbColId.setReferencedTable(targetSqlTableName);
                        }
                        if (createFkIdx) {
                            dbColId.setIndex(true);
                        }
                        String cmt = role.getDocumentation();
                        if (cmt != null && cmt.length() > 0) {
                            dbColId.setComment(cmt);
                        }
                        dbTable.addColumn(dbColId);
                        // handle ordered
                        if (role.isOrdered()) {
                            // add seqeunce attr
                            DbColId dbSeq = new DbColId();
                            dbSeq.setName(roleSqlName + "_" + DbNames.T_SEQ_COL);
                            dbSeq.setNotNull(notNull);
                            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;
                            if (!sqlEnableNull) {
                                if (targetTables.size() > 1) {
                                    // multiple alternative FK columns
                                    notNull = false;
                                } else if (role.getOppEnd().getDestination() != def.getViewable()) {
                                    // other subtypes in of def don't have this FK
                                    notNull = false;
                                } else {
                                    if (role.getCardinality().getMinimum() == 0) {
                                        notNull = false;
                                    } else {
                                        notNull = true;
                                    }
                                }
                            }
                            dbColId.setNotNull(notNull);
                            dbColId.setPrimaryKey(false);
                            if (createFk) {
                                dbColId.setReferencedTable(targetSqlTableName);
                            }
                            if (createFkIdx) {
                                dbColId.setIndex(true);
                            }
                            String cmt = role.getDocumentation();
                            if (cmt != null && cmt.length() > 0) {
                                dbColId.setComment(cmt);
                            }
                            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);
                            }
                        }
                    }
                }
            }
        }
    }
    if (createStdCols) {
        addStdCol(dbTable);
    }
    if (createUnique && !def.isStructure()) {
        // check if UNIQUE mappable
        HashSet wrapperCols = getWrapperCols(def.getAttrv());
        Viewable aclass = def.getViewable();
        Iterator it = aclass.iterator();
        while (it.hasNext()) {
            Object cnstro = it.next();
            if (cnstro instanceof UniquenessConstraint) {
                UniquenessConstraint cnstr = (UniquenessConstraint) cnstro;
                HashSet attrs = getUniqueAttrs(cnstr, wrapperCols);
                // mappable?
                if (attrs != null) {
                    DbIndex dbIndex = new DbIndex();
                    dbIndex.setPrimary(false);
                    dbIndex.setUnique(true);
                    for (Object attro : attrs) {
                        String attrSqlName = null;
                        if (attro instanceof AttributeDef) {
                            attrSqlName = ili2sqlName.mapIliAttributeDef((AttributeDef) attro, def.getSqlTablename(), null);
                        } else if (attro instanceof RoleDef) {
                            RoleDef role = (RoleDef) attro;
                            DbTableName targetSqlTableName = getSqlType(role.getDestination());
                            attrSqlName = ili2sqlName.mapIliRoleDef(role, def.getSqlTablename(), targetSqlTableName.getName());
                        } else {
                            throw new IllegalStateException("unexpected attr " + attro);
                        }
                        DbColumn idxCol = dbTable.getColumn(attrSqlName);
                        dbIndex.addAttr(idxCol);
                    }
                    dbTable.addIndex(dbIndex);
                }
            }
        }
    }
    if (!def.isSecondaryTable()) {
        customMapping.fixupViewable(dbTable, def.getViewable());
    }
}
Also used : DbColumn(ch.ehi.sqlgen.repository.DbColumn) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbIndex(ch.ehi.sqlgen.repository.DbIndex) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet) ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) BasketType(ch.interlis.ili2c.metamodel.BasketType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) TextType(ch.interlis.ili2c.metamodel.TextType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Viewable(ch.interlis.ili2c.metamodel.Viewable) DbColId(ch.ehi.sqlgen.repository.DbColId) UniquenessConstraint(ch.interlis.ili2c.metamodel.UniquenessConstraint) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 5 with ObjectType

use of ch.interlis.ili2c.metamodel.ObjectType in project ili2db by claeis.

the class ToXtfRecordConverter method convertRecord.

public Iom_jObject convertRecord(java.sql.ResultSet rs, Viewable aclass1, FixIomObjectRefs fixref, StructWrapper structWrapper, HashMap structelev, ArrayList<StructWrapper> structQueue, long sqlid) throws SQLException {
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    Iom_jObject iomObj;
    int valuei = 1;
    valuei++;
    if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
        // String t_type=rs.getString(valuei);
        valuei++;
    }
    String sqlIliTid = null;
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            if (createIliTidCol || aclass.getOid() != null) {
                sqlIliTid = rs.getString(valuei);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
                valuei++;
            } else {
                sqlIliTid = Long.toString(sqlid);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
            }
        }
    }
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), sqlIliTid);
        } else {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), null);
        }
        iomObj.setattrvalue(ItfWriter2.INTERNAL_T_ID, Long.toString(sqlid));
        fixref.setRoot(iomObj);
    } else {
        iomObj = (Iom_jObject) structelev.get(Long.toString(sqlid));
        if (createGenericStructRef) {
            valuei += 4;
        } else {
            valuei += 2;
        }
    }
    HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
    for (ViewableWrapper table : aclass.getWrappers()) {
        Iterator iter = table.getAttrIterator();
        while (iter.hasNext()) {
            ViewableTransferElement obj = (ViewableTransferElement) iter.next();
            if (obj.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) obj.obj;
                AttributeDef baseAttr = attr;
                while (true) {
                    AttributeDef baseAttr1 = (AttributeDef) baseAttr.getExtending();
                    if (baseAttr1 == null) {
                        break;
                    }
                    baseAttr = baseAttr1;
                }
                if (!visitedAttrs.contains(baseAttr)) {
                    visitedAttrs.add(baseAttr);
                    if (!baseAttr.isTransient()) {
                        Type proxyType = baseAttr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            valuei = addAttrValue(rs, valuei, sqlid, iomObj, baseAttr, structQueue, table, fixref);
                        }
                    }
                }
            }
            if (obj.obj instanceof RoleDef) {
                RoleDef role = (RoleDef) obj.obj;
                if (role.getExtending() == null) {
                    String roleName = role.getName();
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    boolean refAlreadyDefined = false;
                    for (ViewableWrapper targetTable : targetTables) {
                        String sqlRoleName = ili2sqlName.mapIliRoleDef(role, getSqlType(table.getViewable()).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){
                                long value = rs.getLong(valuei);
                                valuei++;
                                if (!rs.wasNull()) {
                                    if (refAlreadyDefined) {
                                        EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                    } else {
                                        IomObject ref = iomObj.addattrobj(roleName, roleOwner.getScopedName(null));
                                        mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                        refAlreadyDefined = true;
                                    }
                                }
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            long value = rs.getLong(valuei);
                            valuei++;
                            if (!rs.wasNull()) {
                                if (refAlreadyDefined) {
                                    EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                } else {
                                    IomObject ref = iomObj.addattrobj(roleName, "REF");
                                    mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                    refAlreadyDefined = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return iomObj;
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) Iom_jObject(ch.interlis.iom_j.Iom_jObject) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Aggregations

AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)8 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)8 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)8 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)8 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8 Type (ch.interlis.ili2c.metamodel.Type)8 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)8 Iterator (java.util.Iterator)8 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)7 CoordType (ch.interlis.ili2c.metamodel.CoordType)7 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)7 ReferenceType (ch.interlis.ili2c.metamodel.ReferenceType)7 RoleDef (ch.interlis.ili2c.metamodel.RoleDef)7 AreaType (ch.interlis.ili2c.metamodel.AreaType)6 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)6 HashSet (java.util.HashSet)6 NumericType (ch.interlis.ili2c.metamodel.NumericType)5 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)4 Viewable (ch.interlis.ili2c.metamodel.Viewable)4