001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.impl;
043:
044: import java.io.Serializable;
045: import java.util.Enumeration;
046: import java.util.Hashtable;
047: import java.util.Map;
048: import java.util.Vector;
049:
050: import org.netbeans.lib.ddl.CheckConstraintDescriptor;
051: import org.netbeans.lib.ddl.DatabaseSpecification;
052: import org.netbeans.lib.ddl.DDLException;
053: import org.netbeans.lib.ddl.TableColumnDescriptor;
054:
055: /**
056: * Implementation of table column.
057: */
058: public class TableColumn extends AbstractTableColumn implements
059: Serializable, TableColumnDescriptor, CheckConstraintDescriptor {
060: /** String constant for column type */
061: public static final String COLUMN = "COLUMN"; // NOI18N
062: /** String constant for column check */
063: public static final String CHECK = "CHECK"; // NOI18N
064: /** String constant for unique column type */
065: public static final String UNIQUE = "UNIQUE"; // NOI18N
066: /** String constant for primary key */
067: public static final String PRIMARY_KEY = "PRIMARY_KEY"; // NOI18N
068: /** String constant for foreign key */
069: public static final String FOREIGN_KEY = "FOREIGN_KEY"; // NOI18N
070: /** String constant for check constraint */
071: public static final String CHECK_CONSTRAINT = "CHECK_CONSTRAINT"; // NOI18N
072: /** String constant for unique constraint */
073: public static final String UNIQUE_CONSTRAINT = "UNIQUE_CONSTRAINT"; // NOI18N
074: /** String constant for primary key constraint */
075: public static final String PRIMARY_KEY_CONSTRAINT = "PRIMARY_KEY_CONSTRAINT"; // NOI18N
076: /** String constant for foreign key constraint */
077: public static final String FOREIGN_KEY_CONSTRAINT = "FOREIGN_KEY_CONSTRAINT"; // NOI18N
078:
079: /** Column type */
080: int type;
081:
082: /** Column size */
083: int size;
084:
085: /** Column decimal size */
086: int decsize;
087:
088: /** Null allowed */
089: boolean nullable;
090:
091: /** Default value */
092: String defval;
093:
094: /** Check expression */
095: String checke;
096:
097: /** Table constraint columns */
098: Vector constraintColumns;
099:
100: static final long serialVersionUID = 4298150043758715392L;
101:
102: /** Constructor */
103: public TableColumn() {
104: size = 0;
105: decsize = 0;
106: nullable = true;
107: }
108:
109: /** Returns type of column */
110: public int getColumnType() {
111: return type;
112: }
113:
114: /** Sets type of column */
115: public void setColumnType(int columnType) {
116: type = columnType;
117: }
118:
119: /** Returns column size */
120: public int getColumnSize() {
121: return size;
122: }
123:
124: /** Sets size of column */
125: public void setColumnSize(int csize) {
126: size = csize;
127: }
128:
129: /** Returns decimal digits of column */
130: public int getDecimalSize() {
131: return decsize;
132: }
133:
134: /** Sets decimal digits of column */
135: public void setDecimalSize(int dsize) {
136: decsize = dsize;
137: }
138:
139: /** Nulls allowed? */
140: public boolean isNullAllowed() {
141: return nullable;
142: }
143:
144: /** Sets null property */
145: public void setNullAllowed(boolean flag) {
146: nullable = flag;
147: }
148:
149: /** Returns default value of column */
150: public String getDefaultValue() {
151: return defval;
152: }
153:
154: /** Sets default value of column */
155: public void setDefaultValue(String val) {
156: defval = val;
157: }
158:
159: /** Returns column check condition */
160: public String getCheckCondition() {
161: return checke;
162: }
163:
164: /** Sets column check condition */
165: public void setCheckCondition(String val) {
166: checke = val;
167: }
168:
169: /** Returns table constraint columns */
170: public Vector getTableConstraintColumns() {
171: return constraintColumns;
172: }
173:
174: /** Sets column check condition */
175: public void setTableConstraintColumns(Vector columns) {
176: constraintColumns = columns;
177: }
178:
179: /**
180: * Returns properties and it's values supported by this object.
181: * object.name Name of the object; use setObjectName()
182: * object.owner Name of the object; use setObjectOwner()
183: * column.size Size of column
184: * column.decsize Deimal size of size
185: * column.type Type of column
186: * default.value Condition of column
187: * Throws DDLException if object name is not specified.
188: */
189: public Map getColumnProperties(AbstractCommand cmd)
190: throws DDLException {
191: DatabaseSpecification spec = cmd.getSpecification();
192: Map args = super .getColumnProperties(cmd);
193: String stype = spec.getType(type);
194: Vector charactertypes = (Vector) spec.getProperties().get(
195: "CharacterTypes"); // NOI18N
196: String strdelim = (String) spec.getProperties().get(
197: "StringDelimiter"); // NOI18N
198: Vector sizelesstypes = (Vector) spec.getProperties().get(
199: "SizelessTypes"); // NOI18N
200: String coldelim = (String) spec.getProperties().get(
201: "ArgumentListDelimiter"); // NOI18N
202:
203: // Decimal size for sizeless type
204: if (sizelesstypes != null && size > 0) {
205: if (!sizelesstypes.contains(stype)) {
206: if (size > 0)
207: args.put("column.size", String.valueOf(size)); // NOI18N
208: if (decsize > 0)
209: args.put("column.decsize", String.valueOf(decsize)); // NOI18N
210: }
211: }
212:
213: String qdefval = defval;
214:
215: if (qdefval != null
216: && charactertypes.contains(spec.getType(type))
217: && !qdefval.startsWith(strdelim)
218: && !qdefval.endsWith(strdelim))
219: if (!qdefval.startsWith("(" + strdelim)
220: && !qdefval.endsWith(strdelim + ")")) //hack for MSSQLServer, default value is encapsulated in () so I can't generate '()'
221: qdefval = strdelim + defval + strdelim;
222:
223: String dbType = spec.getType(type);
224: String dbTypeSuffix = null;
225: Map suffixTypeMap = (Map) spec.getProperties().get(
226: "TypePrefixSuffixMap"); // NOI18N
227: if (suffixTypeMap != null) {
228: Map dbTypePrefixSuffix = (Map) suffixTypeMap.get(dbType);
229: if (dbTypePrefixSuffix != null) {
230: dbType = (String) dbTypePrefixSuffix.get("Prefix"); // NOI18N
231: dbTypeSuffix = (String) dbTypePrefixSuffix
232: .get("Suffix"); // NOI18N
233: }
234: }
235: args.put("column.type", dbType);
236: if (dbTypeSuffix != null) {
237: args.put("column.type.suffix", dbTypeSuffix);
238: }
239:
240: if (!nullable)
241: args.put("column.notnull", ""); // NOI18N
242:
243: if (!(!nullable && qdefval != null && (qdefval
244: .equalsIgnoreCase("null")
245: || qdefval.equalsIgnoreCase("'null'") || qdefval
246: .equalsIgnoreCase("\"null\"")))) // NOI18N
247: if (defval != null && !defval.equals(""))
248: args.put("default.value", qdefval); // NOI18N
249:
250: if (checke != null)
251: args.put("check.condition", checke); // NOI18N
252: if (constraintColumns != null) {
253: String cols = "";
254: Enumeration col_e = constraintColumns.elements();
255: while (col_e.hasMoreElements()) {
256: Object zrus = col_e.nextElement();
257: Hashtable col = (Hashtable) zrus;
258: boolean inscomma = col_e.hasMoreElements();
259: String colname = (String) col.get("name");
260: cols = cols
261: + (isNewColumn() ? colname : cmd.quote(colname))
262: + (inscomma ? coldelim : ""); //NOI18N
263: }
264: args.put("constraint.columns", cols); // NOI18N
265: }
266: return args;
267: }
268:
269: /** Reads object from stream */
270: public void readObject(java.io.ObjectInputStream in)
271: throws java.io.IOException, ClassNotFoundException {
272: super .readObject(in);
273: type = in.readInt();
274: size = in.readInt();
275: decsize = in.readInt();
276: nullable = in.readBoolean();
277: defval = (String) in.readObject();
278: checke = (String) in.readObject();
279: }
280:
281: /** Writes object to stream */
282: public void writeObject(java.io.ObjectOutputStream out)
283: throws java.io.IOException {
284: super.writeObject(out);
285: out.writeInt(type);
286: out.writeInt(size);
287: out.writeInt(decsize);
288: out.writeBoolean(nullable);
289: out.writeObject(defval);
290: out.writeObject(checke);
291: }
292: }
|