3 * Copyright © 2013 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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.batch.Batch;
21 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
22 import info.globalcode.sql.dk.configuration.Properties;
23 import info.globalcode.sql.dk.configuration.Property;
24 import info.globalcode.sql.dk.formatting.ColumnsHeader;
25 import info.globalcode.sql.dk.formatting.Formatter;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.SQLWarning;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
37 * @author Ing. František Kučera (frantovo.cz)
39 public class DatabaseConnection implements AutoCloseable {
41 private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
42 private static final String JDBC_PROPERTY_USER = "user";
43 public static final String JDBC_PROPERTY_PASSWORD = "password";
44 private DatabaseDefinition databaseDefinition;
45 private Connection connection;
46 private Properties properties;
48 public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
49 this.databaseDefinition = databaseDefinition;
50 this.properties = properties;
52 if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
53 log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
56 Properties credentials = new Properties();
57 credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
58 credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
59 credentials.setDefaults(databaseDefinition.getProperties());
60 properties.setDefaults(credentials);
61 java.util.Properties javaProperties = properties.getJavaProperties();
63 connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
66 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
67 formatter.writeStartBatch();
68 formatter.writeStartDatabase(databaseDefinition);
69 processCommand(sqlCommand, formatter);
70 formatter.writeEndDatabase();
71 formatter.writeEndBatch();
74 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
75 formatter.writeStartBatch();
76 formatter.writeStartDatabase(databaseDefinition);
77 while (batch.hasNext()) {
78 processCommand(batch.next(), formatter);
80 formatter.writeEndDatabase();
81 formatter.writeEndBatch();
84 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
85 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
86 log.log(Level.FINE, "Statement prepared");
87 sqlCommand.parametrize(ps);
89 boolean isRS = ps.execute();
90 log.log(Level.FINE, "Statement executed");
92 try (ResultSet rs = ps.getResultSet()) {
93 processResultSet(sqlCommand, rs, formatter);
96 processUpdateResult(sqlCommand, ps, formatter);
100 while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
101 ResultSet rs = ps.getResultSet();
103 processUpdateResult(sqlCommand, ps, formatter);
105 processResultSet(sqlCommand, rs, formatter);
113 private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
114 formatter.writeStartUpdatesResult();
115 formatter.writeQuery(sqlCommand.getQuery());
116 formatter.writeParameters(sqlCommand.getParameters());
117 formatter.writeUpdatedRowsCount(ps.getUpdateCount());
118 formatter.writeEndUpdatesResult();
121 private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
122 formatter.writeStartResultSet();
123 formatter.writeQuery(sqlCommand.getQuery());
124 formatter.writeParameters(sqlCommand.getParameters());
126 processResultSetRows(rs, formatter);
128 formatter.writeEndResultSet();
131 private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException {
132 formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
133 int columnCount = rs.getMetaData().getColumnCount();
136 formatter.writeStartRow();
138 for (int i = 1; i <= columnCount; i++) {
139 formatter.writeColumnValue(rs.getObject(i));
142 formatter.writeEndRow();
147 private void logWarnings(PreparedStatement ps) throws SQLException {
148 SQLWarning w = ps.getWarnings();
150 log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage());
151 w = w.getNextWarning();
157 * Tests if this connection is live.
159 * @return true if test was successful
160 * @throws SQLException if test fails
162 public boolean test() throws SQLException {
163 connection.getAutoCommit();
168 public void close() throws SQLException {