java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 18:19:38 +0100
branchv_0
changeset 29 d66858b4b563
parent 28 57c44a6baedb
child 34 9335cf31c0f2
permissions -rw-r--r--
more configuration, more JAXB, more formatters
franta-hg@26
     1
/**
franta-hg@26
     2
 * SQL-DK
franta-hg@26
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@26
     4
 *
franta-hg@26
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@26
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@26
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@26
     8
 * (at your option) any later version.
franta-hg@26
     9
 *
franta-hg@26
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@26
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@26
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@26
    13
 * GNU General Public License for more details.
franta-hg@26
    14
 *
franta-hg@26
    15
 * You should have received a copy of the GNU General Public License
franta-hg@26
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@26
    17
 */
franta-hg@27
    18
package info.globalcode.sql.dk;
franta-hg@27
    19
franta-hg@29
    20
import info.globalcode.sql.dk.batch.Batch;
franta-hg@27
    21
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
franta-hg@29
    22
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@28
    23
import java.sql.Connection;
franta-hg@28
    24
import java.sql.DriverManager;
franta-hg@29
    25
import java.sql.PreparedStatement;
franta-hg@29
    26
import java.sql.ResultSet;
franta-hg@28
    27
import java.sql.SQLException;
franta-hg@26
    28
franta-hg@26
    29
/**
franta-hg@26
    30
 *
franta-hg@26
    31
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    32
 */
franta-hg@26
    33
public class DatabaseConnection {
franta-hg@26
    34
franta-hg@26
    35
	private DatabaseDefinition databaseDefinition;
franta-hg@28
    36
	private Connection connection;
franta-hg@26
    37
franta-hg@28
    38
	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
franta-hg@26
    39
		this.databaseDefinition = databaseDefinition;
franta-hg@28
    40
franta-hg@28
    41
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getName(), databaseDefinition.getPassword());
franta-hg@26
    42
	}
franta-hg@29
    43
franta-hg@29
    44
	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    45
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    46
		processCommand(sqlCommand, formatter);
franta-hg@29
    47
		formatter.writeEndDatabase();
franta-hg@29
    48
	}
franta-hg@29
    49
franta-hg@29
    50
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
franta-hg@29
    51
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    52
		while (batch.hasNext()) {
franta-hg@29
    53
			processCommand(batch.next(), formatter);
franta-hg@29
    54
		}
franta-hg@29
    55
		formatter.writeEndDatabase();
franta-hg@29
    56
	}
franta-hg@29
    57
franta-hg@29
    58
	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    59
		SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
franta-hg@29
    60
		switch (commandType) {
franta-hg@29
    61
			case QUERY:
franta-hg@29
    62
				processQueryCommand(sqlCommand, formatter);
franta-hg@29
    63
				break;
franta-hg@29
    64
			case UPDATE:
franta-hg@29
    65
				processUpdateCommand(sqlCommand, formatter);
franta-hg@29
    66
				break;
franta-hg@29
    67
			default:
franta-hg@29
    68
				throw new IllegalArgumentException("Unexpected command type: " + commandType);
franta-hg@29
    69
		}
franta-hg@29
    70
	}
franta-hg@29
    71
franta-hg@29
    72
	private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    73
		formatter.writeStartResultSet();
franta-hg@29
    74
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@29
    75
		/** TODO: formatter.writeParameters(null); */
franta-hg@29
    76
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@29
    77
			sqlCommand.parametrize(ps);
franta-hg@29
    78
			try (ResultSet rs = ps.executeQuery()) {
franta-hg@29
    79
				processResultSet(rs, formatter);
franta-hg@29
    80
			}
franta-hg@29
    81
		}
franta-hg@29
    82
franta-hg@29
    83
		formatter.writeEndResultSet();
franta-hg@29
    84
	}
franta-hg@29
    85
franta-hg@29
    86
	private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    87
		formatter.writeStartUpdatesResult();
franta-hg@29
    88
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@29
    89
		/** TODO: formatter.writeParameters(null); */
franta-hg@29
    90
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@29
    91
			sqlCommand.parametrize(ps);
franta-hg@29
    92
			int updatedRowsCount = ps.executeUpdate();
franta-hg@29
    93
			formatter.writeUpdatedRowsCount(updatedRowsCount);
franta-hg@29
    94
franta-hg@29
    95
			formatter.writeStartGeneratedKeys();
franta-hg@29
    96
			try (ResultSet rs = ps.getGeneratedKeys()) {
franta-hg@29
    97
				processResultSet(rs, formatter);
franta-hg@29
    98
			}
franta-hg@29
    99
			formatter.writeEndGeneratedKeys();
franta-hg@29
   100
franta-hg@29
   101
		}
franta-hg@29
   102
franta-hg@29
   103
		formatter.writeEndUpdatesResult();
franta-hg@29
   104
	}
franta-hg@29
   105
franta-hg@29
   106
	private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
franta-hg@29
   107
		/** TODO: formatter.writeColumnsHeader(null); */
franta-hg@29
   108
		while (rs.next()) {
franta-hg@29
   109
			formatter.writeStartRow();
franta-hg@29
   110
franta-hg@29
   111
			/** TODO: formatter.writeColumnValue(rs.get); */
franta-hg@29
   112
			formatter.writeEndRow();
franta-hg@29
   113
		}
franta-hg@29
   114
	}
franta-hg@26
   115
}