KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > dbschema > util > Column


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.dbschema.util;
23
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27
28 /**
29  *
30  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
31  */

32 public final class Column
33 {
34    private static final String JavaDoc TABLE_NAME = "TABLE_NAME";
35    private static final String JavaDoc COLUMN_NAME = "COLUMN_NAME";
36    private static final String JavaDoc DATA_TYPE = "DATA_TYPE";
37    private static final String JavaDoc TYPE_NAME = "TYPE_NAME";
38    private static final String JavaDoc COLUMN_SIZE = "COLUMN_SIZE";
39    private static final String JavaDoc IS_NULLABLE = "IS_NULLABLE";
40    private static final String JavaDoc COLUMN_DEF = "COLUMN_DEF";
41
42    private final String JavaDoc tableName;
43    private final String JavaDoc name;
44    private final short dataType;
45    private final String JavaDoc typeName;
46    private final int columnSize;
47    private final String JavaDoc nullable;
48    private final String JavaDoc columnDef;
49
50    // Constructors
51

52    public Column(ResultSet JavaDoc rs) throws SQLException JavaDoc
53    {
54       tableName = rs.getString(TABLE_NAME);
55       name = rs.getString(COLUMN_NAME);
56       dataType = rs.getShort(DATA_TYPE);
57       typeName = rs.getString(TYPE_NAME);
58       columnSize = rs.getInt(COLUMN_SIZE);
59       nullable = rs.getString(IS_NULLABLE);
60       columnDef = rs.getString(COLUMN_DEF);
61    }
62
63    // Public
64

65    public String JavaDoc getTableName()
66    {
67       return tableName;
68    }
69
70    public String JavaDoc getName()
71    {
72       return name;
73    }
74
75    public short getDataType()
76    {
77       return dataType;
78    }
79
80    public String JavaDoc getTypeName()
81    {
82       return typeName;
83    }
84
85    public int getColumnSize()
86    {
87       return columnSize;
88    }
89
90    public boolean isNotNullable()
91    {
92       return nullable.equalsIgnoreCase("NO");
93    }
94
95    public String JavaDoc getColumnDef()
96    {
97       return columnDef;
98    }
99
100    public void assertName(String JavaDoc name) throws Exception JavaDoc
101    {
102       if(this.name.equals(name))
103          return;
104       throw new Exception JavaDoc("Column name: is " + this.name + " but expected " + name);
105    }
106
107    public void assertDataType(int dataType) throws Exception JavaDoc
108    {
109       if(this.dataType == dataType)
110          return;
111       throw new Exception JavaDoc("Data type: is " + this.dataType + " but expected " + dataType);
112    }
113
114    public void assertNotNull(boolean notNullable) throws Exception JavaDoc
115    {
116       if(this.nullable.equalsIgnoreCase("NO") == notNullable)
117          return;
118       throw new Exception JavaDoc("Column not nullable: is " + !notNullable + " but expected " + notNullable);
119    }
120
121    public void assertTypeNotNull(int dataType, boolean notNull) throws Exception JavaDoc
122    {
123       assertDataType(dataType);
124       assertNotNull(notNull);
125    }
126
127    public String JavaDoc toString()
128    {
129       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
130       sb.append('[').
131          append(TABLE_NAME).append('=').append(tableName).append(';').
132          append(COLUMN_NAME).append('=').append(name).append(';').
133          append(DATA_TYPE).append('=').append(dataType).append(';').
134          append(TYPE_NAME).append('=').append(typeName).append(';').
135          append(COLUMN_SIZE).append('=').append(columnSize).append(';').
136          append(IS_NULLABLE).append('=').append(nullable).append(';').
137          append(COLUMN_DEF).append('=').append(columnDef).append(';').
138          append(']');
139       return sb.toString();
140    }
141 }
142
Popular Tags