java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 01 Jan 2014 01:06:38 +0100
branchv_0
changeset 106 e9c3583580c8
parent 91 43e8d52091d5
child 107 8189a4a28cd8
permissions -rw-r--r--
use database properties
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@106
    22
import info.globalcode.sql.dk.configuration.Properties;
franta-hg@34
    23
import info.globalcode.sql.dk.formatting.ColumnsHeader;
franta-hg@29
    24
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@28
    25
import java.sql.Connection;
franta-hg@28
    26
import java.sql.DriverManager;
franta-hg@29
    27
import java.sql.PreparedStatement;
franta-hg@29
    28
import java.sql.ResultSet;
franta-hg@28
    29
import java.sql.SQLException;
franta-hg@86
    30
import java.sql.SQLWarning;
franta-hg@55
    31
import java.util.logging.Level;
franta-hg@55
    32
import java.util.logging.Logger;
franta-hg@26
    33
franta-hg@26
    34
/**
franta-hg@26
    35
 *
franta-hg@26
    36
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    37
 */
franta-hg@42
    38
public class DatabaseConnection implements AutoCloseable {
franta-hg@26
    39
franta-hg@55
    40
	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
franta-hg@26
    41
	private DatabaseDefinition databaseDefinition;
franta-hg@28
    42
	private Connection connection;
franta-hg@106
    43
	private Properties properties;
franta-hg@26
    44
franta-hg@106
    45
	public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
franta-hg@26
    46
		this.databaseDefinition = databaseDefinition;
franta-hg@106
    47
		this.properties = properties;
franta-hg@28
    48
franta-hg@106
    49
		properties.setDefaults(databaseDefinition.getProperties());
franta-hg@106
    50
		java.util.Properties javaProperties = properties.getJavaProperties();
franta-hg@106
    51
		javaProperties.setProperty("user", databaseDefinition.getUserName());
franta-hg@106
    52
		javaProperties.setProperty("password", databaseDefinition.getPassword());
franta-hg@106
    53
franta-hg@106
    54
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
franta-hg@26
    55
	}
franta-hg@29
    56
franta-hg@29
    57
	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@91
    58
		formatter.writeStartBatch();
franta-hg@29
    59
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    60
		processCommand(sqlCommand, formatter);
franta-hg@29
    61
		formatter.writeEndDatabase();
franta-hg@91
    62
		formatter.writeEndBatch();
franta-hg@29
    63
	}
franta-hg@29
    64
franta-hg@29
    65
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
franta-hg@91
    66
		formatter.writeStartBatch();
franta-hg@29
    67
		formatter.writeStartDatabase(databaseDefinition);
franta-hg@29
    68
		while (batch.hasNext()) {
franta-hg@29
    69
			processCommand(batch.next(), formatter);
franta-hg@29
    70
		}
franta-hg@29
    71
		formatter.writeEndDatabase();
franta-hg@91
    72
		formatter.writeEndBatch();
franta-hg@29
    73
	}
franta-hg@29
    74
franta-hg@29
    75
	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
franta-hg@29
    76
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
franta-hg@55
    77
			log.log(Level.FINE, "Statement prepared");
franta-hg@29
    78
			sqlCommand.parametrize(ps);
franta-hg@35
    79
franta-hg@35
    80
			boolean isRS = ps.execute();
franta-hg@55
    81
			log.log(Level.FINE, "Statement executed");
franta-hg@35
    82
			if (isRS) {
franta-hg@35
    83
				try (ResultSet rs = ps.getResultSet()) {
franta-hg@37
    84
					processResultSet(sqlCommand, rs, formatter);
franta-hg@35
    85
				}
franta-hg@35
    86
			} else {
franta-hg@37
    87
				processUpdateResult(sqlCommand, ps, formatter);
franta-hg@35
    88
			}
franta-hg@86
    89
			logWarnings(ps);
franta-hg@35
    90
franta-hg@35
    91
			while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
franta-hg@37
    92
				ResultSet rs = ps.getResultSet();
franta-hg@37
    93
				if (rs == null) {
franta-hg@37
    94
					processUpdateResult(sqlCommand, ps, formatter);
franta-hg@37
    95
				} else {
franta-hg@37
    96
					processResultSet(sqlCommand, rs, formatter);
franta-hg@37
    97
					rs.close();
franta-hg@37
    98
				}
franta-hg@86
    99
				logWarnings(ps);
franta-hg@29
   100
			}
franta-hg@29
   101
		}
franta-hg@37
   102
	}
franta-hg@37
   103
franta-hg@37
   104
	private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException {
franta-hg@37
   105
		formatter.writeStartUpdatesResult();
franta-hg@37
   106
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@37
   107
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@41
   108
		formatter.writeUpdatedRowsCount(ps.getUpdateCount());
franta-hg@37
   109
		formatter.writeEndUpdatesResult();
franta-hg@37
   110
	}
franta-hg@37
   111
franta-hg@37
   112
	private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException {
franta-hg@37
   113
		formatter.writeStartResultSet();
franta-hg@37
   114
		formatter.writeQuery(sqlCommand.getQuery());
franta-hg@37
   115
		formatter.writeParameters(sqlCommand.getParameters());
franta-hg@37
   116
franta-hg@37
   117
		processResultSetRows(rs, formatter);
franta-hg@29
   118
franta-hg@29
   119
		formatter.writeEndResultSet();
franta-hg@29
   120
	}
franta-hg@29
   121
franta-hg@37
   122
	private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException {
franta-hg@34
   123
		formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
franta-hg@34
   124
		int columnCount = rs.getMetaData().getColumnCount();
franta-hg@34
   125
franta-hg@29
   126
		while (rs.next()) {
franta-hg@29
   127
			formatter.writeStartRow();
franta-hg@29
   128
franta-hg@34
   129
			for (int i = 1; i <= columnCount; i++) {
franta-hg@34
   130
				formatter.writeColumnValue(rs.getObject(i));
franta-hg@34
   131
			}
franta-hg@34
   132
franta-hg@29
   133
			formatter.writeEndRow();
franta-hg@29
   134
		}
franta-hg@34
   135
franta-hg@29
   136
	}
franta-hg@42
   137
franta-hg@86
   138
	private void logWarnings(PreparedStatement ps) throws SQLException {
franta-hg@86
   139
		SQLWarning w = ps.getWarnings();
franta-hg@86
   140
		while (w != null) {
franta-hg@86
   141
			log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage());
franta-hg@86
   142
			w = w.getNextWarning();
franta-hg@86
   143
		}
franta-hg@86
   144
		ps.clearWarnings();
franta-hg@86
   145
	}
franta-hg@86
   146
franta-hg@65
   147
	/**
franta-hg@65
   148
	 * Tests if this connection is live.
franta-hg@65
   149
	 *
franta-hg@65
   150
	 * @return true if test was successful
franta-hg@65
   151
	 * @throws SQLException if test fails
franta-hg@65
   152
	 */
franta-hg@65
   153
	public boolean test() throws SQLException {
franta-hg@65
   154
		connection.getAutoCommit();
franta-hg@65
   155
		return true;
franta-hg@65
   156
	}
franta-hg@65
   157
franta-hg@42
   158
	@Override
franta-hg@42
   159
	public void close() throws SQLException {
franta-hg@42
   160
		connection.close();
franta-hg@42
   161
	}
franta-hg@26
   162
}