001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist.utils;
020:
021: import java.sql.ResultSet;
022: import java.sql.ResultSetMetaData;
023: import java.sql.SQLException;
024: import java.sql.Types;
025: import jpersist.*;
026:
027: /**
028: * This class provides column information for a given result set.
029: */
030:
031: public class ColumnInformation {
032: int displaySize, type, precision, scale;
033: String className, label, name, typeName, schemaName, tableName;
034:
035: ColumnInformation(String className, String label, String name,
036: int displaySize, int type, String typeName, int precision,
037: int scale, String schemaName, String tableName) {
038: this .className = className;
039: this .label = label;
040: this .name = name;
041: this .displaySize = displaySize;
042: this .type = type;
043: this .typeName = typeName;
044: this .precision = precision;
045: this .scale = scale;
046: this .schemaName = schemaName;
047: this .tableName = tableName;
048: }
049:
050: public String getClassName() {
051: return className;
052: }
053:
054: public String getLabel() {
055: return label;
056: }
057:
058: public String getName() {
059: return name;
060: }
061:
062: public int getDisplaySize() {
063: return displaySize;
064: }
065:
066: public int getType() {
067: return type;
068: }
069:
070: public String getTypeName() {
071: return typeName;
072: }
073:
074: public int getPrecision() {
075: return precision;
076: }
077:
078: public int getScale() {
079: return scale;
080: }
081:
082: public String getSchemaName() {
083: return schemaName;
084: }
085:
086: public String getTableName() {
087: return tableName;
088: }
089:
090: public boolean isTypeQuoted() {
091: return isTypeQuoted(type);
092: }
093:
094: public static boolean isTypeQuoted(int dataType) {
095: if (dataType == Types.DATE || dataType == Types.LONGVARCHAR
096: || dataType == Types.TIME
097: || dataType == Types.TIMESTAMP
098: || dataType == Types.VARCHAR)
099: return true;
100:
101: return false;
102: }
103:
104: public static ColumnInformation[] getColumnInformation(
105: ResultSet resultSet) throws JPersistException, SQLException {
106: ResultSetMetaData metaData = resultSet.getMetaData();
107: ColumnInformation columnInformation[] = new ColumnInformation[metaData
108: .getColumnCount()];
109:
110: for (int i = 0; i < metaData.getColumnCount(); i++) {
111: columnInformation[i] = new ColumnInformation(metaData
112: .getColumnClassName(i + 1), metaData
113: .getColumnLabel(i + 1), metaData
114: .getColumnName(i + 1), metaData
115: .getColumnDisplaySize(i + 1), metaData
116: .getColumnType(i + 1), metaData
117: .getColumnTypeName(i + 1), metaData
118: .getPrecision(i + 1), metaData.getScale(i + 1),
119: metaData.getSchemaName(i + 1), metaData
120: .getTableName(i + 1));
121: }
122:
123: return columnInformation;
124: }
125: }
|