private void makeProposalsFromChildren(
     DBRProgressMonitor monitor,
     DBSObject parent,
     @Nullable String startPart,
     List<SQLCompletionProposal> proposals) {
   if (startPart != null) {
     startPart = wordDetector.removeQuotes(startPart).toUpperCase();
     int divPos = startPart.lastIndexOf(editor.getSyntaxManager().getStructSeparator());
     if (divPos != -1) {
       startPart = startPart.substring(divPos + 1);
     }
   }
   try {
     Collection<? extends DBSObject> children = null;
     if (parent instanceof DBSObjectContainer) {
       children = ((DBSObjectContainer) parent).getChildren(monitor);
     } else if (parent instanceof DBSEntity) {
       children = ((DBSEntity) parent).getAttributes(monitor);
     }
     if (children != null && !children.isEmpty()) {
       for (DBSObject child : children) {
         if (startPart != null && !child.getName().toUpperCase().startsWith(startPart)) {
           continue;
         }
         proposals.add(makeProposalsFromObject(monitor, child));
       }
     }
   } catch (DBException e) {
     log.error(e);
   }
 }
Example #2
0
 @Override
 public String getNodeName() {
   DBSObject object = getObject();
   if (object == null) {
     return DBConstants.NULL_VALUE_LABEL;
   }
   String objectName;
   if (object instanceof DBPOverloadedObject) {
     objectName = ((DBPOverloadedObject) object).getOverloadedName();
   } else {
     objectName = object.getName();
   }
   if (CommonUtils.isEmpty(objectName)) {
     objectName = object.toString();
     if (CommonUtils.isEmpty(objectName)) {
       objectName = object.getClass().getName() + "@" + object.hashCode(); // $NON-NLS-1$
     }
   }
   /*
           if (object instanceof DBSObjectUnique) {
               String uniqueName = ((DBSObjectUnique) object).getUniqueName();
               if (!uniqueName.equals(objectName)) {
                   if (uniqueName.startsWith(objectName)) {
                       uniqueName = uniqueName.substring(objectName.length());
                   }
                   objectName += " (" + uniqueName + ")";
               }
           }
   */
   return objectName;
 }
Example #3
0
 private static boolean equalObjects(DBSObject object1, DBSObject object2) {
   if (object1 == object2) {
     return true;
   }
   if (object1 == null || object2 == null) {
     return false;
   }
   while (object1 != null && object2 != null) {
     if (object1.getClass() != object2.getClass()
         || !CommonUtils.equalObjects(
             DBUtils.getObjectUniqueName(object1), DBUtils.getObjectUniqueName(object2))) {
       return false;
     }
     object1 = object1.getParentObject();
     object2 = object2.getParentObject();
   }
   return true;
 }
  private void makeStructureProposals(
      DBRProgressMonitor monitor, DBPDataSource dataSource, List<SQLCompletionProposal> proposals) {
    final DBSObjectContainer rootContainer =
        DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
    if (rootContainer == null) {
      return;
    }
    DBSObjectContainer selectedContainer = null;
    {
      DBSObject selectedObject = getSelectedObject(dataSource);
      if (selectedObject != null) {
        selectedContainer = DBUtils.getAdapter(DBSObjectContainer.class, selectedObject);
      }
    }

    DBSObjectContainer sc = rootContainer;
    DBSObject childObject = sc;
    List<String> tokens = wordDetector.splitWordPart();

    String lastToken = null;
    for (int i = 0; i < tokens.size(); i++) {
      String token = tokens.get(i);
      if (i == tokens.size() - 1 && !wordDetector.getWordPart().endsWith(".")) {
        lastToken = token;
        break;
      }
      if (sc == null) {
        break;
      }
      // Get next structure container
      try {
        String objectName = DBObjectNameCaseTransformer.transformName(dataSource, token);
        childObject = sc.getChild(monitor, objectName);
        if (childObject == null && i == 0 && selectedContainer != null) {
          // Probably it is from selected object, let's try it
          childObject = selectedContainer.getChild(monitor, objectName);
          if (childObject != null) {
            sc = selectedContainer;
          }
        }
        if (childObject == null) {
          if (i == 0) {
            // Assume it's a table alias ?
            childObject = this.getTableFromAlias(monitor, sc, token);
            if (childObject == null) {
              DBSStructureAssistant structureAssistant =
                  DBUtils.getAdapter(DBSStructureAssistant.class, sc);
              if (structureAssistant != null) {
                Collection<DBSObjectReference> references =
                    structureAssistant.findObjectsByMask(
                        monitor,
                        null,
                        structureAssistant.getAutoCompleteObjectTypes(),
                        wordDetector.removeQuotes(token),
                        wordDetector.isQuoted(token),
                        2);
                if (!references.isEmpty()) {
                  childObject = references.iterator().next().resolveObject(monitor);
                }
              }
            }
          } else {
            // Path element not found. Damn - can't do anything.
            return;
          }
        }

        if (childObject instanceof DBSObjectContainer) {
          sc = (DBSObjectContainer) childObject;
        } else {
          sc = null;
        }
      } catch (DBException e) {
        log.error(e);
        return;
      }
    }
    if (childObject == null) {
      return;
    }
    if (lastToken == null) {
      // Get all children objects as proposals
      makeProposalsFromChildren(monitor, childObject, null, proposals);
    } else {
      // Get matched children
      makeProposalsFromChildren(monitor, childObject, lastToken, proposals);
      if (proposals.isEmpty() || tokens.size() == 1) {
        // At last - try to find child tables by pattern
        DBSStructureAssistant structureAssistant = null;
        for (DBSObject object = childObject; object != null; object = object.getParentObject()) {
          structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, object);
          if (structureAssistant != null) {
            break;
          }
        }
        if (structureAssistant != null) {
          makeProposalsFromAssistant(monitor, structureAssistant, sc, lastToken, proposals);
        }
      }
    }
  }
Example #5
0
 public DBPDataSource getDataSource() {
   DBSObject object = getObject();
   return object == null ? null : object.getDataSource();
 }
  @Override
  public Object getAdapter(Object adaptableObject, Class adapterType) {
    if (adapterType == DBSDataSourceContainer.class) {
      if (adaptableObject instanceof DBNDataSource) {
        return ((DBNDataSource) adaptableObject).getDataSourceContainer();
      }
      DBSObject object = null;
      if (adaptableObject instanceof DBSWrapper) {
        object = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBSObject) {
        object = (DBSObject) adaptableObject;
      }
      if (object == null) {
        return null;
      }
      if (object instanceof DBSDataSourceContainer) {
        return object;
      }
      DBPDataSource dataSource = object.getDataSource();
      return dataSource == null ? null : dataSource.getContainer();
    } else if (DBPObject.class.isAssignableFrom(adapterType)) {
      DBPObject object = null;
      if (adaptableObject instanceof DBSWrapper) {
        object = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBPObject) {
        object = (DBPObject) adaptableObject;
      }
      if (object != null && adapterType.isAssignableFrom(object.getClass())) {
        return object;
      }
    } else if (IResource.class.isAssignableFrom(adapterType)) {
      if (adaptableObject instanceof DBNResource) {
        IResource resource = ((DBNResource) adaptableObject).getResource();
        if (resource != null && adapterType.isAssignableFrom(resource.getClass())) {
          return resource;
        } else {
          return null;
        }
      }
    } else if (adapterType == IPropertySource.class) {
      DBPObject dbObject = null;
      if (adaptableObject instanceof DBSWrapper) {
        dbObject = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBPObject) {
        dbObject = (DBPObject) adaptableObject;
      }
      if (dbObject instanceof IPropertySource) {
        return dbObject;
      }
      if (dbObject instanceof DBPPropertySource) {
        return new PropertySourceDelegate((DBPPropertySource) dbObject);
      }
      if (dbObject instanceof IAdaptable) {
        Object adapter = ((IAdaptable) dbObject).getAdapter(IPropertySource.class);
        if (adapter != null) {
          return adapter;
        }
        adapter = ((IAdaptable) dbObject).getAdapter(DBPPropertySource.class);
        if (adapter != null) {
          return new PropertySourceDelegate((DBPPropertySource) adapter);
        }
      }
      if (dbObject != null) {
        PropertyCollector props = new PropertyCollector(adaptableObject, dbObject, true);
        props.collectProperties();
        if (props.isEmpty() && adaptableObject instanceof DBSObject) {
          // Add default properties
          DBSObject meta = (DBSObject) adaptableObject;
          props.addProperty(
              null, "name", CoreMessages.model_navigator_Name, meta.getName()); // $NON-NLS-1$
          props.addProperty(
              null,
              "desc",
              CoreMessages.model_navigator_Description,
              meta.getDescription()); // $NON-NLS-1$
        }
        return new PropertySourceDelegate(props);
      }
    } else if (adapterType == IWorkbenchAdapter.class) {
      // Workbench adapter
      if (adaptableObject instanceof DBNNode) {
        final DBNNode node = (DBNNode) adaptableObject;
        return new WorkbenchAdapter() {
          @Override
          public ImageDescriptor getImageDescriptor(Object object) {
            return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
          }

          @Override
          public String getLabel(Object o) {
            return node.getNodeName();
          }

          @Override
          public Object getParent(Object o) {
            return node.getParentNode();
          }
        };
      } else {
        return null;
      }
    }
    return null;
  }