提交 a16b8c16 编写于 作者: S Serge Rider

#5384 Virtual model additions. Find object by ID.

上级 447d70d2
......@@ -540,12 +540,61 @@ public final class DBUtils {
if (obj instanceof DBPDataSourceContainer) {
pathStr.append(((DBPDataSourceContainer) obj).getId());
} else {
pathStr.append(getQuotedIdentifier(obj));
pathStr.append(obj.getName());
}
}
return pathStr.toString();
}
/**
* Find object by unique ID.
* Note: this function searches only inside DBSObjectContainer objects.
* Usually it works only for entities and entity containers (schemas, catalogs).
*/
public static DBSObject findObjectById(@NotNull DBRProgressMonitor monitor, @NotNull DBPProject project, @NotNull String objectId) throws DBException {
String[] names = objectId.split("/");
DBPDataSourceContainer dataSourceContainer = project.getDataSourceRegistry().getDataSource(names[0]);
if (dataSourceContainer == null) {
log.debug("Can't find datasource '" + names[0] + "' for object ID " + objectId);
return null;
}
if (!dataSourceContainer.isConnected()) {
dataSourceContainer.connect(monitor, true, true);
}
DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
log.debug("Null datasource in container " + dataSourceContainer.getId());
return null;
}
DBSObjectContainer sc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
if (sc != null) {
for (int i = 1; i < names.length - 1; i++) {
String name = names[i];
DBSObject child = sc.getChild(monitor, name);
if (child == null) {
log.debug("Can't find child container " + name + " in container " + DBUtils.getObjectFullName(sc, DBPEvaluationContext.UI));
return null;
}
if (child instanceof DBSObjectContainer) {
sc = (DBSObjectContainer) child;
} else {
log.debug("Child object '" + name + "' is not a container");
return null;
}
}
}
if (sc != null) {
String objectName = names[names.length - 1];
DBSObject object = sc.getChild(monitor, objectName);
if (object == null) {
log.debug("Child object '" + objectName + "' not found in container " + DBUtils.getObjectFullName(sc, DBPEvaluationContext.UI));
return null;
}
return object;
}
return null;
}
public static boolean isNullValue(@Nullable Object value)
{
return (value == null || (value instanceof DBDValue && ((DBDValue) value).isNull()));
......
......@@ -129,6 +129,9 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
for (Map<String, Object> fkObject : JSONUtils.getObjectList(map, "foreign-keys")) {
String entityId = JSONUtils.getString(fkObject, "entity");
String refConsId = JSONUtils.getString(fkObject, "constraint");
Map<String, Object> attributes = JSONUtils.getObject(fkObject, "attributes");
//DBUtils.findObjectById()
if (entityForeignKeys == null) entityForeignKeys = new ArrayList<>();
log.warn("Virtual foreign keys load is not implemented yet");
......
......@@ -135,11 +135,12 @@ class DBVModelSerializerModern implements DBVModelSerializer
List<DBVEntityForeignKeyColumn> refAttrs = fk.getAttributeReferences(null);
if (!CommonUtils.isEmpty(refAttrs)) {
json.name("attributes");
json.beginArray();
json.beginObject();
for (DBVEntityForeignKeyColumn cc : refAttrs) {
json.value(cc.getAttributeName());
json.name(cc.getAttributeName());
json.value(cc.getRefAttributeName());
}
json.endArray();
json.endObject();
}
json.endObject();
}
......
......@@ -29,6 +29,7 @@ import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.DBValueFormatting;
import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
......@@ -238,7 +239,7 @@ class EditVirtualEntityDialog extends BaseDialog {
TableItem item = new TableItem(fkTable, SWT.NONE);
item.setImage(0, DBeaverIcons.getImage(DBIcon.TREE_FOREIGN_KEY));
if (fk.getReferencedConstraint() != null) {
item.setText(0, fk.getReferencedConstraint().getParentObject().getName());
item.setText(0, DBUtils.getObjectFullName(fk.getReferencedConstraint().getParentObject(), DBPEvaluationContext.UI));
}
String ownAttrNames = fk.getAttributes().stream().map(DBVEntityForeignKeyColumn::getAttributeName)
.collect(Collectors.joining(","));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册