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 DatabaseDefinition databaseDefinition;
43 private Connection connection;
44 private Properties properties;
46 public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
47 this.databaseDefinition = databaseDefinition;
48 this.properties = properties;
50 Properties credentials = new Properties();
51 credentials.add(new Property("user", databaseDefinition.getUserName()));
52 credentials.add(new Property("password", databaseDefinition.getPassword()));
53 credentials.setDefaults(databaseDefinition.getProperties());
54 properties.setDefaults(credentials);
55 java.util.Properties javaProperties = properties.getJavaProperties();
57 connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
60 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
61 formatter.writeStartBatch();
62 formatter.writeStartDatabase(databaseDefinition);
63 processCommand(sqlCommand, formatter);
64 formatter.writeEndDatabase();
65 formatter.writeEndBatch();
68 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
69 formatter.writeStartBatch();
70 formatter.writeStartDatabase(databaseDefinition);
71 while (batch.hasNext()) {
72 processCommand(batch.next(), formatter);
74 formatter.writeEndDatabase();
75 formatter.writeEndBatch();
78 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
79 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
80 log.log(Level.FINE, "Statement prepared");
81 sqlCommand.parametrize(ps);
83 boolean isRS = ps.execute();
84 log.log(Level.FINE, "Statement executed");
86 try (ResultSet rs = ps.getResultSet()) {
87 processResultSet(sqlCommand, rs, formatter);
90 processUpdateResult(sqlCommand, ps, formatter);
94 while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
95 ResultSet rs = ps.getResultSet();
97 processUpdateResult(sqlCommand, ps, formatter);
99 processResultSet(sqlCommand, rs, formatter);
107 private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
108 formatter.writeStartUpdatesResult();
109 formatter.writeQuery(sqlCommand.getQuery());
110 formatter.writeParameters(sqlCommand.getParameters());
111 formatter.writeUpdatedRowsCount(ps.getUpdateCount());
112 formatter.writeEndUpdatesResult();
115 private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
116 formatter.writeStartResultSet();
117 formatter.writeQuery(sqlCommand.getQuery());
118 formatter.writeParameters(sqlCommand.getParameters());
120 processResultSetRows(rs, formatter);
122 formatter.writeEndResultSet();
125 private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException {
126 formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
127 int columnCount = rs.getMetaData().getColumnCount();
130 formatter.writeStartRow();
132 for (int i = 1; i <= columnCount; i++) {
133 formatter.writeColumnValue(rs.getObject(i));
136 formatter.writeEndRow();
141 private void logWarnings(PreparedStatement ps) throws SQLException {
142 SQLWarning w = ps.getWarnings();
144 log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage());
145 w = w.getNextWarning();
151 * Tests if this connection is live.
153 * @return true if test was successful
154 * @throws SQLException if test fails
156 public boolean test() throws SQLException {
157 connection.getAutoCommit();
162 public void close() throws SQLException {