Search in sources :

Example 1 with DbTable

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

the class MetaAttrUtility method addMetaAttributesTable.

// Create meta-attributes table
public static void addMetaAttributesTable(DbSchema schema) {
    DbTable tab = new DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.META_ATTRIBUTES_TAB));
    DbColVarchar ilielementCol = new DbColVarchar();
    ilielementCol.setName(DbNames.META_ATTRIBUTES_TAB_ILIELEMENT_COL);
    ilielementCol.setNotNull(true);
    ilielementCol.setSize(255);
    tab.addColumn(ilielementCol);
    DbColVarchar attrnameCol = new DbColVarchar();
    attrnameCol.setName(DbNames.META_ATTRIBUTES_TAB_ATTRNAME_COL);
    attrnameCol.setNotNull(true);
    attrnameCol.setSize(1024);
    tab.addColumn(attrnameCol);
    DbColVarchar attrvalueCol = new DbColVarchar();
    attrvalueCol.setName(DbNames.META_ATTRIBUTES_TAB_ATTRVALUE_COL);
    attrvalueCol.setNotNull(true);
    attrvalueCol.setSize(1024);
    tab.addColumn(attrvalueCol);
    schema.addTable(tab);
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 2 with DbTable

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

the class GeneratorMsSql method visitIndex.

@Override
public void visitIndex(DbIndex idx) throws IOException {
    if (idx.isUnique()) {
        java.io.StringWriter out = new java.io.StringWriter();
        DbTable tab = idx.getTable();
        String tableName = tab.getName().getQName();
        String constraintName = idx.getName();
        if (constraintName == null) {
            String[] colNames = new String[idx.sizeAttr()];
            int i = 0;
            for (Iterator attri = idx.iteratorAttr(); attri.hasNext(); ) {
                DbColumn attr = (DbColumn) attri.next();
                colNames[i++] = attr.getName();
            }
            constraintName = createConstraintName(tab, "key", colNames);
        }
        out.write(getIndent() + "CREATE UNIQUE INDEX " + constraintName + " ON " + tableName + " (");
        String sep = "";
        String condition = " ";
        String sepCondition = " ";
        for (Iterator attri = idx.iteratorAttr(); attri.hasNext(); ) {
            DbColumn attr = (DbColumn) attri.next();
            out.write(sep + attr.getName());
            condition += sepCondition + attr.getName() + " is not null";
            sep = ",";
            sepCondition = " AND ";
        }
        out.write(") WHERE" + condition + newline());
        String stmt = out.toString();
        addCreateLine(new Stmt(stmt));
        out = null;
        if (createdTables.contains(tab.getName())) {
            Statement dbstmt = null;
            try {
                try {
                    dbstmt = conn.createStatement();
                    EhiLogger.traceBackendCmd(stmt);
                    dbstmt.executeUpdate(stmt);
                } finally {
                    dbstmt.close();
                }
            } catch (SQLException ex) {
                IOException iox = new IOException("failed to add UNIQUE to table " + tab.getName());
                iox.initCause(ex);
                throw iox;
            }
        }
    }
}
Also used : DbColumn(ch.ehi.sqlgen.repository.DbColumn) SQLException(java.sql.SQLException) Statement(java.sql.Statement) IOException(java.io.IOException) Iterator(java.util.Iterator) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 3 with DbTable

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

the class FromIliRecordConverter method addParentRef.

private void addParentRef(Viewable parentTable, AttributeDef attr) {
    CompositionType type = (CompositionType) attr.getDomainResolvingAll();
    Table structClass = type.getComponentType();
    // if abstract struct, might have multiple tables!
    for (ViewableWrapper structWrapper : getStructWrappers(structClass)) {
        DbTableName structClassSqlName = structWrapper.getSqlTable();
        // find struct table
        DbTable dbTable = schema.findTable(structClassSqlName);
        // add ref attr
        String refAttrSqlName = ili2sqlName.mapIliAttributeDefReverse(attr, structClassSqlName.getName(), class2wrapper.get(parentTable).getSqlTablename());
        DbColId dbParentId = new DbColId();
        dbParentId.setName(refAttrSqlName);
        // values of other struct attrs will have NULL
        dbParentId.setNotNull(false);
        dbParentId.setPrimaryKey(false);
        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) {
            dbParentId.setComment(cmt.toString());
        }
        if (createFk) {
            dbParentId.setReferencedTable(class2wrapper.get(parentTable).getSqlTable());
        }
        if (createFkIdx) {
            dbParentId.setIndex(true);
        }
        dbTable.addColumn(dbParentId);
    }
}
Also used : DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbColId(ch.ehi.sqlgen.repository.DbColId) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 4 with DbTable

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

the class TransferFromIli method addSettingsTable.

public static void addSettingsTable(DbSchema schema) {
    DbTable tab = new DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.SETTINGS_TAB));
    DbColVarchar tagCol = new DbColVarchar();
    tagCol.setName(DbNames.SETTINGS_TAB_TAG_COL);
    tagCol.setNotNull(true);
    tagCol.setPrimaryKey(true);
    tagCol.setSize(60);
    tab.addColumn(tagCol);
    DbColVarchar settingCol = new DbColVarchar();
    settingCol.setName(DbNames.SETTINGS_TAB_SETTING_COL);
    settingCol.setNotNull(false);
    settingCol.setSize(255);
    tab.addColumn(settingCol);
    schema.addTable(tab);
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 5 with DbTable

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

the class TransferFromIli method addEnumTable.

public void addEnumTable(DbSchema schema) {
    if (Config.CREATE_ENUM_DEFS_SINGLE.equals(createEnumTable)) {
        DbTable tab = new DbTable();
        DbColVarchar thisClass = new DbColVarchar();
        thisClass.setName(DbNames.ENUM_TAB_THIS_COL);
        thisClass.setNotNull(true);
        thisClass.setSize(1024);
        tab.addColumn(thisClass);
        DbColVarchar baseClass = new DbColVarchar();
        baseClass.setName(DbNames.ENUM_TAB_BASE_COL);
        baseClass.setNotNull(false);
        baseClass.setSize(1024);
        tab.addColumn(baseClass);
        DbColNumber seq = new DbColNumber();
        seq.setName(DbNames.ENUM_TAB_SEQ_COL);
        seq.setNotNull(false);
        seq.setSize(4);
        tab.addColumn(seq);
        DbColBoolean inactiveCol = new DbColBoolean();
        inactiveCol.setName(DbNames.ENUM_TAB_INACTIVE_COL);
        inactiveCol.setNotNull(true);
        tab.addColumn(inactiveCol);
        DbColVarchar iliCode = new DbColVarchar();
        iliCode.setName(DbNames.ENUM_TAB_ILICODE_COL);
        iliCode.setNotNull(true);
        iliCode.setSize(1024);
        tab.addColumn(iliCode);
        tab.setName(new DbTableName(schema.getName(), DbNames.ENUM_TAB));
        DbColNumber itfCode = new DbColNumber();
        itfCode.setName(DbNames.ENUM_TAB_ITFCODE_COL);
        itfCode.setNotNull(true);
        itfCode.setSize(4);
        tab.addColumn(itfCode);
        DbColVarchar dispName = new DbColVarchar();
        dispName.setName(DbNames.ENUM_TAB_DISPNAME_COL);
        dispName.setNotNull(true);
        dispName.setSize(250);
        tab.addColumn(dispName);
        DbColVarchar description = new DbColVarchar();
        description.setName(DbNames.ENUM_TAB_DESCRIPTION_COL);
        description.setNotNull(false);
        description.setSize(1024);
        tab.addColumn(description);
        schema.addTable(tab);
    } else if (Config.CREATE_ENUM_DEFS_MULTI.equals(createEnumTable)) {
        addMissingEnumDomains(visitedEnums);
        java.util.Iterator entri = visitedEnums.iterator();
        while (entri.hasNext()) {
            Object entro = entri.next();
            DbTableName thisSqlName = null;
            if (entro instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) entro;
                ch.interlis.ili2c.metamodel.Type type = attr.getDomain();
                if (type instanceof ch.interlis.ili2c.metamodel.TypeAlias) {
                    // skip it
                    continue;
                } else {
                    thisSqlName = getSqlTableNameEnum(attr);
                }
            } else if (entro instanceof Domain) {
                Domain domain = (Domain) entro;
                if (domain == td.INTERLIS.BOOLEAN) {
                    continue;
                }
                thisSqlName = getSqlTableName(domain);
            }
            if (thisSqlName != null) {
                DbTable tab = new DbTable();
                tab.setName(thisSqlName);
                DbColNumber itfCode = new DbColNumber();
                itfCode.setName(DbNames.ENUM_TAB_ITFCODE_COL);
                itfCode.setNotNull(true);
                itfCode.setSize(4);
                itfCode.setPrimaryKey(true);
                tab.addColumn(itfCode);
                DbColVarchar iliCode = new DbColVarchar();
                iliCode.setName(DbNames.ENUM_TAB_ILICODE_COL);
                iliCode.setNotNull(true);
                iliCode.setSize(1024);
                tab.addColumn(iliCode);
                DbColNumber seq = new DbColNumber();
                seq.setName(DbNames.ENUM_TAB_SEQ_COL);
                seq.setNotNull(false);
                seq.setSize(4);
                tab.addColumn(seq);
                DbColBoolean inactiveCol = new DbColBoolean();
                inactiveCol.setName(DbNames.ENUM_TAB_INACTIVE_COL);
                inactiveCol.setNotNull(true);
                tab.addColumn(inactiveCol);
                DbColVarchar dispName = new DbColVarchar();
                dispName.setName(DbNames.ENUM_TAB_DISPNAME_COL);
                dispName.setNotNull(true);
                dispName.setSize(250);
                tab.addColumn(dispName);
                DbColVarchar description = new DbColVarchar();
                description.setName(DbNames.ENUM_TAB_DESCRIPTION_COL);
                description.setNotNull(false);
                description.setSize(1024);
                tab.addColumn(description);
                schema.addTable(tab);
                metaInfo.setTableInfo(tab.getName().getName(), DbExtMetaInfo.TAG_TAB_TABLEKIND, DbExtMetaInfo.TAG_TAB_TABLEKIND_ENUM);
            }
        }
    }
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbColNumber(ch.ehi.sqlgen.repository.DbColNumber) DbColBoolean(ch.ehi.sqlgen.repository.DbColBoolean) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Domain(ch.interlis.ili2c.metamodel.Domain) DbTableName(ch.ehi.sqlgen.repository.DbTableName) 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