Search in sources :

Example 51 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class NodeEditorInputFactory method createElement.

@Override
public IAdaptable createElement(IMemento memento) {
    // Get the node path.
    final String nodePath = memento.getString(TAG_NODE);
    if (nodePath == null) {
        return null;
    }
    final DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
    try {
        final DBNNode node = navigatorModel.getNodeByPath(VoidProgressMonitor.INSTANCE, nodePath);
        if (node != null) {
            return new NodeEditorInput(node);
        }
    } catch (DBException e) {
        log.error("Error opening node '" + nodePath + "'", e);
        return null;
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) NodeEditorInput(org.jkiss.dbeaver.ui.editors.entity.NodeEditorInput) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 52 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class AbstractSearchPage method loadTreeState.

protected static List<DBNNode> loadTreeState(DBRProgressMonitor monitor, DBPPreferenceStore store, String propName) {
    final List<DBNNode> result = new ArrayList<>();
    final String sources = store.getString(propName);
    if (!CommonUtils.isEmpty(sources)) {
        // Keep broken datasources to make connect attempt only once
        Set<DBNDataSource> brokenDataSources = new HashSet<>();
        // Find all nodes
        //$NON-NLS-1$
        StringTokenizer st = new StringTokenizer(sources, "|");
        while (st.hasMoreTokens()) {
            String nodePath = st.nextToken();
            try {
                DBNDataSource dsNode = DBeaverCore.getInstance().getNavigatorModel().getDataSourceByPath(nodePath);
                if (dsNode == null || brokenDataSources.contains(dsNode)) {
                    continue;
                }
                DBNNode node = DBeaverCore.getInstance().getNavigatorModel().getNodeByPath(monitor, dsNode.getOwnerProject(), nodePath);
                if (node != null) {
                    result.add(node);
                } else {
                    brokenDataSources.add(dsNode);
                }
            } catch (DBException e) {
                log.error(e);
            }
        }
    }
    return result;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource)

Example 53 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class SearchDataQuery method findRows.

private DBCStatistics findRows(@NotNull DBCSession session, @NotNull DBSDataContainer dataContainer, @NotNull TestDataReceiver dataReceiver) throws DBCException {
    DBSEntity entity;
    if (dataContainer instanceof DBSEntity) {
        entity = (DBSEntity) dataContainer;
    } else {
        log.warn("Data container " + dataContainer + " isn't entity");
        return null;
    }
    try {
        List<DBDAttributeConstraint> constraints = new ArrayList<>();
        for (DBSEntityAttribute attribute : CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()))) {
            if (params.fastSearch) {
                if (!DBUtils.isIndexedAttribute(session.getProgressMonitor(), attribute)) {
                    continue;
                }
            }
            if (DBUtils.isPseudoAttribute(attribute) || DBUtils.isHiddenObject(attribute)) {
                continue;
            }
            DBCLogicalOperator[] supportedOperators = DBUtils.getAttributeOperators(attribute);
            DBCLogicalOperator operator;
            Object value;
            switch(attribute.getDataKind()) {
                case BOOLEAN:
                    continue;
                case NUMERIC:
                    if (!params.searchNumbers) {
                        continue;
                    }
                    if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        continue;
                    }
                    operator = DBCLogicalOperator.EQUALS;
                    try {
                        value = new Integer(params.searchString);
                    } catch (NumberFormatException e) {
                        try {
                            value = new Long(params.searchString);
                        } catch (NumberFormatException e1) {
                            try {
                                value = new Double(params.searchString);
                            } catch (NumberFormatException e2) {
                                try {
                                    value = new BigDecimal(params.searchString);
                                } catch (Exception e3) {
                                    // Not a number
                                    continue;
                                }
                            }
                        }
                    }
                    break;
                case CONTENT:
                case BINARY:
                    if (!params.searchLOBs) {
                        continue;
                    }
                case STRING:
                    if (attribute.getMaxLength() > 0 && attribute.getMaxLength() < params.searchString.length()) {
                        continue;
                    }
                    if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.LIKE)) {
                        operator = DBCLogicalOperator.LIKE;
                        value = "%" + params.searchString + "%";
                    } else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        operator = DBCLogicalOperator.EQUALS;
                        value = params.searchString;
                    } else {
                        continue;
                    }
                    break;
                default:
                    {
                        // On success search by exact match
                        if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                            continue;
                        }
                        String typeName = attribute.getTypeName();
                        if (typeName.equals(DBConstants.TYPE_NAME_UUID) || typeName.equals(DBConstants.TYPE_NAME_UUID2)) {
                            try {
                                UUID uuid = UUID.fromString(params.searchString);
                                operator = DBCLogicalOperator.EQUALS;
                                value = "'" + uuid.toString() + "'";
                            } catch (Exception e) {
                                // No a UUID
                                continue;
                            }
                        } else {
                            continue;
                        }
                    }
            }
            DBDAttributeConstraint constraint = new DBDAttributeConstraint(attribute, constraints.size());
            constraint.setOperator(operator);
            constraint.setValue(value);
            constraint.setVisible(true);
            constraints.add(constraint);
        }
        if (constraints.isEmpty()) {
            return null;
        }
        dataReceiver.filter = new DBDDataFilter(constraints);
        dataReceiver.filter.setAnyConstraint(true);
        DBCExecutionSource searchSource = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this);
        return dataContainer.readData(searchSource, session, dataReceiver, dataReceiver.filter, -1, -1, 0);
    } catch (DBException e) {
        throw new DBCException("Error finding rows", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) BigDecimal(java.math.BigDecimal) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 54 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class SearchMetadataQuery method run.

@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
    try {
        List<DBSObjectType> objectTypes = params.getObjectTypes();
        String objectNameMask = params.getObjectNameMask();
        if (params.getMatchType() == SearchMetadataConstants.MATCH_INDEX_STARTS_WITH) {
            if (!objectNameMask.endsWith("%")) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                objectNameMask = objectNameMask + "%";
            }
        } else if (params.getMatchType() == SearchMetadataConstants.MATCH_INDEX_CONTAINS) {
            if (!objectNameMask.startsWith("%")) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                objectNameMask = "%" + objectNameMask;
            }
            if (!objectNameMask.endsWith("%")) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                objectNameMask = objectNameMask + "%";
            }
        }
        DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
        DBRProgressMonitor localMonitor = RuntimeUtils.makeMonitor(monitor);
        Collection<DBSObjectReference> objects = structureAssistant.findObjectsByMask(localMonitor, params.getParentObject(), objectTypes.toArray(new DBSObjectType[objectTypes.size()]), objectNameMask, params.isCaseSensitive(), true, params.getMaxResults());
        for (DBSObjectReference reference : objects) {
            if (monitor.isCanceled()) {
                break;
            }
            try {
                DBSObject object = reference.resolveObject(localMonitor);
                if (object != null) {
                    DBNNode node = navigatorModel.getNodeByObject(localMonitor, object, false);
                    if (node != null) {
                        searchResult.addObjects(Collections.singletonList(node));
                    }
                }
            } catch (DBException e) {
                log.error(e);
            }
        }
        return Status.OK_STATUS;
    } catch (DBException e) {
        return GeneralUtils.makeExceptionStatus(e);
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) DBSObjectReference(org.jkiss.dbeaver.model.struct.DBSObjectReference) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 55 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class DB2DataSource method initialize.

// -----------------------
// Initialisation/Structure
// -----------------------
@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
    super.initialize(monitor);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load data source meta info")) {
        // First try to get active schema from special register 'CURRENT SCHEMA'
        this.activeSchemaName = determineActiveSchema(session);
        this.db2CurrentUserPrivileges = new DB2CurrentUserPrivileges(monitor, session, activeSchemaName, this);
    } catch (SQLException e) {
        LOG.warn("Error reading active schema", e);
    }
    try {
        this.dataTypeCache.getAllObjects(monitor, this);
    } catch (DBException e) {
        LOG.warn("Error reading types info", e);
        this.dataTypeCache.setCache(Collections.<DB2DataType>emptyList());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException)

Aggregations

DBException (org.jkiss.dbeaver.DBException)232 SQLException (java.sql.SQLException)58 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)51 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)50 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)43 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)42 ArrayList (java.util.ArrayList)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)23 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)23 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)16 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)14 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)13 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)13 GridData (org.eclipse.swt.layout.GridData)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)12 CoreException (org.eclipse.core.runtime.CoreException)11 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 IStatus (org.eclipse.core.runtime.IStatus)9 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)8 XMLException (org.jkiss.utils.xml.XMLException)8