Search in sources :

Example 11 with DbTableName

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

the class TransferFromIli method addInheritanceTable.

public static void addInheritanceTable(DbSchema schema, int sqlNameSize) {
    DbTable tab = new DbTable();
    tab.setName(new DbTableName(schema.getName(), DbNames.INHERIT_TAB));
    DbColVarchar thisClass = new DbColVarchar();
    thisClass.setName(DbNames.INHERIT_TAB_THIS_COL);
    thisClass.setNotNull(true);
    thisClass.setPrimaryKey(true);
    thisClass.setSize(1024);
    tab.addColumn(thisClass);
    DbColVarchar baseClass = new DbColVarchar();
    baseClass.setName(DbNames.INHERIT_TAB_BASE_COL);
    baseClass.setNotNull(false);
    baseClass.setSize(1024);
    tab.addColumn(baseClass);
    schema.addTable(tab);
}
Also used : DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 12 with DbTableName

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

the class TransferFromIli method generateItfLineTable.

private void generateItfLineTable(AttributeDef attr, int pass) throws Ili2dbException {
    if (pass == 1) {
        DbTableName sqlName = getSqlTableNameItfLineTable(attr);
        DbTable dbTable = new DbTable();
        dbTable.setName(sqlName);
        dbTable.setIliName(attr.getContainer().getScopedName(null) + "." + attr.getName());
        schema.addTable(dbTable);
        return;
    }
    // second pass; add columns
    DbTableName sqlName = getSqlTableNameItfLineTable(attr);
    DbTable dbTable = schema.findTable(sqlName);
    StringBuffer cmt = new StringBuffer();
    String cmtSep = "";
    if (attr.getDocumentation() != null) {
        cmt.append(cmtSep + attr.getDocumentation());
        cmtSep = nl;
    }
    cmt.append(cmtSep + "@iliname " + attr.getContainer().getScopedName(null) + "." + attr.getName());
    cmtSep = nl;
    if (cmt.length() > 0) {
        dbTable.setComment(cmt.toString());
    }
    if (deleteExistingData) {
        dbTable.setDeleteDataIfTableExists(true);
    }
    dbTable.setRequiresSequence(true);
    DbColId dbColId = recConv.addKeyCol(dbTable);
    if (createIliTidCol) {
        recConv.addIliTidCol(dbTable, null);
    }
    if (createBasketCol) {
        // add basketCol
        DbColId t_basket = new DbColId();
        t_basket.setName(DbNames.T_BASKET_COL);
        t_basket.setNotNull(true);
        t_basket.setScriptComment("REFERENCES " + DbNames.BASKETS_TAB);
        if (createFk) {
            t_basket.setReferencedTable(new DbTableName(schema.getName(), DbNames.BASKETS_TAB));
        }
        if (createFkIdx) {
            t_basket.setIndex(true);
        }
        dbTable.addColumn(t_basket);
    }
    if (createDatasetCol) {
        DbColVarchar t_dsName = new DbColVarchar();
        t_dsName.setName(DbNames.T_DATASET_COL);
        t_dsName.setSize(DbNames.DATASETNAME_COL_SIZE);
        t_dsName.setNotNull(true);
        t_dsName.setIndex(true);
        dbTable.addColumn(t_dsName);
    }
    SurfaceOrAreaType type = (SurfaceOrAreaType) attr.getDomainResolvingAll();
    DbColGeometry dbCol = recConv.generatePolylineType(type, attr.getContainer().getScopedName(null) + "." + attr.getName());
    recConv.setCrs(dbCol, attr);
    dbCol.setName(ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr, sqlName.getName()));
    dbCol.setNotNull(true);
    dbTable.addColumn(dbCol);
    if (type instanceof SurfaceType) {
        dbColId = new DbColId();
        dbColId.setName(ili2sqlName.getSqlColNameItfLineTableRefAttr(attr, sqlName.getName()));
        dbColId.setNotNull(true);
        dbColId.setPrimaryKey(false);
        dbColId.setScriptComment("REFERENCES " + recConv.getSqlType((Viewable) attr.getContainer()));
        if (createFk) {
            dbColId.setReferencedTable(recConv.getSqlType((Viewable) attr.getContainer()));
        }
        if (createFkIdx) {
            dbColId.setIndex(true);
        }
        dbTable.addColumn(dbColId);
    }
    Table lineAttrTable = type.getLineAttributeStructure();
    if (lineAttrTable != null) {
        Iterator attri = lineAttrTable.getAttributes();
        while (attri.hasNext()) {
            AttributeDef lineattr = (AttributeDef) attri.next();
            recConv.generateAttr(dbTable, lineAttrTable, lineattr);
        }
    }
    if (createStdCols) {
        AbstractRecordConverter.addStdCol(dbTable);
    }
}
Also used : DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) DbColGeometry(ch.ehi.sqlgen.repository.DbColGeometry) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) DbColId(ch.ehi.sqlgen.repository.DbColId) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 13 with DbTableName

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

the class TransferFromXtf method readExistingSqlObjIds.

private Long readExistingSqlObjIds(boolean isItf, String bid) throws Ili2dbException {
    StringBuilder topicQName = new StringBuilder();
    Long basketSqlId = Ili2db.getBasketSqlIdFromBID(bid, conn, schema, colT_ID, topicQName);
    if (basketSqlId == null) {
        // new basket
        return null;
    }
    Topic topic = TransferToXtf.getTopicDef(td, topicQName.toString());
    if (topic == null) {
        throw new Ili2dbException("unkown topic " + topicQName.toString());
    }
    Model model = (Model) topic.getContainer();
    // for all Viewables
    Iterator iter = null;
    if (isItf) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = getXtfTables(td, topic).iterator();
    }
    HashSet<String> visitedTables = new HashSet<String>();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!TransferToXtf.suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (aclass.isAbstract()) {
                    throw new IllegalArgumentException("unexpected abstract viewable " + aclass.getScopedName(null));
                }
                // get sql name
                DbTableName sqlName = recConv.getSqlType(aclass);
                ViewableWrapper wrapper = recConv.getViewableWrapper(sqlName.getName());
                ViewableWrapper base = wrapper.getExtending();
                while (base != null) {
                    wrapper = base;
                    base = wrapper.getExtending();
                }
                sqlName = wrapper.getSqlTable();
                if (!visitedTables.contains(sqlName.getQName())) {
                    visitedTables.add(sqlName.getQName());
                    // if table exists?
                    if (DbUtility.tableExists(conn, sqlName)) {
                        // dump it
                        EhiLogger.logState(aclass.getScopedName(null) + " read ids...");
                        readObjectSqlIds(!wrapper.includesMultipleTypes(), sqlName, basketSqlId);
                    } else {
                        // skip it
                        EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                    }
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (isItf) {
                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() + " read ids...");
                    readObjectSqlIds(isItf, sqlName, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    return basketSqlId;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

Example 14 with DbTableName

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

the class TransferFromXtf method getInsertStmt.

/**
 * gets an insert statement for a given viewable. Creates only a new
 *  statement if this is not yet seen sqlname.
 * @param sqlname table name of viewable
 * @param sqltable viewable
 * @return insert statement
 */
private String getInsertStmt(boolean isUpdate, Viewable iomClass, ViewableWrapper sqltable, StructWrapper structEle) {
    Object key = null;
    if (!createGenericStructRef && structEle != null && sqltable.getExtending() == null) {
        ViewableWrapper parentTable = recConv.getViewableWrapper(structEle.getParentSqlType());
        key = sqltable.getSqlTablename() + ":" + iomClass.getScopedName(null) + ":" + parentTable.getSqlTablename() + ":" + structEle.getParentAttr();
    } else {
        key = sqltable.getSqlTablename() + ":" + iomClass.getScopedName(null);
    }
    if (isUpdate) {
        if (updateStmts.containsKey(key)) {
            return updateStmts.get(key);
        }
    } else {
        if (insertStmts.containsKey(key)) {
            return insertStmts.get(key);
        }
    }
    String stmt = recConv.createInsertStmt(isUpdate, iomClass, new DbTableName(schema, sqltable.getSqlTablename()), sqltable, structEle);
    EhiLogger.traceBackendCmd(stmt);
    if (isUpdate) {
        updateStmts.put(key, stmt);
    } else {
        insertStmts.put(key, stmt);
    }
    return stmt;
}
Also used : IomObject(ch.interlis.iom.IomObject) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 15 with DbTableName

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

the class TransferFromXtf method deleteObjectsOfBasket.

private void deleteObjectsOfBasket(long basketSqlId, String topicQName) throws Ili2dbException {
    boolean isItf = false;
    Topic topic = TransferToXtf.getTopicDef(td, topicQName);
    if (topic == null) {
        throw new Ili2dbException("unkown topic " + topicQName.toString());
    }
    Model model = (Model) topic.getContainer();
    // for all Viewables
    // see also export
    Iterator iter = null;
    if (isItf) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        // get transferable viewables of topic
        iter = topic.getViewables().iterator();
    }
    HashSet<ViewableWrapper> visitedTables = new HashSet<ViewableWrapper>();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!TransferToXtf.suppressViewable((Viewable) obj)) {
                Viewable aclass1 = (Viewable) obj;
                ViewableWrapper wrapper = class2wrapper.get(aclass1);
                while (wrapper != null) {
                    {
                        if (!visitedTables.contains(wrapper)) {
                            visitedTables.add(wrapper);
                            // if table exists?
                            // get sql name
                            DbTableName sqlName = wrapper.getSqlTable();
                            if (DbUtility.tableExists(conn, sqlName)) {
                                // delete it
                                dropRecords(sqlName, basketSqlId);
                            } else {
                            // skip it; no table
                            }
                        }
                    }
                    for (ViewableWrapper secondary : wrapper.getSecondaryTables()) {
                        if (!visitedTables.contains(secondary)) {
                            visitedTables.add(secondary);
                            // if table exists?
                            // get sql name
                            DbTableName sqlName = secondary.getSqlTable();
                            if (DbUtility.tableExists(conn, sqlName)) {
                                // delete it
                                dropRecords(sqlName, basketSqlId);
                            } else {
                            // skip it; no table
                            }
                        }
                    }
                    wrapper = wrapper.getExtending();
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (isItf) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    dropRecords(sqlName, basketSqlId);
                } else {
                // skip it; no table
                }
            }
        }
    }
    dropExistingStructEles(topicQName, basketSqlId);
    String sqlName = DbNames.IMPORTS_BASKETS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    java.sql.PreparedStatement getstmt = null;
    try {
        String stmt = "DELETE FROM " + sqlName + " WHERE " + DbNames.IMPORTS_BASKETS_TAB_BASKET_COL + "= ?";
        EhiLogger.traceBackendCmd(stmt);
        getstmt = conn.prepareStatement(stmt);
        getstmt.setLong(1, basketSqlId);
        getstmt.executeUpdate();
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to delete from " + sqlName, ex);
    } finally {
        if (getstmt != null) {
            try {
                getstmt.close();
                getstmt = null;
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError(ex);
            }
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) PreparedStatement(java.sql.PreparedStatement) View(ch.interlis.ili2c.metamodel.View) SQLException(java.sql.SQLException) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

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