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.ResultSet;
 | 
				
			||||||
import java.sql.ResultSetMetaData;
 | 
					import java.sql.ResultSetMetaData;
 | 
				
			||||||
import java.sql.SQLException;
 | 
					import java.sql.SQLException;
 | 
				
			||||||
 | 
					import java.sql.SQLSyntaxErrorException;
 | 
				
			||||||
import java.sql.SQLWarning;
 | 
					import java.sql.SQLWarning;
 | 
				
			||||||
import java.sql.Statement;
 | 
					import java.sql.Statement;
 | 
				
			||||||
import java.sql.Types;
 | 
					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.QueryParameter;
 | 
				
			||||||
import at.compax.tools.sql.model.QueryResult;
 | 
					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.QueryResultBuilder;
 | 
				
			||||||
import at.compax.tools.sql.model.QueryResult.Row;
 | 
					import at.compax.tools.sql.model.QueryResult.Row;
 | 
				
			||||||
import at.compax.tools.sql.model.QueryResult.Row.Column;
 | 
					import at.compax.tools.sql.model.QueryResult.Row.Column;
 | 
				
			||||||
@@ -91,8 +93,10 @@ public class SqlExecutor {
 | 
				
			|||||||
					} else if (object instanceof Long) {
 | 
										} else if (object instanceof Long) {
 | 
				
			||||||
						stmt.setLong(parameterIndex, (Long) object);
 | 
											stmt.setLong(parameterIndex, (Long) object);
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
						String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
											stmt.setString(parameterIndex, (String) object);
 | 
				
			||||||
						throw new IllegalArgumentException(message);
 | 
											String message = String.format("Unhandled paramter type <%s>.  Defaulting to String",
 | 
				
			||||||
 | 
													object.getClass().getName());
 | 
				
			||||||
 | 
											log.warn(message);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					log.warn("parameter is null");
 | 
										log.warn("parameter is null");
 | 
				
			||||||
@@ -123,15 +127,18 @@ public class SqlExecutor {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		List<Row> rows = new Vector<Row>();
 | 
							List<Row> rows = new Vector<Row>();
 | 
				
			||||||
		while (rs.next()) {
 | 
							while (rs.next()) {
 | 
				
			||||||
 | 
								ResultSetMetaData metaData = rs.getMetaData();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (rs.isFirst()) {
 | 
				
			||||||
 | 
									queryResultBuilder.columnDefinitions(getColumnDefinitions(metaData));
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			RowBuilder rowBuilder = Row.builder();
 | 
								RowBuilder rowBuilder = Row.builder();
 | 
				
			||||||
			List<Column> columns = new Vector<Column>();
 | 
								List<Column> columns = new Vector<Column>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			ResultSetMetaData metaData = rs.getMetaData();
 | 
					 | 
				
			||||||
			int columnCount = metaData.getColumnCount();
 | 
								int columnCount = metaData.getColumnCount();
 | 
				
			||||||
 | 
					 | 
				
			||||||
			for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
 | 
								for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
 | 
				
			||||||
				ColumnBuilder columnBuilder = Column.builder();
 | 
									ColumnBuilder columnBuilder = Column.builder();
 | 
				
			||||||
				columnBuilder.name(metaData.getColumnLabel(columnIndex));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				int columnType = metaData.getColumnType(columnIndex);
 | 
									int columnType = metaData.getColumnType(columnIndex);
 | 
				
			||||||
				if (columnType == Types.VARCHAR) {
 | 
									if (columnType == Types.VARCHAR) {
 | 
				
			||||||
@@ -139,8 +146,8 @@ public class SqlExecutor {
 | 
				
			|||||||
				} else if (columnType == Types.NUMERIC) {
 | 
									} else if (columnType == Types.NUMERIC) {
 | 
				
			||||||
					columnBuilder.value(rs.getLong(columnIndex));
 | 
										columnBuilder.value(rs.getLong(columnIndex));
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					String message = "Unhandled column type type: " + columnType;
 | 
										String message = String.format("Unhandled column type <%s>.  Defaulting to String", rs.getMetaData().getColumnTypeName(columnIndex));
 | 
				
			||||||
					throw new IllegalArgumentException(message);
 | 
										log.warn(message);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				columns.add(columnBuilder.build());
 | 
									columns.add(columnBuilder.build());
 | 
				
			||||||
@@ -153,6 +160,21 @@ public class SqlExecutor {
 | 
				
			|||||||
		return queryResultBuilder.rows(rows).build();
 | 
							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)
 | 
						private static void handleUserErrors(Connection conn, String sql, Statement stmt)
 | 
				
			||||||
			throws SQLException, SqlExecutionException {
 | 
								throws SQLException, SqlExecutionException {
 | 
				
			||||||
		SQLWarning warnings = stmt.getWarnings();
 | 
							SQLWarning warnings = stmt.getWarnings();
 | 
				
			||||||
@@ -165,6 +187,7 @@ public class SqlExecutor {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static List<UserError> parseUserErrors(Connection conn) throws SQLException {
 | 
						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";
 | 
							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;
 | 
							boolean first = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -135,6 +135,7 @@ public class Main {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			@Cleanup
 | 
								@Cleanup
 | 
				
			||||||
			Connection conn = getConnection(host, port, sid, username, password);
 | 
								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")));
 | 
								BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
 | 
				
			||||||
			String line;
 | 
								String line;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,19 @@ import lombok.ToString;
 | 
				
			|||||||
@ToString
 | 
					@ToString
 | 
				
			||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
					@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
				
			||||||
public class QueryResult {
 | 
					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
 | 
						@Getter
 | 
				
			||||||
	@Builder
 | 
						@Builder
 | 
				
			||||||
	@ToString
 | 
						@ToString
 | 
				
			||||||
@@ -24,8 +37,6 @@ public class QueryResult {
 | 
				
			|||||||
		@ToString
 | 
							@ToString
 | 
				
			||||||
		@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
							@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
				
			||||||
		public static class Column {
 | 
							public static class Column {
 | 
				
			||||||
			@JsonProperty("name")
 | 
					 | 
				
			||||||
			private String name;
 | 
					 | 
				
			||||||
			@JsonProperty("value")
 | 
								@JsonProperty("value")
 | 
				
			||||||
			private Object value;
 | 
								private Object value;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -34,6 +45,7 @@ public class QueryResult {
 | 
				
			|||||||
		private List<Column> columns;
 | 
							private List<Column> columns;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private List<ColumnDefinition> columnDefinitions;
 | 
				
			||||||
	@JsonProperty("rows")
 | 
						@JsonProperty("rows")
 | 
				
			||||||
	private List<Row> rows;
 | 
						private List<Row> rows;
 | 
				
			||||||
	@JsonProperty("userErrors")
 | 
						@JsonProperty("userErrors")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user