Search in sources :

Example 11 with DBException

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

the class ImagePanelEditor method primeEditorValue.

@Override
public void primeEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull ImageViewer control, @NotNull DBDContent value) throws DBException {
    monitor.subTask("Read image value");
    DBDContentStorage data = value.getContents(monitor);
    if (data != null) {
        try (InputStream contentStream = data.getContentStream()) {
            if (!control.loadImage(contentStream)) {
                throw new DBException("Can't load image: " + control.getLastError().getMessage());
            }
        } catch (IOException e) {
            throw new DBException("Error reading stream value", e);
        }
    } else {
        control.clearImage();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) InputStream(java.io.InputStream) IOException(java.io.IOException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 12 with DBException

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

the class ExasolDataSource method getErrorPosition.

@Nullable
@Override
public ErrorPosition[] getErrorPosition(@NotNull DBRProgressMonitor monitor, @NotNull DBCExecutionContext context, @NotNull String query, @NotNull Throwable error) {
    while (error instanceof DBException) {
        if (error.getCause() == null) {
            return null;
        }
        error = error.getCause();
    }
    String message = error.getMessage();
    if (!CommonUtils.isEmpty(message)) {
        Matcher matcher = ERROR_POSITION_PATTERN.matcher(message);
        List<ErrorPosition> positions = new ArrayList<>();
        while (matcher.find()) {
            DBPErrorAssistant.ErrorPosition pos = new DBPErrorAssistant.ErrorPosition();
            pos.info = matcher.group(1);
            pos.line = Integer.parseInt(matcher.group(2)) - 1;
            pos.position = Integer.parseInt(matcher.group(3)) - 1;
            positions.add(pos);
        }
        if (!positions.isEmpty()) {
            return positions.toArray(new ErrorPosition[positions.size()]);
        }
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) DBPErrorAssistant(org.jkiss.dbeaver.model.DBPErrorAssistant) Nullable(org.jkiss.code.Nullable)

Example 13 with DBException

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

the class ExasolDataSource method initialize.

// -----------------------
// Initialization/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.exasolCurrentUserPrivileges = new ExasolCurrentUserPrivileges(monitor, session, this);
        this.driverMajorVersion = session.getMetaData().getDriverMajorVersion();
    } catch (SQLException e) {
        LOG.warn("Error reading active schema", e);
    }
    String schemaSQL = "select b.object_name,b.owner,b.created,b.object_comment from EXA_ALL_OBJECTS b  " + "inner join EXA_SCHEMAS s on b.object_name = s.schema_name  where b.object_type = 'SCHEMA' ";
    if (exasolCurrentUserPrivileges.getatLeastV6()) {
        //additional where clause to filter virtual schemas
        schemaSQL += " and not schema_is_virtual ";
        //build virtual schema cache for >V6 databases
        virtualSchemaCache = new JDBCObjectSimpleCache<>(ExasolVirtualSchema.class, "select" + "	SCHEMA_NAME as OBJECT_NAME," + "	SCHEMA_OWNER AS OWNER," + "	ADAPTER_SCRIPT," + "	LAST_REFRESH," + "	LAST_REFRESH_BY," + "	ADAPTER_NOTES," + "	OBJECT_COMMENT," + "	CREATED" + " from" + "		EXA_VIRTUAL_SCHEMAS s" + "	INNER JOIN" + "		EXA_ALL_OBJECTS o" + "	ON" + "		o.OBJECT_NAME = s.SCHEMA_NAME AND" + "		o.OBJECT_TYPE = 'SCHEMA'");
    }
    schemaSQL += " union all select distinct SCHEMA_NAME as \"OBJECT_NAME\", 'SYS' as owner, cast(null as timestamp) as created, '' as \"OBJECT_COMMENT\" from SYS.EXA_SYSCAT " + "order by b.object_name";
    schemaCache = new JDBCObjectSimpleCache<>(ExasolSchema.class, schemaSQL);
    try {
        this.dataTypeCache.getAllObjects(monitor, this);
    } catch (DBException e) {
        LOG.warn("Error reading types info", e);
        this.dataTypeCache.setCache(Collections.<ExasolDataType>emptyList());
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForUsers()) {
        this.userCache = new JDBCObjectSimpleCache<>(ExasolUser.class, "select * from EXA_DBA_USERS ORDER BY USER_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForRoles()) {
        this.roleCache = new JDBCObjectSimpleCache<>(ExasolRole.class, "SELECT * FROM EXA_DBA_ROLES ORDER BY ROLE_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForConnections()) {
        this.connectionCache = new JDBCObjectSimpleCache<>(ExasolConnection.class, "SELECT * FROM EXA_DBA_CONNECTIONS ORDER BY CONNECTION_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForConnectionPrivs()) {
        this.connectionGrantCache = new JDBCObjectSimpleCache<>(ExasolConnectionGrant.class, "SELECT c.*,P.ADMIN_OPTION,P.GRANTEE FROM SYS.EXA_DBA_CONNECTION_PRIVS P " + "INNER JOIN SYS.EXA_DBA_CONNECTIONS C on P.GRANTED_CONNECTION = C.CONNECTION_NAME ORDER BY P.GRANTEE,C.CONNECTION_NAME ");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForObjectPrivs()) {
        this.baseTableGrantCache = new JDBCObjectSimpleCache<>(ExasolBaseObjectGrant.class, "SELECT " + "	OBJECT_SCHEMA," + "	OBJECT_TYPE," + "	GRANTEE," + "	OBJECT_NAME," + "	GROUP_CONCAT(" + "		DISTINCT PRIVILEGE" + "	ORDER BY" + "		OBJECT_SCHEMA," + "		OBJECT_NAME" + "		SEPARATOR '|'" + "	) as PRIVS " + " FROM" + "	SYS.EXA_DBA_OBJ_PRIVS P" + " GROUP BY" + "	OBJECT_SCHEMA," + "	OBJECT_TYPE," + "	GRANTEE," + "	OBJECT_NAME ORDER BY GRANTEE,OBJECT_SCHEMA,OBJECT_TYPE,OBJECT_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForSystemPrivs()) {
        this.systemGrantCache = new JDBCObjectSimpleCache<>(ExasolSystemGrant.class, "SELECT GRANTEE,PRIVILEGE,ADMIN_OPTION FROM SYS.EXA_DBA_SYS_PRIVS ORDER BY GRANTEE,PRIVILEGE");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForRolePrivs()) {
        this.roleGrantCache = new JDBCObjectSimpleCache<>(ExasolRoleGrant.class, "select r.*,p.ADMIN_OPTION,p.GRANTEE from EXA_DBA_ROLES r " + "INNER JOIN  EXA_DBA_ROLE_PRIVS p ON p.GRANTED_ROLE = r.ROLE_NAME ORDER BY P.GRANTEE,R.ROLE_NAME");
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) ExasolConnectionGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolConnectionGrant) SQLException(java.sql.SQLException) ExasolBaseObjectGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolBaseObjectGrant) ExasolSystemGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolSystemGrant) ExasolUser(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolUser) ExasolRole(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolRole) ExasolRoleGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolRoleGrant)

Example 14 with DBException

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

the class ExasolUtils method generateDDLforTable.

@SuppressWarnings("rawtypes")
public static String generateDDLforTable(DBRProgressMonitor monitor, ExasolDataSource dataSource, ExasolTable exasolTable) throws DBException {
    StringBuilder ddlOutput = new StringBuilder();
    ddlOutput.append("CREATE TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" (");
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Get Table DDL")) {
        try (JDBCStatement dbStat = session.createStatement()) {
            JDBCResultSet rs = dbStat.executeQuery(String.format(TABLE_QUERY_COLUMNS, quoteString(exasolTable.getSchema().getName()), quoteString(exasolTable.getName())));
            // column infos
            List<String> columns = new ArrayList<String>();
            // distribution key infos
            List<String> distKey = new ArrayList<String>();
            while (rs.next()) {
                StringBuilder columnString = new StringBuilder("");
                // double quotation mark for column as the name could be a
                // reserved word
                columnString.append("\n\t\t\"" + rs.getString("COLUMN_NAME") + "\" " + rs.getString("COLUMN_TYPE") + " ");
                // has default value?
                if (rs.getString("COLUMN_DEFAULT") != null)
                    columnString.append("DEFAULT " + rs.getString("COLUMN_DEFAULT") + " ");
                // has identity
                if (rs.getBigDecimal("COLUMN_IDENTITY") != null)
                    columnString.append("IDENTITY " + rs.getBigDecimal("COLUMN_IDENTITY").toString() + " ");
                // has identity
                if (!rs.getBoolean("COLUMN_IS_NULLABLE"))
                    columnString.append("NOT NULL ");
                // comment
                if (rs.getString("COLUMN_COMMENT") != null)
                    // replace ' to double ' -> escape for SQL
                    columnString.append("COMMENT IS '" + rs.getString("COLUMN_COMMENT").replaceAll("'", "''") + "'");
                // if distkey add column to distkey
                if (rs.getBoolean("COLUMN_IS_DISTRIBUTION_KEY"))
                    distKey.add(rs.getString("COLUMN_NAME"));
                columns.add(columnString.toString());
            }
            ddlOutput.append(CommonUtils.joinStrings(",", columns));
            // do we have a distkey?
            if (distKey.size() > 0) {
                ddlOutput.append(",\n\t\t DISTRIBUTE BY " + CommonUtils.joinStrings(",", distKey));
            }
            ddlOutput.append("\n);\n");
        }
        //primary key
        Collection<ExasolTableUniqueKey> pks = exasolTable.getConstraints(monitor);
        if (pks != null & pks.size() > 0) {
            //get only first as there is only 1 primary key
            ExasolTableUniqueKey pk = null;
            pk = pks.iterator().next();
            ArrayList<String> columns = new ArrayList<String>();
            for (DBSEntityAttributeRef c : pk.getAttributeReferences(monitor)) {
                columns.add("\"" + c.getAttribute().getName() + "\"");
            }
            ddlOutput.append("\nALTER TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" ADD CONSTRAINT " + pk.getName() + " PRIMARY KEY (" + CommonUtils.joinStrings(",", columns) + ") " + (pk.getEnabled() ? "ENABLE" : "") + " ;\n");
        }
        //foreign key
        Collection<ExasolTableForeignKey> fks = exasolTable.getAssociations(monitor);
        if (fks != null & fks.size() > 0) {
            //look keys
            for (ExasolTableForeignKey fk : fks) {
                ArrayList<String> columns = new ArrayList<String>();
                for (DBSEntityAttributeRef c : fk.getAttributeReferences(monitor)) {
                    columns.add("\"" + c.getAttribute().getName() + "\"");
                }
                ddlOutput.append("\nALTER TABLE \"" + exasolTable.getSchema().getName() + "\".\"" + exasolTable.getName() + "\" ADD CONSTRAINT " + fk.getName() + " FOREIGN KEY (" + CommonUtils.joinStrings(",", columns) + ") REFERENCES \"" + fk.getReferencedTable().getSchema().getName() + "\".\"" + fk.getReferencedTable().getName() + "\" " + (fk.getEnabled() ? "ENABLE" : "") + " ;\n");
            }
        }
        return ddlOutput.toString();
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    } finally {
        monitor.done();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 15 with DBException

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

the class ExasolServerSessionManager method alterSession.

@Override
public void alterSession(DBCSession session, ExasolServerSession sessionType, Map<String, Object> options) throws DBException {
    try {
        String cmd = String.format(Boolean.TRUE.equals(options.get(PROP_KILL_QUERY)) ? KILL_STMT_CMD : KILL_APP_CMD, sessionType.getSessionID().toString());
        PreparedStatement dbStat = ((JDBCSession) session).prepareStatement(cmd);
        dbStat.execute();
    } catch (SQLException e) {
        throw new DBException(e, session.getDataSource());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

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