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.formatting.ColumnsHeader;
24 import info.globalcode.sql.dk.formatting.Formatter;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.sql.PreparedStatement;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.SQLWarning;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
36 * @author Ing. František Kučera (frantovo.cz)
38 public class DatabaseConnection implements AutoCloseable {
40 private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
41 private DatabaseDefinition databaseDefinition;
42 private Connection connection;
43 private Properties properties;
45 public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
46 this.databaseDefinition = databaseDefinition;
47 this.properties = properties;
49 properties.setDefaults(databaseDefinition.getProperties());
50 java.util.Properties javaProperties = properties.getJavaProperties();
51 javaProperties.setProperty("user", databaseDefinition.getUserName());
52 javaProperties.setProperty("password", databaseDefinition.getPassword());
54 connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
57 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
58 formatter.writeStartBatch();
59 formatter.writeStartDatabase(databaseDefinition);
60 processCommand(sqlCommand, formatter);
61 formatter.writeEndDatabase();
62 formatter.writeEndBatch();
65 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
66 formatter.writeStartBatch();
67 formatter.writeStartDatabase(databaseDefinition);
68 while (batch.hasNext()) {
69 processCommand(batch.next(), formatter);
71 formatter.writeEndDatabase();
72 formatter.writeEndBatch();
75 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
76 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
77 log.log(Level.FINE, "Statement prepared");
78 sqlCommand.parametrize(ps);
80 boolean isRS = ps.execute();
81 log.log(Level.FINE, "Statement executed");
83 try (ResultSet rs = ps.getResultSet()) {
84 processResultSet(sqlCommand, rs, formatter);
87 processUpdateResult(sqlCommand, ps, formatter);
91 while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
92 ResultSet rs = ps.getResultSet();
94 processUpdateResult(sqlCommand, ps, formatter);
96 processResultSet(sqlCommand, rs, formatter);
104 private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
105 formatter.writeStartUpdatesResult();
106 formatter.writeQuery(sqlCommand.getQuery());
107 formatter.writeParameters(sqlCommand.getParameters());
108 formatter.writeUpdatedRowsCount(ps.getUpdateCount());
109 formatter.writeEndUpdatesResult();
112 private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
113 formatter.writeStartResultSet();
114 formatter.writeQuery(sqlCommand.getQuery());
115 formatter.writeParameters(sqlCommand.getParameters());
117 processResultSetRows(rs, formatter);
119 formatter.writeEndResultSet();
122 private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException {
123 formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
124 int columnCount = rs.getMetaData().getColumnCount();
127 formatter.writeStartRow();
129 for (int i = 1; i <= columnCount; i++) {
130 formatter.writeColumnValue(rs.getObject(i));
133 formatter.writeEndRow();
138 private void logWarnings(PreparedStatement ps) throws SQLException {
139 SQLWarning w = ps.getWarnings();
141 log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage());
142 w = w.getNextWarning();
148 * Tests if this connection is live.
150 * @return true if test was successful
151 * @throws SQLException if test fails
153 public boolean test() throws SQLException {
154 connection.getAutoCommit();
159 public void close() throws SQLException {