Restructure QueryResult
The QueryResult now has the ColumnDefinitions as a main property.
This commit is contained in:
		@@ -6,6 +6,7 @@ import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.ResultSetMetaData;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import java.sql.SQLSyntaxErrorException;
 | 
			
		||||
import java.sql.SQLWarning;
 | 
			
		||||
import java.sql.Statement;
 | 
			
		||||
import java.sql.Types;
 | 
			
		||||
@@ -19,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
import at.compax.tools.sql.model.QueryParameter;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult.ColumnDefinition;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult.QueryResultBuilder;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult.Row;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult.Row.Column;
 | 
			
		||||
@@ -91,8 +93,10 @@ public class SqlExecutor {
 | 
			
		||||
					} else if (object instanceof Long) {
 | 
			
		||||
						stmt.setLong(parameterIndex, (Long) object);
 | 
			
		||||
					} else {
 | 
			
		||||
						String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
			
		||||
						throw new IllegalArgumentException(message);
 | 
			
		||||
						stmt.setString(parameterIndex, (String) object);
 | 
			
		||||
						String message = String.format("Unhandled paramter type <%s>.  Defaulting to String",
 | 
			
		||||
								object.getClass().getName());
 | 
			
		||||
						log.warn(message);
 | 
			
		||||
					}
 | 
			
		||||
				} else {
 | 
			
		||||
					log.warn("parameter is null");
 | 
			
		||||
@@ -123,15 +127,18 @@ public class SqlExecutor {
 | 
			
		||||
 | 
			
		||||
		List<Row> rows = new Vector<Row>();
 | 
			
		||||
		while (rs.next()) {
 | 
			
		||||
			ResultSetMetaData metaData = rs.getMetaData();
 | 
			
		||||
 | 
			
		||||
			if (rs.isFirst()) {
 | 
			
		||||
				queryResultBuilder.columnDefinitions(getColumnDefinitions(metaData));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			RowBuilder rowBuilder = Row.builder();
 | 
			
		||||
			List<Column> columns = new Vector<Column>();
 | 
			
		||||
 | 
			
		||||
			ResultSetMetaData metaData = rs.getMetaData();
 | 
			
		||||
			int columnCount = metaData.getColumnCount();
 | 
			
		||||
 | 
			
		||||
			for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
 | 
			
		||||
				ColumnBuilder columnBuilder = Column.builder();
 | 
			
		||||
				columnBuilder.name(metaData.getColumnLabel(columnIndex));
 | 
			
		||||
 | 
			
		||||
				int columnType = metaData.getColumnType(columnIndex);
 | 
			
		||||
				if (columnType == Types.VARCHAR) {
 | 
			
		||||
@@ -139,8 +146,8 @@ public class SqlExecutor {
 | 
			
		||||
				} else if (columnType == Types.NUMERIC) {
 | 
			
		||||
					columnBuilder.value(rs.getLong(columnIndex));
 | 
			
		||||
				} else {
 | 
			
		||||
					String message = "Unhandled column type type: " + columnType;
 | 
			
		||||
					throw new IllegalArgumentException(message);
 | 
			
		||||
					String message = String.format("Unhandled column type <%s>.  Defaulting to String", rs.getMetaData().getColumnTypeName(columnIndex));
 | 
			
		||||
					log.warn(message);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				columns.add(columnBuilder.build());
 | 
			
		||||
@@ -153,6 +160,21 @@ public class SqlExecutor {
 | 
			
		||||
		return queryResultBuilder.rows(rows).build();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static Vector<ColumnDefinition> getColumnDefinitions(ResultSetMetaData metaData) throws SQLException {
 | 
			
		||||
		Vector<ColumnDefinition> columnDefinitions = new Vector<ColumnDefinition>();
 | 
			
		||||
		int columnCount = metaData.getColumnCount();
 | 
			
		||||
 | 
			
		||||
		for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
 | 
			
		||||
			columnDefinitions.add(ColumnDefinition.builder() //
 | 
			
		||||
					.index(columnIndex) //
 | 
			
		||||
					.name(metaData.getColumnLabel(columnIndex)) //
 | 
			
		||||
					.type(metaData.getColumnTypeName(columnIndex)) //
 | 
			
		||||
					.build());
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return columnDefinitions;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static void handleUserErrors(Connection conn, String sql, Statement stmt)
 | 
			
		||||
			throws SQLException, SqlExecutionException {
 | 
			
		||||
		SQLWarning warnings = stmt.getWarnings();
 | 
			
		||||
@@ -165,6 +187,7 @@ public class SqlExecutor {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static List<UserError> parseUserErrors(Connection conn) throws SQLException {
 | 
			
		||||
		log.debug("Retrieving user errors");
 | 
			
		||||
		String userErrorsSql = "select name, type, sequence, line, position, text, attribute, message_number from user_errors where type <> 'JAVA CLASS' order by sequence";
 | 
			
		||||
		boolean first = true;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -135,6 +135,7 @@ public class Main {
 | 
			
		||||
 | 
			
		||||
			@Cleanup
 | 
			
		||||
			Connection conn = getConnection(host, port, sid, username, password);
 | 
			
		||||
			log.info("Waiting for input...");
 | 
			
		||||
 | 
			
		||||
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
 | 
			
		||||
			String line;
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,19 @@ import lombok.ToString;
 | 
			
		||||
@ToString
 | 
			
		||||
@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
			
		||||
public class QueryResult {
 | 
			
		||||
	@Getter
 | 
			
		||||
	@Builder
 | 
			
		||||
	@ToString
 | 
			
		||||
	@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
			
		||||
	public static class ColumnDefinition {
 | 
			
		||||
		@JsonProperty("index")
 | 
			
		||||
		private int index;
 | 
			
		||||
		@JsonProperty("name")
 | 
			
		||||
		private String name;
 | 
			
		||||
		@JsonProperty("type")
 | 
			
		||||
		private String type;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Getter
 | 
			
		||||
	@Builder
 | 
			
		||||
	@ToString
 | 
			
		||||
@@ -24,8 +37,6 @@ public class QueryResult {
 | 
			
		||||
		@ToString
 | 
			
		||||
		@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
			
		||||
		public static class Column {
 | 
			
		||||
			@JsonProperty("name")
 | 
			
		||||
			private String name;
 | 
			
		||||
			@JsonProperty("value")
 | 
			
		||||
			private Object value;
 | 
			
		||||
		}
 | 
			
		||||
@@ -34,6 +45,7 @@ public class QueryResult {
 | 
			
		||||
		private List<Column> columns;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private List<ColumnDefinition> columnDefinitions;
 | 
			
		||||
	@JsonProperty("rows")
 | 
			
		||||
	private List<Row> rows;
 | 
			
		||||
	@JsonProperty("userErrors")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user