Search in sources :

Example 21 with DbTableName

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

the class TableBasedIdGen method addMappingTable.

// CREATE TABLE T_Key_Object (
// T_Key                Text(30) NOT NULL,
// T_LastUniqueId       LONG NOT NULL,
// T_CreateDate         DATE NOT NULL,
// T_LastChange         DATE NOT NULL,
// T_User               TEXT(30) NOT NULL,
// CONSTRAINT PK PRIMARY KEY (T_Key)
// );
@Override
public void addMappingTable(ch.ehi.sqlgen.repository.DbSchema schema) {
    ch.ehi.sqlgen.repository.DbTable tab = new ch.ehi.sqlgen.repository.DbTable();
    tab.setName(new DbTableName(schema.getName(), SQL_T_KEY_OBJECT));
    ch.ehi.sqlgen.repository.DbColVarchar t_key = new ch.ehi.sqlgen.repository.DbColVarchar();
    t_key.setName(SQL_T_KEY);
    t_key.setNotNull(true);
    t_key.setPrimaryKey(true);
    t_key.setSize(30);
    tab.addColumn(t_key);
    ch.ehi.sqlgen.repository.DbColNumber t_lastuniqueid = new ch.ehi.sqlgen.repository.DbColNumber();
    t_lastuniqueid.setName(SQL_T_LASTUNIQUEID);
    t_lastuniqueid.setNotNull(true);
    tab.addColumn(t_lastuniqueid);
    ch.ehi.ili2db.converter.AbstractRecordConverter.addStdCol(tab);
    schema.addTable(tab);
}
Also used : DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 22 with DbTableName

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

the class TransferToXtf method doBasket.

private boolean doBasket(String filename, IoxWriter iomFile, Topic topic, Long basketSqlId, String basketXtfId) throws IoxException {
    Model model = (Model) topic.getContainer();
    boolean referrs = false;
    StartBasketEvent iomBasket = null;
    delayedObjects = new ArrayList<FixIomObjectRefs>();
    // for all Viewables
    Iterator iter = null;
    if (iomFile instanceof ItfWriter) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = topic.getViewables().iterator();
    }
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (languageFilter != null) {
                    aclass = (Viewable) aclass.getTranslationOfOrSame();
                }
                ViewableWrapper wrapper = class2wrapper.get(aclass);
                // get sql name
                DbTableName sqlName = recConv.getSqlType(wrapper.getViewable());
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(aclass.getScopedName(null) + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), basketXtfId);
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpObject(iomFile, aclass, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (iomFile instanceof ItfWriter) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(attr.getContainer().getScopedName(null) + "_" + attr.getName() + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), topic.getScopedName(null));
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpItfTableObject(iomFile, attr, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    if (iomBasket != null) {
        // fix forward references
        for (FixIomObjectRefs fixref : delayedObjects) {
            boolean skipObj = false;
            for (IomObject ref : fixref.getRefs()) {
                long sqlid = fixref.getTargetSqlid(ref);
                if (sqlidPool.containsSqlid(sqlid)) {
                    // fix it
                    ref.setobjectrefoid(sqlidPool.getXtfid(sqlid));
                } else {
                    // object in another basket
                    Viewable aclass = fixref.getTargetClass(ref);
                    // read object
                    String tid = readObjectTid(aclass, sqlid);
                    if (tid == null) {
                        EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " sqlid " + fixref.getTargetSqlid(ref) + " referenced from " + fixref.getRoot().getobjecttag() + " TID " + fixref.getRoot().getobjectoid());
                        referrs = true;
                        skipObj = true;
                    } else {
                        // fix reference
                        ref.setobjectrefoid(tid);
                    }
                }
            }
            if (!skipObj) {
                ObjectEvent objEvent = new ObjectEvent(fixref.getRoot());
                if (languageFilter != null) {
                    objEvent = (ObjectEvent) languageFilter.filter(objEvent);
                }
                if (exportBaseModelFilter != null) {
                    objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
                }
                if (objEvent != null) {
                    if (validator != null)
                        validator.validate(objEvent);
                    iomFile.write(objEvent);
                }
            }
        }
        EndBasketEvent endBasket = new EndBasketEvent();
        if (languageFilter != null) {
            endBasket = (EndBasketEvent) languageFilter.filter(endBasket);
        }
        if (exportBaseModelFilter != null) {
            endBasket = (EndBasketEvent) exportBaseModelFilter.filter(endBasket);
        }
        if (validator != null)
            validator.validate(endBasket);
        iomFile.write(endBasket);
        saveObjStat(iomBasket.getBid(), basketSqlId, filename, iomBasket.getType());
    }
    return referrs;
}
Also used : ObjectEvent(ch.interlis.iox_j.ObjectEvent) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) EndBasketEvent(ch.interlis.iox_j.EndBasketEvent) IomObject(ch.interlis.iom.IomObject) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) PredefinedModel(ch.interlis.ili2c.metamodel.PredefinedModel) TypeModel(ch.interlis.ili2c.metamodel.TypeModel) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) StartBasketEvent(ch.interlis.iox_j.StartBasketEvent) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) ItfWriter(ch.interlis.iom_j.itf.ItfWriter) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 23 with DbTableName

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

the class MsSqlSequenceBasedIdGen method initDbDefs.

@Override
public void initDbDefs(Generator gen) {
    DbTableName sqlName = new DbTableName(schema, SQL_ILI2DB_SEQ_NAME);
    String stmt = "CREATE SEQUENCE " + sqlName.getQName() + " START WITH " + startWith;
    if (minValue != null) {
        stmt = stmt + " MINVALUE " + minValue;
    }
    if (maxValue != null) {
        stmt = stmt + " MAXVALUE " + maxValue;
    }
    stmt = stmt + ";";
    if (gen instanceof GeneratorJdbc) {
        ((GeneratorJdbc) gen).addCreateLine(((GeneratorJdbc) gen).new Stmt(stmt));
        ((GeneratorJdbc) gen).addDropLine(((GeneratorJdbc) gen).new Stmt("DROP SEQUENCE " + sqlName.getQName() + ";"));
    }
    // Check if a sequence exists (PLSQL)
    String seqExist = "IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + sqlName.getQName() + "') AND type = 'SO')\r\n" + "begin\r\n" + stmt + "\r\nend";
    stmt = seqExist;
    EhiLogger.traceBackendCmd(stmt);
    java.sql.PreparedStatement updstmt = null;
    try {
        updstmt = conn.prepareStatement(stmt);
        updstmt.execute();
    } catch (java.sql.SQLException ex) {
        EhiLogger.logError("failed to create sequence " + sqlName.getQName(), ex);
    } finally {
        if (updstmt != null) {
            try {
                updstmt.close();
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError(ex);
            }
        }
    }
}
Also used : GeneratorJdbc(ch.ehi.sqlgen.generator_impl.jdbc.GeneratorJdbc) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 24 with DbTableName

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

the class TransferFromIli method updateSingleEnumTable.

public void updateSingleEnumTable(java.sql.Connection conn) throws Ili2dbException {
    DbTableName tabName = new DbTableName(schema.getName(), DbNames.ENUM_TAB);
    String sqlName = tabName.getName();
    if (tabName.getSchema() != null) {
        sqlName = tabName.getSchema() + "." + sqlName;
    }
    try {
        // insert entries
        String insStmt = "INSERT INTO " + sqlName + " (" + 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 + "," + DbNames.ENUM_TAB_THIS_COL + "," + DbNames.ENUM_TAB_BASE_COL + ") VALUES (?,?,?,?,?,?,?,?)";
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        String thisClass = null;
        try {
            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();
                    thisClass = attr.getContainer().getScopedName(null) + "." + attr.getName();
                    AttributeDef base = (AttributeDef) attr.getExtending();
                    String baseClass = null;
                    if (base != null) {
                        baseClass = base.getContainer().getScopedName(null) + "." + base.getName();
                    }
                    HashSet exstEntries = readEnumTable(conn, true, thisClass, tabName);
                    updateEnumEntries(exstEntries, insPrepStmt, type, thisClass, baseClass);
                } else if (entro instanceof Domain) {
                    Domain domain = (Domain) entro;
                    if (domain == td.INTERLIS.BOOLEAN) {
                        continue;
                    }
                    EnumerationType type = (EnumerationType) domain.getType();
                    thisClass = domain.getScopedName(null);
                    Domain base = (Domain) domain.getExtending();
                    String baseClass = null;
                    if (base != null) {
                        baseClass = base.getScopedName(null);
                    }
                    HashSet exstEntries = readEnumTable(conn, true, thisClass, tabName);
                    updateEnumEntries(exstEntries, insPrepStmt, type, thisClass, baseClass);
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert enum values for type " + thisClass, ex);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update enum-table " + sqlName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) Iterator(java.util.Iterator) 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 25 with DbTableName

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

the class TransferFromIli method readSettings.

public static void readSettings(java.sql.Connection conn, Config settings, String schema) throws Ili2dbException {
    String sqlName = DbNames.SETTINGS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    if (DbUtility.tableExists(conn, new DbTableName(schema, DbNames.SETTINGS_TAB))) {
        try {
            // select entries
            String insStmt = "SELECT " + DbNames.SETTINGS_TAB_TAG_COL + "," + DbNames.SETTINGS_TAB_SETTING_COL + " FROM " + sqlName;
            EhiLogger.traceBackendCmd(insStmt);
            java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
            boolean settingsExists = false;
            try {
                java.sql.ResultSet rs = insPrepStmt.executeQuery();
                while (rs.next()) {
                    String tag = rs.getString(1);
                    String value = rs.getString(2);
                    if (tag.equals(Config.SENDER))
                        continue;
                    settings.setValue(tag, value);
                    settingsExists = true;
                }
                if (settingsExists) {
                    settings.setConfigReadFromDb(true);
                }
            } catch (java.sql.SQLException ex) {
                throw new Ili2dbException("failed to read setting", ex);
            } finally {
                insPrepStmt.close();
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to read settings-table " + sqlName, ex);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

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