exit codes: 0 = success; 4 = SQL error; 3 = other expected error; 1 = unexpected error (implicit 1)
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.formatting.ColumnsHeader;
23 import info.globalcode.sql.dk.formatting.Formatter;
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class DatabaseConnection implements AutoCloseable {
38 private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
39 private DatabaseDefinition databaseDefinition;
40 private Connection connection;
42 public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
43 this.databaseDefinition = databaseDefinition;
45 connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
48 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
49 formatter.writeStartDatabase(databaseDefinition);
50 processCommand(sqlCommand, formatter);
51 formatter.writeEndDatabase();
54 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
55 formatter.writeStartDatabase(databaseDefinition);
56 while (batch.hasNext()) {
57 processCommand(batch.next(), formatter);
59 formatter.writeEndDatabase();
62 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
63 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
64 log.log(Level.FINE, "Statement prepared");
65 sqlCommand.parametrize(ps);
67 boolean isRS = ps.execute();
68 log.log(Level.FINE, "Statement executed");
70 try (ResultSet rs = ps.getResultSet()) {
71 processResultSet(sqlCommand, rs, formatter);
74 processUpdateResult(sqlCommand, ps, formatter);
77 while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
78 ResultSet rs = ps.getResultSet();
80 processUpdateResult(sqlCommand, ps, formatter);
82 processResultSet(sqlCommand, rs, formatter);
89 private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
90 formatter.writeStartUpdatesResult();
91 formatter.writeQuery(sqlCommand.getQuery());
92 formatter.writeParameters(sqlCommand.getParameters());
93 formatter.writeUpdatedRowsCount(ps.getUpdateCount());
94 formatter.writeEndUpdatesResult();
97 private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
98 formatter.writeStartResultSet();
99 formatter.writeQuery(sqlCommand.getQuery());
100 formatter.writeParameters(sqlCommand.getParameters());
102 processResultSetRows(rs, formatter);
104 formatter.writeEndResultSet();
107 private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException {
108 formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
109 int columnCount = rs.getMetaData().getColumnCount();
112 formatter.writeStartRow();
114 for (int i = 1; i <= columnCount; i++) {
115 formatter.writeColumnValue(rs.getObject(i));
118 formatter.writeEndRow();
124 public void close() throws SQLException {