Search in sources :

Example 6 with DbTable

use of ch.ehi.sqlgen.repository.DbTable in project ili2db by claeis.

the class TransferFromIli method addInheritanceTable.

public static void addInheritanceTable(DbSchema schema, int sqlNameSize) {
    DbTable tab = new DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.INHERIT_TAB));
    DbColVarchar thisClass = new DbColVarchar();
    thisClass.setName(DbNames.INHERIT_TAB_THIS_COL);
    thisClass.setNotNull(true);
    thisClass.setPrimaryKey(true);
    thisClass.setSize(1024);
    tab.addColumn(thisClass);
    DbColVarchar baseClass = new DbColVarchar();
    baseClass.setName(DbNames.INHERIT_TAB_BASE_COL);
    baseClass.setNotNull(false);
    baseClass.setSize(1024);
    tab.addColumn(baseClass);
    schema.addTable(tab);
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 7 with DbTable

use of ch.ehi.sqlgen.repository.DbTable in project ili2db by claeis.

the class TransferFromIli method generateItfLineTable.

private void generateItfLineTable(AttributeDef attr, int pass) throws Ili2dbException {
    if (pass == 1) {
        DbTableName sqlName = getSqlTableNameItfLineTable(attr);
        DbTable dbTable = new DbTable();
        dbTable.setName(sqlName);
        dbTable.setIliName(attr.getContainer().getScopedName(null) + "." + attr.getName());
        schema.addTable(dbTable);
        return;
    }
    // second pass; add columns
    DbTableName sqlName = getSqlTableNameItfLineTable(attr);
    DbTable dbTable = schema.findTable(sqlName);
    StringBuffer cmt = new StringBuffer();
    String cmtSep = "";
    if (attr.getDocumentation() != null) {
        cmt.append(cmtSep + attr.getDocumentation());
        cmtSep = nl;
    }
    cmt.append(cmtSep + "@iliname " + attr.getContainer().getScopedName(null) + "." + attr.getName());
    cmtSep = nl;
    if (cmt.length() > 0) {
        dbTable.setComment(cmt.toString());
    }
    if (deleteExistingData) {
        dbTable.setDeleteDataIfTableExists(true);
    }
    dbTable.setRequiresSequence(true);
    DbColId dbColId = recConv.addKeyCol(dbTable);
    if (createIliTidCol) {
        recConv.addIliTidCol(dbTable, null);
    }
    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);
    }
    SurfaceOrAreaType type = (SurfaceOrAreaType) attr.getDomainResolvingAll();
    DbColGeometry dbCol = recConv.generatePolylineType(type, attr.getContainer().getScopedName(null) + "." + attr.getName());
    recConv.setCrs(dbCol, attr);
    dbCol.setName(ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr, sqlName.getName()));
    dbCol.setNotNull(true);
    dbTable.addColumn(dbCol);
    if (type instanceof SurfaceType) {
        dbColId = new DbColId();
        dbColId.setName(ili2sqlName.getSqlColNameItfLineTableRefAttr(attr, sqlName.getName()));
        dbColId.setNotNull(true);
        dbColId.setPrimaryKey(false);
        dbColId.setScriptComment("REFERENCES " + recConv.getSqlType((Viewable) attr.getContainer()));
        if (createFk) {
            dbColId.setReferencedTable(recConv.getSqlType((Viewable) attr.getContainer()));
        }
        if (createFkIdx) {
            dbColId.setIndex(true);
        }
        dbTable.addColumn(dbColId);
    }
    Table lineAttrTable = type.getLineAttributeStructure();
    if (lineAttrTable != null) {
        Iterator attri = lineAttrTable.getAttributes();
        while (attri.hasNext()) {
            AttributeDef lineattr = (AttributeDef) attri.next();
            recConv.generateAttr(dbTable, lineAttrTable, lineattr);
        }
    }
    if (createStdCols) {
        AbstractRecordConverter.addStdCol(dbTable);
    }
}
Also used : DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) DbColGeometry(ch.ehi.sqlgen.repository.DbColGeometry) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) DbColId(ch.ehi.sqlgen.repository.DbColId) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 8 with DbTable

use of ch.ehi.sqlgen.repository.DbTable 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 9 with DbTable

use of ch.ehi.sqlgen.repository.DbTable in project ili2db by claeis.

the class TransferFromIli method addModelsTable.

public static void addModelsTable(DbSchema schema, Settings config) {
    DbTable tab = new DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.MODELS_TAB));
    DbColVarchar fileCol = new DbColVarchar();
    fileCol.setName(DbNames.MODELS_TAB_FILE_COL);
    fileCol.setNotNull(true);
    fileCol.setSize(250);
    tab.addColumn(fileCol);
    DbColVarchar iliversionCol = new DbColVarchar();
    iliversionCol.setName(DbNames.MODELS_TAB_ILIVERSION_COL);
    iliversionCol.setNotNull(true);
    iliversionCol.setSize(3);
    tab.addColumn(iliversionCol);
    DbColVarchar importsCol = new DbColVarchar();
    importsCol.setName(DbNames.MODELS_TAB_MODELNAME_COL);
    importsCol.setNotNull(true);
    int modelNameColSize = DbColVarchar.UNLIMITED;
    String modelNameColSizeStr = config.getValue(Config.MODELS_TAB_MODELNAME_COLSIZE);
    if (modelNameColSizeStr != null) {
        try {
            modelNameColSize = Integer.parseInt(modelNameColSizeStr);
        } catch (NumberFormatException e) {
        }
    }
    importsCol.setSize(modelNameColSize);
    tab.addColumn(importsCol);
    DbColVarchar contentCol = new DbColVarchar();
    contentCol.setName(DbNames.MODELS_TAB_CONTENT_COL);
    contentCol.setNotNull(true);
    contentCol.setSize(DbColVarchar.UNLIMITED);
    tab.addColumn(contentCol);
    DbColDateTime importDateCol = new DbColDateTime();
    importDateCol.setName(DbNames.MODELS_TAB_IMPORTDATE_COL);
    importDateCol.setNotNull(true);
    tab.addColumn(importDateCol);
    DbIndex pk = new DbIndex();
    pk.setPrimary(true);
    pk.addAttr(importsCol);
    pk.addAttr(iliversionCol);
    tab.addIndex(pk);
    schema.addTable(tab);
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbColDateTime(ch.ehi.sqlgen.repository.DbColDateTime) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbIndex(ch.ehi.sqlgen.repository.DbIndex) DbTable(ch.ehi.sqlgen.repository.DbTable)

Aggregations

DbTable (ch.ehi.sqlgen.repository.DbTable)9 DbTableName (ch.ehi.sqlgen.repository.DbTableName)8 DbColVarchar (ch.ehi.sqlgen.repository.DbColVarchar)7 Iterator (java.util.Iterator)4 DbColId (ch.ehi.sqlgen.repository.DbColId)3 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)3 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)3 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)3 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)2 DbColumn (ch.ehi.sqlgen.repository.DbColumn)2 DbIndex (ch.ehi.sqlgen.repository.DbIndex)2 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)2 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)2 Table (ch.interlis.ili2c.metamodel.Table)2 Viewable (ch.interlis.ili2c.metamodel.Viewable)2 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)1 DbColBoolean (ch.ehi.sqlgen.repository.DbColBoolean)1 DbColDateTime (ch.ehi.sqlgen.repository.DbColDateTime)1 DbColGeometry (ch.ehi.sqlgen.repository.DbColGeometry)1 DbColNumber (ch.ehi.sqlgen.repository.DbColNumber)1