Search in sources :

Example 21 with DBCException

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

the class ConfigImportWizard method adaptConnectionUrl.

protected void adaptConnectionUrl(ImportConnectionInfo connectionInfo) throws DBException {
    String sampleURL = connectionInfo.getDriverInfo().getSampleURL();
    if (connectionInfo.getDriver() != null) {
        sampleURL = connectionInfo.getDriver().getSampleURL();
    }
    //connectionInfo.getDriver()
    String url = connectionInfo.getUrl();
    if (url != null) {
        // Parse url
        final DriverDescriptor.MetaURL metaURL = DriverDescriptor.parseSampleURL(sampleURL);
        int sourceOffset = 0;
        List<String> urlComponents = metaURL.getUrlComponents();
        for (int i = 0, urlComponentsSize = urlComponents.size(); i < urlComponentsSize; i++) {
            String component = urlComponents.get(i);
            if (component.length() > 2 && component.charAt(0) == '{' && component.charAt(component.length() - 1) == '}' && metaURL.getAvailableProperties().contains(component.substring(1, component.length() - 1))) {
                // Property
                int partEnd;
                if (i < urlComponentsSize - 1) {
                    // Find next component
                    final String nextComponent = urlComponents.get(i + 1);
                    partEnd = url.indexOf(nextComponent, sourceOffset);
                    if (partEnd == -1) {
                        if (nextComponent.equals(":")) {
                            // Try to find another divider - dbvis sometimes contains bad sample URLs (e.g. for Oracle)
                            partEnd = url.indexOf("/", sourceOffset);
                        }
                        if (partEnd == -1) {
                            if (connectionInfo.getHost() == null) {
                                throw new DBException("Can't parse URL '" + url + "' with pattern '" + sampleURL + "'. String '" + nextComponent + "' not found after '" + component);
                            } else {
                                // We have connection properties anyway
                                url = null;
                                break;
                            }
                        }
                    }
                } else {
                    partEnd = url.length();
                }
                String propertyValue = url.substring(sourceOffset, partEnd);
                switch(component) {
                    case "{host}":
                        connectionInfo.setHost(propertyValue);
                        break;
                    case "{port}":
                        connectionInfo.setPort(propertyValue);
                        break;
                    case "{database}":
                        connectionInfo.setDatabase(propertyValue);
                        break;
                    default:
                        if (connectionInfo.getHost() == null) {
                            throw new DBException("Unsupported property " + component);
                        }
                }
                sourceOffset = partEnd;
            } else {
                // Static string
                sourceOffset += component.length();
            }
        }
    }
    if (url == null) {
        if (connectionInfo.getDriver() == null) {
            throw new DBCException("Can't detect target driver for '" + connectionInfo.getAlias() + "'");
        }
        if (connectionInfo.getHost() == null) {
            throw new DBCException("No URL and no host name - can't import connection '" + connectionInfo.getAlias() + "'");
        }
        // No URL - generate from props
        DBPConnectionConfiguration conConfig = new DBPConnectionConfiguration();
        conConfig.setHostName(connectionInfo.getHost());
        conConfig.setHostPort(connectionInfo.getPort());
        conConfig.setDatabaseName(connectionInfo.getDatabase());
        url = connectionInfo.getDriver().getDataSourceProvider().getConnectionURL(connectionInfo.getDriver(), conConfig);
        connectionInfo.setUrl(url);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 22 with DBCException

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

the class MySQLView method loadAdditionalInfo.

private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBCException {
    if (!isPersisted() || getContainer().isSystem()) {
        additionalInfo.loaded = true;
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table status")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + MySQLConstants.META_TABLE_VIEWS + " WHERE " + MySQLConstants.COL_TABLE_SCHEMA + "=? AND " + MySQLConstants.COL_TABLE_NAME + "=?")) {
            dbStat.setString(1, getContainer().getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    try {
                        additionalInfo.setCheckOption(CheckOption.valueOf(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CHECK_OPTION)));
                    } catch (IllegalArgumentException e) {
                        log.warn(e);
                    }
                    additionalInfo.setDefiner(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DEFINER));
                    additionalInfo.setDefinition(SQLUtils.formatSQL(getDataSource(), JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_VIEW_DEFINITION)));
                    additionalInfo.setUpdatable("YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_UPDATABLE)));
                }
                additionalInfo.loaded = true;
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 23 with DBCException

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

the class MySQLPlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    String plainQuery = SQLUtils.stripComments(SQLUtils.getDialectFromObject(session.getDataSource()), query).toUpperCase();
    if (!plainQuery.startsWith("SELECT")) {
        throw new DBCException("Only SELECT statements could produce execution plan");
    }
    JDBCSession connection = (JDBCSession) session;
    try {
        try (JDBCPreparedStatement dbStat = connection.prepareStatement("EXPLAIN EXTENDED " + query)) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                rootNodes = new ArrayList<>();
                while (dbResult.next()) {
                    MySQLPlanNode node = new MySQLPlanNode(null, dbResult);
                    rootNodes.add(node);
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 24 with DBCException

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

the class OracleContentBFILE method openFile.

private void openFile() throws DBCException {
    if (opened) {
        return;
    }
    try {
        BeanUtils.invokeObjectMethod(bfile, "openFile");
        opened = true;
    } catch (Throwable e) {
        throw new DBCException(e, dataSource);
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 25 with DBCException

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

the class OracleContentBFILE method getContents.

@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
    if (storage == null && bfile != null) {
        try {
            openFile();
            long contentLength = getContentLength();
            DBPPlatform platform = dataSource.getContainer().getPlatform();
            if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
                try {
                    try (InputStream bs = getInputStream()) {
                        storage = BytesContentStorage.createFromStream(bs, contentLength, getDefaultEncoding());
                    }
                } catch (IOException e) {
                    throw new DBCException("IO error while reading content", e);
                }
            } else {
                // Create new local storage
                File tempFile;
                try {
                    tempFile = ContentUtils.createTempContentFile(monitor, platform, "blob" + bfile.hashCode());
                } catch (IOException e) {
                    throw new DBCException("Can't create temporary file", e);
                }
                try (OutputStream os = new FileOutputStream(tempFile)) {
                    try (InputStream bs = getInputStream()) {
                        ContentUtils.copyStreams(bs, contentLength, os, monitor);
                    }
                } catch (IOException e) {
                    ContentUtils.deleteTempFile(tempFile);
                    throw new DBCException("IO error while copying stream", e);
                } catch (Throwable e) {
                    ContentUtils.deleteTempFile(tempFile);
                    throw new DBCException(e, dataSource);
                }
                this.storage = new TemporaryContentStorage(platform, tempFile, getDefaultEncoding());
            }
            // Free blob - we don't need it anymore
            releaseBlob();
        } finally {
            closeFile();
        }
    }
    return storage;
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) TemporaryContentStorage(org.jkiss.dbeaver.model.impl.TemporaryContentStorage) DBPPlatform(org.jkiss.dbeaver.model.app.DBPPlatform)

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