java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
branchv_0
changeset 179 236332caeb29
parent 178 5a5fc66f11b1
child 192 a32bfcbdee51
equal deleted inserted replaced
178:5a5fc66f11b1 179:236332caeb29
    15  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    17  */
    18 package info.globalcode.sql.dk;
    18 package info.globalcode.sql.dk;
    19 
    19 
       
    20 import static info.globalcode.sql.dk.jmx.ConnectionManagement.incrementCounter;
       
    21 import static info.globalcode.sql.dk.jmx.ConnectionManagement.resetCounter;
    20 import info.globalcode.sql.dk.batch.Batch;
    22 import info.globalcode.sql.dk.batch.Batch;
    21 import info.globalcode.sql.dk.batch.BatchException;
    23 import info.globalcode.sql.dk.batch.BatchException;
    22 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    23 import info.globalcode.sql.dk.configuration.Properties;
    25 import info.globalcode.sql.dk.configuration.Properties;
    24 import info.globalcode.sql.dk.configuration.Property;
    26 import info.globalcode.sql.dk.configuration.Property;
    25 import info.globalcode.sql.dk.formatting.ColumnsHeader;
    27 import info.globalcode.sql.dk.formatting.ColumnsHeader;
    26 import info.globalcode.sql.dk.formatting.Formatter;
    28 import info.globalcode.sql.dk.formatting.Formatter;
       
    29 import info.globalcode.sql.dk.jmx.ConnectionManagement;
       
    30 import info.globalcode.sql.dk.jmx.ConnectionManagement.COUNTER;
    27 import java.sql.Connection;
    31 import java.sql.Connection;
    28 import java.sql.DriverManager;
    32 import java.sql.DriverManager;
    29 import java.sql.PreparedStatement;
    33 import java.sql.PreparedStatement;
    30 import java.sql.ResultSet;
    34 import java.sql.ResultSet;
    31 import java.sql.SQLException;
    35 import java.sql.SQLException;
    48 	private static final String JDBC_PROPERTY_USER = "user";
    52 	private static final String JDBC_PROPERTY_USER = "user";
    49 	public static final String JDBC_PROPERTY_PASSWORD = "password";
    53 	public static final String JDBC_PROPERTY_PASSWORD = "password";
    50 	private final DatabaseDefinition databaseDefinition;
    54 	private final DatabaseDefinition databaseDefinition;
    51 	private final Connection connection;
    55 	private final Connection connection;
    52 	private final Properties properties;
    56 	private final Properties properties;
       
    57 	/**
       
    58 	 * Could be null = JMX is disabled → must check, see functions in
       
    59 	 * {@linkplain ConnectionManagement}
       
    60 	 */
       
    61 	private final ConnectionManagement connectionMBean;
    53 
    62 
    54 	public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
    63 	/**
       
    64 	 *
       
    65 	 * @param databaseDefinition DB url, name, password etc.
       
    66 	 * @param properties additional properties from CLI
       
    67 	 * @param connectionMBean JMX management bean | null = disabled JMX reporting
       
    68 	 * @throws SQLException
       
    69 	 */
       
    70 	public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties, ConnectionManagement connectionMBean) throws SQLException {
    55 		this.databaseDefinition = databaseDefinition;
    71 		this.databaseDefinition = databaseDefinition;
    56 		this.properties = properties;
    72 		this.properties = properties;
       
    73 		this.connectionMBean = connectionMBean;
    57 
    74 
    58 		if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
    75 		if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
    59 			log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
    76 			log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
    60 		}
    77 		}
    61 
    78 
    95 		formatter.writeEndDatabase();
   112 		formatter.writeEndDatabase();
    96 		formatter.writeEndBatch();
   113 		formatter.writeEndBatch();
    97 	}
   114 	}
    98 
   115 
    99 	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
   116 	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
       
   117 		incrementCounter(connectionMBean, COUNTER.COMMAND);
       
   118 		resetCounter(connectionMBean, COUNTER.RECORD_CURRENT);
       
   119 		
   100 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
   120 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
   101 			log.log(Level.FINE, "Statement prepared");
   121 			log.log(Level.FINE, "Statement prepared");
   102 			sqlCommand.parametrize(ps);
   122 			sqlCommand.parametrize(ps);
   103 
   123 
   104 			boolean isRS = ps.execute();
   124 			boolean isRS = ps.execute();
   133 		formatter.writeStartResultSet(new ColumnsHeader(rs.getMetaData()));
   153 		formatter.writeStartResultSet(new ColumnsHeader(rs.getMetaData()));
   134 
   154 
   135 		int columnCount = rs.getMetaData().getColumnCount();
   155 		int columnCount = rs.getMetaData().getColumnCount();
   136 
   156 
   137 		while (rs.next()) {
   157 		while (rs.next()) {
       
   158 			incrementCounter(connectionMBean, COUNTER.RECORD_CURRENT);
       
   159 			incrementCounter(connectionMBean, COUNTER.RECORD_TOTAL);
       
   160 			
   138 			formatter.writeStartRow();
   161 			formatter.writeStartRow();
   139 
   162 
   140 			for (int i = 1; i <= columnCount; i++) {
   163 			for (int i = 1; i <= columnCount; i++) {
   141 				formatter.writeColumnValue(rs.getObject(i));
   164 				formatter.writeColumnValue(rs.getObject(i));
   142 			}
   165 			}