Search in sources :

Example 76 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class SQLInformationProvider method getInformation2.

@Override
public Object getInformation2(ITextViewer textViewer, IRegion subject) {
    if (implementation != null) {
        Object s = implementation.getHoverInfo2(textViewer, subject);
        if (s != null) {
            return s;
        }
    }
    //SQLCompletionProposal proposal = new SQLCompletionProposal();
    SQLContextInformer informer = new SQLContextInformer(editor, editor.getSyntaxManager());
    informer.searchInformation(subject);
    DBSObject object = null;
    if (informer.hasObjects()) {
        // Make object description
        DBRProgressMonitor monitor = VoidProgressMonitor.INSTANCE;
        final DBSObjectReference objectRef = informer.getObjectReferences().get(0);
        try {
            object = objectRef.resolveObject(monitor);
        } catch (DBException e) {
            // Can't resolve
            return e.getMessage();
        }
    } else if (ArrayUtils.isEmpty(informer.getKeywords())) {
        return null;
    }
    return SQLContextInformer.readAdditionalProposalInfo(null, editor.getDataSource(), object, informer.getKeywords(), informer.getKeywordType());
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) DBSObjectReference(org.jkiss.dbeaver.model.struct.DBSObjectReference) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 77 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class BaseTextEditor method saveToExternalFile.

public void saveToExternalFile() {
    IEditorInput editorInput = getEditorInput();
    IFile curFile = EditorUtils.getFileFromInput(editorInput);
    String fileName = curFile == null ? null : curFile.getName();
    final Document document = getDocument();
    final File saveFile = DialogUtils.selectFileForSave(getSite().getShell(), "Save SQL script", new String[] { "*.sql", "*.txt", "*", "*.*" }, fileName);
    if (document == null || saveFile == null) {
        return;
    }
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(final DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    StringReader cr = new StringReader(document.get());
                    ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InterruptedException e) {
    // do nothing
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(getSite().getShell(), "Save failed", null, e.getTargetException());
    }
    afterSaveToFile(saveFile);
}
Also used : IFile(org.eclipse.core.resources.IFile) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) Document(org.eclipse.jface.text.Document) IFile(org.eclipse.core.resources.IFile) IEditorInput(org.eclipse.ui.IEditorInput) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 78 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class JDBCCollection method makeCollectionFromArray.

/////////////////////////////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////////////////////////////
@NotNull
public static JDBCCollection makeCollectionFromArray(@NotNull JDBCSession session, @NotNull DBSTypedObject column, Array array) throws DBCException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    DBSDataType elementType = null;
    if (column instanceof DBSTypedObjectEx) {
        DBSDataType arrayType = ((DBSTypedObjectEx) column).getDataType();
        if (arrayType != null) {
            elementType = arrayType.getComponentType(monitor);
        }
    }
    if (elementType == null) {
        try {
            if (array == null) {
                String arrayTypeName = column.getTypeName();
                DBSDataType arrayType = session.getDataSource().resolveDataType(monitor, arrayTypeName);
                if (arrayType != null) {
                    elementType = arrayType.getComponentType(monitor);
                }
            } else {
                String baseTypeName = array.getBaseTypeName();
                elementType = session.getDataSource().resolveDataType(monitor, baseTypeName);
            }
        } catch (Exception e) {
            throw new DBCException("Error resolving data type", e);
        }
    }
    try {
        if (elementType == null) {
            if (array == null) {
                throw new DBCException("Can't resolve NULL array data type");
            }
            try {
                return makeCollectionFromResultSet(session, array, null);
            } catch (SQLException e) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
        }
        try {
            return makeCollectionFromArray(session, array, elementType);
        } catch (SQLException e) {
            if (array == null) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
            try {
                return makeCollectionFromResultSet(session, array, elementType);
            } catch (SQLException e1) {
                //$NON-NLS-1$
                throw new DBCException(e1, session.getDataSource());
            }
        }
    } catch (DBException e) {
        //$NON-NLS-1$
        throw new DBCException("Can't extract array data from JDBC array", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) SQLException(java.sql.SQLException) DBSTypedObjectEx(org.jkiss.dbeaver.model.struct.DBSTypedObjectEx) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException) NotNull(org.jkiss.code.NotNull)

Example 79 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class JDBCTable method readData.

@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    boolean hasLimits = firstRow >= 0 && maxRows > 0;
    DBPDataSource dataSource = session.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try {
        readRequiredMeta(monitor);
    } catch (DBException e) {
        log.warn(e);
    }
    DBDPseudoAttribute rowIdAttribute = (flags & FLAG_READ_PSEUDO) != 0 ? DBUtils.getRowIdAttribute(this) : null;
    // Always use alias if we have criteria or ROWID.
    // Some criteria doesn't work without alias
    // (e.g. structured attributes in Oracle requires table alias)
    String tableAlias = null;
    if ((dataFilter != null && dataFilter.hasConditions()) || rowIdAttribute != null) {
        if (dataSource instanceof SQLDataSource) {
            if (((SQLDataSource) dataSource).getSQLDialect().supportsAliasInSelect()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
        }
    }
    if (rowIdAttribute != null && tableAlias == null) {
        log.warn("Can't query ROWID - table alias not supported");
        rowIdAttribute = null;
    }
    StringBuilder query = new StringBuilder(100);
    query.append("SELECT ");
    appendSelectSource(monitor, query, tableAlias, rowIdAttribute);
    query.append(" FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
    if (tableAlias != null) {
        //$NON-NLS-1$
        query.append(" ").append(tableAlias);
    }
    appendQueryConditions(query, tableAlias, dataFilter);
    appendQueryOrder(query, tableAlias, dataFilter);
    String sqlQuery = query.toString();
    statistics.setQueryText(sqlQuery);
    monitor.subTask(ModelMessages.model_jdbc_fetch_table_data);
    try (DBCStatement dbStat = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        if (monitor.isCanceled()) {
            return statistics;
        }
        if (dbStat instanceof JDBCStatement && maxRows > 0) {
            boolean useFetchSize = getDataSource().getContainer().getPreferenceStore().getBoolean(ModelPreferences.RESULT_SET_USE_FETCH_SIZE);
            if (useFetchSize) {
                try {
                    ((JDBCStatement) dbStat).setFetchSize(firstRow < 0 || maxRows <= 0 ? DEFAULT_READ_FETCH_SIZE : (int) (firstRow + maxRows));
                } catch (Exception e) {
                    log.warn(e);
                }
            }
        }
        long startTime = System.currentTimeMillis();
        boolean executeResult = dbStat.executeStatement();
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        if (executeResult) {
            DBCResultSet dbResult = dbStat.openResultSet();
            if (dbResult != null && !monitor.isCanceled()) {
                try {
                    dataReceiver.fetchStart(session, dbResult, firstRow, maxRows);
                    startTime = System.currentTimeMillis();
                    long rowCount = 0;
                    while (dbResult.nextRow()) {
                        if (monitor.isCanceled() || (hasLimits && rowCount >= maxRows)) {
                            // Fetch not more than max rows
                            break;
                        }
                        dataReceiver.fetchRow(session, dbResult);
                        rowCount++;
                        if (rowCount % 100 == 0) {
                            monitor.subTask(rowCount + ModelMessages.model_jdbc__rows_fetched);
                            monitor.worked(100);
                        }
                    }
                    statistics.setFetchTime(System.currentTimeMillis() - startTime);
                    statistics.setRowsFetched(rowCount);
                } finally {
                    // First - close cursor
                    try {
                        dbResult.close();
                    } catch (Throwable e) {
                        //$NON-NLS-1$
                        log.error("Error closing result set", e);
                    }
                    // Then - signal that fetch was ended
                    try {
                        dataReceiver.fetchEnd(session, dbResult);
                    } catch (Throwable e) {
                        //$NON-NLS-1$
                        log.error("Error while finishing result set fetch", e);
                    }
                }
            }
        }
        return statistics;
    } finally {
        dataReceiver.close();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBException(org.jkiss.dbeaver.DBException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) NotNull(org.jkiss.code.NotNull)

Aggregations

DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)79 DBException (org.jkiss.dbeaver.DBException)45 InvocationTargetException (java.lang.reflect.InvocationTargetException)36 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)30 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)14 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 ArrayList (java.util.ArrayList)8 IStatus (org.eclipse.core.runtime.IStatus)8 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)6 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)6 GridData (org.eclipse.swt.layout.GridData)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)6 List (java.util.List)5 NotNull (org.jkiss.code.NotNull)5 File (java.io.File)4 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4