All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mvnsearch.boot.xtermjs.commands.sql.QueryResult Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package org.mvnsearch.boot.xtermjs.commands.sql;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * SQL query result
 *
 * @author linux_china
 */
public class QueryResult {

	private String SQL;

	private long elapsed;

	private List columns = new ArrayList();

	private List columnTypes = new ArrayList();

	private List> rows = new ArrayList>();

	public String getSQL() {
		return SQL;
	}

	public void setSQL(String SQL) {
		this.SQL = SQL;
	}

	public long getElapsed() {
		return elapsed;
	}

	public void setElapsed(long elapsed) {
		this.elapsed = elapsed;
	}

	public List getColumns() {
		return columns;
	}

	public void setColumns(List columns) {
		this.columns = columns;
	}

	public void addColumn(String name) {
		this.columns.add(name);
	}

	public List getColumnTypes() {
		return columnTypes;
	}

	public void setColumnTypes(List columnTypes) {
		this.columnTypes = columnTypes;
	}

	public void addColumnType(String columnType) {
		this.columnTypes.add(columnType);
	}

	public List> getRows() {
		return rows;
	}

	public void setRows(List> rows) {
		this.rows = rows;
	}

	public void addRow(List row) {
		this.rows.add(row);
	}

	@Override
	public String toString() {
		return "columns:" + columns.size() + ", rows: " + rows.size();
	}

	@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
	public String getTable() {
		List columnsWidth = new ArrayList();
		for (String column : columns) {
			columnsWidth.add(column.length());
		}
		for (List row : rows) {
			for (int i = 0; i < row.size(); i++) {
				String cellValue = getCellValue(i, row.get(i));
				int cellWidth = cellValue.getBytes().length;
				if (cellWidth > columnsWidth.get(i)) {
					columnsWidth.set(i, cellWidth);
				}
			}
		}
		List lines = new ArrayList();
		StringBuilder header = new StringBuilder();
		for (Integer width : columnsWidth) {
			header.append(StringUtils.rightPad("+", width + 3, "-"));
		}
		header.append("+");
		lines.add(header.toString());
		StringBuilder columnBuilder = new StringBuilder();
		for (int i = 0; i < columns.size(); i++) {
			columnBuilder.append("| " + StringUtils.rightPad(columns.get(i), columnsWidth.get(i) + 1));
		}
		columnBuilder.append("|");
		lines.add(columnBuilder.toString());
		lines.add(header.toString());
		for (List row : getRows()) {
			StringBuilder rowBuilder = new StringBuilder();
			for (int i = 0; i < row.size(); i++) {
				String cellValue = getCellValue(i, row.get(i));
				int bytelen = cellValue.getBytes().length;
				int charlen = cellValue.toCharArray().length;
				rowBuilder
						.append("| " + StringUtils.rightPad(cellValue, columnsWidth.get(i) + 1 - (bytelen - charlen)));
			}
			rowBuilder.append("|");
			lines.add(rowBuilder.toString());
		}
		lines.add(header.toString());
		return StringUtils.join(lines, "\r\n");
	}

	private String getCellValue(int i, Object value) {
		if (columnTypes.get(i).toLowerCase().contains("lob")) {
			return "LOB";
		}
		else {
			return value == null ? "" : value.toString().trim();
		}
	}

	public String getStatics() {
		if (rows.isEmpty()) {
			return "Empty set (" + elapsed / 1000.0 + " sec)";
		}
		else if (rows.size() == 1) {
			return "1 row in set (" + elapsed / 1000.0 + " sec)";
		}
		else {
			return rows.size() + " rows in set (" + elapsed / 1000.0 + " sec)";
		}
	}

}