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);
}
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());
}
}
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());
}
}
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());
}
}
}
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;
}
});
}
}
}
}
Aggregations