Search in sources :

Example 16 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by serge-rider.

the class SQLQueryJob method executeStatement.

private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, long startTime, SQLQueryResult curResult) throws DBCException {
    AbstractExecutionSource source = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), partSite.getPart(), sqlQuery);
    source.setScriptContext(scriptContext);
    final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, rsOffset, rsMaxRows);
    DBExecUtils.setStatementFetchSize(dbcStatement, rsOffset, rsMaxRows, fetchSize);
    curStatement = dbcStatement;
    int statementTimeout = getDataSourceContainer().getPreferenceStore().getInt(SQLPreferenceConstants.STATEMENT_TIMEOUT);
    if (statementTimeout > 0) {
        try {
            dbcStatement.setStatementTimeout(statementTimeout);
        } catch (Throwable e) {
            log.debug("Can't set statement timeout:" + e.getMessage());
        }
    }
    // Execute statement
    try {
        DBRProgressMonitor monitor = session.getProgressMonitor();
        monitor.subTask("Execute query");
        boolean hasResultSet = dbcStatement.executeStatement();
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();
        curResult.setHasResultSet(hasResultSet);
        long updateCount = -1;
        while (true) {
            // Fetch data only if we have to fetch all results or if it is rs requested
            if (fetchResultSetNumber < 0 || fetchResultSetNumber == resultSetNumber) {
                if (hasResultSet && fetchResultSets) {
                    DBCResultSet resultSet;
                    try {
                        resultSet = dbcStatement.openResultSet();
                    } catch (DBCException e) {
                        DBPErrorAssistant.ErrorType errorType = DBExecUtils.discoverErrorType(session.getDataSource(), e);
                        if (errorType == DBPErrorAssistant.ErrorType.RESULT_SET_MISSING) {
                            // We need to ignore this error and try to get next results
                            if (dbcStatement.nextResults()) {
                                continue;
                            }
                        }
                        throw e;
                    }
                    if (resultSet == null) {
                        // Kind of bug in the driver. It says it has resultset but returns null
                        break;
                    } else {
                        DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
                        if (dataReceiver != null) {
                            hasResultSet = fetchQueryData(session, resultSet, curResult, curResult.addExecuteResult(true), dataReceiver, true);
                        }
                    }
                }
            }
            if (!hasResultSet) {
                try {
                    updateCount = dbcStatement.getUpdateRowCount();
                    SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(false);
                    if (updateCount >= 0) {
                        executeResult.setUpdateCount(updateCount);
                        statistics.addRowsUpdated(updateCount);
                    }
                } catch (DBCException e) {
                    // In some cases we can't read update count
                    // This is bad but we can live with it
                    // Just print a warning
                    log.warn("Can't obtain update count", e);
                }
            }
            if (hasResultSet && fetchResultSets) {
                resultSetNumber++;
                fetchResultSetNumber = resultSetNumber;
            }
            if (!hasResultSet && updateCount < 0) {
                // Nothing else to fetch
                break;
            }
            if (session.getDataSource().getInfo().supportsMultipleResults()) {
                try {
                    hasResultSet = dbcStatement.nextResults();
                } catch (DBCException e) {
                    if (session.getDataSource().getInfo().isMultipleResultsFetchBroken()) {
                        log.error(e);
                        // #2792: Check this twice. Some drivers (e.g. Sybase jConnect)
                        // throw error on n'th result fetch - but it still can keep fetching next results
                        hasResultSet = dbcStatement.nextResults();
                    } else {
                        throw e;
                    }
                }
                updateCount = hasResultSet ? -1 : 0;
            } else {
                break;
            }
        }
        ;
    } finally {
        try {
            curResult.addWarnings(dbcStatement.getStatementWarnings());
        } catch (Throwable e) {
            log.warn("Can't read execution warnings", e);
        }
        // monitor.subTask("Close query");
        if (!keepStatementOpen()) {
            closeStatement();
        }
    }
}
Also used : DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 17 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.

the class ResultSetViewer method readRowCount.

/**
 * Reads row count and sets value in status label
 */
private long readRowCount(DBRProgressMonitor monitor) throws DBException {
    final DBCExecutionContext executionContext = getExecutionContext();
    DBSDataContainer dataContainer = getDataContainer();
    if (executionContext == null || dataContainer == null) {
        throw new DBException(ModelMessages.error_not_connected_to_database);
    }
    long[] result = new long[1];
    DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
        try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Read total row count")) {
            long rowCount = dataContainer.countData(new AbstractExecutionSource(dataContainer, executionContext, this), session, model.getDataFilter(), DBSDataContainer.FLAG_NONE);
            model.setTotalRowCount(rowCount);
            result[0] = rowCount;
        } catch (DBCException e) {
            throw new InvocationTargetException(e);
        }
    });
    return result[0];
}
Also used : DBException(org.jkiss.dbeaver.DBException) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.

the class SQLQueryJob method executeStatement.

private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, long startTime, SQLQueryResult curResult) throws DBCException {
    AbstractExecutionSource source = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), partSite.getPart(), sqlQuery);
    source.setScriptContext(scriptContext);
    final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, rsOffset, rsMaxRows);
    DBExecUtils.setStatementFetchSize(dbcStatement, rsOffset, rsMaxRows, fetchSize);
    curStatement = dbcStatement;
    int statementTimeout = getDataSourceContainer().getPreferenceStore().getInt(SQLPreferenceConstants.STATEMENT_TIMEOUT);
    if (statementTimeout > 0) {
        try {
            dbcStatement.setStatementTimeout(statementTimeout);
        } catch (Throwable e) {
            log.debug("Can't set statement timeout:" + e.getMessage());
        }
    }
    // Execute statement
    try {
        DBRProgressMonitor monitor = session.getProgressMonitor();
        monitor.subTask("Execute query");
        boolean hasResultSet = dbcStatement.executeStatement();
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();
        curResult.setHasResultSet(hasResultSet);
        long updateCount = -1;
        while (true) {
            // Fetch data only if we have to fetch all results or if it is rs requested
            if (fetchResultSetNumber < 0 || fetchResultSetNumber == resultSetNumber) {
                if (hasResultSet && fetchResultSets) {
                    DBCResultSet resultSet;
                    try {
                        resultSet = dbcStatement.openResultSet();
                    } catch (DBCException e) {
                        DBPErrorAssistant.ErrorType errorType = DBExecUtils.discoverErrorType(session.getDataSource(), e);
                        if (errorType == DBPErrorAssistant.ErrorType.RESULT_SET_MISSING) {
                            // We need to ignore this error and try to get next results
                            if (dbcStatement.nextResults()) {
                                continue;
                            }
                        }
                        throw e;
                    }
                    if (resultSet == null) {
                        // Kind of bug in the driver. It says it has resultset but returns null
                        break;
                    } else {
                        DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
                        if (dataReceiver != null) {
                            hasResultSet = fetchQueryData(session, resultSet, curResult, curResult.addExecuteResult(true), dataReceiver, true);
                        }
                    }
                }
            }
            if (!hasResultSet) {
                try {
                    updateCount = dbcStatement.getUpdateRowCount();
                    SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(false);
                    if (updateCount >= 0) {
                        executeResult.setUpdateCount(updateCount);
                        statistics.addRowsUpdated(updateCount);
                    }
                } catch (DBCException e) {
                    // In some cases we can't read update count
                    // This is bad but we can live with it
                    // Just print a warning
                    log.warn("Can't obtain update count", e);
                }
            }
            if (hasResultSet && fetchResultSets) {
                resultSetNumber++;
                fetchResultSetNumber = resultSetNumber;
            }
            if (!hasResultSet && updateCount < 0) {
                // Nothing else to fetch
                break;
            }
            if (session.getDataSource().getInfo().supportsMultipleResults()) {
                try {
                    hasResultSet = dbcStatement.nextResults();
                } catch (DBCException e) {
                    if (session.getDataSource().getInfo().isMultipleResultsFetchBroken()) {
                        log.error(e);
                        // #2792: Check this twice. Some drivers (e.g. Sybase jConnect)
                        // throw error on n'th result fetch - but it still can keep fetching next results
                        hasResultSet = dbcStatement.nextResults();
                    } else {
                        throw e;
                    }
                }
                updateCount = hasResultSet ? -1 : 0;
            } else {
                break;
            }
        }
        ;
    } finally {
        try {
            curResult.addWarnings(dbcStatement.getStatementWarnings());
        } catch (Throwable e) {
            log.warn("Can't read execution warnings", e);
        }
        // monitor.subTask("Close query");
        if (!keepStatementOpen()) {
            closeStatement();
        }
    }
}
Also used : DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 19 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.

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.findAttributeIndex(session.getProgressMonitor(), attribute) == null) {
                    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 = Integer.valueOf(params.searchString);
                    } catch (NumberFormatException e) {
                        try {
                            value = Long.valueOf(params.searchString);
                        } catch (NumberFormatException e1) {
                            try {
                                value = Double.valueOf(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 (!params.isCaseSensitive() && ArrayUtils.contains(supportedOperators, DBCLogicalOperator.ILIKE)) {
                        operator = DBCLogicalOperator.ILIKE;
                        value = "%" + params.searchString + "%";
                    } else 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, 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 20 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.

the class DatabaseMappingContainer method readAttributes.

private void readAttributes(DBRProgressMonitor monitor) throws DBException {
    if (source instanceof DBSEntity && !(source instanceof DBSDocumentContainer)) {
        for (DBSEntityAttribute attr : CommonUtils.safeCollection(((DBSEntity) source).getAttributes(monitor))) {
            if (DBUtils.isHiddenObject(attr)) {
                continue;
            }
            addAttributeMapping(monitor, attr);
        }
    } else {
        // Seems to be a dynamic query. Execute it to get metadata
        DBPDataSource dataSource = source.getDataSource();
        assert (dataSource != null);
        DBCExecutionContext context;
        if (source instanceof DBPContextProvider) {
            context = ((DBPContextProvider) source).getExecutionContext();
        } else {
            context = DBUtils.getDefaultContext(source, false);
        }
        if (context == null) {
            throw new DBCException("No execution context");
        }
        DBExecUtils.tryExecuteRecover(monitor, context.getDataSource(), monitor1 -> {
            try (DBCSession session = context.openSession(monitor1, DBCExecutionPurpose.META, "Read query meta data")) {
                MetadataReceiver receiver = new MetadataReceiver();
                try {
                    source.readData(new AbstractExecutionSource(source, session.getExecutionContext(), this), session, receiver, null, 0, 1, DBSDataContainer.FLAG_NONE, 1);
                    for (DBDAttributeBinding attr : receiver.attributes) {
                        if (DBUtils.isHiddenObject(attr)) {
                            continue;
                        }
                        addAttributeMapping(monitor1, attr);
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    }
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource)

Aggregations

AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)21 DBException (org.jkiss.dbeaver.DBException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)4 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)3 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)3 BigDecimal (java.math.BigDecimal)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)2 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)2 SQLQueryDataContainer (org.jkiss.dbeaver.model.sql.data.SQLQueryDataContainer)2 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)2 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)2 IOException (java.io.IOException)1 MockGeneratorDescriptor (org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor)1 MockValueGenerator (org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator)1 DBDAttributeValue (org.jkiss.dbeaver.model.data.DBDAttributeValue)1 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)1