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.modules.db.explorer.infos;
043:
044: import java.io.IOException;
045: import java.sql.ResultSet;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.text.MessageFormat;
049: import org.netbeans.modules.db.explorer.DbUtilities;
050:
051: import org.netbeans.lib.ddl.impl.AbstractCommand;
052: import org.netbeans.lib.ddl.impl.CreateTable;
053: import org.netbeans.lib.ddl.impl.DriverSpecification;
054: import org.netbeans.lib.ddl.impl.ModifyColumn;
055: import org.netbeans.lib.ddl.impl.RemoveColumn;
056: import org.netbeans.lib.ddl.impl.Specification;
057: import org.netbeans.lib.ddl.impl.TableColumn;
058:
059: import org.netbeans.api.db.explorer.DatabaseException;
060: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
061:
062: public class ColumnNodeInfo extends DatabaseNodeInfo {
063: static final long serialVersionUID = -1470704512178901918L;
064:
065: public boolean canAdd(Map propmap, String propname) {
066: if (propname.equals("decdigits")) { //NOI18N
067: int type = ((Integer) get("datatype")).intValue(); //NOI18N
068: return (type == java.sql.Types.FLOAT
069: || type == java.sql.Types.REAL || type == java.sql.Types.DOUBLE);
070: }
071:
072: return super .canAdd(propmap, propname);
073: }
074:
075: public Object getProperty(String key) {
076: if (key.equals("columnsize") || key.equals("decdigits")
077: || key.equals("ordpos") || key.equals("key_seq")) { //NOI18N
078: Object val = get(key);
079: if (val instanceof String)
080: return Integer.valueOf((String) val);
081: }
082: if (key.equals("isnullable")) { //NOI18N
083: String nullable = (String) get(key);
084: boolean eq = (nullable == null) ? false : (nullable)
085: .toUpperCase().equals("YES"); //NOI18N
086: return eq ? Boolean.TRUE : Boolean.FALSE;
087: }
088: return super .getProperty(key);
089: }
090:
091: public void delete() throws IOException {
092: try {
093: String code = getCode();
094: String table = (String) get(DatabaseNode.TABLE);
095: Specification spec = (Specification) getSpecification();
096: RemoveColumn cmd = (RemoveColumn) spec
097: .createCommandRemoveColumn(table);
098: cmd.removeColumn((String) get(code));
099: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
100: cmd.execute();
101:
102: // refresh list of columns after column drop
103: //getParent().refreshChildren();
104: fireRefresh();
105: } catch (Exception exc) {
106: DbUtilities.reportError(bundle().getString(
107: "ERR_UnableToDeleteColumn"), exc.getMessage()); // NOI18N
108: }
109: }
110:
111: public TableColumn getColumnSpecification()
112: throws DatabaseException {
113: TableColumn col = null;
114:
115: try {
116: Specification spec = (Specification) getSpecification();
117: CreateTable cmd = (CreateTable) spec
118: .createCommandCreateTable("DUMMY"); //NOI18N
119: String code = getCode();
120:
121: if (code.equals(DatabaseNode.PRIMARY_KEY)) {
122: col = (TableColumn) cmd
123: .createPrimaryKeyColumn(getName());
124: } else if (code.equals(DatabaseNode.INDEXED_COLUMN)) {
125: col = (TableColumn) cmd.createUniqueColumn(getName());
126: } else if (code.equals(DatabaseNode.FOREIGN_KEY)) {
127: col = null;
128: } else if (code.equals(DatabaseNode.COLUMN)) {
129: col = (TableColumn) cmd.createColumn(getName());
130: } else {
131: String message = MessageFormat.format(bundle()
132: .getString("EXC_UnknownCode"),
133: new String[] { code }); // NOI18N
134: throw new DatabaseException(message);
135: }
136:
137: DriverSpecification drvSpec = getDriverSpecification();
138: drvSpec.getColumns((String) get(DatabaseNode.TABLE),
139: (String) get(code));
140: ResultSet rs = drvSpec.getResultSet();
141: if (rs != null) {
142: rs.next();
143: HashMap rset = drvSpec.getRow();
144:
145: try {
146: //hack because of MSSQL ODBC problems - see DriverSpecification.getRow() for more info - shouln't be thrown
147: col.setColumnType(Integer.parseInt((String) rset
148: .get(new Integer(5))));
149: col.setColumnSize(Integer.parseInt((String) rset
150: .get(new Integer(7))));
151: } catch (NumberFormatException exc) {
152: col.setColumnType(0);
153: col.setColumnSize(0);
154: }
155:
156: col.setNullAllowed(((String) rset.get(new Integer(18)))
157: .toUpperCase().equals("YES")); //NOI18N
158: col.setDefaultValue((String) rset.get(new Integer(13)));
159: rset.clear();
160:
161: rs.close();
162: }
163: } catch (Exception e) {
164: throw new DatabaseException(e);
165: }
166:
167: return col;
168: }
169:
170: // catalog,schema,tablename,name,datatype,typename,
171: // columnsize,bufflen,decdigits,radix,nullable,remarks,coldef,
172: // reserved1,reserved2,octetlen,ordpos,isnullable
173:
174: public void setProperty(String key, Object obj) {
175: try {
176: if (key.equals("remarks")) //NOI18N
177: setRemarks((String) obj);
178: else if (key.equals("isnullable")) { //NOI18N
179: setNullAllowed(((Boolean) obj).booleanValue());
180: obj = (((Boolean) obj).equals(Boolean.TRUE) ? "YES"
181: : "NO"); //NOI18N
182: } else if (key.equals("columnsize")) //NOI18N
183: setColumnSize((Integer) obj);
184: else if (key.equals("decdigits")) //NOI18N
185: setDecimalDigits((Integer) obj);
186: else if (key.equals("coldef")) //NOI18N
187: setDefaultValue((String) obj);
188: else if (key.equals("datatype")) //NOI18N
189: setDataType((Integer) obj);
190:
191: super .setProperty(key, obj);
192: } catch (Exception e) {
193: e.printStackTrace();
194: }
195: }
196:
197: public void setRemarks(String rem) throws DatabaseException {
198: String tablename = (String) get(DatabaseNode.TABLE);
199: Specification spec = (Specification) getSpecification();
200: try {
201: AbstractCommand cmd = spec.createCommandCommentTable(
202: tablename, rem);
203: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
204: cmd.execute();
205: } catch (Exception e) {
206: throw new DatabaseException(e.getMessage());
207: }
208: }
209:
210: public void setColumnSize(Integer size) throws DatabaseException {
211: try {
212: Specification spec = (Specification) getSpecification();
213: ModifyColumn cmd = (ModifyColumn) spec
214: .createCommandModifyColumn(getTable());
215: TableColumn col = getColumnSpecification();
216: col.setColumnSize(size.intValue());
217: cmd.setColumn(col);
218: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
219: cmd.execute();
220: } catch (Exception e) {
221: throw new DatabaseException(e.getMessage());
222: }
223: }
224:
225: public void setDecimalDigits(Integer size) throws DatabaseException {
226: try {
227: Specification spec = (Specification) getSpecification();
228: ModifyColumn cmd = (ModifyColumn) spec
229: .createCommandModifyColumn(getTable());
230: TableColumn col = getColumnSpecification();
231: col.setDecimalSize(size.intValue());
232: cmd.setColumn(col);
233: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
234: cmd.execute();
235: } catch (Exception e) {
236: throw new DatabaseException(e.getMessage());
237: }
238: }
239:
240: public void setDefaultValue(String val) throws DatabaseException {
241: try {
242: Specification spec = (Specification) getSpecification();
243: ModifyColumn cmd = (ModifyColumn) spec
244: .createCommandModifyColumn(getTable());
245: TableColumn col = getColumnSpecification();
246: col.setDefaultValue(val);
247: cmd.setColumn(col);
248: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
249: cmd.execute();
250: } catch (Exception e) {
251: throw new DatabaseException(e.getMessage());
252: }
253: }
254:
255: public void setNullAllowed(boolean flag) throws DatabaseException {
256: try {
257: Specification spec = (Specification) getSpecification();
258: ModifyColumn cmd = (ModifyColumn) spec
259: .createCommandModifyColumn(getTable());
260: TableColumn col = getColumnSpecification();
261: col.setNullAllowed(flag);
262: cmd.setColumn(col);
263: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
264: cmd.execute();
265: } catch (Exception e) {
266: throw new DatabaseException(e.getMessage());
267: }
268: }
269:
270: public void setDataType(Integer type) throws DatabaseException {
271: try {
272: Specification spec = (Specification) getSpecification();
273: ModifyColumn cmd = (ModifyColumn) spec
274: .createCommandModifyColumn(getTable());
275: TableColumn col = getColumnSpecification();
276: col.setColumnType(type.intValue());
277: cmd.setColumn(col);
278: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
279: cmd.execute();
280: } catch (Exception e) {
281: throw new DatabaseException(e.getMessage());
282: }
283: }
284:
285: /**
286: * Using name of column for hashCode computation.
287: *
288: * @return computed hashCode based on name of column
289: */
290: public int hashCode() {
291: return getName().hashCode();
292: }
293:
294: }
|