java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 25 Sep 2014 17:50:40 +0200
branchv_0
changeset 179 236332caeb29
parent 178 5a5fc66f11b1
child 192 a32bfcbdee51
permissions -rw-r--r--
Basic JMX management/reporting – counters for commands and records
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
27
24aa5199bfd6 DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    18
package info.globalcode.sql.dk;
24aa5199bfd6 DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    19
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    20
import static info.globalcode.sql.dk.jmx.ConnectionManagement.incrementCounter;
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    21
import static info.globalcode.sql.dk.jmx.ConnectionManagement.resetCounter;
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    22
import info.globalcode.sql.dk.batch.Batch;
146
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    23
import info.globalcode.sql.dk.batch.BatchException;
27
24aa5199bfd6 DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    24
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 91
diff changeset
    25
import info.globalcode.sql.dk.configuration.Properties;
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
    26
import info.globalcode.sql.dk.configuration.Property;
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    27
import info.globalcode.sql.dk.formatting.ColumnsHeader;
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    28
import info.globalcode.sql.dk.formatting.Formatter;
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    29
import info.globalcode.sql.dk.jmx.ConnectionManagement;
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    30
import info.globalcode.sql.dk.jmx.ConnectionManagement.COUNTER;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    31
import java.sql.Connection;
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    32
import java.sql.DriverManager;
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    33
import java.sql.PreparedStatement;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    34
import java.sql.ResultSet;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    35
import java.sql.SQLException;
86
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
    36
import java.sql.SQLWarning;
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    37
import java.util.logging.Level;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    38
import java.util.logging.Logger;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
/**
155
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    41
 * Represents connected database. Is derived from {@linkplain DatabaseDefinition}.
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    42
 * Wraps {@linkplain Connection}.
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    43
 *
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    44
 * Is responsible for executing {@linkplain SQLCommand} and passing results to the
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    45
 * {@linkplain Formatter}.
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
 * @author Ing. František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
 */
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    49
public class DatabaseConnection implements AutoCloseable {
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    51
	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
108
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    52
	private static final String JDBC_PROPERTY_USER = "user";
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    53
	public static final String JDBC_PROPERTY_PASSWORD = "password";
178
5a5fc66f11b1 final properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    54
	private final DatabaseDefinition databaseDefinition;
5a5fc66f11b1 final properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    55
	private final Connection connection;
5a5fc66f11b1 final properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    56
	private final Properties properties;
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    57
	/**
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    58
	 * Could be null = JMX is disabled → must check, see functions in
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    59
	 * {@linkplain ConnectionManagement}
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    60
	 */
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    61
	private final ConnectionManagement connectionMBean;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    63
	/**
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    64
	 *
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    65
	 * @param databaseDefinition DB url, name, password etc.
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    66
	 * @param properties additional properties from CLI
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    67
	 * @param connectionMBean JMX management bean | null = disabled JMX reporting
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    68
	 * @throws SQLException
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    69
	 */
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    70
	public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties, ConnectionManagement connectionMBean) throws SQLException {
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		this.databaseDefinition = databaseDefinition;
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 91
diff changeset
    72
		this.properties = properties;
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
    73
		this.connectionMBean = connectionMBean;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    74
108
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    75
		if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    76
			log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    77
		}
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    78
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
    79
		Properties credentials = new Properties();
108
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    80
		credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    81
		credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
    82
		credentials.setDefaults(databaseDefinition.getProperties());
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
    83
		properties.setDefaults(credentials);
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 91
diff changeset
    84
		java.util.Properties javaProperties = properties.getJavaProperties();
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 91
diff changeset
    85
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 91
diff changeset
    86
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	}
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    88
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    89
	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
91
43e8d52091d5 Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents: 86
diff changeset
    90
		formatter.writeStartBatch();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    91
		formatter.writeStartDatabase(databaseDefinition);
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    92
		formatter.writeStartStatement();
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    93
		formatter.writeQuery(sqlCommand.getQuery());
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    94
		formatter.writeParameters(sqlCommand.getParameters());
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    95
		processCommand(sqlCommand, formatter);
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    96
		formatter.writeEndStatement();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    97
		formatter.writeEndDatabase();
91
43e8d52091d5 Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents: 86
diff changeset
    98
		formatter.writeEndBatch();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    99
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   100
146
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
   101
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException, BatchException {
91
43e8d52091d5 Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents: 86
diff changeset
   102
		formatter.writeStartBatch();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   103
		formatter.writeStartDatabase(databaseDefinition);
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   104
		while (batch.hasNext()) {
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   105
			SQLCommand sqlCommand = batch.next();
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   106
			formatter.writeStartStatement();
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   107
			formatter.writeQuery(sqlCommand.getQuery());
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   108
			formatter.writeParameters(sqlCommand.getParameters());
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   109
			processCommand(sqlCommand, formatter);
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   110
			formatter.writeEndStatement();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   111
		}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   112
		formatter.writeEndDatabase();
91
43e8d52091d5 Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents: 86
diff changeset
   113
		formatter.writeEndBatch();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   114
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   115
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   116
	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   117
		incrementCounter(connectionMBean, COUNTER.COMMAND);
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   118
		resetCounter(connectionMBean, COUNTER.RECORD_CURRENT);
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   119
		
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   120
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   121
			log.log(Level.FINE, "Statement prepared");
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   122
			sqlCommand.parametrize(ps);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   123
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   124
			boolean isRS = ps.execute();
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   125
			log.log(Level.FINE, "Statement executed");
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   126
			if (isRS) {
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   127
				try (ResultSet rs = ps.getResultSet()) {
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   128
					processResultSet(rs, formatter);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   129
				}
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   130
			} else {
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   131
				processUpdateResult(ps, formatter);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   132
			}
86
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   133
			logWarnings(ps);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   134
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   135
			while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   136
				ResultSet rs = ps.getResultSet();
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   137
				if (rs == null) {
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   138
					processUpdateResult(ps, formatter);
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   139
				} else {
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   140
					processResultSet(rs, formatter);
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   141
					rs.close();
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   142
				}
86
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   143
				logWarnings(ps);
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   144
			}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   145
		}
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   146
	}
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   147
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   148
	private void processUpdateResult(PreparedStatement ps, Formatter formatter) throws SQLException {
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   149
		formatter.writeUpdatesResult(ps.getUpdateCount());
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   150
	}
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   151
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   152
	private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   153
		formatter.writeStartResultSet(new ColumnsHeader(rs.getMetaData()));
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   154
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   155
		int columnCount = rs.getMetaData().getColumnCount();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   156
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   157
		while (rs.next()) {
179
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   158
			incrementCounter(connectionMBean, COUNTER.RECORD_CURRENT);
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   159
			incrementCounter(connectionMBean, COUNTER.RECORD_TOTAL);
236332caeb29 Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents: 178
diff changeset
   160
			
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   161
			formatter.writeStartRow();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   162
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   163
			for (int i = 1; i <= columnCount; i++) {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   164
				formatter.writeColumnValue(rs.getObject(i));
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   165
			}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   166
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   167
			formatter.writeEndRow();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   168
		}
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   169
142
da1e38386d84 Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
   170
		formatter.writeEndResultSet();
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   171
	}
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   172
86
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   173
	private void logWarnings(PreparedStatement ps) throws SQLException {
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   174
		SQLWarning w = ps.getWarnings();
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   175
		while (w != null) {
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   176
			log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage());
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   177
			w = w.getNextWarning();
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   178
		}
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   179
		ps.clearWarnings();
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   180
	}
6b0eb3b22eb8 log SQLWarnings
František Kučera <franta-hg@frantovo.cz>
parents: 65
diff changeset
   181
65
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   182
	/**
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   183
	 * Tests if this connection is live.
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   184
	 *
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   185
	 * @return true if test was successful
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   186
	 * @throws SQLException if test fails
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   187
	 */
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   188
	public boolean test() throws SQLException {
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   189
		connection.getAutoCommit();
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   190
		return true;
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   191
	}
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   192
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   193
	@Override
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   194
	public void close() throws SQLException {
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   195
		connection.close();
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   196
	}
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
}