001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi.ddl;
020:
021: /**
022: * An abstraction of a databse table column.
023: *
024: * @author Michael Bell
025: * @version $Revision: 1.1 $
026: *
027: */
028: public class ColumnDefinition {
029:
030: /**
031: * Constant indicating a date data type.
032: */
033: static public int DATE = 0;
034:
035: /**
036: * Constant indicating a text data type.
037: */
038: static public int TEXT = 1;
039:
040: /**
041: * Constant indicating a number data type.
042: */
043: static public int NUMBER = 2;
044:
045: /**
046: * Constant indicating a long text data type.
047: */
048: static public int LONG_TEXT = 3;
049:
050: /**
051: * Constant indicating a boolean data type.
052: */
053: static public int BOOLEAN = 4;
054:
055: /**
056: * The name of the column.
057: */
058: private String m_sColumnName = null;
059:
060: /**
061: * The data type of the column.
062: */
063: private int m_nDataType = 1;
064:
065: /**
066: * Default value for column.
067: */
068: Object m_default = null;
069:
070: /**
071: * <code>boolean</code> flag to indicate whether column can be NULL.
072: */
073: boolean m_bAllowNull = false;
074:
075: /**
076: * <code>boolean</code> flag to indicate whether column value has to be unique.
077: */
078:
079: boolean m_bIsUnique = false;
080:
081: /**
082: * <code>boolean</code> flag to indicate whether column is a primary key.
083: */
084: boolean m_bIsPrimaryKey = false;
085:
086: /**
087: * The table name that this column references as a foreign key.
088: */
089: String m_sForeignKeyRef = null;
090:
091: /**
092: * Creates a column definition for a column with the given name
093: * and datatype.
094: *
095: * @param sColName the column name
096: * @param nDataType the data type
097: */
098: public ColumnDefinition(String sColName, int nDataType) {
099: m_sColumnName = sColName;
100: m_nDataType = nDataType;
101: }
102:
103: /**
104: * Creates a column definition for a column with the given name
105: * ,datatype and whether the column can be NULL.
106: *
107: * @param sColName the column name
108: * @param nDataType the data type
109: * @param bAllowNull <code>true</code> if the column can be NULL
110: */
111: public ColumnDefinition(String sColName, int nDataType,
112: boolean bAllowNull) {
113: this (sColName, nDataType);
114: m_bAllowNull = bAllowNull;
115: }
116:
117: /**
118: * Creates a column definition for a column.
119: *
120: * @param sColName the column name
121: * @param bIsPrimaryKey <code>true</code> if the column is a primary key
122: * @param nDataType the data type
123: */
124: public ColumnDefinition(String sColName, boolean bIsPrimaryKey,
125: int nDataType) {
126: this (sColName, nDataType);
127: m_bIsPrimaryKey = bIsPrimaryKey;
128: }
129:
130: /**
131: * Creates a column definition for a column.
132: *
133: * @param sColName the column name
134: * @param bIsPrimaryKey <code>true</code> if the column is a primary key
135: * @param nDataType the data type
136: * @param bAllowNull <code>true</code> if the column can be NULL
137: */
138: public ColumnDefinition(String sColName, boolean bIsPrimaryKey,
139: int nDataType, boolean bAllowNull) {
140: this (sColName, bIsPrimaryKey, nDataType);
141: m_bAllowNull = bAllowNull;
142: }
143:
144: /**
145: * Creates a column definition for a column.
146: *
147: * @param sColName the column name
148: * @param nDataType the data type
149: * @param defaultVal the default value
150: */
151: public ColumnDefinition(String sColName, int nDataType,
152: Object defaultVal) {
153: this (sColName, nDataType);
154: m_default = defaultVal;
155: }
156:
157: /**
158: * Creates a column definition for a column.
159: *
160: * @param sColName the column name
161: * @param bIsPrimaryKey <code>true</code> if the column is a primary key
162: * @param nDataType the data type
163: * @param defaultVal the default value
164: */
165: public ColumnDefinition(String sColName, boolean bIsPrimaryKey,
166: int nDataType, Object defaultVal) {
167: this (sColName, nDataType, defaultVal);
168: m_bIsPrimaryKey = bIsPrimaryKey;
169: m_bIsUnique = true;
170: }
171:
172: /**
173: * Returns the column name.
174: *
175: * @return the column name
176: */
177: public String getName() {
178: return m_sColumnName;
179: }
180:
181: /**
182: * Returns the data type.
183: *
184: * @return the data type
185: */
186: public int getDataType() {
187: return m_nDataType;
188: }
189:
190: /**
191: * Returns <code>true</code> if the column is a primary key.
192: *
193: * @return <code>true</code> if the column is a primary key
194: */
195: public boolean isPrimaryKey() {
196: return m_bIsPrimaryKey;
197: }
198:
199: /**
200: * Returns <code>true</code> if the column values must be unique.
201: *
202: * @return <code>true</code> if the column values must be unique
203: */
204: public boolean isUnique() {
205: return m_bIsUnique;
206: }
207:
208: /**
209: * Sets whether the column values must be unique.
210: *
211: * @param bIsUnique <code>true</code> if the column values must be unique
212: */
213: public void setIsUnique(boolean bIsUnique) {
214: m_bIsUnique = bIsUnique;
215: }
216:
217: /**
218: * Returns <code>true</code> if the column can have NULL values.
219: *
220: * @return <code>true</code> if the column can have NULL values
221: */
222: public boolean allowNulls() {
223: return m_bAllowNull;
224: }
225:
226: /**
227: * Returns the default value for the column.
228: *
229: * @return the default value for the column
230: */
231: public Object getDefault() {
232: return m_default;
233: }
234:
235: /**
236: * Returns <code>true</code> if the column is/has a foreign key.
237: *
238: * @return <code>true</code> if the column is/has a foreign key
239: */
240: public boolean isForeignKey() {
241: return m_sForeignKeyRef != null;
242: }
243:
244: /**
245: * Returns the table name of the foreign key reference.
246: *
247: * @return the foreign key reference
248: */
249: public String getForeignKeyReference() {
250: return m_sForeignKeyRef;
251: }
252:
253: /**
254: * Adds a foreign key reference .
255: *
256: * @param string the table name of the foreign key reference
257: */
258: public void addForeignKey(String sForeignKeyRef) {
259: m_sForeignKeyRef = sForeignKeyRef;
260: }
261:
262: }
|