java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/ColumnsHeader.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 245 b6ff5b7a8422
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package info.globalcode.sql.dk.formatting;
    18 
    19 import java.sql.ResultSetMetaData;
    20 import java.sql.SQLException;
    21 import java.util.ArrayList;
    22 import java.util.List;
    23 
    24 /**
    25  *
    26  * @author Ing. František Kučera (frantovo.cz)
    27  */
    28 public class ColumnsHeader {
    29 
    30 	private final ResultSetMetaData metaData;
    31 
    32 	public ColumnsHeader(ResultSetMetaData metaData) {
    33 		this.metaData = metaData;
    34 	}
    35 
    36 	public int getColumnCount() {
    37 		try {
    38 			return metaData.getColumnCount();
    39 		} catch (SQLException e) {
    40 			throw new IllegalStateException("Error during getting column count.", e);
    41 		}
    42 	}
    43 
    44 	public List<ColumnDescriptor> getColumnDescriptors() {
    45 		try {
    46 			int count = metaData.getColumnCount();
    47 			List<ColumnDescriptor> list = new ArrayList<>(count);
    48 
    49 			for (int i = 1; i <= count; i++) {
    50 				ColumnDescriptor cd = new ColumnDescriptor();
    51 
    52 				cd.setFirstColumn(i == 1);
    53 				cd.setLastColumn(i == count);
    54 				cd.setColumnNumber(i);
    55 
    56 				cd.setLabel(metaData.getColumnLabel(i));
    57 				cd.setName(metaData.getColumnName(i));
    58 				cd.setType(metaData.getColumnType(i));
    59 				cd.setTypeName(metaData.getColumnTypeName(i));
    60 				cd.setTableName(metaData.getTableName(i));
    61 				list.add(cd);
    62 			}
    63 
    64 			return list;
    65 		} catch (SQLException e) {
    66 			throw new IllegalStateException("Error during building column descriptors.", e);
    67 		}
    68 	}
    69 }