Search in sources :

Example 1 with SQLFormatter

use of org.jkiss.dbeaver.model.sql.format.SQLFormatter in project dbeaver by dbeaver.

the class SQLUtils method formatSQL.

public static String formatSQL(SQLDataSource dataSource, String query) {
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    syntaxManager.init(dataSource.getSQLDialect(), dataSource.getContainer().getPreferenceStore());
    SQLFormatterConfiguration configuration = new SQLFormatterConfiguration(syntaxManager);
    SQLFormatter formatter = dataSource.getDataSource().getContainer().getPlatform().getSQLFormatterRegistry().createFormatter(configuration);
    if (formatter == null) {
        return query;
    }
    return formatter.format(query, configuration);
}
Also used : SQLFormatter(org.jkiss.dbeaver.model.sql.format.SQLFormatter) SQLFormatterConfiguration(org.jkiss.dbeaver.model.sql.format.SQLFormatterConfiguration)

Example 2 with SQLFormatter

use of org.jkiss.dbeaver.model.sql.format.SQLFormatter in project dbeaver by serge-rider.

the class PrefPageSQLFormat method showFormatterSettings.

private void showFormatterSettings() {
    if (curConfigurator != null) {
        curConfigurator.saveSettings(getTargetPreferenceStore());
    }
    UIUtils.disposeChildControls(formatterConfigPlaceholder);
    SQLFormatterDescriptor selFormatter = formatters.get(formatterSelector.getSelectionIndex());
    try {
        SQLFormatter sqlFormatter = selFormatter.createFormatter();
        // FIXME: for now we support only predefined list of formatters
        if (sqlFormatter instanceof SQLFormatterTokenized) {
            curConfigurator = new SQLTokenizedFormatterConfigurationPage();
        } else if (sqlFormatter instanceof SQLFormatterExternal) {
            curConfigurator = new SQLExternalFormatterConfigurationPage();
        } else {
            curConfigurator = GeneralUtils.adapt(sqlFormatter, SQLFormatterConfigurator.class);
        }
        if (curConfigurator instanceof IDialogPage) {
            curConfigurator.configure(selFormatter, () -> {
                curConfigurator.saveSettings(getTargetPreferenceStore());
                formatSQL();
            });
            ((IDialogPage) curConfigurator).createControl(formatterConfigPlaceholder);
            curConfigurator.loadSettings(getTargetPreferenceStore());
        }
    } catch (DBException e) {
        log.error("Error creating formatter configurator", e);
        setMessage(CommonUtils.toString(e.getMessage()), SWT.ICON_ERROR);
        return;
    }
    ((Composite) getControl()).layout(true, true);
    if (isDataSourcePreferencePage()) {
        enablePreferenceContent(useDataSourceSettings());
    }
}
Also used : SQLFormatterDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLFormatterDescriptor) DBException(org.jkiss.dbeaver.DBException) SQLFormatter(org.jkiss.dbeaver.model.sql.format.SQLFormatter) SQLFormatterTokenized(org.jkiss.dbeaver.model.sql.format.tokenized.SQLFormatterTokenized) Composite(org.eclipse.swt.widgets.Composite) SQLFormatterExternal(org.jkiss.dbeaver.model.sql.format.external.SQLFormatterExternal) SQLExternalFormatterConfigurationPage(org.jkiss.dbeaver.ui.editors.sql.preferences.format.SQLExternalFormatterConfigurationPage) IDialogPage(org.eclipse.jface.dialogs.IDialogPage) SQLTokenizedFormatterConfigurationPage(org.jkiss.dbeaver.ui.editors.sql.preferences.format.tokenized.SQLTokenizedFormatterConfigurationPage)

Example 3 with SQLFormatter

use of org.jkiss.dbeaver.model.sql.format.SQLFormatter in project dbeaver by dbeaver.

the class SQLFormatterConfigurationRegistry method createAndConfigureFormatter.

@Override
@Nullable
public SQLFormatter createAndConfigureFormatter(SQLFormatterConfiguration configuration) {
    final String formatterId = configuration.getFormatterId();
    SQLFormatterDescriptor formatterDesc = getFormatter(formatterId);
    if (formatterDesc == null) {
        log.error("Formatter '" + formatterId + "' not found");
        return null;
    }
    try {
        SQLFormatter formatter = formatterDesc.createFormatter();
        SQLFormatterConfigurer configurer = formatterDesc.createConfigurer();
        if (configurer != null) {
            if (!configurer.configure(formatterDesc.getLabel(), formatter, configuration)) {
                return null;
            }
        }
        return formatter;
    } catch (DBException e) {
        log.error("Error creating and configuring formatter", e);
        return null;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLFormatter(org.jkiss.dbeaver.model.sql.format.SQLFormatter) Nullable(org.jkiss.code.Nullable)

Example 4 with SQLFormatter

use of org.jkiss.dbeaver.model.sql.format.SQLFormatter in project dbeaver by dbeaver.

the class SQLFormattingStrategy method format.

@Override
public String format(String content, boolean isLineStart, String indentation, int[] positions) {
    final String[] indentPrefixes = svConfig.getIndentPrefixes(sourceViewer, IDocument.DEFAULT_CONTENT_TYPE);
    SQLFormatterConfiguration configuration = new SQLFormatterConfiguration(sqlSyntax);
    configuration.setIndentString(indentPrefixes[0]);
    SQLFormatter formatter = SQLFormatterConfigurationRegistry.getInstance().createFormatter(configuration);
    if (formatter == null) {
        return content;
    }
    return formatter.format(content, configuration);
}
Also used : SQLFormatter(org.jkiss.dbeaver.model.sql.format.SQLFormatter) SQLFormatterConfiguration(org.jkiss.dbeaver.model.sql.format.SQLFormatterConfiguration)

Example 5 with SQLFormatter

use of org.jkiss.dbeaver.model.sql.format.SQLFormatter in project dbeaver by serge-rider.

the class SQLFormattingStrategy method format.

@Override
public String format(String content, boolean isLineStart, String indentation, int[] positions) {
    final String[] indentPrefixes = svConfig.getIndentPrefixes(sourceViewer, IDocument.DEFAULT_CONTENT_TYPE);
    SQLFormatterConfiguration configuration = new SQLFormatterConfiguration(svConfig.getSQLEditor().getDataSource(), sqlSyntax);
    configuration.setIndentString(indentPrefixes[0]);
    SQLFormatter formatter = SQLFormatterConfigurationRegistry.getInstance().createFormatter(configuration);
    if (formatter == null) {
        return content;
    }
    return formatter.format(content, configuration);
}
Also used : SQLFormatter(org.jkiss.dbeaver.model.sql.format.SQLFormatter) SQLFormatterConfiguration(org.jkiss.dbeaver.model.sql.format.SQLFormatterConfiguration)

Aggregations

SQLFormatter (org.jkiss.dbeaver.model.sql.format.SQLFormatter)5 SQLFormatterConfiguration (org.jkiss.dbeaver.model.sql.format.SQLFormatterConfiguration)3 DBException (org.jkiss.dbeaver.DBException)2 IDialogPage (org.eclipse.jface.dialogs.IDialogPage)1 Composite (org.eclipse.swt.widgets.Composite)1 Nullable (org.jkiss.code.Nullable)1 SQLFormatterExternal (org.jkiss.dbeaver.model.sql.format.external.SQLFormatterExternal)1 SQLFormatterTokenized (org.jkiss.dbeaver.model.sql.format.tokenized.SQLFormatterTokenized)1 SQLFormatterDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLFormatterDescriptor)1 SQLExternalFormatterConfigurationPage (org.jkiss.dbeaver.ui.editors.sql.preferences.format.SQLExternalFormatterConfigurationPage)1 SQLTokenizedFormatterConfigurationPage (org.jkiss.dbeaver.ui.editors.sql.preferences.format.tokenized.SQLTokenizedFormatterConfigurationPage)1