Search in sources :

Example 66 with DBException

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

the class PostgreStructValueHandler method convertStringToStruct.

private JDBCCompositeStatic convertStringToStruct(@NotNull DBCSession session, @NotNull PostgreDataType compType, @NotNull String value) throws DBException {
    if (value.startsWith("(") && value.endsWith(")")) {
        value = value.substring(1, value.length() - 1);
    }
    final Collection<PostgreDataTypeAttribute> attributes = compType.getAttributes(session.getProgressMonitor());
    if (attributes == null) {
        throw new DBException("Composite type '" + compType.getTypeName() + "' has no attributes");
    }
    String[] parsedValues = PostgreUtils.parseObjectString(value);
    if (parsedValues.length != attributes.size()) {
        log.debug("Number o attributes (" + attributes.size() + ") doesn't match actual number of parsed strings (" + parsedValues.length + ")");
    }
    Object[] attrValues = new Object[attributes.size()];
    Iterator<PostgreDataTypeAttribute> attrIter = attributes.iterator();
    for (int i = 0; i < parsedValues.length && attrIter.hasNext(); i++) {
        final PostgreDataTypeAttribute itemAttr = attrIter.next();
        attrValues[i] = PostgreUtils.convertStringToValue(itemAttr, parsedValues[i], true);
    }
    Struct contents = new JDBCStructImpl(compType.getTypeName(), attrValues);
    return new JDBCCompositeStatic(session, compType, contents);
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) PostgreDataTypeAttribute(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataTypeAttribute) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) Struct(java.sql.Struct)

Example 67 with DBException

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

the class PostgreMetaModel method loadTriggers.

@Override
public List<PostgreGenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT trigger_name,event_manipulation,action_order,action_condition,action_statement,action_orientation,action_timing\n" + "FROM INFORMATION_SCHEMA.TRIGGERS\n" + "WHERE ");
        if (table == null) {
            sql.append("trigger_schema=? AND event_object_table IS NULL");
        } else {
            sql.append("event_object_schema=? AND event_object_table=?");
        }
        try (JDBCPreparedStatement dbStat = session.prepareStatement(sql.toString())) {
            if (table == null) {
                dbStat.setString(1, container.getSchema().getName());
            } else {
                dbStat.setString(1, table.getSchema().getName());
                dbStat.setString(2, table.getName());
            }
            Map<String, PostgreGenericTrigger> result = new LinkedHashMap<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, "trigger_name");
                    if (name == null) {
                        continue;
                    }
                    String manipulation = JDBCUtils.safeGetString(dbResult, "event_manipulation");
                    PostgreGenericTrigger trigger = result.get(name);
                    if (trigger != null) {
                        trigger.addManipulation(manipulation);
                        continue;
                    }
                    String description = "";
                    trigger = new PostgreGenericTrigger(container, table, name, description, manipulation, JDBCUtils.safeGetString(dbResult, "action_orientation"), JDBCUtils.safeGetString(dbResult, "action_timing"), JDBCUtils.safeGetString(dbResult, "action_statement"));
                    result.put(name, trigger);
                }
            }
            return new ArrayList<>(result.values());
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
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) ArrayList(java.util.ArrayList) PostgreGenericTrigger(org.jkiss.dbeaver.ext.postgresql.model.PostgreGenericTrigger) LinkedHashMap(java.util.LinkedHashMap)

Example 68 with DBException

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

the class PostgreSessionManager method alterSession.

@Override
public void alterSession(DBCSession session, PostgreSession sessionType, Map<String, Object> options) throws DBException {
    try {
        try (JDBCPreparedStatement dbStat = ((JDBCSession) session).prepareStatement("SELECT pg_catalog.pg_terminate_backend(?)")) {
            dbStat.setInt(1, sessionType.getPid());
            dbStat.execute();
        }
    } catch (SQLException e) {
        throw new DBException(e, session.getDataSource());
    }
}
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)

Example 69 with DBException

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

the class PostgreRole method getPermissions.

@Override
public Collection<PostgrePermission> getPermissions(DBRProgressMonitor monitor) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read role privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM information_schema.table_privileges WHERE table_catalog=? AND grantee=?")) {
            dbStat.setString(1, getDatabase().getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                Map<String, List<PostgrePrivilege>> privs = new LinkedHashMap<>();
                while (dbResult.next()) {
                    PostgrePrivilege privilege = new PostgrePrivilege(dbResult);
                    String tableId = privilege.getTableSchema() + "." + privilege.getTableName();
                    List<PostgrePrivilege> privList = privs.get(tableId);
                    if (privList == null) {
                        privList = new ArrayList<>();
                        privs.put(tableId, privList);
                    }
                    privList.add(privilege);
                }
                // Pack to permission list
                List<PostgrePermission> result = new ArrayList<>(privs.size());
                for (List<PostgrePrivilege> priv : privs.values()) {
                    result.add(new PostgreRolePermission(this, priv.get(0).getTableSchema(), priv.get(0).getTableName(), priv));
                }
                Collections.sort(result);
                return result;
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 70 with DBException

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

the class PostgreStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String tableNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.oid,x.relname,x.relnamespace,x.relkind FROM pg_catalog.pg_class x " + "WHERE x.relkind in('r','v','m') AND x.relname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.relname LIMIT " + maxResults)) {
        dbStat.setString(1, tableNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                final long tableId = JDBCUtils.safeGetLong(dbResult, "oid");
                final String tableName = JDBCUtils.safeGetString(dbResult, "relname");
                final PostgreClass.RelKind tableType = PostgreClass.RelKind.valueOf(JDBCUtils.safeGetString(dbResult, "relkind"));
                final PostgreSchema tableSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(tableName, tableSchema, null, tableType == PostgreClass.RelKind.r ? PostgreTable.class : (tableType == PostgreClass.RelKind.v ? PostgreView.class : PostgreMaterializedView.class), RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        PostgreTableBase table = tableSchema.getTable(monitor, tableId);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in schema '" + tableSchema.getName() + "'");
                        }
                        return table;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

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