Search in sources :

Example 6 with DbTableName

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

the class TransferFromIli method readIliFiles.

public static ch.interlis.ilirepository.IliFiles readIliFiles(java.sql.Connection conn, String schema) throws Ili2dbException {
    String sqlName = DbNames.MODELS_TAB;
    if (!DbUtility.tableExists(conn, new DbTableName(schema, sqlName))) {
        return null;
    }
    ch.interlis.ilirepository.IliFiles ret = new ch.interlis.ilirepository.IliFiles();
    try {
        String reposUri = conn.getMetaData().getURL();
        if (schema != null) {
            sqlName = schema + "." + sqlName;
            reposUri = reposUri + "/" + schema;
        }
        // select entries
        String insStmt = "SELECT " + DbNames.MODELS_TAB_FILE_COL + "," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + " FROM " + sqlName;
        if (isMsSqlServer(conn)) {
            // 'file' is keyword in sql server
            insStmt = "SELECT \"" + DbNames.MODELS_TAB_FILE_COL + "\"," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + " FROM " + sqlName;
        }
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        try {
            java.sql.ResultSet rs = insPrepStmt.executeQuery();
            while (rs.next()) {
                String file = rs.getString(1);
                double iliversion = Double.parseDouble(rs.getString(2));
                String imports = rs.getString(3);
                ch.interlis.ili2c.modelscan.IliFile iliFile = IliImportsUtility.parseIliImports(iliversion, imports);
                iliFile.setPath(file);
                iliFile.setRepositoryUri(reposUri);
                ret.addFile(iliFile);
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to read IliFiles from db", ex);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to read models-table " + sqlName, ex);
    }
    return ret;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) IliFiles(ch.interlis.ilirepository.IliFiles) SQLException(java.sql.SQLException) DbTableName(ch.ehi.sqlgen.repository.DbTableName) IliFiles(ch.interlis.ilirepository.IliFiles)

Example 7 with DbTableName

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

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

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

the class TransferFromIli method updateMultiEnumTable.

public void updateMultiEnumTable(java.sql.Connection conn) throws Ili2dbException {
    addMissingEnumDomains(visitedEnums);
    java.util.Iterator entri = visitedEnums.iterator();
    while (entri.hasNext()) {
        Object entro = entri.next();
        if (entro instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) entro;
            if (attr.getDomain() instanceof ch.interlis.ili2c.metamodel.TypeAlias) {
                continue;
            }
            EnumerationType type = (EnumerationType) attr.getDomainResolvingAll();
            String thisClass = attr.getContainer().getScopedName(null) + "." + attr.getName();
            DbTableName thisSqlName = getSqlTableNameEnum(attr);
            HashSet exstEntries = readEnumTable(conn, false, thisClass, thisSqlName);
            try {
                // insert entries
                String stmt = "INSERT INTO " + thisSqlName + " (" + DbNames.ENUM_TAB_SEQ_COL + "," + DbNames.ENUM_TAB_ILICODE_COL + "," + DbNames.ENUM_TAB_ITFCODE_COL + "," + DbNames.ENUM_TAB_DISPNAME_COL + "," + DbNames.ENUM_TAB_INACTIVE_COL + "," + DbNames.ENUM_TAB_DESCRIPTION_COL + ") VALUES (?,?,?,?,?,?)";
                EhiLogger.traceBackendCmd(stmt);
                java.sql.PreparedStatement ps = conn.prepareStatement(stmt);
                try {
                    updateEnumEntries(exstEntries, ps, type, null, null);
                } catch (java.sql.SQLException ex) {
                    throw new Ili2dbException("failed to insert enum values for type " + thisClass, ex);
                } finally {
                    ps.close();
                }
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to update enum-table " + thisSqlName, ex);
            }
        } else if (entro instanceof Domain) {
            Domain domain = (Domain) entro;
            if (domain == td.INTERLIS.BOOLEAN) {
                continue;
            }
            EnumerationType type = (EnumerationType) domain.getType();
            String thisClass = domain.getScopedName(null);
            DbTableName thisSqlName = getSqlTableName(domain);
            HashSet exstEntries = readEnumTable(conn, false, thisClass, thisSqlName);
            try {
                // insert entries
                String stmt = "INSERT INTO " + thisSqlName + " (" + DbNames.ENUM_TAB_SEQ_COL + "," + DbNames.ENUM_TAB_ILICODE_COL + "," + DbNames.ENUM_TAB_ITFCODE_COL + "," + DbNames.ENUM_TAB_DISPNAME_COL + "," + DbNames.ENUM_TAB_INACTIVE_COL + "," + DbNames.ENUM_TAB_DESCRIPTION_COL + ") VALUES (?,?,?,?,?,?)";
                EhiLogger.traceBackendCmd(stmt);
                java.sql.PreparedStatement ps = conn.prepareStatement(stmt);
                try {
                    updateEnumEntries(exstEntries, ps, type, null, null);
                } catch (java.sql.SQLException ex) {
                    throw new Ili2dbException("failed to insert enum values for type " + thisClass, ex);
                } finally {
                    ps.close();
                }
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to update enum-table " + thisSqlName, ex);
            }
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) Iterator(java.util.Iterator) SQLException(java.sql.SQLException) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) SQLException(java.sql.SQLException) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Domain(ch.interlis.ili2c.metamodel.Domain) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

Example 10 with DbTableName

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

the class TransferFromIli method addTableMappingTable.

public static void addTableMappingTable(ch.ehi.sqlgen.repository.DbSchema schema) {
    ch.ehi.sqlgen.repository.DbTable tab = new ch.ehi.sqlgen.repository.DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.CLASSNAME_TAB));
    ch.ehi.sqlgen.repository.DbColVarchar iliClassName = new ch.ehi.sqlgen.repository.DbColVarchar();
    iliClassName.setName(DbNames.CLASSNAME_TAB_ILINAME_COL);
    iliClassName.setNotNull(true);
    iliClassName.setSize(1024);
    iliClassName.setPrimaryKey(true);
    tab.addColumn(iliClassName);
    ch.ehi.sqlgen.repository.DbColVarchar sqlTableName = new ch.ehi.sqlgen.repository.DbColVarchar();
    sqlTableName.setName(DbNames.CLASSNAME_TAB_SQLNAME_COL);
    sqlTableName.setNotNull(true);
    sqlTableName.setSize(1024);
    tab.addColumn(sqlTableName);
    schema.addTable(tab);
}
Also used : DbTable(ch.ehi.sqlgen.repository.DbTable) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTable(ch.ehi.sqlgen.repository.DbTable)

Aggregations

DbTableName (ch.ehi.sqlgen.repository.DbTableName)33 DbTable (ch.ehi.sqlgen.repository.DbTable)11 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)10 DbColVarchar (ch.ehi.sqlgen.repository.DbColVarchar)10 Iterator (java.util.Iterator)10 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)9 SQLException (java.sql.SQLException)8 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)7 GeneratorJdbc (ch.ehi.sqlgen.generator_impl.jdbc.GeneratorJdbc)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)4 Viewable (ch.interlis.ili2c.metamodel.Viewable)4 StdLogEvent (ch.ehi.basics.logging.StdLogEvent)3 ConverterException (ch.ehi.ili2db.converter.ConverterException)3 SqlColumnConverter (ch.ehi.ili2db.converter.SqlColumnConverter)3 CustomMapping (ch.ehi.ili2db.fromili.CustomMapping)3 ModelElementSelector (ch.ehi.ili2db.fromili.ModelElementSelector)3