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);
80 boolean isRS = ps.execute();
82 try (ResultSet rs = ps.getResultSet()) {
83 processResultSet(rs, formatter);
87 * TODO: process UPDATE command
91 while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
93 * TODO: process more RS or UPDATEs
98 formatter.writeEndResultSet();
101 private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
102 formatter.writeStartUpdatesResult();
103 formatter.writeQuery(sqlCommand.getQuery());
104 formatter.writeParameters(sqlCommand.getParameters());
105 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
106 sqlCommand.parametrize(ps);
107 int updatedRowsCount = ps.executeUpdate();
108 formatter.writeUpdatedRowsCount(updatedRowsCount);
110 formatter.writeStartGeneratedKeys();
111 try (ResultSet rs = ps.getGeneratedKeys()) {
112 processResultSet(rs, formatter);
114 formatter.writeEndGeneratedKeys();
118 formatter.writeEndUpdatesResult();
121 private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
122 formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
123 int columnCount = rs.getMetaData().getColumnCount();
126 formatter.writeStartRow();
128 for (int i = 1; i <= columnCount; i++) {
129 formatter.writeColumnValue(rs.getObject(i));
132 formatter.writeEndRow();