Search in sources :

Example 6 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class ColumnNameMapping method readAttrMappingTable.

public void readAttrMappingTable(java.sql.Connection conn, String schema) throws Ili2dbException {
    String mapTableName = DbNames.ATTRNAME_TAB;
    if (schema != null) {
        mapTableName = schema + "." + mapTableName;
    }
    // create table
    String stmt = "SELECT " + DbNames.ATTRNAME_TAB_ILINAME_COL + ", " + DbNames.ATTRNAME_TAB_SQLNAME_COL + ", " + DbNames.ATTRNAME_TAB_OWNER_COL + ", " + DbNames.ATTRNAME_TAB_TARGET_COL + " FROM " + mapTableName;
    java.sql.Statement dbstmt = null;
    try {
        dbstmt = conn.createStatement();
        java.sql.ResultSet rs = dbstmt.executeQuery(stmt);
        while (rs.next()) {
            String iliname = rs.getString(DbNames.ATTRNAME_TAB_ILINAME_COL);
            String sqlname = rs.getString(DbNames.ATTRNAME_TAB_SQLNAME_COL);
            String owner = rs.getString(DbNames.ATTRNAME_TAB_OWNER_COL);
            String target = rs.getString(DbNames.ATTRNAME_TAB_TARGET_COL);
            // EhiLogger.debug("map: "+iliname+"->"+sqlname);
            addAttrNameMapping(iliname, sqlname, owner, target);
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to query mapping-table " + mapTableName, ex);
    } finally {
        if (dbstmt != null) {
            try {
                dbstmt.close();
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to close query of " + mapTableName, ex);
            }
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException)

Example 7 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class ColumnNameMapping method updateAttrMappingTable.

public void updateAttrMappingTable(java.sql.Connection conn, String schema) throws Ili2dbException {
    HashSet<AttrMappingKey> exstEntries = readAttrMappingTableEntries(conn, schema);
    String mapTabName = DbNames.ATTRNAME_TAB;
    if (schema != null) {
        mapTabName = schema + "." + mapTabName;
    }
    // create table
    try {
        // insert mapping entries
        String stmt = "INSERT INTO " + mapTabName + " (" + DbNames.ATTRNAME_TAB_ILINAME_COL + "," + DbNames.ATTRNAME_TAB_SQLNAME_COL + "," + DbNames.ATTRNAME_TAB_OWNER_COL + "," + DbNames.ATTRNAME_TAB_TARGET_COL + ") VALUES (?,?,?,?)";
        EhiLogger.traceBackendCmd(stmt);
        java.sql.PreparedStatement ps = conn.prepareStatement(stmt);
        AttrMappingKey entry1 = null;
        try {
            for (AttrMappingKey entry : attrNameIli2sql.keySet()) {
                if (!exstEntries.contains(entry)) {
                    entry1 = entry;
                    String sqlname = attrNameIli2sql.get(entry);
                    ps.setString(1, entry.getIliname());
                    ps.setString(2, sqlname);
                    ps.setString(3, entry.getOwner());
                    ps.setString(4, entry.getTarget());
                    ps.executeUpdate();
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert attrname-mapping " + entry1, ex);
        } finally {
            ps.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update mapping-table " + mapTabName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException)

Example 8 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException 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 9 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException 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 Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class TransferFromIli method addModels.

public static void addModels(java.sql.Connection conn, TransferDescription td, String schema) throws Ili2dbException {
    // read existing models from db
    IliFiles iliModelsInDb = TransferFromIli.readIliFiles(conn, schema);
    String sqlName = DbNames.MODELS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis());
    try {
        // insert entries
        String insStmt = "INSERT INTO " + sqlName + " (" + DbNames.MODELS_TAB_FILE_COL + "," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + "," + DbNames.MODELS_TAB_CONTENT_COL + "," + DbNames.MODELS_TAB_IMPORTDATE_COL + ") VALUES (?,?,?,?,?)";
        if (isMsSqlServer(conn)) {
            // 'file' is keyword in sql server
            insStmt = "INSERT INTO " + sqlName + " (\"" + DbNames.MODELS_TAB_FILE_COL + "\"," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + "," + DbNames.MODELS_TAB_CONTENT_COL + "," + DbNames.MODELS_TAB_IMPORTDATE_COL + ") VALUES (?,?,?,?,?)";
        }
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        java.util.Iterator entri = td.iterator();
        HashMap<java.io.File, ch.interlis.ili2c.modelscan.IliFile> ilifiles = new HashMap<java.io.File, ch.interlis.ili2c.modelscan.IliFile>();
        while (entri.hasNext()) {
            Object entro = entri.next();
            if (entro instanceof ch.interlis.ili2c.metamodel.Model) {
                if (entro instanceof ch.interlis.ili2c.metamodel.PredefinedModel) {
                    continue;
                }
                ch.interlis.ili2c.metamodel.Model model = (ch.interlis.ili2c.metamodel.Model) entro;
                java.io.File file = new java.io.File(model.getFileName());
                ch.interlis.ili2c.modelscan.IliFile ilifile = null;
                if (ilifiles.containsKey(file)) {
                    ilifile = ilifiles.get(file);
                } else {
                    ilifile = new ch.interlis.ili2c.modelscan.IliFile();
                    ilifile.setFilename(file);
                    ilifiles.put(file, ilifile);
                }
                ch.interlis.ili2c.modelscan.IliModel ilimodel = new ch.interlis.ili2c.modelscan.IliModel();
                ilimodel.setIliVersion(Double.parseDouble(model.getIliVersion()));
                ilimodel.setName(model.getName());
                Model[] imports = model.getImporting();
                for (Model importm : imports) {
                    ilimodel.addDepenedency(importm.getName());
                }
                Model translatedModel = (Model) model.getTranslationOf();
                if (translatedModel != null) {
                    ilimodel.addDepenedency(translatedModel.getName());
                }
                ilifile.addModel(ilimodel);
            }
        }
        try {
            for (ch.interlis.ili2c.modelscan.IliFile ilifile : ilifiles.values()) {
                ch.interlis.ili2c.modelscan.IliModel ilimodel = (ch.interlis.ili2c.modelscan.IliModel) ilifile.iteratorModel().next();
                if (iliModelsInDb == null || iliModelsInDb.getFileWithModel(ilimodel.getName(), ilimodel.getIliVersion()) == null) {
                    insPrepStmt.clearParameters();
                    insPrepStmt.setString(1, ilifile.getFilename().getName());
                    insPrepStmt.setString(2, Double.toString(ilifile.getIliVersion()));
                    insPrepStmt.setString(3, IliImportsUtility.getIliImports(ilifile));
                    insPrepStmt.setString(4, readFileAsString(ilifile.getFilename()));
                    insPrepStmt.setTimestamp(5, today);
                    insPrepStmt.executeUpdate();
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert model", ex);
        } catch (IOException e) {
            throw new Ili2dbException("failed to update models-table " + sqlName, e);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update models-table " + sqlName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) IliFiles(ch.interlis.ilirepository.IliFiles) Iterator(java.util.Iterator) IOException(java.io.IOException) Model(ch.interlis.ili2c.metamodel.Model) Model(ch.interlis.ili2c.metamodel.Model)

Aggregations

Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)31 SQLException (java.sql.SQLException)13 DbTableName (ch.ehi.sqlgen.repository.DbTableName)10 HashSet (java.util.HashSet)10 Iterator (java.util.Iterator)8 HashMap (java.util.HashMap)7 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)6 Viewable (ch.interlis.ili2c.metamodel.Viewable)6 PreparedStatement (java.sql.PreparedStatement)5 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)4 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)4 Model (ch.interlis.ili2c.metamodel.Model)4 Domain (ch.interlis.ili2c.metamodel.Domain)3 Topic (ch.interlis.ili2c.metamodel.Topic)3 View (ch.interlis.ili2c.metamodel.View)3 IomObject (ch.interlis.iom.IomObject)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 DbTable (ch.ehi.sqlgen.repository.DbTable)2 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)2