KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > TableColumn


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.impl;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.netbeans.lib.ddl.CheckConstraintDescriptor;
29 import org.netbeans.lib.ddl.DatabaseSpecification;
30 import org.netbeans.lib.ddl.DDLException;
31 import org.netbeans.lib.ddl.TableColumnDescriptor;
32
33 /**
34 * Implementation of table column.
35 */

36 public class TableColumn extends AbstractTableColumn implements Serializable JavaDoc, TableColumnDescriptor, CheckConstraintDescriptor {
37     /** String constant for column type */
38     public static final String JavaDoc COLUMN = "COLUMN"; // NOI18N
39
/** String constant for column check */
40     public static final String JavaDoc CHECK = "CHECK"; // NOI18N
41
/** String constant for unique column type */
42     public static final String JavaDoc UNIQUE = "UNIQUE"; // NOI18N
43
/** String constant for primary key */
44     public static final String JavaDoc PRIMARY_KEY = "PRIMARY_KEY"; // NOI18N
45
/** String constant for foreign key */
46     public static final String JavaDoc FOREIGN_KEY = "FOREIGN_KEY"; // NOI18N
47
/** String constant for check constraint */
48     public static final String JavaDoc CHECK_CONSTRAINT = "CHECK_CONSTRAINT"; // NOI18N
49
/** String constant for unique constraint */
50     public static final String JavaDoc UNIQUE_CONSTRAINT = "UNIQUE_CONSTRAINT"; // NOI18N
51
/** String constant for primary key constraint */
52     public static final String JavaDoc PRIMARY_KEY_CONSTRAINT = "PRIMARY_KEY_CONSTRAINT"; // NOI18N
53
/** String constant for foreign key constraint */
54     public static final String JavaDoc FOREIGN_KEY_CONSTRAINT = "FOREIGN_KEY_CONSTRAINT"; // NOI18N
55

56     /** Column type */
57     int type;
58
59     /** Column size */
60     int size;
61
62     /** Column decimal size */
63     int decsize;
64
65     /** Null allowed */
66     boolean nullable;
67
68     /** Default value */
69     String JavaDoc defval;
70
71     /** Check expression */
72     String JavaDoc checke;
73     
74     /** Table constraint columns */
75     Vector JavaDoc constraintColumns;
76
77     static final long serialVersionUID =4298150043758715392L;
78     /** Constructor */
79     public TableColumn() {
80         size = 0;
81         decsize = 0;
82         nullable = true;
83     }
84
85     /** Returns type of column */
86     public int getColumnType() {
87         return type;
88     }
89
90     /** Sets type of column */
91     public void setColumnType(int columnType) {
92         type = columnType;
93     }
94
95     /** Returns column size */
96     public int getColumnSize() {
97         return size;
98     }
99
100     /** Sets size of column */
101     public void setColumnSize(int csize) {
102         size = csize;
103     }
104
105     /** Returns decimal digits of column */
106     public int getDecimalSize() {
107         return decsize;
108     }
109
110     /** Sets decimal digits of column */
111     public void setDecimalSize(int dsize) {
112         decsize = dsize;
113     }
114
115     /** Nulls allowed? */
116     public boolean isNullAllowed() {
117         return nullable;
118     }
119
120     /** Sets null property */
121     public void setNullAllowed(boolean flag) {
122         nullable = flag;
123     }
124
125     /** Returns default value of column */
126     public String JavaDoc getDefaultValue() {
127         return defval;
128     }
129
130     /** Sets default value of column */
131     public void setDefaultValue(String JavaDoc val) {
132         defval = val;
133     }
134
135     /** Returns column check condition */
136     public String JavaDoc getCheckCondition() {
137         return checke;
138     }
139
140     /** Sets column check condition */
141     public void setCheckCondition(String JavaDoc val) {
142         checke = val;
143     }
144
145     /** Returns table constraint columns */
146     public Vector JavaDoc getTableConstraintColumns() {
147         return constraintColumns;
148     }
149
150     /** Sets column check condition */
151     public void setTableConstraintColumns(Vector JavaDoc columns) {
152         constraintColumns = columns;
153     }
154
155     /**
156     * Returns properties and it's values supported by this object.
157     * object.name Name of the object; use setObjectName()
158     * object.owner Name of the object; use setObjectOwner()
159     * column.size Size of column
160     * column.decsize Deimal size of size
161     * column.type Type of column
162     * default.value Condition of column
163     * Throws DDLException if object name is not specified.
164     */

165     public Map JavaDoc getColumnProperties(AbstractCommand cmd) throws DDLException {
166         DatabaseSpecification spec = cmd.getSpecification();
167         Map JavaDoc args = super.getColumnProperties(cmd);
168         String JavaDoc stype = spec.getType(type);
169         Vector JavaDoc charactertypes = (Vector JavaDoc)spec.getProperties().get("CharacterTypes"); // NOI18N
170
String JavaDoc strdelim = (String JavaDoc)spec.getProperties().get("StringDelimiter"); // NOI18N
171
Vector JavaDoc sizelesstypes = (Vector JavaDoc)spec.getProperties().get("SizelessTypes"); // NOI18N
172
String JavaDoc coldelim = (String JavaDoc)spec.getProperties().get("ArgumentListDelimiter"); // NOI18N
173

174         // Decimal size for sizeless type
175
if (sizelesstypes != null && size > 0) {
176             if (!sizelesstypes.contains(stype)) {
177                 if (size > 0)
178                     args.put("column.size", String.valueOf(size)); // NOI18N
179
if (decsize > 0)
180                     args.put("column.decsize", String.valueOf(decsize)); // NOI18N
181
}
182         }
183
184         String JavaDoc qdefval = defval;
185
186         if (qdefval != null && charactertypes.contains(spec.getType(type)) && !qdefval.startsWith(strdelim) && !qdefval.endsWith(strdelim))
187             if (!qdefval.startsWith("(" + strdelim) && !qdefval.endsWith(strdelim + ")")) //hack for MSSQLServer, default value is encapsulated in () so I can't generate '()'
188
qdefval = strdelim + defval + strdelim;
189         
190         String JavaDoc dbType = spec.getType(type);
191         String JavaDoc dbTypeSuffix = null;
192         Map JavaDoc suffixTypeMap = (Map JavaDoc)spec.getProperties().get("TypePrefixSuffixMap"); // NOI18N
193
if (suffixTypeMap != null) {
194             Map JavaDoc dbTypePrefixSuffix = (Map JavaDoc)suffixTypeMap.get(dbType);
195             if (dbTypePrefixSuffix != null) {
196                 dbType = (String JavaDoc)dbTypePrefixSuffix.get("Prefix"); // NOI18N
197
dbTypeSuffix = (String JavaDoc)dbTypePrefixSuffix.get("Suffix"); // NOI18N
198
}
199         }
200         args.put("column.type", dbType);
201         if (dbTypeSuffix != null) {
202             args.put("column.type.suffix", dbTypeSuffix);
203         }
204         
205         if (!nullable)
206             args.put("column.notnull", ""); // NOI18N
207

208         if (!(! nullable && qdefval != null && (qdefval.equalsIgnoreCase("null") || qdefval.equalsIgnoreCase("'null'") || qdefval.equalsIgnoreCase("\"null\"")))) // NOI18N
209
if (defval != null && !defval.equals(""))
210                 args.put("default.value", qdefval); // NOI18N
211

212         if (checke != null)
213             args.put("check.condition", checke); // NOI18N
214
if (constraintColumns != null) {
215             String JavaDoc cols = "";
216             Enumeration JavaDoc col_e = constraintColumns.elements();
217             while (col_e.hasMoreElements()) {
218                 Object JavaDoc zrus = col_e.nextElement();
219                 Hashtable JavaDoc col = (Hashtable JavaDoc)zrus;
220                 boolean inscomma = col_e.hasMoreElements();
221                 cols = cols + cmd.quote((String JavaDoc) col.get("name")) + (inscomma ? coldelim : "" ); //NOI18N
222
}
223             args.put("constraint.columns", cols); // NOI18N
224
}
225         return args;
226     }
227
228     /** Reads object from stream */
229     public void readObject(java.io.ObjectInputStream JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
230         super.readObject(in);
231         type = in.readInt();
232         size = in.readInt();
233         decsize = in.readInt();
234         nullable = in.readBoolean();
235         defval = (String JavaDoc)in.readObject();
236         checke = (String JavaDoc)in.readObject();
237     }
238
239     /** Writes object to stream */
240     public void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
241         super.writeObject(out);
242         out.writeInt(type);
243         out.writeInt(size);
244         out.writeInt(decsize);
245         out.writeBoolean(nullable);
246         out.writeObject(defval);
247         out.writeObject(checke);
248     }
249 }
250
Popular Tags