use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class TransferFromXtf method writeObject.
/**
* if structEle==null, iomObj is an object. If structEle!=null iomObj is a struct value.
*/
private void writeObject(String datasetName, long basketSqlId, IomObject iomObj, StructWrapper structEle, Map<String, ClassStat> objStat) throws java.sql.SQLException, ConverterException {
String tag = iomObj.getobjecttag();
// EhiLogger.debug("tag "+tag);
Object modelele = tag2class.get(tag);
if (modelele == null) {
if (!unknownTypev.contains(tag)) {
EhiLogger.logError("unknown type <" + tag + ">, line " + Integer.toString(iomObj.getobjectline()) + ", col " + Integer.toString(iomObj.getobjectcol()));
}
return;
}
// is it a SURFACE or AREA line table?
if (createItfLineTables && modelele instanceof AttributeDef) {
writeItfLineTableObject(datasetName, basketSqlId, iomObj, (AttributeDef) modelele);
return;
}
// ASSERT: an ordinary class/table
Viewable aclass1 = (Viewable) modelele;
String sqlType = (String) ili2sqlName.mapIliClassDef(aclass1);
long sqlId;
boolean updateObj = false;
// is it an object?
if (structEle == null) {
// map oid of transfer file to a sql id
String tid = iomObj.getobjectoid();
if (tid != null && tid.length() > 0) {
sqlId = oidPool.getObjSqlId(Ili2cUtility.getRootViewable(aclass1).getScopedName(null), tid);
if (functionCode == Config.FC_UPDATE && existingObjectsContains(sqlType, sqlId)) {
updateObj = true;
existingObjectsRemove(sqlType, sqlId);
}
} else {
// it is an assoc without tid
// get a new sql id
sqlId = oidPool.newObjSqlId();
}
} else {
// it is a struct value
// get a new sql id
sqlId = oidPool.newObjSqlId();
}
updateObjStat(objStat, tag, sqlId);
// loop over all classes; start with leaf, end with the base of the inheritance hierarchy
ViewableWrapper aclass = class2wrapper.get(aclass1);
while (aclass != null) {
{
String insert = getInsertStmt(updateObj, aclass1, aclass, structEle);
EhiLogger.traceBackendCmd(insert);
PreparedStatement ps = conn.prepareStatement(insert);
try {
recConv.writeRecord(basketSqlId, iomObj, aclass1, structEle, aclass, sqlType, sqlId, updateObj, ps, structQueue);
ps.executeUpdate();
} finally {
ps.close();
}
}
for (ViewableWrapper secondary : aclass.getSecondaryTables()) {
// secondarytable contains attributes of this class?
if (secondary.containsAttributes(recConv.getIomObjectAttrs(aclass1))) {
String insert = getInsertStmt(updateObj, aclass1, secondary, structEle);
EhiLogger.traceBackendCmd(insert);
PreparedStatement ps = conn.prepareStatement(insert);
try {
recConv.writeRecord(basketSqlId, iomObj, aclass1, structEle, secondary, sqlType, sqlId, updateObj, ps, structQueue);
ps.executeUpdate();
} finally {
ps.close();
}
}
}
aclass = aclass.getExtending();
}
}
use of ch.ehi.ili2db.mapping.ViewableWrapper 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;
}
use of ch.ehi.ili2db.mapping.ViewableWrapper 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);
}
}
}
}
use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class TransferFromXtf method dropExistingStructEles.
private void dropExistingStructEles(String topic, long basketSqlId) {
// get all structs that are reachable from this topic
HashSet<AbstractClassDef> classv = getStructs(topic);
// delete all structeles
HashSet<ViewableWrapper> visitedTables = new HashSet<ViewableWrapper>();
for (AbstractClassDef aclass1 : classv) {
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
}
}
}
wrapper = wrapper.getExtending();
}
}
}
use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class FromIliRecordConverter method generateTable.
public void generateTable(ViewableWrapper def, int pass) throws Ili2dbException {
if (!def.isSecondaryTable()) {
surfaceAttrs = new ArrayList<AttributeDef>();
}
// EhiLogger.debug("viewable "+def);
if (pass == 1) {
DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
DbTable dbTable = new DbTable();
dbTable.setName(sqlName);
schema.addTable(dbTable);
return;
}
// second pass; add columns
DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
DbTable dbTable = schema.findTable(sqlName);
ViewableWrapper base = def.getExtending();
{
StringBuffer cmt = new StringBuffer();
String cmtSep = "";
if (!def.isSecondaryTable()) {
dbTable.setIliName(def.getViewable().getScopedName(null));
if (def.getViewable().getDocumentation() != null) {
cmt.append(cmtSep + def.getViewable().getDocumentation());
cmtSep = nl;
}
cmt.append(cmtSep + "@iliname " + def.getViewable().getScopedName(null));
cmtSep = nl;
}
if (cmt.length() > 0) {
dbTable.setComment(cmt.toString());
}
}
if (deleteExistingData) {
dbTable.setDeleteDataIfTableExists(true);
}
if (base == null && !def.isSecondaryTable()) {
dbTable.setRequiresSequence(true);
}
String baseRef = "";
DbColId dbColId = addKeyCol(dbTable);
if (base != null) {
dbColId.setScriptComment("REFERENCES " + base.getViewable().getScopedName(null));
if (createFk) {
dbColId.setReferencedTable(getSqlType(base.getViewable()));
}
} else if (def.isSecondaryTable()) {
if (createFk) {
dbColId.setReferencedTable(new DbTableName(schema.getName(), def.getMainTable().getSqlTablename()));
}
}
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);
}
DbColumn dbCol;
if (base == null && !def.isSecondaryTable()) {
if (createTypeDiscriminator || def.includesMultipleTypes()) {
dbCol = createSqlTypeCol(DbNames.T_TYPE_COL);
dbTable.addColumn(dbCol);
}
// if CLASS
if (!def.isStructure()) {
if (createIliTidCol || def.getOid() != null) {
addIliTidCol(dbTable, def.getOid());
}
}
// if STRUCTURE, add ref to parent
if (def.isStructure()) {
if (createGenericStructRef) {
// add parentid
DbColId dbParentId = new DbColId();
dbParentId.setName(DbNames.T_PARENT_ID_COL);
dbParentId.setNotNull(true);
dbParentId.setPrimaryKey(false);
dbTable.addColumn(dbParentId);
// add parent_type
dbCol = createSqlTypeCol(DbNames.T_PARENT_TYPE_COL);
dbTable.addColumn(dbCol);
// add parent_attr
dbCol = createSqlTypeCol(DbNames.T_PARENT_ATTR_COL);
dbTable.addColumn(dbCol);
} else {
// add reference to parent for each structAttr when generating structAttr
}
// add sequence attr
DbColId dbSeq = new DbColId();
dbSeq.setName(DbNames.T_SEQ_COL);
// dbSeq.setNotNull(true); // must be optional for cases where struct is exdended by a class
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}
}
// body
Iterator<ViewableTransferElement> iter = def.getAttrIterator();
while (iter.hasNext()) {
ViewableTransferElement obj = iter.next();
if (obj.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) obj.obj;
if (attr.getExtending() == null) {
try {
if (createItfLineTables && attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
surfaceAttrs.add(attr);
}
if (!attr.isTransient()) {
Type proxyType = attr.getDomain();
if (proxyType != null && (proxyType instanceof ObjectType)) {
// skip implicit particles (base-viewables) of views
} else {
generateAttr(dbTable, def.getViewable(), attr);
}
}
} catch (Exception ex) {
throw new Ili2dbException(attr.getContainer().getScopedName(null) + "." + attr.getName(), ex);
}
} else {
if (attr.isDomainBoolean()) {
} else if (createEnumColAsItfCode && attr.getDomainResolvingAll() instanceof EnumerationType) {
throw new Ili2dbException("EXTENDED attributes with type enumeration not supported");
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
if (role.getExtending() == null) {
// not an embedded role and roledef not defined in a lightweight association?
if (!obj.embedded && !def.isAssocLightweight()) {
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for (ViewableWrapper targetTable : targetTables) {
dbColId = new DbColId();
DbTableName targetSqlTableName = targetTable.getSqlTable();
String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlName.getName(), targetSqlTableName.getName(), targetTables.size() > 1);
dbColId.setName(roleSqlName);
boolean notNull = false;
if (!sqlEnableNull) {
if (targetTables.size() > 1) {
// multiple alternative FK columns
notNull = false;
} else {
notNull = true;
}
}
dbColId.setNotNull(notNull);
dbColId.setPrimaryKey(false);
if (createFk) {
dbColId.setReferencedTable(targetSqlTableName);
}
if (createFkIdx) {
dbColId.setIndex(true);
}
String cmt = role.getDocumentation();
if (cmt != null && cmt.length() > 0) {
dbColId.setComment(cmt);
}
dbTable.addColumn(dbColId);
// handle ordered
if (role.isOrdered()) {
// add seqeunce attr
DbColId dbSeq = new DbColId();
dbSeq.setName(roleSqlName + "_" + DbNames.T_SEQ_COL);
dbSeq.setNotNull(notNull);
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}
}
}
// a role of an embedded association?
if (obj.embedded) {
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if (roleOwner.getDerivedFrom() == null) {
// role is oppend;
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for (ViewableWrapper targetTable : targetTables) {
dbColId = new DbColId();
DbTableName targetSqlTableName = targetTable.getSqlTable();
String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlName.getName(), targetSqlTableName.getName(), targetTables.size() > 1);
dbColId.setName(roleSqlName);
boolean notNull = false;
if (!sqlEnableNull) {
if (targetTables.size() > 1) {
// multiple alternative FK columns
notNull = false;
} else if (role.getOppEnd().getDestination() != def.getViewable()) {
// other subtypes in of def don't have this FK
notNull = false;
} else {
if (role.getCardinality().getMinimum() == 0) {
notNull = false;
} else {
notNull = true;
}
}
}
dbColId.setNotNull(notNull);
dbColId.setPrimaryKey(false);
if (createFk) {
dbColId.setReferencedTable(targetSqlTableName);
}
if (createFkIdx) {
dbColId.setIndex(true);
}
String cmt = role.getDocumentation();
if (cmt != null && cmt.length() > 0) {
dbColId.setComment(cmt);
}
customMapping.fixupEmbeddedLink(dbTable, dbColId, roleOwner, role, targetSqlTableName, colT_ID);
dbTable.addColumn(dbColId);
// handle ordered
if (role.getOppEnd().isOrdered()) {
// add seqeunce attr
DbColId dbSeq = new DbColId();
dbSeq.setName(roleSqlName + "_" + DbNames.T_SEQ_COL);
dbSeq.setNotNull(notNull);
dbSeq.setPrimaryKey(false);
dbTable.addColumn(dbSeq);
}
}
}
}
}
}
}
if (createStdCols) {
addStdCol(dbTable);
}
if (createUnique && !def.isStructure()) {
// check if UNIQUE mappable
HashSet wrapperCols = getWrapperCols(def.getAttrv());
Viewable aclass = def.getViewable();
Iterator it = aclass.iterator();
while (it.hasNext()) {
Object cnstro = it.next();
if (cnstro instanceof UniquenessConstraint) {
UniquenessConstraint cnstr = (UniquenessConstraint) cnstro;
HashSet attrs = getUniqueAttrs(cnstr, wrapperCols);
// mappable?
if (attrs != null) {
DbIndex dbIndex = new DbIndex();
dbIndex.setPrimary(false);
dbIndex.setUnique(true);
for (Object attro : attrs) {
String attrSqlName = null;
if (attro instanceof AttributeDef) {
attrSqlName = ili2sqlName.mapIliAttributeDef((AttributeDef) attro, def.getSqlTablename(), null);
} else if (attro instanceof RoleDef) {
RoleDef role = (RoleDef) attro;
DbTableName targetSqlTableName = getSqlType(role.getDestination());
attrSqlName = ili2sqlName.mapIliRoleDef(role, def.getSqlTablename(), targetSqlTableName.getName());
} else {
throw new IllegalStateException("unexpected attr " + attro);
}
DbColumn idxCol = dbTable.getColumn(attrSqlName);
dbIndex.addAttr(idxCol);
}
dbTable.addIndex(dbIndex);
}
}
}
}
if (!def.isSecondaryTable()) {
customMapping.fixupViewable(dbTable, def.getViewable());
}
}
Aggregations