java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:31:55 +0100
branchv_0
changeset 34 9335cf31c0f2
parent 29 d66858b4b563
child 35 b2ff3b2d58b2
permissions -rw-r--r--
first working version
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@34
    22
import info.globalcode.sql.dk.formatting.ColumnsHeader;
franta-hg@29
    23
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@28
    24
import java.sql.Connection;
franta-hg@28
    25
import java.sql.DriverManager;
franta-hg@29
    26
import java.sql.PreparedStatement;
franta-hg@29
    27
import java.sql.ResultSet;
franta-hg@28
    28
import java.sql.SQLException;
franta-hg@26
    29
franta-hg@26
    30
/**
franta-hg@26
    31
 *
franta-hg@26
    32
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    33
 */
franta-hg@26
    34
public class DatabaseConnection {
franta-hg@26
    35
franta-hg@26
    36
	private DatabaseDefinition databaseDefinition;
franta-hg@28
    37
	private Connection connection;
franta-hg@26
    38
franta-hg@28
    39
	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
franta-hg@26
    40
		this.databaseDefinition = databaseDefinition;
franta-hg@28
    41
franta-hg@34
    42
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
franta-hg@26
    43
	}
franta-hg@29
    44
franta-hg@29
    45
	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    46
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    47
		processCommand(sqlCommand, formatter);
franta-hg@29
    48
		formatter.writeEndDatabase();
franta-hg@29
    49
	}
franta-hg@29
    50
franta-hg@29
    51
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
franta-hg@29
    52
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    53
		while (batch.hasNext()) {
franta-hg@29
    54
			processCommand(batch.next(), formatter);
franta-hg@29
    55
		}
franta-hg@29
    56
		formatter.writeEndDatabase();
franta-hg@29
    57
	}
franta-hg@29
    58
franta-hg@29
    59
	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    60
		SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
franta-hg@29
    61
		switch (commandType) {
franta-hg@29
    62
			case QUERY:
franta-hg@29
    63
				processQueryCommand(sqlCommand, formatter);
franta-hg@29
    64
				break;
franta-hg@29
    65
			case UPDATE:
franta-hg@29
    66
				processUpdateCommand(sqlCommand, formatter);
franta-hg@29
    67
				break;
franta-hg@29
    68
			default:
franta-hg@29
    69
				throw new IllegalArgumentException("Unexpected command type: " + commandType);
franta-hg@29
    70
		}
franta-hg@29
    71
	}
franta-hg@29
    72
franta-hg@29
    73
	private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    74
		formatter.writeStartResultSet();
franta-hg@29
    75
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@34
    76
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@29
    77
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@29
    78
			sqlCommand.parametrize(ps);
franta-hg@29
    79
			try (ResultSet rs = ps.executeQuery()) {
franta-hg@29
    80
				processResultSet(rs, formatter);
franta-hg@29
    81
			}
franta-hg@29
    82
		}
franta-hg@29
    83
franta-hg@29
    84
		formatter.writeEndResultSet();
franta-hg@29
    85
	}
franta-hg@29
    86
franta-hg@29
    87
	private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    88
		formatter.writeStartUpdatesResult();
franta-hg@29
    89
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@34
    90
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@29
    91
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@29
    92
			sqlCommand.parametrize(ps);
franta-hg@29
    93
			int updatedRowsCount = ps.executeUpdate();
franta-hg@29
    94
			formatter.writeUpdatedRowsCount(updatedRowsCount);
franta-hg@29
    95
franta-hg@29
    96
			formatter.writeStartGeneratedKeys();
franta-hg@29
    97
			try (ResultSet rs = ps.getGeneratedKeys()) {
franta-hg@29
    98
				processResultSet(rs, formatter);
franta-hg@29
    99
			}
franta-hg@29
   100
			formatter.writeEndGeneratedKeys();
franta-hg@29
   101
franta-hg@29
   102
		}
franta-hg@29
   103
franta-hg@29
   104
		formatter.writeEndUpdatesResult();
franta-hg@29
   105
	}
franta-hg@29
   106
franta-hg@29
   107
	private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
franta-hg@34
   108
		formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
franta-hg@34
   109
		int columnCount = rs.getMetaData().getColumnCount();
franta-hg@34
   110
franta-hg@29
   111
		while (rs.next()) {
franta-hg@29
   112
			formatter.writeStartRow();
franta-hg@29
   113
franta-hg@34
   114
			for (int i = 1; i <= columnCount; i++) {
franta-hg@34
   115
				formatter.writeColumnValue(rs.getObject(i));
franta-hg@34
   116
			}
franta-hg@34
   117
franta-hg@29
   118
			formatter.writeEndRow();
franta-hg@29
   119
		}
franta-hg@34
   120
franta-hg@29
   121
	}
franta-hg@26
   122
}