java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 18:19:38 +0100
branchv_0
changeset 29 d66858b4b563
parent 28 57c44a6baedb
child 34 9335cf31c0f2
permissions -rw-r--r--
more configuration, more JAXB, more formatters
     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.Formatter;
    23 import java.sql.Connection;
    24 import java.sql.DriverManager;
    25 import java.sql.PreparedStatement;
    26 import java.sql.ResultSet;
    27 import java.sql.SQLException;
    28 
    29 /**
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class DatabaseConnection {
    34 
    35 	private DatabaseDefinition databaseDefinition;
    36 	private Connection connection;
    37 
    38 	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
    39 		this.databaseDefinition = databaseDefinition;
    40 
    41 		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getName(), databaseDefinition.getPassword());
    42 	}
    43 
    44 	public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    45 		formatter.writeStartDatabase(databaseDefinition);
    46 		processCommand(sqlCommand, formatter);
    47 		formatter.writeEndDatabase();
    48 	}
    49 
    50 	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
    51 		formatter.writeStartDatabase(databaseDefinition);
    52 		while (batch.hasNext()) {
    53 			processCommand(batch.next(), formatter);
    54 		}
    55 		formatter.writeEndDatabase();
    56 	}
    57 
    58 	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    59 		SQLCommand.COMMAND_TYPE commandType = sqlCommand.getCommandType();
    60 		switch (commandType) {
    61 			case QUERY:
    62 				processQueryCommand(sqlCommand, formatter);
    63 				break;
    64 			case UPDATE:
    65 				processUpdateCommand(sqlCommand, formatter);
    66 				break;
    67 			default:
    68 				throw new IllegalArgumentException("Unexpected command type: " + commandType);
    69 		}
    70 	}
    71 
    72 	private void processQueryCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    73 		formatter.writeStartResultSet();
    74 		formatter.writeQuery(sqlCommand.getQuery());
    75 		/** TODO: formatter.writeParameters(null); */
    76 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
    77 			sqlCommand.parametrize(ps);
    78 			try (ResultSet rs = ps.executeQuery()) {
    79 				processResultSet(rs, formatter);
    80 			}
    81 		}
    82 
    83 		formatter.writeEndResultSet();
    84 	}
    85 
    86 	private void processUpdateCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    87 		formatter.writeStartUpdatesResult();
    88 		formatter.writeQuery(sqlCommand.getQuery());
    89 		/** TODO: formatter.writeParameters(null); */
    90 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
    91 			sqlCommand.parametrize(ps);
    92 			int updatedRowsCount = ps.executeUpdate();
    93 			formatter.writeUpdatedRowsCount(updatedRowsCount);
    94 
    95 			formatter.writeStartGeneratedKeys();
    96 			try (ResultSet rs = ps.getGeneratedKeys()) {
    97 				processResultSet(rs, formatter);
    98 			}
    99 			formatter.writeEndGeneratedKeys();
   100 
   101 		}
   102 
   103 		formatter.writeEndUpdatesResult();
   104 	}
   105 
   106 	private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException {
   107 		/** TODO: formatter.writeColumnsHeader(null); */
   108 		while (rs.next()) {
   109 			formatter.writeStartRow();
   110 
   111 			/** TODO: formatter.writeColumnValue(rs.get); */
   112 			formatter.writeEndRow();
   113 		}
   114 	}
   115 }