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;
32 * @author Ing. František Kučera (frantovo.cz)
34 public class DatabaseConnection {
36 private DatabaseDefinition databaseDefinition;
37 private Connection connection;
39 public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
40 this.databaseDefinition = databaseDefinition;
42 connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
45 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
46 formatter.writeStartDatabase(databaseDefinition);
47 processCommand(sqlCommand, formatter);
48 formatter.writeEndDatabase();
51 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
52 formatter.writeStartDatabase(databaseDefinition);
53 while (batch.hasNext()) {
54 processCommand(batch.next(), formatter);
56 formatter.writeEndDatabase();
59 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
60 SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
61 switch (commandType) {
63 processQueryCommand(sqlCommand, formatter);
66 processUpdateCommand(sqlCommand, formatter);
69 throw new IllegalArgumentException("Unexpected command type: " + commandType);
73 private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
74 formatter.writeStartResultSet();
75 formatter.writeQuery(sqlCommand.getQuery());
76 formatter.writeParameters(sqlCommand.getParameters());
77 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
78 sqlCommand.parametrize(ps);
79 try (ResultSet rs = ps.executeQuery()) {
80 processResultSet(rs, formatter);
84 formatter.writeEndResultSet();
87 private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
88 formatter.writeStartUpdatesResult();
89 formatter.writeQuery(sqlCommand.getQuery());
90 formatter.writeParameters(sqlCommand.getParameters());
91 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
92 sqlCommand.parametrize(ps);
93 int updatedRowsCount = ps.executeUpdate();
94 formatter.writeUpdatedRowsCount(updatedRowsCount);
96 formatter.writeStartGeneratedKeys();
97 try (ResultSet rs = ps.getGeneratedKeys()) {
98 processResultSet(rs, formatter);
100 formatter.writeEndGeneratedKeys();
104 formatter.writeEndUpdatesResult();
107 private void processResultSet(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();