Search in sources :

Example 76 with DBException

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

the class OracleDataSource 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) {
            break;
        }
        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(1)) - 1;
            pos.position = Integer.parseInt(matcher.group(2)) - 1;
            positions.add(pos);
        }
        if (!positions.isEmpty()) {
            return positions.toArray(new ErrorPosition[positions.size()]);
        }
    }
    if (error instanceof SQLException && SQLState.SQL_42000.getCode().equals(((SQLException) error).getSQLState())) {
        try (JDBCSession session = (JDBCSession) context.openSession(monitor, DBCExecutionPurpose.UTIL, "Extract last error position")) {
            try (CallableStatement stat = session.prepareCall("declare\n" + "  l_cursor integer default dbms_sql.open_cursor; \n" + "begin \n" + "  begin \n" + "  dbms_sql.parse(  l_cursor, ?, dbms_sql.native ); \n" + "    exception \n" + "      when others then ? := dbms_sql.last_error_position; \n" + "    end; \n" + "    dbms_sql.close_cursor( l_cursor );\n" + "end;")) {
                stat.setString(1, query);
                stat.registerOutParameter(2, Types.INTEGER);
                stat.execute();
                int errorPos = stat.getInt(2);
                if (errorPos <= 0) {
                    return null;
                }
                DBPErrorAssistant.ErrorPosition pos = new DBPErrorAssistant.ErrorPosition();
                pos.position = errorPos;
                return new ErrorPosition[] { pos };
            } catch (SQLException e) {
                // Something went wrong
                log.debug("Can't extract parse error info: " + e.getMessage());
            }
        }
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) Nullable(org.jkiss.code.Nullable)

Example 77 with DBException

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

the class OracleTablePhysical method getPartitionInfo.

@PropertyGroup
@LazyProperty(cacheValidator = PartitionInfoValidator.class)
public PartitionInfo getPartitionInfo(DBRProgressMonitor monitor) throws DBException {
    if (partitionInfo == null && partitioned) {
        try (final JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load partitioning info")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM ALL_PART_TABLES WHERE OWNER=? AND TABLE_NAME=?")) {
                dbStat.setString(1, getContainer().getName());
                dbStat.setString(2, getName());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    if (dbResult.next()) {
                        partitionInfo = new PartitionInfo(monitor, this.getDataSource(), dbResult);
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
    return partitionInfo;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 78 with DBException

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

the class FireBirdUtils method getProcedureSource.

public static String getProcedureSource(DBRProgressMonitor monitor, GenericProcedure procedure) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, procedure.getDataSource(), "Load procedure source code")) {
        DatabaseMetaData fbMetaData = session.getOriginal().getMetaData();
        String source = (String) fbMetaData.getClass().getMethod("getProcedureSourceCode", String.class).invoke(fbMetaData, procedure.getName());
        if (CommonUtils.isEmpty(source)) {
            return null;
        }
        return getProcedureSourceWithHeader(monitor, procedure, source);
    } catch (SQLException e) {
        throw new DBException("Can't read source code of procedure '" + procedure.getName() + "'", e);
    } catch (Exception e) {
        log.debug(e);
        return null;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 79 with DBException

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

the class SSHTunnelImpl method closeTunnel.

@Override
public void closeTunnel(DBRProgressMonitor monitor) throws DBException, IOException {
    if (session != null) {
        RuntimeUtils.runTask(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    session.disconnect();
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        }, "Close SSH session", 1000);
        session = null;
    }
}
Also used : DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Example 80 with DBException

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

the class SQLQueryTransformerCount method transformQuery.

@Override
public SQLQuery transformQuery(SQLDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getQuery());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            List<SelectItem> selectItems = new ArrayList<>();
            Function countFunc = new Function();
            countFunc.setName("count");
            countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
            SelectItem countItem = new SelectExpressionItem(countFunc);
            selectItems.add(countItem);
            select.setSelectItems(selectItems);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getQuery() + "] can't be modified");
        }
    } catch (JSQLParserException e) {
        throw new DBException("Can't transform query to SELECT count(*)", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

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