1 19 20 package org.netbeans.modules.db.explorer.infos; 21 22 import java.io.IOException ; 23 import java.sql.ResultSet ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.text.MessageFormat ; 27 28 import org.openide.DialogDisplayer; 29 import org.openide.NotifyDescriptor; 30 31 import org.netbeans.lib.ddl.impl.AbstractCommand; 32 import org.netbeans.lib.ddl.impl.CreateTable; 33 import org.netbeans.lib.ddl.impl.DriverSpecification; 34 import org.netbeans.lib.ddl.impl.ModifyColumn; 35 import org.netbeans.lib.ddl.impl.RemoveColumn; 36 import org.netbeans.lib.ddl.impl.Specification; 37 import org.netbeans.lib.ddl.impl.TableColumn; 38 39 import org.netbeans.api.db.explorer.DatabaseException; 40 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 41 42 public class ColumnNodeInfo extends DatabaseNodeInfo { 43 static final long serialVersionUID =-1470704512178901918L; 44 45 public boolean canAdd(Map propmap, String propname) { 46 if (propname.equals("decdigits")) { int type = ((Integer ) get("datatype")).intValue(); return (type == java.sql.Types.FLOAT || type == java.sql.Types.REAL || type == java.sql.Types.DOUBLE); 49 } 50 51 return super.canAdd(propmap, propname); 52 } 53 54 public Object getProperty(String key) { 55 if (key.equals("columnsize") || key.equals("decdigits") || key.equals("ordpos") || key.equals("key_seq")) { Object val = get(key); 57 if (val instanceof String ) 58 return Integer.valueOf((String ) val); 59 } 60 if (key.equals("isnullable")) { String nullable = (String ) get(key); 62 boolean eq = (nullable == null) ? false : (nullable).toUpperCase().equals("YES"); return eq ? Boolean.TRUE : Boolean.FALSE; 64 } 65 return super.getProperty(key); 66 } 67 68 public void delete() throws IOException { 69 try { 70 String code = getCode(); 71 String table = (String ) get(DatabaseNode.TABLE); 72 Specification spec = (Specification) getSpecification(); 73 RemoveColumn cmd = (RemoveColumn) spec.createCommandRemoveColumn(table); 74 cmd.removeColumn((String ) get(code)); 75 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 76 cmd.execute(); 77 78 fireRefresh(); 81 } catch (Exception exc) { 85 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(exc.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); 86 } 88 } 89 90 public TableColumn getColumnSpecification() throws DatabaseException { 91 TableColumn col = null; 92 93 try { 94 Specification spec = (Specification) getSpecification(); 95 CreateTable cmd = (CreateTable) spec.createCommandCreateTable("DUMMY"); String code = getCode(); 97 98 if (code.equals(DatabaseNode.PRIMARY_KEY)) { 99 col = (TableColumn)cmd.createPrimaryKeyColumn(getName()); 100 } else if (code.equals(DatabaseNode.INDEXED_COLUMN)) { 101 col = (TableColumn)cmd.createUniqueColumn(getName()); 102 } else if (code.equals(DatabaseNode.FOREIGN_KEY)) { 103 col = null; 104 } else if (code.equals(DatabaseNode.COLUMN)) { 105 col = (TableColumn)cmd.createColumn(getName()); 106 } else { 107 String message = MessageFormat.format(bundle().getString("EXC_UnknownCode"), new String [] {code}); throw new DatabaseException(message); 109 } 110 111 DriverSpecification drvSpec = getDriverSpecification(); 112 drvSpec.getColumns((String ) get(DatabaseNode.TABLE), (String )get(code)); 113 ResultSet rs = drvSpec.getResultSet(); 114 if (rs != null) { 115 rs.next(); 116 HashMap rset = drvSpec.getRow(); 117 118 try { 119 col.setColumnType(Integer.parseInt((String ) rset.get(new Integer (5)))); 121 col.setColumnSize(Integer.parseInt((String ) rset.get(new Integer (7)))); 122 } catch (NumberFormatException exc) { 123 col.setColumnType(0); 124 col.setColumnSize(0); 125 } 126 127 col.setNullAllowed(((String ) rset.get(new Integer (18))).toUpperCase().equals("YES")); col.setDefaultValue((String ) rset.get(new Integer (13))); 129 rset.clear(); 130 131 rs.close(); 132 } 133 } catch (Exception e) { 134 throw new DatabaseException(e.getMessage()); 135 } 136 137 return col; 138 } 139 140 144 public void setProperty(String key, Object obj) { 145 try { 146 if (key.equals("remarks")) setRemarks((String )obj); 148 else if (key.equals("isnullable")) { setNullAllowed(((Boolean ) obj).booleanValue()); 150 obj = (((Boolean ) obj).equals(Boolean.TRUE) ? "YES" : "NO"); } else if (key.equals("columnsize")) setColumnSize((Integer ) obj); 153 else if (key.equals("decdigits")) setDecimalDigits((Integer ) obj); 155 else if (key.equals("coldef")) setDefaultValue((String ) obj); 157 else if (key.equals("datatype")) setDataType((Integer ) obj); 159 160 super.setProperty(key, obj); 161 } catch (Exception e) { 162 e.printStackTrace(); 163 } 164 } 165 166 public void setRemarks(String rem) throws DatabaseException { 167 String tablename = (String ) get(DatabaseNode.TABLE); 168 Specification spec = (Specification) getSpecification(); 169 try { 170 AbstractCommand cmd = spec.createCommandCommentTable(tablename, rem); 171 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 172 cmd.execute(); 173 } catch (Exception e) { 174 throw new DatabaseException(e.getMessage()); 175 } 176 } 177 178 public void setColumnSize(Integer size) throws DatabaseException { 179 try { 180 Specification spec = (Specification) getSpecification(); 181 ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable()); 182 TableColumn col = getColumnSpecification(); 183 col.setColumnSize(size.intValue()); 184 cmd.setColumn(col); 185 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 186 cmd.execute(); 187 } catch (Exception e) { 188 throw new DatabaseException(e.getMessage()); 189 } 190 } 191 192 public void setDecimalDigits(Integer size) throws DatabaseException { 193 try { 194 Specification spec = (Specification) getSpecification(); 195 ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable()); 196 TableColumn col = getColumnSpecification(); 197 col.setDecimalSize(size.intValue()); 198 cmd.setColumn(col); 199 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 200 cmd.execute(); 201 } catch (Exception e) { 202 throw new DatabaseException(e.getMessage()); 203 } 204 } 205 206 public void setDefaultValue(String val) throws DatabaseException { 207 try { 208 Specification spec = (Specification) getSpecification(); 209 ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable()); 210 TableColumn col = getColumnSpecification(); 211 col.setDefaultValue(val); 212 cmd.setColumn(col); 213 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 214 cmd.execute(); 215 } catch (Exception e) { 216 throw new DatabaseException(e.getMessage()); 217 } 218 } 219 220 public void setNullAllowed(boolean flag) throws DatabaseException { 221 try { 222 Specification spec = (Specification) getSpecification(); 223 ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable()); 224 TableColumn col = getColumnSpecification(); 225 col.setNullAllowed(flag); 226 cmd.setColumn(col); 227 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 228 cmd.execute(); 229 } catch (Exception e) { 230 throw new DatabaseException(e.getMessage()); 231 } 232 } 233 234 public void setDataType(Integer type) throws DatabaseException { 235 try { 236 Specification spec = (Specification) getSpecification(); 237 ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable()); 238 TableColumn col = getColumnSpecification(); 239 col.setColumnType(type.intValue()); 240 cmd.setColumn(col); 241 cmd.setObjectOwner((String ) get(DatabaseNodeInfo.SCHEMA)); 242 cmd.execute(); 243 } catch (Exception e) { 244 throw new DatabaseException(e.getMessage()); 245 } 246 } 247 248 253 public int hashCode() { 254 return getName().hashCode(); 255 } 256 257 } 258 | Popular Tags |