java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:55:07 +0100
branchv_0
changeset 35 b2ff3b2d58b2
parent 34 9335cf31c0f2
child 37 9e6f8e5d5f98
permissions -rw-r--r--
accept SQL commands returning more ResultSets
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import info.globalcode.sql.dk.batch.Batch;
    21 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    22 import info.globalcode.sql.dk.formatting.ColumnsHeader;
    23 import info.globalcode.sql.dk.formatting.Formatter;
    24 import java.sql.Connection;
    25 import java.sql.DriverManager;
    26 import java.sql.PreparedStatement;
    27 import java.sql.ResultSet;
    28 import java.sql.SQLException;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class DatabaseConnection {
    35 
    36 	private DatabaseDefinition databaseDefinition;
    37 	private Connection connection;
    38 
    39 	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
    40 		this.databaseDefinition = databaseDefinition;
    41 
    42 		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
    43 	}
    44 
    45 	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    46 		formatter.writeStartDatabase(databaseDefinition);
    47 		processCommand(sqlCommand, formatter);
    48 		formatter.writeEndDatabase();
    49 	}
    50 
    51 	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
    52 		formatter.writeStartDatabase(databaseDefinition);
    53 		while (batch.hasNext()) {
    54 			processCommand(batch.next(), formatter);
    55 		}
    56 		formatter.writeEndDatabase();
    57 	}
    58 
    59 	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    60 		SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
    61 		switch (commandType) {
    62 			case QUERY:
    63 				processQueryCommand(sqlCommand, formatter);
    64 				break;
    65 			case UPDATE:
    66 				processUpdateCommand(sqlCommand, formatter);
    67 				break;
    68 			default:
    69 				throw new IllegalArgumentException("Unexpected command type: " + commandType);
    70 		}
    71 	}
    72 
    73 	private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    74 		formatter.writeStartResultSet();
    75 		formatter.writeQuery(sqlCommand.getQuery());
    76 		formatter.writeParameters(sqlCommand.getParameters());
    77 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
    78 			sqlCommand.parametrize(ps);
    79 
    80 			boolean isRS = ps.execute();
    81 			if (isRS) {
    82 				try (ResultSet rs = ps.getResultSet()) {
    83 					processResultSet(rs, formatter);
    84 				}
    85 			} else {
    86 				/**
    87 				 * TODO: process UPDATE command
    88 				 */
    89 			}
    90 
    91 			while (ps.getMoreResults() || ps.getUpdateCount() > -1) {
    92 				/**
    93 				 * TODO: process more RS or UPDATEs
    94 				 */
    95 			}
    96 		}
    97 
    98 		formatter.writeEndResultSet();
    99 	}
   100 
   101 	private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
   102 		formatter.writeStartUpdatesResult();
   103 		formatter.writeQuery(sqlCommand.getQuery());
   104 		formatter.writeParameters(sqlCommand.getParameters());
   105 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
   106 			sqlCommand.parametrize(ps);
   107 			int updatedRowsCount = ps.executeUpdate();
   108 			formatter.writeUpdatedRowsCount(updatedRowsCount);
   109 
   110 			formatter.writeStartGeneratedKeys();
   111 			try (ResultSet rs = ps.getGeneratedKeys()) {
   112 				processResultSet(rs, formatter);
   113 			}
   114 			formatter.writeEndGeneratedKeys();
   115 
   116 		}
   117 
   118 		formatter.writeEndUpdatesResult();
   119 	}
   120 
   121 	private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
   122 		formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
   123 		int columnCount = rs.getMetaData().getColumnCount();
   124 
   125 		while (rs.next()) {
   126 			formatter.writeStartRow();
   127 
   128 			for (int i = 1; i <= columnCount; i++) {
   129 				formatter.writeColumnValue(rs.getObject(i));
   130 			}
   131 
   132 			formatter.writeEndRow();
   133 		}
   134 
   135 	}
   136 }