1 19 20 package org.netbeans.lib.ddl.impl; 21 22 import java.io.Serializable ; 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.openide.DialogDisplayer; 30 import org.openide.ErrorManager; 31 import org.openide.NotifyDescriptor; 32 import org.openide.util.NbBundle; 33 import org.openide.windows.IOProvider; 34 import org.openide.windows.OutputWriter; 35 36 import org.netbeans.lib.ddl.DatabaseSpecification; 37 import org.netbeans.lib.ddl.DDLCommand; 38 import org.netbeans.lib.ddl.DDLException; 39 import org.netbeans.lib.ddl.util.CommandFormatter; 40 import org.openide.windows.InputOutput; 41 42 46 public class AbstractCommand implements Serializable , DDLCommand { 47 48 private DatabaseSpecification spec; 49 50 51 boolean executionWithException; 52 53 54 private String format; 55 56 57 private String owner, name; 58 59 60 private Map addprops; 61 62 static final long serialVersionUID =-560515030304320086L; 63 64 private String quoteStr; 65 66 67 public DatabaseSpecification getSpecification() { 68 return spec; 69 } 70 71 76 public void setSpecification(DatabaseSpecification specification) { 77 spec = specification; 78 } 79 80 85 public void setFormat(String fmt) { 86 format = fmt; 87 } 88 89 90 public String getObjectName() { 91 return name; 92 } 93 94 97 public void setObjectName(String nam) { 98 name = nam; 99 } 100 101 102 public String getObjectOwner() { 103 if (owner != null) 104 if (owner.trim().equals("")) 105 setObjectOwner(null); 106 107 return owner; 108 } 109 110 113 public void setObjectOwner(String objectowner) { 114 owner = objectowner; 115 } 116 117 118 public Object getProperty(String pname) { 119 return addprops.get(pname); 120 } 121 122 123 public void setProperty(String pname, Object pval) { 124 if (addprops == null) 125 addprops = new HashMap (); 126 addprops.put(pname, pval); 127 } 128 129 135 public Map getCommandProperties() throws DDLException { 136 HashMap args = new HashMap (); 137 if (addprops != null) 138 args.putAll(addprops); 139 String oname = getObjectName(); 140 if (oname != null) 141 args.put("object.name", quote(getObjectName())); else 143 throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Unknown")); args.put("object.owner", quote(getObjectOwner())); 146 return args; 147 } 148 149 156 public void execute() throws DDLException { 157 String fcmd; 158 Connection fcon = null; 159 boolean opened = false; 160 executionWithException = false; 161 162 try { 163 fcmd = getCommand(); 164 } catch (Exception e) { 165 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 166 executionWithException = true; 167 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToFormat")+"\n" + format + "\n" + e.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); return; 169 } 170 171 if (spec.getSpecificationFactory().isDebugMode()) { 173 174 try { 175 InputOutput io = IOProvider.getDefault().getIO(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("LBL_Output_Window"), false); 176 io.select(); 177 OutputWriter ow = io.getOut(); if (ow != null) { 179 ow.println(fcmd); 180 ow.println(" "); } else 182 throw new Exception (); 183 184 } catch (Exception e) { 185 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, e.getMessage() + "\n" + fcmd); } 187 } 188 189 try { 190 fcon = spec.getJDBCConnection(); 191 if (fcon == null) { 192 fcon = spec.openJDBCConnection(); 193 opened = true; 194 } 195 196 Statement stat = fcon.createStatement(); 197 stat.execute(fcmd); 198 stat.close(); 199 } catch (Exception e) { 200 executionWithException = true; 201 if (opened && fcon != null) 202 spec.closeJDBCConnection(); 203 204 throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToExecute")+"\n" + fcmd + "\n" + e.getMessage()); } 206 207 if (opened) 208 spec.closeJDBCConnection(); 209 } 210 211 218 public String getCommand() throws DDLException { 219 if (format == null) 220 throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_NoFormatSpec")); try { 222 Map props = getCommandProperties(); 223 return CommandFormatter.format(format, props); 224 } catch (Exception e) { 225 throw new DDLException(e.getMessage()); 226 } 227 } 228 229 230 public boolean wasException() { 231 return executionWithException; 232 } 233 234 private String getQuoteString() { 235 try { 236 quoteStr = getSpecification().getJDBCConnection().getMetaData().getIdentifierQuoteString(); 237 238 if (getSpecification().getJDBCConnection().getMetaData().getDatabaseProductName().indexOf("Firebird") != -1) quoteStr = ""; 241 } catch (SQLException exc) { 242 } 244 if (quoteStr == null) 245 quoteStr = ""; else 247 quoteStr.trim(); 248 249 return quoteStr; 250 } 251 252 protected String quote(String name) { 253 if (name == null || name.equals("")) 254 return name; 255 256 if (quoteStr == null) 257 quoteStr = getQuoteString(); 258 259 return quoteStr + name + quoteStr; 260 } 261 262 263 public void readObject(java.io.ObjectInputStream in) throws java.io.IOException , ClassNotFoundException { 264 format = (String )in.readObject(); 265 owner = (String )in.readObject(); 266 name = (String )in.readObject(); 267 addprops = (Map )in.readObject(); 268 } 269 270 271 public void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { 272 out.writeObject(format); 274 out.writeObject(owner); 275 out.writeObject(name); 276 out.writeObject(addprops); 277 } 278 } 279 | Popular Tags |