franta-hg@22: /**
franta-hg@22: * SQL-DK
franta-hg@22: * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@22: *
franta-hg@22: * This program is free software: you can redistribute it and/or modify
franta-hg@22: * it under the terms of the GNU General Public License as published by
franta-hg@22: * the Free Software Foundation, either version 3 of the License, or
franta-hg@22: * (at your option) any later version.
franta-hg@22: *
franta-hg@22: * This program is distributed in the hope that it will be useful,
franta-hg@22: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@22: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@22: * GNU General Public License for more details.
franta-hg@22: *
franta-hg@22: * You should have received a copy of the GNU General Public License
franta-hg@22: * along with this program. If not, see .
franta-hg@22: */
franta-hg@22: package info.globalcode.sql.dk.formatting;
franta-hg@22:
franta-hg@23: import java.sql.ResultSetMetaData;
franta-hg@23: import java.sql.SQLException;
franta-hg@23: import java.util.ArrayList;
franta-hg@23: import java.util.List;
franta-hg@23:
franta-hg@22: /**
franta-hg@22: *
franta-hg@22: * @author Ing. František Kučera (frantovo.cz)
franta-hg@22: */
franta-hg@22: public class ColumnsHeader {
franta-hg@39:
franta-hg@37: private ResultSetMetaData metaData;
franta-hg@39:
franta-hg@23: public ColumnsHeader(ResultSetMetaData metaData) {
franta-hg@23: this.metaData = metaData;
franta-hg@23: }
franta-hg@39:
franta-hg@22: public int getColumnCount() {
franta-hg@23: try {
franta-hg@23: return metaData.getColumnCount();
franta-hg@23: } catch (SQLException e) {
franta-hg@23: throw new IllegalStateException("Error during getting column count.", e);
franta-hg@23: }
franta-hg@22: }
franta-hg@39:
franta-hg@23: public List getColumnDescriptors() {
franta-hg@23: try {
franta-hg@23: int count = metaData.getColumnCount();
franta-hg@23: List list = new ArrayList<>(count);
franta-hg@39:
franta-hg@23: for (int i = 1; i <= count; i++) {
franta-hg@23: ColumnDescriptor cd = new ColumnDescriptor();
franta-hg@39:
franta-hg@37: cd.setFirstColumn(i == 1);
franta-hg@37: cd.setLastColumn(i == count);
franta-hg@39: cd.setColumnNumber(i);
franta-hg@39:
franta-hg@23: cd.setLabel(metaData.getColumnLabel(i));
franta-hg@23: cd.setName(metaData.getColumnName(i));
franta-hg@23: cd.setType(metaData.getColumnType(i));
franta-hg@23: cd.setTypeName(metaData.getColumnTypeName(i));
franta-hg@23: /** TODO: more properties */
franta-hg@23: list.add(cd);
franta-hg@23: }
franta-hg@39:
franta-hg@23: return list;
franta-hg@23: } catch (SQLException e) {
franta-hg@23: throw new IllegalStateException("Error during building column descriptors.", e);
franta-hg@23: }
franta-hg@23: }
franta-hg@22: }