use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class FromIliRecordConverter method addParentRef.
private void addParentRef(Viewable parentTable, AttributeDef attr) {
CompositionType type = (CompositionType) attr.getDomainResolvingAll();
Table structClass = type.getComponentType();
// if abstract struct, might have multiple tables!
for (ViewableWrapper structWrapper : getStructWrappers(structClass)) {
DbTableName structClassSqlName = structWrapper.getSqlTable();
// find struct table
DbTable dbTable = schema.findTable(structClassSqlName);
// add ref attr
String refAttrSqlName = ili2sqlName.mapIliAttributeDefReverse(attr, structClassSqlName.getName(), class2wrapper.get(parentTable).getSqlTablename());
DbColId dbParentId = new DbColId();
dbParentId.setName(refAttrSqlName);
// values of other struct attrs will have NULL
dbParentId.setNotNull(false);
dbParentId.setPrimaryKey(false);
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) {
dbParentId.setComment(cmt.toString());
}
if (createFk) {
dbParentId.setReferencedTable(class2wrapper.get(parentTable).getSqlTable());
}
if (createFkIdx) {
dbParentId.setIndex(true);
}
dbTable.addColumn(dbParentId);
}
}
use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class TransferFromIli method generatModelEles.
private void generatModelEles(java.util.List<Element> modelEles, int pass) throws Ili2dbException {
Iterator modeli = modelEles.iterator();
while (modeli.hasNext()) {
Object modelo = modeli.next();
if (modelo instanceof Model) {
Model model = (Model) modelo;
// generateModel(model);
} else if (modelo instanceof Topic) {
// generateTopic((Topic)modelo);
} else if (modelo instanceof Domain) {
if (pass == 2) {
generateDomain((Domain) modelo);
visitedElements.add((Domain) modelo);
}
} else if (modelo instanceof Viewable) {
if (modelo instanceof Table && ((Table) modelo).isIli1LineAttrStruct()) {
// skip it
} else if ((modelo instanceof View) && !isTransferableView(modelo)) {
// skip it
} else {
try {
ViewableWrapper wrapper = class2wrapper.get((Viewable) modelo);
if (wrapper != null) {
generateViewable(wrapper, pass);
}
if (pass == 2) {
visitedElements.add((Viewable) modelo);
}
} catch (Ili2dbException ex) {
throw new Ili2dbException("mapping of " + ((Viewable) modelo).getScopedName(null) + " failed", ex);
}
}
} else if (modelo instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) modelo;
if (attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
generateItfLineTable(attr, pass);
} else if (attr.getDomainResolvingAll() instanceof EnumerationType) {
if (pass == 2) {
visitedEnums.add(attr);
}
} else {
// skip it
}
} else {
// skip it
}
}
}
use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class FromXtfRecordConverter method getViewableWrapperOfAbstractClass.
private ViewableWrapper getViewableWrapperOfAbstractClass(Viewable abstractClass, Viewable concreteClass) {
if (abstractClass == concreteClass) {
return class2wrapper.get(concreteClass);
}
ArrayList<ViewableWrapper> rets = new ArrayList<ViewableWrapper>();
rets.add(class2wrapper.get(concreteClass));
Viewable superClass = (Viewable) concreteClass.getExtending();
while (superClass != null) {
ViewableWrapper ret2 = class2wrapper.get(superClass);
if (ret2 != null) {
if (!rets.contains(ret2)) {
rets.add(0, ret2);
}
}
if (superClass == abstractClass) {
break;
}
superClass = (Viewable) superClass.getExtending();
}
for (int i = 0; i < rets.size(); i++) {
ViewableWrapper ret = rets.get(i);
if (i == rets.size() - 1) {
return ret;
}
if (!TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(trafoConfig.getViewableConfig(ret.getViewable(), TrafoConfigNames.INHERITANCE_TRAFO))) {
return ret;
}
}
return null;
}
use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.
the class FromXtfRecordConverter method createInsertStmt.
/**
* creates an insert statement for a given viewable.
* @param sqlTableName table name of viewable
* @param aclass viewable
* @return insert statement
*/
public String createInsertStmt(boolean isUpdate, Viewable iomClass, DbTableName sqlTableName, ViewableWrapper aclass, StructWrapper structEle) {
StringBuffer ret = new StringBuffer();
StringBuffer values = new StringBuffer();
// WHERE some_column=some_value;
if (isUpdate) {
ret.append("UPDATE ");
} else {
ret.append("INSERT INTO ");
}
ret.append(sqlTableName.getQName());
String sep = null;
if (isUpdate) {
sep = " SET ";
} else {
sep = " (";
}
// add T_Id
if (!isUpdate) {
ret.append(sep);
ret.append(colT_ID);
values.append("?");
sep = ",";
}
// add T_basket
if (createBasketCol) {
ret.append(sep);
ret.append(DbNames.T_BASKET_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
if (createDatasetCol) {
ret.append(sep);
ret.append(DbNames.T_DATASET_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
if (!aclass.isSecondaryTable()) {
// if root, add type
if (aclass.getExtending() == null) {
if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
ret.append(sep);
ret.append(DbNames.T_TYPE_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
// if Class
if (!aclass.isStructure()) {
if (!isUpdate) {
if (createIliTidCol || aclass.getOid() != null) {
ret.append(sep);
ret.append(DbNames.T_ILI_TID_COL);
values.append(",?");
sep = ",";
}
}
}
// if STRUCTURE, add ref to parent
if (aclass.isStructure()) {
if (structEle == null) {
// struct is extended by a class and current object is an instance of the class
} else {
// current object is an instance of the structure
if (createGenericStructRef) {
ret.append(sep);
ret.append(DbNames.T_PARENT_ID_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
ret.append(sep);
ret.append(DbNames.T_PARENT_TYPE_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
// attribute name in parent class
ret.append(sep);
ret.append(DbNames.T_PARENT_ATTR_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
} else {
ret.append(sep);
Viewable parentViewable = getViewable(structEle.getParentSqlType());
ViewableWrapper parentTable = getViewableWrapperOfAbstractClass((Viewable) structEle.getParentAttr().getContainer(), parentViewable);
ret.append(ili2sqlName.mapIliAttributeDefReverse(structEle.getParentAttr(), sqlTableName.getName(), parentTable.getSqlTablename()));
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
// seqeunce (not null if LIST)
ret.append(sep);
ret.append(DbNames.T_SEQ_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
}
}
}
HashSet attrs = getIomObjectAttrs(iomClass);
Iterator iter = aclass.getAttrIterator();
while (iter.hasNext()) {
ViewableTransferElement obj = (ViewableTransferElement) iter.next();
if (obj.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) obj.obj;
if (attrs.contains(attr)) {
if (!attr.isTransient()) {
Type proxyType = attr.getDomain();
if (proxyType != null && (proxyType instanceof ObjectType)) {
// skip implicit particles (base-viewables) of views
} else {
sep = addAttrToInsertStmt(isUpdate, ret, values, sep, attr, sqlTableName.getName());
}
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
if (role.getExtending() == null) {
if (attrs.contains(role)) {
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for (ViewableWrapper targetTable : targetTables) {
String roleName = ili2sqlName.mapIliRoleDef(role, sqlTableName.getName(), targetTable.getSqlTablename(), targetTables.size() > 1);
// a role of an embedded association?
if (obj.embedded) {
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if (roleOwner.getDerivedFrom() == null) {
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
} else {
// TODO if(orderPos!=0){
ret.append(sep);
ret.append(roleName);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
}
}
}
}
}
// stdcols
if (createStdCols) {
ret.append(sep);
ret.append(DbNames.T_LAST_CHANGE_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
if (!isUpdate) {
ret.append(sep);
ret.append(DbNames.T_CREATE_DATE_COL);
values.append(",?");
sep = ",";
}
ret.append(sep);
ret.append(DbNames.T_USER_COL);
if (isUpdate) {
ret.append("=?");
} else {
values.append(",?");
}
sep = ",";
}
if (isUpdate) {
// WHERE some_column=some_value;
// add T_Id
ret.append(" WHERE ");
ret.append(colT_ID);
ret.append("=?");
} else {
ret.append(") VALUES (");
ret.append(values);
ret.append(")");
}
return ret.toString();
}
use of ch.ehi.ili2db.mapping.ViewableWrapper 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;
}
Aggregations