/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-log.2018 Serge Rider (serge@jkiss.org)
 *
 * 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.format.sqlworkbenchj;

import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.utils.CommonUtils;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * External SQL formatter
 */
public class SQLWorkbenchJManager {

    private static final Log log = Log.getLog(SQLWorkbenchJManager.class);

    private static SQLWorkbenchJManager instance;
    private final File workbenchPath;
    private final URLClassLoader wbClassLoader;

    public static SQLWorkbenchJManager getInstance() {
        return instance;
    }

    static void initManager(File path) throws DBException {
        if (instance == null || !instance.workbenchPath.equals(path)) {
            initializeManager(path);
        }
    }

    public static void initializeManager(File path) throws DBException {
        instance = new SQLWorkbenchJManager(path);
    }

    public SQLWorkbenchJManager(File wbPath) throws DBException {
        this.workbenchPath = wbPath;
        File wbJar = new File(workbenchPath, "sqlworkbench.jar");
        if (!wbJar.exists()) {
            throw new DBException("SQL Workbench/J jar file not found: " + wbJar.getAbsolutePath());
        }
        try {

            wbClassLoader = new URLClassLoader(new URL[] { wbJar.toURI().toURL() });
            Class<?> wbManagerClass = wbClassLoader.loadClass("workbench.WbManager");

            wbManagerClass.getMethod("initConsoleMode").invoke(null);
        } catch (Exception e) {
            throw new DBException("Error initializing SQL Workbench/J manager", e);
        }
    }

    public String format(DBPDataSource dataSource, String source) throws DBException {
        try {
            Class<?> wbFormatterClass = wbClassLoader.loadClass("workbench.sql.formatter.WbSqlFormatter");

            String driverClassName = dataSource.getContainer().getDriver().getDriverClassName();
            String formatType = "mysql";
            if (driverClassName.contains("mysql")) {
                formatType = "mysql";
            } else if (driverClassName.contains("postgresql")) {
                formatType = "postgresql";
            } else if (driverClassName.contains("oracle")) {
                formatType = "oracle";
            } else if (driverClassName.contains("db2")) {
                formatType = "db2";
            }
            Object wbFormatterInstance = wbFormatterClass.getConstructor(CharSequence.class, String.class).newInstance(source, formatType);
            Object formatResult = wbFormatterClass.getMethod("getFormattedSql").invoke(wbFormatterInstance);
            if (formatResult != null) {
                return CommonUtils.toString(formatResult);
            }

            return source;
        } catch (Exception e) {
            throw new DBException("Error calling SQL Workbench/J formatter", e);
        }
    }

}
@@ -1084,16 +1084,269 @@
 .class);
+%0A    private static final String MYSQL_FORMAT_TYPE = %22mysql%22;%0A    private static final String POSTGRESQL_FORMAT_TYPE = %22postgresql%22;%0A    private static final String ORACLE_FORMAT_TYPE = %22oracle%22;%0A    private static final String DB_2_FORMAT_TYPE = %22db2%22;
 %0A%0A    pr
@@ -2813,1046 +2813,1275 @@
 ing 
-driverClassName = dataSource.getContainer().getDriver().getDriverClassName();%0A            String formatType = %22mysql%22;%0A            if (driverClassName.contains(%22mysql%22)) %7B%0A                formatType = %22mysql%22;%0A            %7D else if (driverClassName.contains(%22postgresql%22)) %7B%0A                formatType = %22postgresql%22;%0A            %7D else if (driverClassName.contains(%22oracle%22)) %7B%0A                formatType = %22oracle%22;%0A            %7D else if (driverClassName.contains(%22db2%22)) %7B%0A                formatType = %22db2%22;%0A            %7D%0A            Object wbFormatterInstance = wbFormatterClass.getConstructor(CharSequence.class, String.class).newInstance(source, formatType);%0A            Object formatResult = wbFormatterClass.getMethod(%22getFormattedSql%22).invoke(wbFormatterInstance);%0A            if (formatResult != null) %7B%0A                return CommonUtils.toString(formatResult);%0A            %7D%0A%0A            return source;%0A        %7D catch (Exception e) %7B%0A            throw new DBException(%22Error calling SQL Workbench/J formatter%22, e);%0A        %7D
+formatType = getFormatType(dataSource);%0A            Object wbFormatterInstance = wbFormatterClass.getConstructor(CharSequence.class, String.class).newInstance(source, formatType);%0A            Object formatResult = wbFormatterClass.getMethod(%22getFormattedSql%22).invoke(wbFormatterInstance);%0A            if (formatResult != null) %7B%0A                return CommonUtils.toString(formatResult);%0A            %7D%0A%0A            return source;%0A        %7D catch (Exception e) %7B%0A            throw new DBException(%22Error calling SQL Workbench/J formatter%22, e);%0A        %7D%0A    %7D%0A%0A    private String getFormatType(DBPDataSource dataSource) %7B%0A        String defaultFormatType = MYSQL_FORMAT_TYPE;%0A        if (dataSource == null)%7B%0A            return defaultFormatType;%0A        %7D%0A%0A        String driverClassName = dataSource.getContainer().getDriver().getDriverClassName();%0A        String formatType = defaultFormatType;%0A%0A        if (driverClassName.contains(POSTGRESQL_FORMAT_TYPE)) %7B%0A            formatType = POSTGRESQL_FORMAT_TYPE;%0A        %7D else if (driverClassName.contains(ORACLE_FORMAT_TYPE)) %7B%0A            formatType = ORACLE_FORMAT_TYPE;%0A        %7D else if (driverClassName.contains(DB_2_FORMAT_TYPE)) %7B%0A            formatType = DB_2_FORMAT_TYPE;%0A        %7D%0A        return formatType;
 %0A   

Use MySQL formatter type for WBFormatter, when there is not configured connections.