001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.dbschema.util;
023:
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026:
027: /**
028: *
029: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
030: */
031: public final class Column {
032: private static final String TABLE_NAME = "TABLE_NAME";
033: private static final String COLUMN_NAME = "COLUMN_NAME";
034: private static final String DATA_TYPE = "DATA_TYPE";
035: private static final String TYPE_NAME = "TYPE_NAME";
036: private static final String COLUMN_SIZE = "COLUMN_SIZE";
037: private static final String IS_NULLABLE = "IS_NULLABLE";
038: private static final String COLUMN_DEF = "COLUMN_DEF";
039:
040: private final String tableName;
041: private final String name;
042: private final short dataType;
043: private final String typeName;
044: private final int columnSize;
045: private final String nullable;
046: private final String columnDef;
047:
048: // Constructors
049:
050: public Column(ResultSet rs) throws SQLException {
051: tableName = rs.getString(TABLE_NAME);
052: name = rs.getString(COLUMN_NAME);
053: dataType = rs.getShort(DATA_TYPE);
054: typeName = rs.getString(TYPE_NAME);
055: columnSize = rs.getInt(COLUMN_SIZE);
056: nullable = rs.getString(IS_NULLABLE);
057: columnDef = rs.getString(COLUMN_DEF);
058: }
059:
060: // Public
061:
062: public String getTableName() {
063: return tableName;
064: }
065:
066: public String getName() {
067: return name;
068: }
069:
070: public short getDataType() {
071: return dataType;
072: }
073:
074: public String getTypeName() {
075: return typeName;
076: }
077:
078: public int getColumnSize() {
079: return columnSize;
080: }
081:
082: public boolean isNotNullable() {
083: return nullable.equalsIgnoreCase("NO");
084: }
085:
086: public String getColumnDef() {
087: return columnDef;
088: }
089:
090: public void assertName(String name) throws Exception {
091: if (this .name.equals(name))
092: return;
093: throw new Exception("Column name: is " + this .name
094: + " but expected " + name);
095: }
096:
097: public void assertDataType(int dataType) throws Exception {
098: if (this .dataType == dataType)
099: return;
100: throw new Exception("Data type: is " + this .dataType
101: + " but expected " + dataType);
102: }
103:
104: public void assertNotNull(boolean notNullable) throws Exception {
105: if (this .nullable.equalsIgnoreCase("NO") == notNullable)
106: return;
107: throw new Exception("Column not nullable: is " + !notNullable
108: + " but expected " + notNullable);
109: }
110:
111: public void assertTypeNotNull(int dataType, boolean notNull)
112: throws Exception {
113: assertDataType(dataType);
114: assertNotNull(notNull);
115: }
116:
117: public String toString() {
118: StringBuffer sb = new StringBuffer();
119: sb.append('[').append(TABLE_NAME).append('=').append(tableName)
120: .append(';').append(COLUMN_NAME).append('=').append(
121: name).append(';').append(DATA_TYPE).append('=')
122: .append(dataType).append(';').append(TYPE_NAME).append(
123: '=').append(typeName).append(';').append(
124: COLUMN_SIZE).append('=').append(columnSize)
125: .append(';').append(IS_NULLABLE).append('=').append(
126: nullable).append(';').append(COLUMN_DEF)
127: .append('=').append(columnDef).append(';').append(']');
128: return sb.toString();
129: }
130: }
|