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.Formatter;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
31 * @author Ing. František Kučera (frantovo.cz)
33 public class DatabaseConnection {
35 private DatabaseDefinition databaseDefinition;
36 private Connection connection;
38 public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
39 this.databaseDefinition = databaseDefinition;
41 connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getName(), databaseDefinition.getPassword());
44 public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
45 formatter.writeStartDatabase(databaseDefinition);
46 processCommand(sqlCommand, formatter);
47 formatter.writeEndDatabase();
50 public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
51 formatter.writeStartDatabase(databaseDefinition);
52 while (batch.hasNext()) {
53 processCommand(batch.next(), formatter);
55 formatter.writeEndDatabase();
58 private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
59 SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
60 switch (commandType) {
62 processQueryCommand(sqlCommand, formatter);
65 processUpdateCommand(sqlCommand, formatter);
68 throw new IllegalArgumentException("Unexpected command type: " + commandType);
72 private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
73 formatter.writeStartResultSet();
74 formatter.writeQuery(sqlCommand.getQuery());
75 /** TODO: formatter.writeParameters(null); */
76 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
77 sqlCommand.parametrize(ps);
78 try (ResultSet rs = ps.executeQuery()) {
79 processResultSet(rs, formatter);
83 formatter.writeEndResultSet();
86 private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
87 formatter.writeStartUpdatesResult();
88 formatter.writeQuery(sqlCommand.getQuery());
89 /** TODO: formatter.writeParameters(null); */
90 try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
91 sqlCommand.parametrize(ps);
92 int updatedRowsCount = ps.executeUpdate();
93 formatter.writeUpdatedRowsCount(updatedRowsCount);
95 formatter.writeStartGeneratedKeys();
96 try (ResultSet rs = ps.getGeneratedKeys()) {
97 processResultSet(rs, formatter);
99 formatter.writeEndGeneratedKeys();
103 formatter.writeEndUpdatesResult();
106 private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
107 /** TODO: formatter.writeColumnsHeader(null); */
109 formatter.writeStartRow();
111 /** TODO: formatter.writeColumnValue(rs.get); */
112 formatter.writeEndRow();