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