1 /*
2 * Copyright (c) 2021 Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 * Contributors:
8 * bahlef - initial API and implementation and/or initial documentation
9 */
10 package de.funfried.netbeans.plugins.external.formatter.sql.dbeaver;
11
12 import java.util.Properties;
13
14 import org.netbeans.api.annotations.common.CheckForNull;
15
16 import com.diffplug.spotless.sql.dbeaver.DBeaverSQLFormatterConfiguration;
17 import com.diffplug.spotless.sql.dbeaver.SQLTokenizedFormatter;
18
19 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
20
21 /**
22 * Delegation class to the DBeaver SQL formatter implementation.
23 *
24 * @author bahlef
25 */
26 public final class DBeaverFormatterWrapper {
27 /**
28 * Package private Constructor for creating a new instance of {@link DBeaverFormatterWrapper}.
29 */
30 DBeaverFormatterWrapper() {
31 }
32
33 /**
34 * Formats the given {@code code} with the given configurations and returns
35 * the formatted code.
36 *
37 * @param code the unformatted SQL code
38 * @param properties Formatting Options as {@link Properties}
39 *
40 * @return the formatted SQL code
41 *
42 * @throws FormattingFailedException if the external formatter failed to format the given code
43 */
44 @CheckForNull
45 public String format(String code, Properties properties) throws FormattingFailedException {
46 if (code == null) {
47 return null;
48 }
49
50 try {
51 DBeaverSQLFormatterConfiguration configuration = new DBeaverSQLFormatterConfiguration(properties);
52 SQLTokenizedFormatter sqlTokenizedFormatter = new SQLTokenizedFormatter(configuration);
53 return sqlTokenizedFormatter.format(code);
54 } catch (Exception ex) {
55 throw new FormattingFailedException(ex);
56 }
57 }
58 }