001: /*
002: * $Id: Column.java,v 1.34 2005/12/20 18:32:40 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb;
042:
043: import java.io.Serializable;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: /**
048: * Describes a column within a {@link Table}.
049: *
050: * @version $Revision: 1.34 $ $Date: 2005/12/20 18:32:40 $
051: * @author Chuck Burdick
052: * @author Rodney Waldhoff
053: * @author Ahimanikya Satapathy
054: */
055: public class Column implements Serializable {
056:
057: /**
058: * Key for setting and retrieving the sqlType in this column's configuration
059: */
060: public static final String COLUMN_SQL_TYPE_CONFIG_KEY = "sqlType";
061:
062: /**
063: * Key for setting and retrieving the {@link DataType}in this column's configuration
064: */
065: public static final String DATA_TYPE_CONFIG_KEY = "type";
066:
067: /**
068: * Key for setting and retrieving the {@link Selectable default value}in this
069: * column's configuration
070: */
071: public static final String DEFAULT_VALUE_CONFIG_KEY = "defaultValue";
072:
073: /**
074: * Key for setting and retrieving the name in this column's configuration
075: */
076: public static final String NAME_CONFIG_KEY = "name";
077:
078: /**
079: * Key for setting and retrieving the Identity column generation type
080: */
081: public static final String IDENTITY_GENERATION_TYPE = "identityType";
082:
083: public static final String GENERATED_ALWAYS = "always";
084: public static final String GENERATED_BY_DEFAULT = "default";
085:
086: /**
087: * Key for setting and retrieving generated column expression
088: */
089: public static final String GENERATED_COLUMN_TYPE = "generatedCol";
090:
091: /**
092: * Create column with the given <i>name </i> and <i>type </i>.
093: *
094: * @param name the name of this column, which MUST NOT be <code>null</code>
095: * @param type the {@link DataType}of this column, which MUST NOT be
096: * <code>null</code>
097: * @throws NullPointerException if either parameter is <code>null</code>
098: */
099: public Column(String name, DataType type)
100: throws NullPointerException {
101: this (name, type, null);
102: }
103:
104: /**
105: * Create column with the given <i>name </i> and <i>type </i>.
106: *
107: * @param name the name of this column, which MUST NOT be <code>null</code>
108: * @param type the {@link DataType}of this column, which MUST NOT be
109: * <code>null</code>
110: * @param config name-value pairs that configure this column
111: * @param defaultValue the {@link Selectable default value}for this column, which may
112: * be <code>null</code>
113: * @throws NullPointerException if either name or type is <code>null</code>
114: */
115: public Column(String name, DataType type, Selectable defaultValue)
116: throws NullPointerException {
117: if (null == name) {
118: throw new NullPointerException(
119: "name parameter must not be null");
120: }
121: if (null == type) {
122: throw new NullPointerException(
123: "type parameter must not be null");
124: }
125: _config = new HashMap();
126: getConfiguration().put(NAME_CONFIG_KEY, name);
127: getConfiguration().put(DATA_TYPE_CONFIG_KEY, type);
128: getConfiguration().put(DEFAULT_VALUE_CONFIG_KEY, defaultValue);
129: }
130:
131: /** Two {@link Column}s are equal if they have the same name. */
132: public boolean equals(Object that) {
133: if (that instanceof Column) {
134: Column col = (Column) that;
135: return getName().equals(col.getName());
136: }
137: return false;
138: }
139:
140: public final Map getConfiguration() {
141: return _config;
142: }
143:
144: /**
145: * Get the {@link DataType}of this column.
146: */
147: public final DataType getDataType() {
148: return (DataType) getConfiguration().get(DATA_TYPE_CONFIG_KEY);
149: }
150:
151: public final Selectable getDefault() {
152: return (Selectable) getConfiguration().get(
153: DEFAULT_VALUE_CONFIG_KEY);
154: }
155:
156: /**
157: * Get the name of this column.
158: */
159: public final String getName() {
160: return (String) getConfiguration().get(NAME_CONFIG_KEY);
161: }
162:
163: public int getScale() {
164: return getDataType().getScale();
165: }
166:
167: public int getSize() {
168: return getDataType().getPrecision();
169: }
170:
171: public String getSqlType() {
172: return (String) getConfiguration().get(
173: COLUMN_SQL_TYPE_CONFIG_KEY);
174: }
175:
176: public boolean hasDefault() {
177: return null != getDefault();
178: }
179:
180: public boolean isIdentityColumn() {
181: return null != getIdentityType();
182: }
183:
184: public boolean isGeneratedAlways() {
185: return GENERATED_ALWAYS.equals(getIdentityType());
186: }
187:
188: public boolean isGeneratedByDefault() {
189: return GENERATED_BY_DEFAULT.equals(getIdentityType());
190: }
191:
192: public boolean isDerivedColumn() {
193: return GENERATED_ALWAYS.equals(getConfiguration().get(
194: GENERATED_COLUMN_TYPE));
195: }
196:
197: public void setGeneratedColType(String type) {
198: getConfiguration().put(GENERATED_COLUMN_TYPE, type);
199: }
200:
201: public String getGeneratedColType() {
202: return (String) getConfiguration().get(GENERATED_COLUMN_TYPE);
203: }
204:
205: public final String getIdentityType() {
206: return (String) getConfiguration()
207: .get(IDENTITY_GENERATION_TYPE);
208: }
209:
210: public void setIdentityType(String type) {
211: getConfiguration().put(IDENTITY_GENERATION_TYPE, type);
212: }
213:
214: public int hashCode() {
215: return getName().hashCode();
216: }
217:
218: public void setSqlType(String type) {
219: getConfiguration().put(COLUMN_SQL_TYPE_CONFIG_KEY, type);
220: }
221:
222: public String toString() {
223: return getName();
224: }
225:
226: private Map _config;
227: private static final long serialVersionUID = -9163914166152422736L;
228: }
|