franta-hg@26: /** franta-hg@26: * SQL-DK franta-hg@26: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@26: * franta-hg@26: * This program is free software: you can redistribute it and/or modify franta-hg@26: * it under the terms of the GNU General Public License as published by franta-hg@26: * the Free Software Foundation, either version 3 of the License, or franta-hg@26: * (at your option) any later version. franta-hg@26: * franta-hg@26: * This program is distributed in the hope that it will be useful, franta-hg@26: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@26: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@26: * GNU General Public License for more details. franta-hg@26: * franta-hg@26: * You should have received a copy of the GNU General Public License franta-hg@26: * along with this program. If not, see . franta-hg@26: */ franta-hg@27: package info.globalcode.sql.dk; franta-hg@27: franta-hg@29: import info.globalcode.sql.dk.batch.Batch; franta-hg@27: import info.globalcode.sql.dk.configuration.DatabaseDefinition; franta-hg@34: import info.globalcode.sql.dk.formatting.ColumnsHeader; franta-hg@29: import info.globalcode.sql.dk.formatting.Formatter; franta-hg@28: import java.sql.Connection; franta-hg@28: import java.sql.DriverManager; franta-hg@29: import java.sql.PreparedStatement; franta-hg@29: import java.sql.ResultSet; franta-hg@28: import java.sql.SQLException; franta-hg@26: franta-hg@26: /** franta-hg@26: * franta-hg@26: * @author Ing. František Kučera (frantovo.cz) franta-hg@26: */ franta-hg@42: public class DatabaseConnection implements AutoCloseable { franta-hg@26: franta-hg@26: private DatabaseDefinition databaseDefinition; franta-hg@28: private Connection connection; franta-hg@26: franta-hg@28: public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException { franta-hg@26: this.databaseDefinition = databaseDefinition; franta-hg@28: franta-hg@34: connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword()); franta-hg@26: } franta-hg@29: franta-hg@29: public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException { franta-hg@29: formatter.writeStartDatabase(databaseDefinition); franta-hg@29: processCommand(sqlCommand, formatter); franta-hg@29: formatter.writeEndDatabase(); franta-hg@29: } franta-hg@29: franta-hg@29: public void executeBatch(Batch batch, Formatter formatter) throws SQLException { franta-hg@29: formatter.writeStartDatabase(databaseDefinition); franta-hg@29: while (batch.hasNext()) { franta-hg@29: processCommand(batch.next(), formatter); franta-hg@29: } franta-hg@29: formatter.writeEndDatabase(); franta-hg@29: } franta-hg@29: franta-hg@29: private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { franta-hg@29: try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) { franta-hg@29: sqlCommand.parametrize(ps); franta-hg@35: franta-hg@35: boolean isRS = ps.execute(); franta-hg@35: if (isRS) { franta-hg@35: try (ResultSet rs = ps.getResultSet()) { franta-hg@37: processResultSet(sqlCommand, rs, formatter); franta-hg@35: } franta-hg@35: } else { franta-hg@37: processUpdateResult(sqlCommand, ps, formatter); franta-hg@35: } franta-hg@35: franta-hg@35: while (ps.getMoreResults() || ps.getUpdateCount() > -1) { franta-hg@37: ResultSet rs = ps.getResultSet(); franta-hg@37: if (rs == null) { franta-hg@37: processUpdateResult(sqlCommand, ps, formatter); franta-hg@37: } else { franta-hg@37: processResultSet(sqlCommand, rs, formatter); franta-hg@37: rs.close(); franta-hg@37: } franta-hg@29: } franta-hg@29: } franta-hg@37: } franta-hg@37: franta-hg@37: private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException { franta-hg@37: formatter.writeStartUpdatesResult(); franta-hg@37: formatter.writeQuery(sqlCommand.getQuery()); franta-hg@37: formatter.writeParameters(sqlCommand.getParameters()); franta-hg@41: formatter.writeUpdatedRowsCount(ps.getUpdateCount()); franta-hg@37: formatter.writeEndUpdatesResult(); franta-hg@37: } franta-hg@37: franta-hg@37: private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException { franta-hg@37: formatter.writeStartResultSet(); franta-hg@37: formatter.writeQuery(sqlCommand.getQuery()); franta-hg@37: formatter.writeParameters(sqlCommand.getParameters()); franta-hg@37: franta-hg@37: processResultSetRows(rs, formatter); franta-hg@29: franta-hg@29: formatter.writeEndResultSet(); franta-hg@29: } franta-hg@29: franta-hg@37: private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException { franta-hg@34: formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData())); franta-hg@34: int columnCount = rs.getMetaData().getColumnCount(); franta-hg@34: franta-hg@29: while (rs.next()) { franta-hg@29: formatter.writeStartRow(); franta-hg@29: franta-hg@34: for (int i = 1; i <= columnCount; i++) { franta-hg@34: formatter.writeColumnValue(rs.getObject(i)); franta-hg@34: } franta-hg@34: franta-hg@29: formatter.writeEndRow(); franta-hg@29: } franta-hg@34: franta-hg@29: } franta-hg@42: franta-hg@42: @Override franta-hg@42: public void close() throws SQLException { franta-hg@42: connection.close(); franta-hg@42: } franta-hg@26: }