Search in sources :

Example 31 with Ili2dbException

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

the class TrafoConfig method updateTrafoConfig.

public void updateTrafoConfig(java.sql.Connection conn, String schema) throws Ili2dbException {
    HashMap<String, HashMap<String, String>> existingEntries = read(conn, schema);
    String sqlName = DbNames.TRAFO_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    try {
        // update entries
        {
            // UPDATE table_name
            // SET column1=value1,column2=value2,...
            // WHERE some_column=some_value;
            String updStmt = "UPDATE " + sqlName + " SET " + DbNames.TRAFO_TAB_SETTING_COL + "=? WHERE " + DbNames.TRAFO_TAB_ILINAME_COL + "=? AND " + DbNames.TRAFO_TAB_TAG_COL + "=?";
            EhiLogger.traceBackendCmd(updStmt);
            java.sql.PreparedStatement updPrepStmt = conn.prepareStatement(updStmt);
            try {
                ;
                for (String iliname : config.keySet()) {
                    HashMap<String, String> values = config.get(iliname);
                    for (String tag : values.keySet()) {
                        if (containsSetting(existingEntries, iliname, tag)) {
                            // update
                            String value = values.get(tag);
                            updPrepStmt.clearParameters();
                            updPrepStmt.setString(1, value);
                            updPrepStmt.setString(2, iliname);
                            updPrepStmt.setString(3, tag);
                            updPrepStmt.executeUpdate();
                        }
                    }
                }
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to update trafo", ex);
            } finally {
                updPrepStmt.close();
            }
        }
        // insert entries
        {
            String insStmt = "INSERT INTO " + sqlName + " (" + DbNames.TRAFO_TAB_ILINAME_COL + "," + DbNames.TRAFO_TAB_TAG_COL + "," + DbNames.TRAFO_TAB_SETTING_COL + ") VALUES (?,?,?)";
            EhiLogger.traceBackendCmd(insStmt);
            java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
            try {
                ;
                for (String iliname : config.keySet()) {
                    HashMap<String, String> values = config.get(iliname);
                    for (String tag : values.keySet()) {
                        if (!containsSetting(existingEntries, iliname, tag)) {
                            // insert
                            String value = values.get(tag);
                            insPrepStmt.clearParameters();
                            insPrepStmt.setString(1, iliname);
                            insPrepStmt.setString(2, tag);
                            insPrepStmt.setString(3, value);
                            insPrepStmt.executeUpdate();
                        }
                    }
                }
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to insert trafo", ex);
            } finally {
                insPrepStmt.close();
            }
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update trafo-table " + sqlName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) HashMap(java.util.HashMap)

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