java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java
branchv_0
changeset 23 d8faf91519a5
parent 22 37fe883f8410
child 37 9e6f8e5d5f98
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java	Fri Dec 20 22:01:06 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java	Fri Dec 20 22:19:10 2013 +0100
     1.3 @@ -17,19 +17,49 @@
     1.4   */
     1.5  package info.globalcode.sql.dk.formatting;
     1.6  
     1.7 +import java.sql.ResultSetMetaData;
     1.8 +import java.sql.SQLException;
     1.9 +import java.util.ArrayList;
    1.10 +import java.util.List;
    1.11 +
    1.12  /**
    1.13   *
    1.14   * @author Ing. František Kučera (frantovo.cz)
    1.15   */
    1.16  public class ColumnsHeader {
    1.17  
    1.18 +	ResultSetMetaData metaData;
    1.19 +
    1.20 +	public ColumnsHeader(ResultSetMetaData metaData) {
    1.21 +		this.metaData = metaData;
    1.22 +	}
    1.23 +
    1.24  	public int getColumnCount() {
    1.25 -		/**
    1.26 -		 * TODO: getColumnCount
    1.27 -		 */
    1.28 -		throw new RuntimeException("Not yet implemented");
    1.29 +		try {
    1.30 +			return metaData.getColumnCount();
    1.31 +		} catch (SQLException e) {
    1.32 +			throw new IllegalStateException("Error during getting column count.", e);
    1.33 +		}
    1.34  	}
    1.35 -	/**
    1.36 -	 * TODO: columns descriptor
    1.37 -	 */
    1.38 +
    1.39 +	public List<ColumnDescriptor> getColumnDescriptors() {
    1.40 +		try {
    1.41 +			int count = metaData.getColumnCount();
    1.42 +			List<ColumnDescriptor> list = new ArrayList<>(count);
    1.43 +
    1.44 +			for (int i = 1; i <= count; i++) {
    1.45 +				ColumnDescriptor cd = new ColumnDescriptor();
    1.46 +				cd.setLabel(metaData.getColumnLabel(i));
    1.47 +				cd.setName(metaData.getColumnName(i));
    1.48 +				cd.setType(metaData.getColumnType(i));
    1.49 +				cd.setTypeName(metaData.getColumnTypeName(i));
    1.50 +				/** TODO: more properties */
    1.51 +				list.add(cd);
    1.52 +			}
    1.53 +
    1.54 +			return list;
    1.55 +		} catch (SQLException e) {
    1.56 +			throw new IllegalStateException("Error during building column descriptors.", e);
    1.57 +		}
    1.58 +	}
    1.59  }