Search in sources :

Example 1 with RoleDef

use of ch.interlis.ili2c.metamodel.RoleDef 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 RoleDef

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

the class TransferToXtf method genClassHelper.

private void genClassHelper(Viewable aclass) {
    boolean doStruct = false;
    if (aclass instanceof Table) {
        doStruct = !((Table) aclass).isIdentifiable();
    }
    if (doStruct) {
        expgen.println("private void add" + aclass.getName() + "(String parentTid,String parentAttrSql,IomObject parent,String parentAttrIli)");
    } else {
        expgen.println("private void add" + aclass.getName() + "(String subset)");
        String addany = "if(iliClassName.equals(\"" + aclass.getScopedName(null) + "\")){add" + aclass.getName() + "(select);}";
        addanyLines.add(addany);
    }
    expgen.println("{");
    expgen.indent();
    expgen.println("String tabName=\"" + createQueryStmtFromClause(aclass) + "\";");
    expgen.println("String stmt=\"" + recConv.createQueryStmt(aclass, null, null) + "\";");
    if (!doStruct) {
        expgen.println("if(subset!=null){");
        expgen.println("stmt=stmt+\" AND \"+subset;");
        expgen.println("}");
    }
    expgen.println("EhiLogger.traceBackendCmd(stmt);");
    expgen.println("java.sql.PreparedStatement dbstmt = null;");
    expgen.println("try{");
    expgen.indent();
    // expgen.println("dbstmt = conn.createStatement();");
    // expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery(stmt);");
    expgen.println("dbstmt = conn.prepareStatement(stmt);");
    expgen.println("dbstmt.clearParameters();");
    if (doStruct) {
        expgen.println("dbstmt.setString(1,parentTid);");
        expgen.println("dbstmt.setString(2,parentAttrSql);");
    }
    expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery();");
    expgen.println("while(rs.next()){");
    expgen.indent();
    expgen.println("String tid=DbUtility.getString(rs,\"T_Id\",false,tabName);");
    expgen.println("String recInfo=tabName+\" \"+tid;");
    expgen.println("IomObject iomObj;");
    if (!doStruct) {
        expgen.println("iomObj=newObject(\"" + aclass.getScopedName(null) + "\",mapId(\"" + recConv.getSqlType(aclass) + "\",tid));");
    } else {
        expgen.println("iomObj=(IomObject)parent.addattrobj(parentAttrIli,\"" + aclass.getScopedName(null) + "\");");
    }
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attr.getExtending() == null) {
                String attrName = attr.getName();
                String sqlAttrName = ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null);
                Type type = attr.getDomain();
                if ((type instanceof TypeAlias) && Ili2cUtility.isBoolean(td, type)) {
                    expgen.println("Boolean prop_" + attrName + "=Db2Xtf.getBoolean(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
                } else {
                    type = attr.getDomainResolvingAliases();
                    if (type instanceof CompositionType) {
                        // enque iomObj as parent
                        // enqueueStructAttr(new StructWrapper(tid,attr,iomObj));
                        CompositionType ct = (CompositionType) type;
                        expgen.println("add" + ct.getComponentType().getName() + "(tid,\"" + ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null) + "\",iomObj,\"" + attr.getName() + "\");");
                    } else if (type instanceof PolylineType) {
                    } else if (type instanceof SurfaceOrAreaType) {
                    } else if (type instanceof CoordType) {
                    } else if (type instanceof EnumerationType) {
                        String enumName = null;
                        if (attr.getDomain() instanceof TypeAlias) {
                            Domain domainDef = ((TypeAlias) attr.getDomain()).getAliasing();
                            enumName = domainDef.getScopedName(null);
                        } else {
                            enumName = aclass.getScopedName(null) + "->" + attrName;
                            enumTypes.add(enumName);
                        }
                        expgen.println("String prop_" + attrName + "=Db2Xtf.getEnum(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,getEnumMapper(\"" + enumName + "\"),iomObj,\"" + attrName + "\");");
                    } else {
                        expgen.println("String prop_" + attrName + "=Db2Xtf.getString(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
                    }
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                String roleName = role.getName();
                String sqlRoleName = ili2sqlName.mapIliRoleDef(role, recConv.getSqlType(aclass).getName(), recConv.getSqlType(role.getDestination()).getName());
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // TODO if(orderPos!=0){
                        expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "true" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"" + roleOwner.getScopedName(null) + "\");");
                    }
                } else {
                    // TODO if(orderPos!=0){
                    expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "false" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"REF\");");
                }
            }
        }
    }
    // add referenced and referencing objects if it is not a struct
    if (!doStruct && aclass instanceof AbstractClassDef) {
        AbstractClassDef aclass2 = (AbstractClassDef) aclass;
        Iterator associ = aclass2.getTargetForRoles();
        while (associ.hasNext()) {
            RoleDef roleThis = (RoleDef) associ.next();
            if (roleThis.getKind() == RoleDef.Kind.eAGGREGATE || roleThis.getKind() == RoleDef.Kind.eCOMPOSITE) {
                RoleDef oppEnd = roleThis.getOppEnd();
                if (roleThis.isAssociationEmbedded()) {
                    expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"T_Id\",prop_" + oppEnd.getName() + ");");
                } else if (oppEnd.isAssociationEmbedded()) {
                    expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",tid);");
                } else {
                    expgen.println("addPendingObject(\"" + ((AssociationDef) (oppEnd.getContainer())).getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",prop_" + roleThis.getName() + ");");
                }
            }
        }
    }
    // writeObject if it is not a struct
    if (!doStruct) {
        expgen.println("writeObject(iomObj);");
    }
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}catch(java.sql.SQLException ex){");
    expgen.indent();
    expgen.println("EhiLogger.logError(\"failed to query \"+tabName,ex);");
    expgen.unindent();
    expgen.println("}finally{");
    expgen.indent();
    expgen.println("if(dbstmt!=null){");
    expgen.indent();
    expgen.println("try{");
    expgen.indent();
    expgen.println("dbstmt.close();");
    expgen.unindent();
    expgen.println("}catch(java.sql.SQLException ex){");
    expgen.indent();
    expgen.println("EhiLogger.logError(\"failed to close query of \"+tabName,ex);");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) TypeAlias(ch.interlis.ili2c.metamodel.TypeAlias) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) Domain(ch.interlis.ili2c.metamodel.Domain) CoordType(ch.interlis.ili2c.metamodel.CoordType)

Example 3 with RoleDef

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

the class Viewable2TableMapper method addProps.

private void addProps(ViewableWrapper viewable, List<ViewableTransferElement> attrv, Iterator<ViewableTransferElement> iter) {
    boolean hasGeometry = false;
    // only one geometry column per table?
    if (singleGeom) {
        for (ViewableTransferElement attrE : attrv) {
            if (attrE.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) attrE.obj;
                ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
                if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO)))) || (Ili2cUtility.isMultiPointAttr(getTransferDescription(attr), attr) && (coalesceMultiPoint || TrafoConfigNames.MULTIPOINT_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTIPOINT_TRAFO))))) {
                    hasGeometry = true;
                    break;
                }
            }
        }
    }
    while (iter.hasNext()) {
        ViewableTransferElement obj = iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            String sqlname = trafoConfig.getAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE);
            // attribute configured to be in a secondary table?
            if (sqlname != null) {
                // add attribute to given secondary table
                ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
                if (attrWrapper == null) {
                    attrWrapper = viewable.createSecondaryTable(sqlname);
                }
                List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
                attrProps.add(obj);
                attrWrapper.setAttrv(attrProps);
            } else {
                // only one geometry column per table?
                if (singleGeom) {
                    ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
                    if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO))))) {
                        if (createItfLineTables && type instanceof ch.interlis.ili2c.metamodel.SurfaceOrAreaType) {
                        // ignore it; will be created by legacy code
                        } else {
                            // table already has a geometry column?
                            if (hasGeometry) {
                                // create a new secondary table
                                sqlname = nameMapping.mapGeometryAsTable(attr);
                                ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
                                if (attrWrapper == null) {
                                    attrWrapper = viewable.createSecondaryTable(sqlname);
                                }
                                // add attribute to new secondary table
                                List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
                                attrProps.add(obj);
                                attrWrapper.setAttrv(attrProps);
                                trafoConfig.setAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE, sqlname);
                            } else {
                                // table has not yet a geometry column
                                // add it
                                hasGeometry = true;
                                attrv.add(obj);
                            }
                        }
                    } else {
                        // not a Geom type
                        attrv.add(obj);
                    }
                } else {
                    attrv.add(obj);
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            AssociationDef roleOwner = (AssociationDef) role.getContainer();
            // not an embedded role and roledef not defined in a lightweight association?
            if (!obj.embedded && !roleOwner.isLightweight()) {
                attrv.add(obj);
            }
            // a role of an embedded association?
            if (obj.embedded) {
                if (roleOwner.getDerivedFrom() == null) {
                    attrv.add(obj);
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 4 with RoleDef

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

the class FromIliRecordConverter method getUniqueAttrs.

private HashSet getUniqueAttrs(UniquenessConstraint cnstr, HashSet wrapperCols) {
    if (cnstr.getLocal()) {
        return null;
    }
    HashSet ret = new HashSet();
    UniqueEl attribs = cnstr.getElements();
    Iterator attri = attribs.iteratorAttribute();
    for (; attri.hasNext(); ) {
        ObjectPath path = (ObjectPath) attri.next();
        PathEl[] pathEles = path.getPathElements();
        if (pathEles.length != 1) {
            return null;
        }
        PathEl pathEle = pathEles[0];
        if (pathEle instanceof AttributeRef) {
            AttributeDef attr = ((AttributeRef) pathEle).getAttr();
            while (attr.getExtending() != null) {
                attr = (AttributeDef) attr.getExtending();
            }
            if (!wrapperCols.contains(attr)) {
                return null;
            }
            ret.add(attr);
        } else if (pathEle instanceof PathElAssocRole) {
            RoleDef role = ((PathElAssocRole) pathEle).getRole();
            while (role.getExtending() != null) {
                role = (RoleDef) role.getExtending();
            }
            if (!wrapperCols.contains(role)) {
                return null;
            }
            ret.add(role);
        } else {
            return null;
        }
    }
    return ret;
}
Also used : ObjectPath(ch.interlis.ili2c.metamodel.ObjectPath) PathElAssocRole(ch.interlis.ili2c.metamodel.PathElAssocRole) AttributeRef(ch.interlis.ili2c.metamodel.AttributeRef) UniqueEl(ch.interlis.ili2c.metamodel.UniqueEl) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) PathEl(ch.interlis.ili2c.metamodel.PathEl) HashSet(java.util.HashSet)

Example 5 with RoleDef

use of ch.interlis.ili2c.metamodel.RoleDef 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)

Aggregations

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