java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 18:22:19 +0100
branchv_0
changeset 83 9563232ea0b7
parent 65 f05be87239ad
child 86 6b0eb3b22eb8
permissions -rw-r--r--
bash completion: license
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@55
    29
import java.util.logging.Level;
franta-hg@55
    30
import java.util.logging.Logger;
franta-hg@26
    31
franta-hg@26
    32
/**
franta-hg@26
    33
 *
franta-hg@26
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    35
 */
franta-hg@42
    36
public class DatabaseConnection implements AutoCloseable {
franta-hg@26
    37
franta-hg@55
    38
	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
franta-hg@26
    39
	private DatabaseDefinition databaseDefinition;
franta-hg@28
    40
	private Connection connection;
franta-hg@26
    41
franta-hg@28
    42
	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
franta-hg@26
    43
		this.databaseDefinition = databaseDefinition;
franta-hg@28
    44
franta-hg@34
    45
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
franta-hg@26
    46
	}
franta-hg@29
    47
franta-hg@29
    48
	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    49
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    50
		processCommand(sqlCommand, formatter);
franta-hg@29
    51
		formatter.writeEndDatabase();
franta-hg@29
    52
	}
franta-hg@29
    53
franta-hg@29
    54
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
franta-hg@29
    55
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    56
		while (batch.hasNext()) {
franta-hg@29
    57
			processCommand(batch.next(), formatter);
franta-hg@29
    58
		}
franta-hg@29
    59
		formatter.writeEndDatabase();
franta-hg@29
    60
	}
franta-hg@29
    61
franta-hg@29
    62
	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    63
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@55
    64
			log.log(Level.FINE, "Statement prepared");
franta-hg@29
    65
			sqlCommand.parametrize(ps);
franta-hg@35
    66
franta-hg@35
    67
			boolean isRS = ps.execute();
franta-hg@55
    68
			log.log(Level.FINE, "Statement executed");
franta-hg@35
    69
			if (isRS) {
franta-hg@35
    70
				try (ResultSet rs = ps.getResultSet()) {
franta-hg@37
    71
					processResultSet(sqlCommand, rs, formatter);
franta-hg@35
    72
				}
franta-hg@35
    73
			} else {
franta-hg@37
    74
				processUpdateResult(sqlCommand, ps, formatter);
franta-hg@35
    75
			}
franta-hg@35
    76
franta-hg@35
    77
			while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
franta-hg@37
    78
				ResultSet rs = ps.getResultSet();
franta-hg@37
    79
				if (rs == null) {
franta-hg@37
    80
					processUpdateResult(sqlCommand, ps, formatter);
franta-hg@37
    81
				} else {
franta-hg@37
    82
					processResultSet(sqlCommand, rs, formatter);
franta-hg@37
    83
					rs.close();
franta-hg@37
    84
				}
franta-hg@29
    85
			}
franta-hg@29
    86
		}
franta-hg@37
    87
	}
franta-hg@37
    88
franta-hg@37
    89
	private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
franta-hg@37
    90
		formatter.writeStartUpdatesResult();
franta-hg@37
    91
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@37
    92
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@41
    93
		formatter.writeUpdatedRowsCount(ps.getUpdateCount());
franta-hg@37
    94
		formatter.writeEndUpdatesResult();
franta-hg@37
    95
	}
franta-hg@37
    96
franta-hg@37
    97
	private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
franta-hg@37
    98
		formatter.writeStartResultSet();
franta-hg@37
    99
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@37
   100
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@37
   101
franta-hg@37
   102
		processResultSetRows(rs, formatter);
franta-hg@29
   103
franta-hg@29
   104
		formatter.writeEndResultSet();
franta-hg@29
   105
	}
franta-hg@29
   106
franta-hg@37
   107
	private void processResultSetRows(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@42
   122
franta-hg@65
   123
	/**
franta-hg@65
   124
	 * Tests if this connection is live.
franta-hg@65
   125
	 *
franta-hg@65
   126
	 * @return true if test was successful
franta-hg@65
   127
	 * @throws SQLException if test fails
franta-hg@65
   128
	 */
franta-hg@65
   129
	public boolean test() throws SQLException {
franta-hg@65
   130
		connection.getAutoCommit();
franta-hg@65
   131
		return true;
franta-hg@65
   132
	}
franta-hg@65
   133
franta-hg@42
   134
	@Override
franta-hg@42
   135
	public void close() throws SQLException {
franta-hg@42
   136
		connection.close();
franta-hg@42
   137
	}
franta-hg@26
   138
}