Search in sources :

Example 6 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class DB2PlanAnalyser method explain.

// ----------------
// Business Methods
// ----------------
public void explain(JDBCSession session) throws DBCException {
    Integer stmtNo = STMT_NO_GEN.incrementAndGet();
    String explainStmt = String.format(PT_EXPLAIN, stmtNo, query);
    LOG.debug("Schema=" + planTableSchema + " : " + explainStmt);
    try {
        // Start by cleaning old rows for safety
        cleanExplainTables(session, stmtNo, planTableSchema);
        // Explain
        try (JDBCPreparedStatement dbStat = session.prepareStatement(String.format(PT_EXPLAIN, stmtNo, query))) {
            dbStat.execute();
        }
        // Build Node Structure
        try (JDBCPreparedStatement dbStat = session.prepareStatement(String.format(SEL_STMT, planTableSchema))) {
            dbStat.setInt(1, stmtNo);
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                dbResult.next();
                db2PlanStatement = new DB2PlanStatement(session, dbResult, planTableSchema);
            }
        }
        listNodes = db2PlanStatement.buildNodes();
        // Clean afterward
        cleanExplainTables(session, stmtNo, planTableSchema);
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 7 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCCollection method makeCollectionFromResultSet.

@NotNull
private static JDBCCollection makeCollectionFromResultSet(@NotNull JDBCSession session, @NotNull Array array, @Nullable DBSDataType elementType) throws SQLException, DBException {
    ResultSet dbResult = array.getResultSet();
    if (dbResult == null) {
        throw new DBCException("JDBC array type was not resolved and result set was not provided by driver. Return NULL.");
    }
    DBDValueHandler valueHandler;
    if (elementType == null) {
        JDBCColumnMetaData itemMeta = new JDBCColumnMetaData(session.getDataSource(), dbResult.getMetaData(), 1);
        elementType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), itemMeta.getTypeName());
        if (elementType == null) {
            elementType = new JDBCDataType(session.getDataSource(), itemMeta);
        }
        valueHandler = DBUtils.findValueHandler(session, itemMeta);
    } else {
        valueHandler = DBUtils.findValueHandler(session, elementType);
    }
    try {
        try (DBCResultSet resultSet = JDBCResultSetImpl.makeResultSet(session, null, dbResult, ModelMessages.model_jdbc_array_result_set, true)) {
            List<Object> data = new ArrayList<>();
            while (dbResult.next()) {
                // Fetch second column - it contains value
                data.add(valueHandler.fetchValueObject(session, resultSet, elementType, 1));
            }
            return new JDBCCollection(elementType, valueHandler, data.toArray());
        }
    } finally {
        try {
            dbResult.close();
        } catch (SQLException e) {
            //$NON-NLS-1$
            log.debug("Can't close array result set", e);
        }
    }
}
Also used : JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) SQLException(java.sql.SQLException) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) JDBCColumnMetaData(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) NotNull(org.jkiss.code.NotNull)

Example 8 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCReference method getReferencedObject.

@Override
public Object getReferencedObject(DBCSession session) throws DBCException {
    if (refObject == null) {
        try {
            session.getProgressMonitor().beginTask("Retrieve references object", 3);
            try {
                session.getProgressMonitor().worked(1);
                Object refValue = value.getObject();
                session.getProgressMonitor().worked(1);
                DBDValueHandler valueHandler = DBUtils.findValueHandler(session, type);
                refObject = valueHandler.getValueFromObject(session, type, refValue, false);
                session.getProgressMonitor().worked(1);
            } finally {
                session.getProgressMonitor().done();
            }
        } catch (SQLException e) {
            throw new DBCException("Can't obtain object reference");
        }
    }
    return refObject;
}
Also used : SQLException(java.sql.SQLException) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 9 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCContentValueHandler method getValueFromObject.

@Override
public DBDContent getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    if (object == null) {
        // Create wrapper using column type
        switch(type.getTypeID()) {
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
            case java.sql.Types.NVARCHAR:
            case java.sql.Types.LONGVARCHAR:
            case java.sql.Types.LONGNVARCHAR:
                return new JDBCContentChars(session.getDataSource(), null);
            case java.sql.Types.CLOB:
            case java.sql.Types.NCLOB:
                return new JDBCContentCLOB(session.getDataSource(), null);
            case java.sql.Types.BINARY:
            case java.sql.Types.VARBINARY:
            case java.sql.Types.LONGVARBINARY:
                return new JDBCContentBytes(session.getDataSource());
            case java.sql.Types.BLOB:
                return new JDBCContentBLOB(session.getDataSource(), null);
            case java.sql.Types.SQLXML:
                return new JDBCContentXML(session.getDataSource(), null);
            default:
                log.error(ModelMessages.model_jdbc_unsupported_column_type_ + type.getTypeName());
                return new JDBCContentBytes(session.getDataSource());
        }
    } else if (object instanceof byte[]) {
        return new JDBCContentBytes(session.getDataSource(), (byte[]) object);
    } else if (object instanceof String) {
        // So it is possible that real object type isn't string
        switch(type.getTypeID()) {
            case java.sql.Types.BINARY:
            case java.sql.Types.VARBINARY:
            case java.sql.Types.LONGVARBINARY:
                return new JDBCContentBytes(session.getDataSource(), (String) object);
            default:
                // String by default
                return new JDBCContentChars(session.getDataSource(), (String) object);
        }
    } else if (object instanceof Blob) {
        final JDBCContentBLOB blob = new JDBCContentBLOB(session.getDataSource(), (Blob) object);
        final DBPPreferenceStore preferenceStore = session.getDataSource().getContainer().getPreferenceStore();
        if (preferenceStore.getBoolean(ModelPreferences.CONTENT_CACHE_BLOB) && blob.getLOBLength() < preferenceStore.getLong(ModelPreferences.CONTENT_CACHE_MAX_SIZE)) {
            // Precache content
            blob.getContents(session.getProgressMonitor());
        }
        return blob;
    } else if (object instanceof Clob) {
        JDBCContentCLOB clob = new JDBCContentCLOB(session.getDataSource(), (Clob) object);
        final DBPPreferenceStore preferenceStore = session.getDataSource().getContainer().getPreferenceStore();
        if (preferenceStore.getBoolean(ModelPreferences.CONTENT_CACHE_CLOB) && clob.getLOBLength() < preferenceStore.getLong(ModelPreferences.CONTENT_CACHE_MAX_SIZE)) {
            // Precache content
            clob.getContents(session.getProgressMonitor());
        }
        return clob;
    } else if (object instanceof SQLXML) {
        return new JDBCContentXML(session.getDataSource(), (SQLXML) object);
    } else if (object instanceof InputStream) {
        // Some weird drivers returns InputStream instead of Xlob.
        // Copy stream to byte array
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final InputStream stream = (InputStream) object;
        try {
            IOUtils.copyStream(stream, buffer);
        } catch (Exception e) {
            throw new DBCException("Error reading content stream", e);
        }
        IOUtils.close(stream);
        return new JDBCContentBytes(session.getDataSource(), buffer.toByteArray());
    } else if (object instanceof Reader) {
        // Copy reader to string
        StringWriter buffer = new StringWriter();
        final Reader reader = (Reader) object;
        try {
            IOUtils.copyText(reader, buffer);
        } catch (Exception e) {
            throw new DBCException("Error reading content reader", e);
        }
        IOUtils.close(reader);
        return new JDBCContentChars(session.getDataSource(), buffer.toString());
    } else if (object instanceof DBDContent) {
        if (copy && object instanceof DBDValueCloneable) {
            return (DBDContent) ((DBDValueCloneable) object).cloneValue(session.getProgressMonitor());
        }
        return (DBDContent) object;
    } else {
        throw new DBCException(ModelMessages.model_jdbc_unsupported_value_type_ + object.getClass().getName());
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

Example 10 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class DataSourceDescriptor method connect.

public boolean connect(DBRProgressMonitor monitor, boolean initialize, boolean reflect) throws DBException {
    if (connecting) {
        log.debug("Can't connect - connect/disconnect is in progress");
        return false;
    }
    if (this.isConnected()) {
        log.debug("Can't connect - already connected");
        return false;
    }
    log.debug("Connect with '" + getName() + "' (" + getId() + ")");
    //final String oldPassword = getConnectionConfiguration().getUserPassword();
    if (!isSavePassword()) {
        // Ask for password
        if (!DataSourceHandler.askForPassword(this, null)) {
            DataSourceHandler.updateDataSourceObject(this);
            return false;
        }
    }
    processEvents(monitor, DBPConnectionEventType.BEFORE_CONNECT);
    connecting = true;
    tunnelConnectionInfo = null;
    try {
        // Handle tunnel
        // Open tunnel and replace connection info with new one
        this.tunnel = null;
        DBWHandlerConfiguration tunnelConfiguration = null;
        for (DBWHandlerConfiguration handler : connectionInfo.getDeclaredHandlers()) {
            if (handler.isEnabled() && handler.getType() == DBWHandlerType.TUNNEL) {
                tunnelConfiguration = handler;
                break;
            }
        }
        monitor.beginTask("Connect to " + getName(), tunnelConfiguration != null ? 3 : 2);
        if (tunnelConfiguration != null) {
            monitor.subTask("Initialize tunnel");
            tunnel = tunnelConfiguration.createHandler(DBWTunnel.class);
            try {
                if (!tunnelConfiguration.isSavePassword() && tunnel.needsPassword(tunnelConfiguration)) {
                    if (!DataSourceHandler.askForPassword(this, tunnelConfiguration)) {
                        DataSourceHandler.updateDataSourceObject(this);
                        tunnel = null;
                        return false;
                    }
                }
                /*
                    for (DBWHandlerConfiguration handler : getConnectionConfiguration().getDeclaredHandlers()) {
                        if (handler.isEnabled() && handler.isSecured() && !handler.isSavePassword()) {
                            if (!DataSourceHandler.askForPassword(this, handler)) {
                                DataSourceHandler.updateDataSourceObject(this);
                                return false;
                            }
                        }
                    }
*/
                tunnelConnectionInfo = tunnel.initializeTunnel(monitor, registry.getPlatform(), tunnelConfiguration, connectionInfo);
            } catch (Exception e) {
                throw new DBCException("Can't initialize tunnel", e);
            }
            monitor.worked(1);
        }
        monitor.subTask("Connect to data source");
        dataSource = getDriver().getDataSourceProvider().openDataSource(monitor, this);
        monitor.worked(1);
        if (initialize) {
            monitor.subTask("Initialize data source");
            try {
                dataSource.initialize(monitor);
            } catch (Throwable e) {
                log.error("Error initializing datasource", e);
            }
            // Change connection properties
            initConnectionState(monitor);
        }
        connectFailed = false;
        connectTime = new Date();
        processEvents(monitor, DBPConnectionEventType.AFTER_CONNECT);
        if (reflect) {
            getRegistry().notifyDataSourceListeners(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, DataSourceDescriptor.this, true));
        }
        log.debug("Connected (" + getId() + ")");
        return true;
    } catch (Exception e) {
        log.debug("Connection failed (" + getId() + ")");
        if (tunnel != null) {
            try {
                tunnel.closeTunnel(monitor);
            } catch (IOException e1) {
                log.error("Error closing tunnel", e);
            } finally {
                tunnel = null;
                tunnelConnectionInfo = null;
            }
        }
        // Failed
        connectFailed = true;
        //if (reflect) {
        getRegistry().notifyDataSourceListeners(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, DataSourceDescriptor.this, false));
        //}
        if (e instanceof DBException) {
            throw (DBException) e;
        } else {
            throw new DBException("Internal error connecting to " + getName(), e);
        }
    } finally {
        monitor.done();
        connecting = false;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) DBWTunnel(org.jkiss.dbeaver.model.net.DBWTunnel) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IOException(java.io.IOException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Aggregations

DBCException (org.jkiss.dbeaver.model.exec.DBCException)60 SQLException (java.sql.SQLException)28 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)16 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)14 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)13 DBException (org.jkiss.dbeaver.DBException)11 NotNull (org.jkiss.code.NotNull)9 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)5 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tree (org.eclipse.swt.widgets.Tree)3 TreeColumn (org.eclipse.swt.widgets.TreeColumn)3 TreeItem (org.eclipse.swt.widgets.TreeItem)3 Nullable (org.jkiss.code.Nullable)3 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)3 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3