Skip to content

Commit

Permalink
#16433 Split server output based on its severity (#18696)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jan 18, 2023
1 parent 6a0647a commit a687624
Show file tree
Hide file tree
Showing 21 changed files with 596 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.jdbc.*;
import org.jkiss.dbeaver.model.exec.output.DBCServerOutputReader;
import org.jkiss.dbeaver.model.exec.output.DBCOutputWriter;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.impl.jdbc.*;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache;
Expand All @@ -54,7 +56,6 @@
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.StandardConstants;

import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -872,7 +873,13 @@ public void enableServerOutput(DBRProgressMonitor monitor, DBCExecutionContext c
}

@Override
public void readServerOutput(@NotNull DBRProgressMonitor monitor, @NotNull DBCExecutionContext context, @Nullable DBCExecutionResult executionResult, @Nullable DBCStatement statement, @NotNull PrintWriter output) throws DBCException {
public void readServerOutput(
@NotNull DBRProgressMonitor monitor,
@NotNull DBCExecutionContext context,
@Nullable DBCExecutionResult executionResult,
@Nullable DBCStatement statement,
@NotNull DBCOutputWriter output
) throws DBCException {
try (JDBCSession session = (JDBCSession) context.openSession(monitor, DBCExecutionPurpose.UTIL, "Read DBMS output")) {
try (CallableStatement getLineProc = session.getOriginal().prepareCall("{CALL DBMS_OUTPUT.GET_LINE(?, ?)}")) {
getLineProc.registerOutParameter(1, java.sql.Types.VARCHAR);
Expand All @@ -882,11 +889,7 @@ public void readServerOutput(@NotNull DBRProgressMonitor monitor, @NotNull DBCEx
getLineProc.execute();
status = getLineProc.getInt(2);
if (status == 0) {
String str = getLineProc.getString(1);
if (str != null) {
output.write(str);
}
output.write('\n');
output.println(null, getLineProc.getString(1));
}
}
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class PostgreConstants {
public static final String PUBLIC_SCHEMA_NAME = "public";

// Settings names from 'pg_options' view
public static final String OPTION_CLIENT_MIN_MESSAGES = "client_min_messages";
public static final String OPTION_STANDARD_CONFORMING_STRINGS = "standard_conforming_strings";

public static final String PG_OBJECT_CLASS = "org.postgresql.util.PGobject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.jdbc.*;
import org.jkiss.dbeaver.model.exec.output.DBCServerOutputReader;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.impl.AsyncServerOutputReader;
import org.jkiss.dbeaver.model.impl.app.DefaultCertificateStorage;
import org.jkiss.dbeaver.model.impl.jdbc.*;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectLookupCache;
Expand Down Expand Up @@ -159,7 +159,7 @@ protected void initializeRemoteInstance(@NotNull DBRProgressMonitor monitor) thr
getDefaultInstance().checkInstanceConnection(monitor, false);
try {
// Preload some settings, if available
settingCache.getObject(monitor, this, PostgreConstants.OPTION_STANDARD_CONFORMING_STRINGS);
settingCache.getAllObjects(monitor, this);
} catch (DBException e) {
// ignore
}
Expand Down Expand Up @@ -556,7 +556,7 @@ public <T> T getAdapter(Class<T> adapter)
if (adapter == DBSStructureAssistant.class) {
return adapter.cast(new PostgreStructureAssistant(this));
} else if (adapter == DBCServerOutputReader.class) {
return adapter.cast(new AsyncServerOutputReader());
return adapter.cast(new PostgreServerOutputReader());
} else if (adapter == DBAServerSessionManager.class) {
return adapter.cast(new PostgreSessionManager(this));
} else if (adapter == DBCQueryPlanner.class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.postgresql.model;

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.output.DBCServerOutputReaderExt;
import org.jkiss.dbeaver.model.exec.output.DBCOutputSeverity;
import org.jkiss.dbeaver.model.exec.output.DBCOutputWriter;
import org.jkiss.dbeaver.model.impl.AsyncServerOutputReader;
import org.jkiss.utils.BeanUtils;

import java.util.Arrays;

public class PostgreServerOutputReader extends AsyncServerOutputReader implements DBCServerOutputReaderExt {
private static final String PSQL_WARNING_CLASS = "org.postgresql.util.PSQLWarning";
private static final String PSQL_WARNING_GET_SERVER_ERROR_MESSAGE_METHOD = "getServerErrorMessage";
private static final String SERVER_ERROR_MESSAGE_GET_SEVERITY_METHOD = "getSeverity";

@NotNull
@Override
public DBCOutputSeverity[] getSupportedSeverities(@NotNull DBCExecutionContext context) {
final PostgreDataSource dataSource = (PostgreDataSource) context.getDataSource();
final PostgreSetting setting = dataSource.getSetting(PostgreConstants.OPTION_CLIENT_MIN_MESSAGES);
final PostgreOutputSeverity[] values = PostgreOutputSeverity.values();

if (setting != null) {
for (int i = 0; i < values.length; i++) {
if (values[i].name().equalsIgnoreCase(setting.getValue())) {
return Arrays.copyOfRange(values, i, values.length);
}
}
}

return values;
}

@Override
protected void dumpWarning(@NotNull DBCOutputWriter output, @NotNull Throwable warning) {
output.println(getSeverity(warning), warning.getMessage());
}

@Nullable
private static DBCOutputSeverity getSeverity(@NotNull Throwable warning) {
if (!PSQL_WARNING_CLASS.equals(warning.getClass().getName())) {
return null;
}
try {
final Object obj = BeanUtils.invokeObjectMethod(warning, PSQL_WARNING_GET_SERVER_ERROR_MESSAGE_METHOD);
final String severity = (String) BeanUtils.invokeObjectMethod(obj, SERVER_ERROR_MESSAGE_GET_SEVERITY_METHOD);
return PostgreOutputSeverity.valueOf(severity);
} catch (Throwable ignored) {
return null;
}
}


private enum PostgreOutputSeverity implements DBCOutputSeverity {
DEBUG("Debug"),
LOG("Log"),
NOTICE("Notice"),
WARNING("Warning"),
ERROR("Error"),
INFO("Info");

private final String name;

PostgreOutputSeverity(@NotNull String name) {
this.name = name;
}

@NotNull
@Override
public String getName() {
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBCScriptContext;
import org.jkiss.dbeaver.model.exec.DBCScriptContextListener;
import org.jkiss.dbeaver.model.exec.output.DBCOutputWriter;
import org.jkiss.dbeaver.model.impl.OutputWriterAdapter;
import org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor;
import org.jkiss.dbeaver.model.sql.registry.SQLCommandsRegistry;
import org.jkiss.dbeaver.model.sql.registry.SQLQueryParameterRegistry;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class SQLScriptContext implements DBCScriptContext {
@Nullable
private final File sourceFile;
@NotNull
private final PrintWriter outputWriter;
private final DBCOutputWriter outputWriter;

private SQLParametersProvider parametersProvider;
private boolean ignoreParameters;
Expand All @@ -70,10 +72,20 @@ public SQLScriptContext(
@NotNull Writer outputWriter,
@Nullable SQLParametersProvider parametersProvider)
{
this(parentContext, contextProvider, sourceFile, new OutputWriterAdapter(new PrintWriter(outputWriter)), parametersProvider);
}

public SQLScriptContext(
@Nullable SQLScriptContext parentContext,
@NotNull DBPContextProvider contextProvider,
@Nullable File sourceFile,
@NotNull DBCOutputWriter outputWriter,
@Nullable SQLParametersProvider parametersProvider
) {
this.parentContext = parentContext;
this.contextProvider = contextProvider;
this.sourceFile = sourceFile;
this.outputWriter = new PrintWriter(outputWriter);
this.outputWriter = outputWriter;
this.parametersProvider = parametersProvider;
}

Expand Down Expand Up @@ -215,7 +227,7 @@ public void setData(String key, Object value) {

@Override
@NotNull
public PrintWriter getOutputWriter() {
public DBCOutputWriter getOutputWriter() {
return outputWriter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public boolean handleCommand(SQLControlCommand command, SQLScriptContext scriptC
if (parameter != null) {
parameter = GeneralUtils.replaceVariables(parameter, new ScriptVariablesResolver(scriptContext));
}
scriptContext.getOutputWriter().println(parameter);
scriptContext.getOutputWriter().println(null, parameter);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, lo
Throwable[] warnings = statement.getStatementWarnings();
if (warnings != null) {
for (Throwable warning : warnings) {
scriptContext.getOutputWriter().println(warning.getMessage());
scriptContext.getOutputWriter().println(null, warning.getMessage());
}
}
} catch (Throwable e) {
Expand Down
1 change: 1 addition & 0 deletions plugins/org.jkiss.dbeaver.model/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Export-Package: org.jkiss.dbeaver,
org.jkiss.dbeaver.model.exec,
org.jkiss.dbeaver.model.exec.compile,
org.jkiss.dbeaver.model.exec.jdbc,
org.jkiss.dbeaver.model.exec.output,
org.jkiss.dbeaver.model.exec.plan,
org.jkiss.dbeaver.model.exec.trace,
org.jkiss.dbeaver.model.fs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.jkiss.dbeaver.model.exec;

import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.exec.output.DBCOutputWriter;

import java.io.PrintWriter;
import java.util.List;

Expand Down Expand Up @@ -69,7 +72,8 @@ public VariableInfo(String name, Object value, VariableType type) {

void setData(String key, Object value);

PrintWriter getOutputWriter();
@NotNull
DBCOutputWriter getOutputWriter();

void addListener(DBCScriptContextListener listener);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.model.exec.output;

import org.jkiss.dbeaver.model.DBPNamedObject;

public interface DBCOutputSeverity extends DBPNamedObject {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.model.exec.output;

import org.jkiss.code.Nullable;

public interface DBCOutputWriter {
void println(@Nullable DBCOutputSeverity severity, @Nullable String message);

void flush();
}
Loading

0 comments on commit a687624

Please sign in to comment.