Search in sources :

Example 1 with DbColNumber

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

the class GeneratorMsSql method visitTableBeginConstraint.

@Override
public void visitTableBeginConstraint(DbTable dbTab) throws IOException {
    super.visitTableBeginConstraint(dbTab);
    String sqlTabName = dbTab.getName().getQName();
    for (Iterator dbColi = dbTab.iteratorColumn(); dbColi.hasNext(); ) {
        DbColumn dbCol = (DbColumn) dbColi.next();
        if (dbCol.getReferencedTable() != null) {
            String createstmt = null;
            String action = "";
            if (dbCol.getOnUpdateAction() != null) {
                action = action + " ON UPDATE " + dbCol.getOnUpdateAction();
            }
            if (dbCol.getOnDeleteAction() != null) {
                action = action + " ON DELETE " + dbCol.getOnDeleteAction();
            }
            String constraintName = createConstraintName(dbTab, "fkey", dbCol.getName());
            // +action+" DEFERRABLE INITIALLY DEFERRED";
            createstmt = "ALTER TABLE " + sqlTabName + " ADD CONSTRAINT " + constraintName + " FOREIGN KEY ( " + dbCol.getName() + " ) REFERENCES " + dbCol.getReferencedTable().getQName();
            String dropstmt = null;
            dropstmt = "ALTER TABLE " + sqlTabName + " DROP CONSTRAINT " + constraintName;
            addConstraint(dbTab, constraintName, createstmt, dropstmt);
        }
        if (dbCol instanceof DbColNumber && (((DbColNumber) dbCol).getMinValue() != null || ((DbColNumber) dbCol).getMaxValue() != null)) {
            DbColNumber dbColNum = (DbColNumber) dbCol;
            String createstmt = null;
            String action = "";
            if (dbColNum.getMinValue() != null || dbColNum.getMaxValue() != null) {
                if (dbColNum.getMaxValue() == null) {
                    action = ">=" + dbColNum.getMinValue();
                } else if (dbColNum.getMinValue() == null) {
                    action = "<=" + dbColNum.getMaxValue();
                } else {
                    action = "BETWEEN " + dbColNum.getMinValue() + " AND " + dbColNum.getMaxValue();
                }
            }
            String constraintName = createConstraintName(dbTab, "check", dbCol.getName());
            createstmt = "ALTER TABLE " + sqlTabName + " ADD CONSTRAINT " + constraintName + " CHECK( " + dbCol.getName() + " " + action + ")";
            String dropstmt = null;
            dropstmt = "ALTER TABLE " + sqlTabName + " DROP CONSTRAINT " + constraintName;
            addConstraint(dbTab, constraintName, createstmt, dropstmt);
        } else if (dbCol instanceof DbColDecimal && (((DbColDecimal) dbCol).getMinValue() != null || ((DbColDecimal) dbCol).getMaxValue() != null)) {
            DbColDecimal dbColNum = (DbColDecimal) dbCol;
            String createstmt = null;
            String action = "";
            if (dbColNum.getMinValue() != null || dbColNum.getMaxValue() != null) {
                if (dbColNum.getMaxValue() == null) {
                    action = ">=" + dbColNum.getMinValue();
                } else if (dbColNum.getMinValue() == null) {
                    action = "<=" + dbColNum.getMaxValue();
                } else {
                    action = "BETWEEN " + dbColNum.getMinValue() + " AND " + dbColNum.getMaxValue();
                }
            }
            String constraintName = createConstraintName(dbTab, "check", dbCol.getName());
            createstmt = "ALTER TABLE " + sqlTabName + " ADD CONSTRAINT " + constraintName + " CHECK( " + dbCol.getName() + " " + action + ")";
            String dropstmt = null;
            dropstmt = "ALTER TABLE " + sqlTabName + " DROP CONSTRAINT " + constraintName;
            addConstraint(dbTab, constraintName, createstmt, dropstmt);
        }
    }
}
Also used : DbColumn(ch.ehi.sqlgen.repository.DbColumn) Iterator(java.util.Iterator) DbColDecimal(ch.ehi.sqlgen.repository.DbColDecimal) DbColNumber(ch.ehi.sqlgen.repository.DbColNumber)

Example 2 with DbColNumber

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

the class FromIliRecordConverter method createSimpleDbCol.

private boolean createSimpleDbCol(DbTable dbTable, Viewable aclass, AttributeDef attr, Type type, Holder<DbColumn> dbCol, Holder<Unit> unitDef, Holder<Boolean> mText, ArrayList<DbColumn> dbColExts) {
    if (attr.isDomainBoolean()) {
        dbCol.value = new DbColBoolean();
    } else if (attr.isDomainIli1Date()) {
        dbCol.value = new DbColDate();
    } else if (attr.isDomainIliUuid()) {
        dbCol.value = new DbColUuid();
    } else if (attr.isDomainIli2Date()) {
        dbCol.value = new DbColDate();
    } else if (attr.isDomainIli2DateTime()) {
        dbCol.value = new DbColDateTime();
    } else if (attr.isDomainIli2Time()) {
        dbCol.value = new DbColTime();
    } else if (type instanceof BasketType) {
        // skip it; type no longer exists in ili 2.3
        dbCol.value = null;
    } else if (type instanceof EnumerationType) {
        visitedEnumsAttrs.add(attr);
        if (createEnumColAsItfCode) {
            DbColId ret = new DbColId();
            dbCol.value = ret;
        } else {
            DbColVarchar ret = new DbColVarchar();
            ret.setSize(255);
            dbCol.value = ret;
        }
    } else if (type instanceof NumericType) {
        if (type.isAbstract()) {
        } else {
            PrecisionDecimal min = ((NumericType) type).getMinimum();
            PrecisionDecimal max = ((NumericType) type).getMaximum();
            int minLen = min.toString().length();
            int maxLen = max.toString().length();
            if (min.toString().startsWith("-")) {
                minLen -= 1;
            }
            if (max.toString().startsWith("-")) {
                maxLen -= 1;
            }
            if (min.getAccuracy() > 0) {
                DbColDecimal ret = new DbColDecimal();
                int size = Math.max(minLen, maxLen) - 1;
                int precision = min.getAccuracy();
                // EhiLogger.debug("attr "+ attr.getName()+", maxStr <"+maxStr+">, size "+Integer.toString(size)+", precision "+Integer.toString(precision));
                ret.setSize(size);
                ret.setPrecision(precision);
                if (createNumCheck) {
                    ret.setMinValue(min.doubleValue());
                    ret.setMaxValue(max.doubleValue());
                }
                dbCol.value = ret;
            } else {
                DbColNumber ret = new DbColNumber();
                int size = Math.max(minLen, maxLen);
                ret.setSize(size);
                if (createNumCheck) {
                    ret.setMinValue((int) min.doubleValue());
                    ret.setMaxValue((int) max.doubleValue());
                }
                dbCol.value = ret;
            }
            unitDef.value = ((NumericType) type).getUnit();
        }
    } else if (type instanceof TextType) {
        DbColVarchar ret = new DbColVarchar();
        if (((TextType) type).getMaxLength() > 0) {
            ret.setSize(((TextType) type).getMaxLength());
        } else {
            ret.setSize(DbColVarchar.UNLIMITED);
        }
        if (!((TextType) type).isNormalized()) {
            mText.value = true;
        }
        dbCol.value = ret;
    } else if (type instanceof BlackboxType) {
        if (((BlackboxType) type).getKind() == BlackboxType.eXML) {
            DbColXml ret = new DbColXml();
            dbCol.value = ret;
        } else {
            DbColBlob ret = new DbColBlob();
            dbCol.value = ret;
        }
    } else {
        return false;
    }
    return true;
}
Also used : NumericType(ch.interlis.ili2c.metamodel.NumericType) DbColXml(ch.ehi.sqlgen.repository.DbColXml) DbColBlob(ch.ehi.sqlgen.repository.DbColBlob) BasketType(ch.interlis.ili2c.metamodel.BasketType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbColTime(ch.ehi.sqlgen.repository.DbColTime) DbColDecimal(ch.ehi.sqlgen.repository.DbColDecimal) DbColDateTime(ch.ehi.sqlgen.repository.DbColDateTime) DbColNumber(ch.ehi.sqlgen.repository.DbColNumber) DbColDate(ch.ehi.sqlgen.repository.DbColDate) UniquenessConstraint(ch.interlis.ili2c.metamodel.UniquenessConstraint) TextType(ch.interlis.ili2c.metamodel.TextType) PrecisionDecimal(ch.interlis.ili2c.metamodel.PrecisionDecimal) DbColBoolean(ch.ehi.sqlgen.repository.DbColBoolean) DbColUuid(ch.ehi.sqlgen.repository.DbColUuid) DbColId(ch.ehi.sqlgen.repository.DbColId)

Example 3 with DbColNumber

use of ch.ehi.sqlgen.repository.DbColNumber 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)

Example 4 with DbColNumber

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

the class GeneratorMsSql method visitColumn.

@Override
public void visitColumn(DbTable dbTab, DbColumn column) throws IOException {
    String type = "";
    if (column instanceof DbColBoolean) {
        type = "BIT";
    } else if (column instanceof DbColDateTime) {
        type = "DATETIME";
    } else if (column instanceof DbColDate) {
        type = "DATE";
    } else if (column instanceof DbColTime) {
        type = "TIME";
    } else if (column instanceof DbColDecimal) {
        DbColDecimal col = (DbColDecimal) column;
        type = "DECIMAL(" + Integer.toString(col.getSize()) + "," + Integer.toString(col.getPrecision()) + ")";
    } else if (column instanceof DbColGeometry) {
        type = "GEOMETRY";
    } else if (column instanceof DbColId) {
        type = "BIGINT";
    } else if (column instanceof DbColUuid) {
        type = "VARCHAR(36)";
    } else if (column instanceof DbColNumber) {
        DbColNumber col = (DbColNumber) column;
        type = "NUMERIC(" + Integer.toString(col.getSize()) + ")";
    } else if (column instanceof DbColVarchar) {
        int colsize = ((DbColVarchar) column).getSize();
        if (colsize != DbColVarchar.UNLIMITED)
            type = "VARCHAR(" + Integer.toString(colsize) + ")";
        else
            type = "VARCHAR(MAX)";
    } else {
        type = "VARCHAR(MAX)";
    }
    String isNull = column.isNotNull() ? "NOT NULL" : "NULL";
    if (column.isPrimaryKey()) {
        isNull = "PRIMARY KEY";
    }
    String sep = " ";
    String defaultValue = "";
    if (column.getDefaultValue() != null) {
        defaultValue = sep + "DEFAULT (" + column.getDefaultValue() + ")";
        sep = " ";
    }
    String name = column.getName();
    out.write(getIndent() + colSep + "[" + name + "] " + type + " " + isNull + defaultValue + newline());
    colSep = ",";
}
Also used : DbColBoolean(ch.ehi.sqlgen.repository.DbColBoolean) DbColGeometry(ch.ehi.sqlgen.repository.DbColGeometry) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbColTime(ch.ehi.sqlgen.repository.DbColTime) DbColDecimal(ch.ehi.sqlgen.repository.DbColDecimal) DbColDateTime(ch.ehi.sqlgen.repository.DbColDateTime) DbColId(ch.ehi.sqlgen.repository.DbColId) DbColUuid(ch.ehi.sqlgen.repository.DbColUuid) DbColNumber(ch.ehi.sqlgen.repository.DbColNumber) DbColDate(ch.ehi.sqlgen.repository.DbColDate)

Aggregations

DbColNumber (ch.ehi.sqlgen.repository.DbColNumber)4 DbColBoolean (ch.ehi.sqlgen.repository.DbColBoolean)3 DbColDecimal (ch.ehi.sqlgen.repository.DbColDecimal)3 DbColVarchar (ch.ehi.sqlgen.repository.DbColVarchar)3 DbColDate (ch.ehi.sqlgen.repository.DbColDate)2 DbColDateTime (ch.ehi.sqlgen.repository.DbColDateTime)2 DbColId (ch.ehi.sqlgen.repository.DbColId)2 DbColTime (ch.ehi.sqlgen.repository.DbColTime)2 DbColUuid (ch.ehi.sqlgen.repository.DbColUuid)2 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)2 Iterator (java.util.Iterator)2 DbColBlob (ch.ehi.sqlgen.repository.DbColBlob)1 DbColGeometry (ch.ehi.sqlgen.repository.DbColGeometry)1 DbColXml (ch.ehi.sqlgen.repository.DbColXml)1 DbColumn (ch.ehi.sqlgen.repository.DbColumn)1 DbTable (ch.ehi.sqlgen.repository.DbTable)1 DbTableName (ch.ehi.sqlgen.repository.DbTableName)1 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)1 BasketType (ch.interlis.ili2c.metamodel.BasketType)1 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)1