View Javadoc
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.sqlformatter;
11  
12  import org.netbeans.api.annotations.common.CheckForNull;
13  
14  import com.github.vertical_blank.sqlformatter.SqlFormatter;
15  import com.github.vertical_blank.sqlformatter.core.FormatConfig;
16  import com.github.vertical_blank.sqlformatter.languages.Dialect;
17  
18  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
19  
20  /**
21   * Delegation class to the Vertical Blank SQL formatter implementation.
22   *
23   * @author bahlef
24   */
25  public final class SQLFormatterWrapper {
26  	/**
27  	 * Package private Constructor for creating a new instance of {@link SQLFormatterWrapper}.
28  	 */
29  	SQLFormatterWrapper() {
30  	}
31  
32  	/**
33  	 * Formats the given {@code code} with the given configurations and returns
34  	 * the formatted code.
35  	 *
36  	 * @param code the unformatted SQL code
37  	 * @param dialect the {@link Dialect} to use
38  	 * @param formatConfig Formatting options as a {@link FormatConfig} object
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, Dialect dialect, FormatConfig formatConfig) throws FormattingFailedException {
46  		if (code == null) {
47  			return null;
48  		}
49  
50  		try {
51  			return SqlFormatter.of(dialect).format(code, formatConfig);
52  		} catch (Exception ex) {
53  			throw new FormattingFailedException(ex);
54  		}
55  	}
56  }