tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
3 * Copyright © 2015 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.configuration;
19 import info.globalcode.sql.dk.*;
20 import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER;
21 import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD;
22 import java.sql.Connection;
23 import java.sql.Driver;
24 import java.sql.DriverManager;
25 import java.sql.SQLException;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Unmarshaller;
32 * Configuration loader – deserializes Configuration from the XML file.
34 * @author Ing. František Kučera (frantovo.cz)
38 private static final Logger log = Logger.getLogger(Loader.class.getName());
40 public Configuration loadConfiguration() throws ConfigurationException {
42 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class.getPackage().getName(), Configuration.class.getClassLoader());
43 Unmarshaller u = jaxb.createUnmarshaller();
44 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
45 } catch (Exception e) {
46 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
51 * JDBC connection should not be used directly in SQL-DK.
53 * @see DatabaseDefinition#connect(info.globalcode.sql.dk.configuration.Properties)
55 * @param databaseDefinition
57 * @throws java.sql.SQLException
59 public static Connection jdbcConnect(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
60 synchronized (properties) {
62 * Avoid rewriting the properties. Usually, the connection is created only once, but
63 * with --test-connection and with SQL-DK JDBC driver, it might be reused.
65 properties = properties.clone();
67 if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
68 log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
70 Properties credentials = new Properties();
71 credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
72 credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
73 credentials.setDefaults(databaseDefinition.getProperties());
74 properties.setDefaults(credentials);
75 java.util.Properties javaProperties = properties.getJavaProperties();
77 String driverClassName = databaseDefinition.getDriver();
78 final String url = databaseDefinition.getUrl();
79 if (driverClassName == null) {
80 log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url);
81 return DriverManager.getConnection(url, javaProperties);
83 log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url});
85 Class<Driver> driverClass = (Class<Driver>) Class.forName(driverClassName);
86 Driver driver = driverClass.newInstance();
87 Connection connection = driver.connect(url, javaProperties);
88 if (connection == null) {
89 log.log(Level.SEVERE, "Driver „{0}“ returend null → it does not accept the URL: „{1}“", new Object[]{driverClassName, url});
90 throw new SQLException("Unable to connect: driver returned null.");
94 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
95 throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e);