1 19 20 package org.netbeans.lib.ddl.impl; 21 22 import java.beans.Beans ; 23 import java.sql.Connection ; 24 import java.sql.DatabaseMetaData ; 25 import java.sql.SQLException ; 26 import java.text.MessageFormat ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.openide.util.NbBundle; 31 32 import org.netbeans.lib.ddl.*; 33 import org.netbeans.lib.ddl.adaptors.DatabaseMetaDataAdaptor; 34 35 import org.openide.*; 36 37 public class Specification implements DatabaseSpecification { 38 39 40 private HashMap desc; 41 42 43 private Connection jdbccon; 44 45 46 SpecificationFactory factory; 47 48 49 String adaptorClass; 50 DatabaseMetaData dmdAdaptor; 51 52 public static final String CREATE_TABLE = "CreateTableCommand"; 53 public static final String RENAME_TABLE = "RenameTableCommand"; 54 public static final String DROP_TABLE = "DropTableCommand"; 55 public static final String COMMENT_TABLE = "CommentTableCommand"; 56 public static final String ADD_COLUMN = "AddColumnCommand"; 57 public static final String MODIFY_COLUMN = "ModifyColumnCommand"; 58 public static final String RENAME_COLUMN = "RenameColumnCommand"; 59 public static final String REMOVE_COLUMN = "RemoveColumnCommand"; 60 public static final String CREATE_INDEX = "CreateIndexCommand"; 61 public static final String DROP_INDEX = "DropIndexCommand"; 62 public static final String ADD_CONSTRAINT = "AddConstraintCommand"; 63 public static final String DROP_CONSTRAINT = "DropConstraintCommand"; 64 public static final String CREATE_VIEW = "CreateViewCommand"; 65 public static final String RENAME_VIEW = "RenameViewCommand"; 66 public static final String COMMENT_VIEW = "CommentViewCommand"; 67 public static final String DROP_VIEW = "DropViewCommand"; 68 public static final String CREATE_PROCEDURE = "CreateProcedureCommand"; 69 public static final String DROP_PROCEDURE = "DropProcedureCommand"; 70 public static final String CREATE_FUNCTION = "CreateFunctionCommand"; 71 public static final String DROP_FUNCTION = "DropFunctionCommand"; 72 public static final String CREATE_TRIGGER = "CreateTriggerCommand"; 73 public static final String DROP_TRIGGER = "DropTriggerCommand"; 74 75 76 public Specification(HashMap description) 77 { 78 desc = description; 79 } 80 81 82 public Specification(HashMap description, Connection c) 83 { 84 desc = description; 85 jdbccon = c; 86 } 87 88 89 public Map getProperties() 90 { 91 return (Map )desc; 92 } 93 94 95 public Map getCommandProperties(String command) 96 { 97 return (Map )desc.get(command); 98 } 99 100 101 public DBConnection getConnection() 102 { 103 return (DBConnection)desc.get("connection"); } 105 106 public DatabaseSpecificationFactory getSpecificationFactory() 107 { 108 return factory; 109 } 110 111 public void setSpecificationFactory(DatabaseSpecificationFactory fac) 112 { 113 factory = (SpecificationFactory)fac; 114 } 115 116 public String getMetaDataAdaptorClassName() 117 { 118 if (adaptorClass == null || adaptorClass.length() == 0) { 119 adaptorClass = "org.netbeans.lib.ddl.adaptors.DefaultAdaptor"; } 121 122 return adaptorClass; 123 } 124 125 public void setMetaDataAdaptorClassName(String name) 126 { 127 if (name.startsWith("Database.Adaptors.")) adaptorClass = name; 129 else 130 adaptorClass = "Database.Adaptors."+name; dmdAdaptor = null; 133 } 134 135 136 public DatabaseMetaData getMetaData() throws SQLException 137 { 138 try { 139 140 if (dmdAdaptor == null) { 141 if (jdbccon != null) { 142 String adc = getMetaDataAdaptorClassName(); 143 if (adc != null) { 144 ClassLoader loader = Class.forName(adc).getClassLoader(); 145 dmdAdaptor = (DatabaseMetaData ) Beans.instantiate(loader, adc); 146 if (dmdAdaptor instanceof DatabaseMetaDataAdaptor) { 147 ((DatabaseMetaDataAdaptor)dmdAdaptor).setConnection(jdbccon); 148 } else throw new ClassNotFoundException (NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_AdaptorInterface")); } else throw new ClassNotFoundException (NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_AdaptorUnspecClass")); } 151 } 152 153 return dmdAdaptor; 154 155 } catch (Exception ex) { 156 ex.printStackTrace(); 157 throw new SQLException (ex.getMessage()); 158 } 159 } 160 161 166 public Connection openJDBCConnection() 167 throws DDLException 168 { 169 if (jdbccon != null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnOpen")); DBConnection dbcon = getConnection(); 171 if (dbcon == null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnNot")); try { 173 jdbccon = dbcon.createJDBCConnection(); 174 } catch (Exception e) { 175 throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnNot")); 176 } 177 178 return jdbccon; 179 } 180 181 187 public Connection getJDBCConnection() 188 { 189 return jdbccon; 190 } 191 192 public void closeJDBCConnection() 193 throws DDLException 194 { 195 if (jdbccon == null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnNot")); try { 197 jdbccon.close(); 198 jdbccon = null; 199 } catch (SQLException e) { 200 throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnUnableClose")); } 202 } 203 204 210 public DDLCommand createCommand(String commandName) 211 throws CommandNotSupportedException 212 { 213 return createCommand(commandName, null); 214 } 215 216 221 public DDLCommand createCommand(String commandName, String tableName) 222 throws CommandNotSupportedException 223 { 224 String classname; 225 Class cmdclass; 226 AbstractCommand cmd; 227 HashMap cprops = (HashMap )desc.get(commandName); 228 if (cprops != null) classname = (String )cprops.get("Class"); else throw new CommandNotSupportedException(commandName, 231 MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_CommandNotSupported"), new String [] {commandName})); try { 233 cmdclass = Class.forName(classname); 234 cmd = (AbstractCommand)cmdclass.newInstance(); 235 } catch (Exception e) { 236 throw new CommandNotSupportedException(commandName, 237 MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableFindOrInitCommand"), new String [] {classname, commandName, e.getMessage()})); } 239 240 cmd.setObjectName(tableName); 241 cmd.setSpecification(this); 242 cmd.setFormat((String )cprops.get("Format")); return cmd; 244 } 245 246 249 public CreateTable createCommandCreateTable(String tableName) 250 throws CommandNotSupportedException 251 { 252 return (CreateTable)createCommand(CREATE_TABLE, tableName); 253 } 254 255 259 public CommentTable createCommandCommentTable(String tableName, String comment) 260 throws CommandNotSupportedException 261 { 262 CommentTable cmd = (CommentTable)createCommand(COMMENT_TABLE, tableName); 263 cmd.setComment(comment); 264 return cmd; 265 } 266 267 270 public AbstractCommand createCommandDropTable(String tableName) 271 throws CommandNotSupportedException 272 { 273 return (AbstractCommand)createCommand(DROP_TABLE, tableName); 274 } 275 276 279 public RenameTable createCommandRenameTable(String tableName, String newName) 280 throws CommandNotSupportedException 281 { 282 RenameTable cmd = (RenameTable)createCommand(RENAME_TABLE, tableName); 283 cmd.setNewName(newName); 284 return cmd; 285 } 286 287 288 public AddColumn createCommandAddColumn(String tableName) 289 throws CommandNotSupportedException 290 { 291 return (AddColumn)createCommand(ADD_COLUMN, tableName); 292 } 293 294 295 public ModifyColumn createCommandModifyColumn(String tableName) 296 throws CommandNotSupportedException 297 { 298 ModifyColumn cmd = (ModifyColumn)createCommand(MODIFY_COLUMN, tableName); 299 return cmd; 300 } 301 302 303 public RenameColumn createCommandRenameColumn(String tableName) 304 throws CommandNotSupportedException 305 { 306 RenameColumn cmd = (RenameColumn)createCommand(RENAME_COLUMN, tableName); 307 return cmd; 308 } 309 310 313 public RemoveColumn createCommandRemoveColumn(String tableName) 314 throws CommandNotSupportedException 315 { 316 RemoveColumn rcol = (RemoveColumn)createCommand(REMOVE_COLUMN, tableName); 317 return rcol; 318 } 319 320 324 public CreateIndex createCommandCreateIndex(String tableName) 325 throws CommandNotSupportedException 326 { 327 CreateIndex cicmd = (CreateIndex)createCommand(CREATE_INDEX, tableName); 328 return cicmd; 329 } 330 331 334 public DropIndex createCommandDropIndex(String tablename) 335 throws CommandNotSupportedException 336 { 337 DropIndex dcmd = (DropIndex)createCommand(DROP_INDEX, tablename); 338 return dcmd; 339 } 340 341 344 public CreateView createCommandCreateView(String viewname) 345 throws CommandNotSupportedException 346 { 347 return (CreateView)createCommand(CREATE_VIEW, viewname); 348 } 349 350 353 public RenameView createCommandRenameView(String tableName, String newName) 354 throws CommandNotSupportedException 355 { 356 RenameView cmd = (RenameView)createCommand(RENAME_VIEW, tableName); 357 cmd.setNewName(newName); 358 return cmd; 359 } 360 361 365 public CommentView createCommandCommentView(String viewName, String comment) 366 throws CommandNotSupportedException 367 { 368 CommentView cmd = (CommentView)createCommand(COMMENT_VIEW, viewName); 369 cmd.setComment(comment); 370 return cmd; 371 } 372 373 376 public AbstractCommand createCommandDropView(String viewname) 377 throws CommandNotSupportedException 378 { 379 return (AbstractCommand)createCommand(DROP_VIEW, viewname); 380 } 381 382 385 public CreateProcedure createCommandCreateProcedure(String name) 386 throws CommandNotSupportedException 387 { 388 return (CreateProcedure)createCommand(CREATE_PROCEDURE, name); 389 } 390 391 394 public AbstractCommand createCommandDropProcedure(String name) 395 throws CommandNotSupportedException 396 { 397 return (AbstractCommand)createCommand(DROP_PROCEDURE, name); 398 } 399 400 403 public CreateFunction createCommandCreateFunction(String name) 404 throws CommandNotSupportedException 405 { 406 return (CreateFunction)createCommand(CREATE_FUNCTION, name); 407 } 408 409 412 public AbstractCommand createCommandDropFunction(String name) 413 throws CommandNotSupportedException 414 { 415 return (AbstractCommand)createCommand(DROP_FUNCTION, name); 416 } 417 418 421 public CreateTrigger createCommandCreateTrigger(String name, String tablename, int timing) 422 throws CommandNotSupportedException 423 { 424 CreateTrigger ctrig = (CreateTrigger)createCommand(CREATE_TRIGGER, name); 425 ctrig.setTableName(tablename); 426 ctrig.setTiming(timing); 427 return ctrig; 428 } 429 430 433 public AbstractCommand createCommandDropTrigger(String name) 434 throws CommandNotSupportedException 435 { 436 return (AbstractCommand)createCommand(DROP_TRIGGER, name); 437 } 438 439 440 public Map getTypeMap() 441 { 442 return (Map )desc.get("TypeMap"); } 444 445 446 public String getType(int type) 447 { 448 String typestr = ""; 449 String ret; 450 Map typemap = getTypeMap(); 451 452 switch(type) { 453 case java.sql.Types.ARRAY: typestr = "ARRAY"; break; case java.sql.Types.BIGINT: typestr = "BIGINT"; break; case java.sql.Types.BINARY: typestr = "BINARY"; break; case java.sql.Types.BIT: typestr = "BIT"; break; case java.sql.Types.BLOB: typestr = "BLOB"; break; case java.sql.Types.CHAR: typestr = "CHAR"; break; case java.sql.Types.CLOB: typestr = "CLOB"; break; case java.sql.Types.DATE: typestr = "DATE"; break; case java.sql.Types.DECIMAL: typestr = "DECIMAL"; break; case java.sql.Types.DISTINCT: typestr = "DISTINCT"; break; case java.sql.Types.DOUBLE: typestr = "DOUBLE"; break; case java.sql.Types.FLOAT: typestr = "FLOAT"; break; case java.sql.Types.INTEGER: typestr = "INTEGER"; break; case java.sql.Types.JAVA_OBJECT: typestr = "JAVA_OBJECT"; break; case java.sql.Types.LONGVARBINARY: typestr = "LONGVARBINARY"; break; case java.sql.Types.LONGVARCHAR: typestr = "LONGVARCHAR"; break; case java.sql.Types.NUMERIC: typestr = "NUMERIC"; break; case java.sql.Types.OTHER: typestr = "OTHER"; break; case java.sql.Types.REAL: typestr = "REAL"; break; case java.sql.Types.REF: typestr = "REF"; break; case java.sql.Types.SMALLINT: typestr = "SMALLINT"; break; case java.sql.Types.TIME: typestr = "TIME"; break; case java.sql.Types.TIMESTAMP: typestr = "TIMESTAMP"; break; case java.sql.Types.TINYINT: typestr = "TINYINT"; break; case java.sql.Types.VARBINARY: typestr = "VARBINARY"; break; case java.sql.Types.VARCHAR: typestr = "VARCHAR"; break; } 480 481 ret = (String ) typemap.get("java.sql.Types." + typestr); if (ret == null) 483 ret = typestr; 484 485 return ret; 486 } 487 488 489 public static int getType(String type) 490 { 491 if (type.equals("java.sql.Types.ARRAY")) return java.sql.Types.ARRAY; if (type.equals("java.sql.Types.BIGINT")) return java.sql.Types.BIGINT; if (type.equals("java.sql.Types.BINARY")) return java.sql.Types.BINARY; if (type.equals("java.sql.Types.BIT")) return java.sql.Types.BIT; if (type.equals("java.sql.Types.BLOB")) return java.sql.Types.BLOB; if (type.equals("java.sql.Types.CHAR")) return java.sql.Types.CHAR; if (type.equals("java.sql.Types.CLOB")) return java.sql.Types.CLOB; if (type.equals("java.sql.Types.DATE")) return java.sql.Types.DATE; if (type.equals("java.sql.Types.DECIMAL")) return java.sql.Types.DECIMAL; if (type.equals("java.sql.Types.DISTINCT")) return java.sql.Types.DISTINCT; if (type.equals("java.sql.Types.DOUBLE")) return java.sql.Types.DOUBLE; if (type.equals("java.sql.Types.FLOAT")) return java.sql.Types.FLOAT; if (type.equals("java.sql.Types.INTEGER")) return java.sql.Types.INTEGER; if (type.equals("java.sql.Types.JAVA_OBJECT")) return java.sql.Types.JAVA_OBJECT; if (type.equals("java.sql.Types.LONGVARBINARY")) return java.sql.Types.LONGVARBINARY; if (type.equals("java.sql.Types.LONGVARCHAR")) return java.sql.Types.LONGVARCHAR; if (type.equals("java.sql.Types.NUMERIC")) return java.sql.Types.NUMERIC; if (type.equals("java.sql.Types.OTHER")) return java.sql.Types.OTHER; if (type.equals("java.sql.Types.REAL")) return java.sql.Types.REAL; if (type.equals("java.sql.Types.REF")) return java.sql.Types.REF; if (type.equals("java.sql.Types.SMALLINT")) return java.sql.Types.SMALLINT; if (type.equals("java.sql.Types.TIME")) return java.sql.Types.TIME; if (type.equals("java.sql.Types.TIMESTAMP")) return java.sql.Types.TIMESTAMP; if (type.equals("java.sql.Types.TINYINT")) return java.sql.Types.TINYINT; if (type.equals("java.sql.Types.VARBINARY")) return java.sql.Types.VARBINARY; if (type.equals("java.sql.Types.VARCHAR")) return java.sql.Types.VARCHAR; 518 return -1; 519 } 520 } 521 | Popular Tags |