KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > Specification


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.impl;
21
22 import java.beans.Beans JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
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     /** Used DBConnection */
40     private HashMap JavaDoc desc;
41
42     /** Used JDBC Connection */
43     private Connection JavaDoc jdbccon;
44
45     /** Owned factory */
46     SpecificationFactory factory;
47
48     /** Metadata adaptor */
49     String JavaDoc adaptorClass;
50     DatabaseMetaData JavaDoc dmdAdaptor;
51
52     public static final String JavaDoc CREATE_TABLE = "CreateTableCommand";
53     public static final String JavaDoc RENAME_TABLE = "RenameTableCommand";
54     public static final String JavaDoc DROP_TABLE = "DropTableCommand";
55     public static final String JavaDoc COMMENT_TABLE = "CommentTableCommand";
56     public static final String JavaDoc ADD_COLUMN = "AddColumnCommand";
57     public static final String JavaDoc MODIFY_COLUMN = "ModifyColumnCommand";
58     public static final String JavaDoc RENAME_COLUMN = "RenameColumnCommand";
59     public static final String JavaDoc REMOVE_COLUMN = "RemoveColumnCommand";
60     public static final String JavaDoc CREATE_INDEX = "CreateIndexCommand";
61     public static final String JavaDoc DROP_INDEX = "DropIndexCommand";
62     public static final String JavaDoc ADD_CONSTRAINT = "AddConstraintCommand";
63     public static final String JavaDoc DROP_CONSTRAINT = "DropConstraintCommand";
64     public static final String JavaDoc CREATE_VIEW = "CreateViewCommand";
65     public static final String JavaDoc RENAME_VIEW = "RenameViewCommand";
66     public static final String JavaDoc COMMENT_VIEW = "CommentViewCommand";
67     public static final String JavaDoc DROP_VIEW = "DropViewCommand";
68     public static final String JavaDoc CREATE_PROCEDURE = "CreateProcedureCommand";
69     public static final String JavaDoc DROP_PROCEDURE = "DropProcedureCommand";
70     public static final String JavaDoc CREATE_FUNCTION = "CreateFunctionCommand";
71     public static final String JavaDoc DROP_FUNCTION = "DropFunctionCommand";
72     public static final String JavaDoc CREATE_TRIGGER = "CreateTriggerCommand";
73     public static final String JavaDoc DROP_TRIGGER = "DropTriggerCommand";
74
75     /** Constructor */
76     public Specification(HashMap JavaDoc description)
77     {
78         desc = description;
79     }
80
81     /** Constructor */
82     public Specification(HashMap JavaDoc description, Connection JavaDoc c)
83     {
84         desc = description;
85         jdbccon = c;
86     }
87
88     /** Returns all database properties */
89     public Map JavaDoc getProperties()
90     {
91         return (Map JavaDoc)desc;
92     }
93
94     /** Returns command description */
95     public Map JavaDoc getCommandProperties(String JavaDoc command)
96     {
97         return (Map JavaDoc)desc.get(command);
98     }
99
100     /** Returns used connection */
101     public DBConnection getConnection()
102     {
103         return (DBConnection)desc.get("connection"); // NOI18N
104
}
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 JavaDoc getMetaDataAdaptorClassName()
117     {
118         if (adaptorClass == null || adaptorClass.length() == 0) {
119             adaptorClass = "org.netbeans.lib.ddl.adaptors.DefaultAdaptor"; // NOI18N
120
}
121
122         return adaptorClass;
123     }
124
125     public void setMetaDataAdaptorClassName(String JavaDoc name)
126     {
127         if (name.startsWith("Database.Adaptors.")) // NOI18N
128
adaptorClass = name;
129         else
130             adaptorClass = "Database.Adaptors."+name; // NOI18N
131
// System.out.println("Metadata adaptor class set = "+adaptorClass);
132
dmdAdaptor = null;
133     }
134
135     /** Returns database metadata */
136     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
137     {
138         try {
139
140             if (dmdAdaptor == null) {
141                 if (jdbccon != null) {
142                     String JavaDoc adc = getMetaDataAdaptorClassName();
143                     if (adc != null) {
144                         ClassLoader JavaDoc loader = Class.forName(adc).getClassLoader();
145                         dmdAdaptor = (DatabaseMetaData JavaDoc) Beans.instantiate(loader, adc);
146                         if (dmdAdaptor instanceof DatabaseMetaDataAdaptor) {
147                             ((DatabaseMetaDataAdaptor)dmdAdaptor).setConnection(jdbccon);
148                         } else throw new ClassNotFoundException JavaDoc(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_AdaptorInterface")); //NOI18N
149
} else throw new ClassNotFoundException JavaDoc(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_AdaptorUnspecClass")); //NOI18N
150
}
151             }
152
153             return dmdAdaptor;
154
155         } catch (Exception JavaDoc ex) {
156             ex.printStackTrace();
157             throw new SQLException JavaDoc(ex.getMessage());
158         }
159     }
160
161     /** Opens JDBC Connection.
162     * This method usually calls command when it need to process something.
163     * But you can call it explicitly and leave connection open until last
164     * command gets executed. Don't forget to close it.
165     */

166     public Connection JavaDoc openJDBCConnection()
167     throws DDLException
168     {
169         if (jdbccon != null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnOpen")); //NOI18N
170
DBConnection dbcon = getConnection();
171         if (dbcon == null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnNot")); //NOI18N
172
try {
173             jdbccon = dbcon.createJDBCConnection();
174         } catch (Exception JavaDoc e) {
175             throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnNot"));
176         }
177
178         return jdbccon;
179     }
180
181     /** Returns JDBC connection.
182     * Commands must test if the connection is not open yet; if you simply call
183     * openJDBCConnection without test (and the connection will be open by user),
184     * a DDLException throws. This is a self-checking mechanism; you must always
185     * close used connection.
186     */

187     public Connection JavaDoc 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")); //NOI18n
196
try {
197             jdbccon.close();
198             jdbccon = null;
199         } catch (SQLException JavaDoc e) {
200             throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ConnUnableClose")); //NOI18N
201
}
202     }
203
204     /** Creates command identified by commandName. Command names will include
205     * create/rename/drop table/view/index/column and comment table/column. It
206     * returns null if command specified by commandName was not found. Used
207     * system allows developers to extend db-specification files and simply
208     * address new commands (everybody can implement createXXXCommand()).
209     */

210     public DDLCommand createCommand(String JavaDoc commandName)
211     throws CommandNotSupportedException
212     {
213         return createCommand(commandName, null);
214     }
215
216     /** Creates command identified by commandName on table tableName.
217     * Returns null if command specified by commandName was not found. It does not
218     * check tableName existency; it simply waits for relevant execute() command
219     * which fires SQLException.
220     */

221     public DDLCommand createCommand(String JavaDoc commandName, String JavaDoc tableName)
222     throws CommandNotSupportedException
223     {
224         String JavaDoc classname;
225         Class JavaDoc cmdclass;
226         AbstractCommand cmd;
227         HashMap JavaDoc cprops = (HashMap JavaDoc)desc.get(commandName);
228         if (cprops != null) classname = (String JavaDoc)cprops.get("Class"); // NOI18N
229
//else throw new CommandNotSupportedException(commandName, "command "+commandName+" is not supported by system");
230
else throw new CommandNotSupportedException(commandName,
231             MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_CommandNotSupported"), new String JavaDoc[] {commandName})); // NOI18N
232
try {
233             cmdclass = Class.forName(classname);
234             cmd = (AbstractCommand)cmdclass.newInstance();
235         } catch (Exception JavaDoc e) {
236             throw new CommandNotSupportedException(commandName,
237                 MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableFindOrInitCommand"), new String JavaDoc[] {classname, commandName, e.getMessage()})); // NOI18N
238
}
239
240         cmd.setObjectName(tableName);
241         cmd.setSpecification(this);
242         cmd.setFormat((String JavaDoc)cprops.get("Format")); // NOI18N
243
return cmd;
244     }
245
246     /** Create table command
247     * @param tableName Name of the table
248     */

249     public CreateTable createCommandCreateTable(String JavaDoc tableName)
250     throws CommandNotSupportedException
251     {
252         return (CreateTable)createCommand(CREATE_TABLE, tableName);
253     }
254
255     /** Comment table command
256     * @param tableName Name of the table
257     * @param comment New comment
258     */

259     public CommentTable createCommandCommentTable(String JavaDoc tableName, String JavaDoc comment)
260     throws CommandNotSupportedException
261     {
262         CommentTable cmd = (CommentTable)createCommand(COMMENT_TABLE, tableName);
263         cmd.setComment(comment);
264         return cmd;
265     }
266
267     /** Drop table command
268     * @param tableName Name of the table
269     */

270     public AbstractCommand createCommandDropTable(String JavaDoc tableName)
271     throws CommandNotSupportedException
272     {
273         return (AbstractCommand)createCommand(DROP_TABLE, tableName);
274     }
275
276     /** Drop table command
277     * @param tableName Name of the table
278     */

279     public RenameTable createCommandRenameTable(String JavaDoc tableName, String JavaDoc newName)
280     throws CommandNotSupportedException
281     {
282         RenameTable cmd = (RenameTable)createCommand(RENAME_TABLE, tableName);
283         cmd.setNewName(newName);
284         return cmd;
285     }
286
287     /** Add column */
288     public AddColumn createCommandAddColumn(String JavaDoc tableName)
289     throws CommandNotSupportedException
290     {
291         return (AddColumn)createCommand(ADD_COLUMN, tableName);
292     }
293
294     /** Modify column */
295     public ModifyColumn createCommandModifyColumn(String JavaDoc tableName)
296     throws CommandNotSupportedException
297     {
298         ModifyColumn cmd = (ModifyColumn)createCommand(MODIFY_COLUMN, tableName);
299         return cmd;
300     }
301
302     /** Rename column */
303     public RenameColumn createCommandRenameColumn(String JavaDoc tableName)
304     throws CommandNotSupportedException
305     {
306         RenameColumn cmd = (RenameColumn)createCommand(RENAME_COLUMN, tableName);
307         return cmd;
308     }
309
310     /** Remove column
311     * @param tableName Name of the table
312     */

313     public RemoveColumn createCommandRemoveColumn(String JavaDoc tableName)
314     throws CommandNotSupportedException
315     {
316         RemoveColumn rcol = (RemoveColumn)createCommand(REMOVE_COLUMN, tableName);
317         return rcol;
318     }
319
320     /** Create index
321     * @param indexName Name of index
322     * @param tableName Name of the table
323     */

324     public CreateIndex createCommandCreateIndex(String JavaDoc tableName)
325     throws CommandNotSupportedException
326     {
327         CreateIndex cicmd = (CreateIndex)createCommand(CREATE_INDEX, tableName);
328         return cicmd;
329     }
330
331     /** Drop index
332     * @param indexName Name of index
333     */

334     public DropIndex createCommandDropIndex(String JavaDoc tablename)
335     throws CommandNotSupportedException
336     {
337         DropIndex dcmd = (DropIndex)createCommand(DROP_INDEX, tablename);
338         return dcmd;
339     }
340
341     /** Create view
342     * @param viewname Name of index
343     */

344     public CreateView createCommandCreateView(String JavaDoc viewname)
345     throws CommandNotSupportedException
346     {
347         return (CreateView)createCommand(CREATE_VIEW, viewname);
348     }
349
350     /** Drop table command
351     * @param tableName Name of the table
352     */

353     public RenameView createCommandRenameView(String JavaDoc tableName, String JavaDoc newName)
354     throws CommandNotSupportedException
355     {
356         RenameView cmd = (RenameView)createCommand(RENAME_VIEW, tableName);
357         cmd.setNewName(newName);
358         return cmd;
359     }
360
361     /** Comment view command
362     * @param tableName Name of the view
363     * @param comment New comment
364     */

365     public CommentView createCommandCommentView(String JavaDoc viewName, String JavaDoc comment)
366     throws CommandNotSupportedException
367     {
368         CommentView cmd = (CommentView)createCommand(COMMENT_VIEW, viewName);
369         cmd.setComment(comment);
370         return cmd;
371     }
372
373     /** Drop view
374     * @param viewname Name of index
375     */

376     public AbstractCommand createCommandDropView(String JavaDoc viewname)
377     throws CommandNotSupportedException
378     {
379         return (AbstractCommand)createCommand(DROP_VIEW, viewname);
380     }
381
382     /** Create procedure
383     * @param viewname Name of procedure
384     */

385     public CreateProcedure createCommandCreateProcedure(String JavaDoc name)
386     throws CommandNotSupportedException
387     {
388         return (CreateProcedure)createCommand(CREATE_PROCEDURE, name);
389     }
390
391     /** Drop procedure
392     * @param viewname Name of procedure
393     */

394     public AbstractCommand createCommandDropProcedure(String JavaDoc name)
395     throws CommandNotSupportedException
396     {
397         return (AbstractCommand)createCommand(DROP_PROCEDURE, name);
398     }
399
400     /** Create function
401     * @param viewname Name of function
402     */

403     public CreateFunction createCommandCreateFunction(String JavaDoc name)
404     throws CommandNotSupportedException
405     {
406         return (CreateFunction)createCommand(CREATE_FUNCTION, name);
407     }
408
409     /** Drop function
410     * @param viewname Name of function
411     */

412     public AbstractCommand createCommandDropFunction(String JavaDoc name)
413     throws CommandNotSupportedException
414     {
415         return (AbstractCommand)createCommand(DROP_FUNCTION, name);
416     }
417
418     /** Create trigger
419     * @param viewname Name of trigger
420     */

421     public CreateTrigger createCommandCreateTrigger(String JavaDoc name, String JavaDoc 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     /** Drop trigger
431     * @param viewname Name of trigger
432     */

433     public AbstractCommand createCommandDropTrigger(String JavaDoc name)
434     throws CommandNotSupportedException
435     {
436         return (AbstractCommand)createCommand(DROP_TRIGGER, name);
437     }
438
439     /** Returns type map */
440     public Map JavaDoc getTypeMap()
441     {
442         return (Map JavaDoc)desc.get("TypeMap"); // NOI18N
443
}
444
445     /** Returns DBType where maps specified java type */
446     public String JavaDoc getType(int type)
447     {
448         String JavaDoc typestr = "";
449         String JavaDoc ret;
450         Map JavaDoc typemap = getTypeMap();
451
452         switch(type) {
453         case java.sql.Types.ARRAY: typestr = "ARRAY"; break; // NOI18N
454
case java.sql.Types.BIGINT: typestr = "BIGINT"; break; // NOI18N
455
case java.sql.Types.BINARY: typestr = "BINARY"; break; // NOI18N
456
case java.sql.Types.BIT: typestr = "BIT"; break; // NOI18N
457
case java.sql.Types.BLOB: typestr = "BLOB"; break; // NOI18N
458
case java.sql.Types.CHAR: typestr = "CHAR"; break; // NOI18N
459
case java.sql.Types.CLOB: typestr = "CLOB"; break; // NOI18N
460
case java.sql.Types.DATE: typestr = "DATE"; break; // NOI18N
461
case java.sql.Types.DECIMAL: typestr = "DECIMAL"; break; // NOI18N
462
case java.sql.Types.DISTINCT: typestr = "DISTINCT"; break; // NOI18N
463
case java.sql.Types.DOUBLE: typestr = "DOUBLE"; break; // NOI18N
464
case java.sql.Types.FLOAT: typestr = "FLOAT"; break; // NOI18N
465
case java.sql.Types.INTEGER: typestr = "INTEGER"; break; // NOI18N
466
case java.sql.Types.JAVA_OBJECT: typestr = "JAVA_OBJECT"; break; // NOI18N
467
case java.sql.Types.LONGVARBINARY: typestr = "LONGVARBINARY"; break; // NOI18N
468
case java.sql.Types.LONGVARCHAR: typestr = "LONGVARCHAR"; break; // NOI18N
469
case java.sql.Types.NUMERIC: typestr = "NUMERIC"; break; // NOI18N
470
case java.sql.Types.OTHER: typestr = "OTHER"; break; // NOI18N
471
case java.sql.Types.REAL: typestr = "REAL"; break; // NOI18N
472
case java.sql.Types.REF: typestr = "REF"; break; // NOI18N
473
case java.sql.Types.SMALLINT: typestr = "SMALLINT"; break; // NOI18N
474
case java.sql.Types.TIME: typestr = "TIME"; break; // NOI18N
475
case java.sql.Types.TIMESTAMP: typestr = "TIMESTAMP"; break; // NOI18N
476
case java.sql.Types.TINYINT: typestr = "TINYINT"; break; // NOI18N
477
case java.sql.Types.VARBINARY: typestr = "VARBINARY"; break; // NOI18N
478
case java.sql.Types.VARCHAR: typestr = "VARCHAR"; break; // NOI18N
479
}
480
481         ret = (String JavaDoc) typemap.get("java.sql.Types." + typestr); // NOI18N
482
if (ret == null)
483             ret = typestr;
484         
485         return ret;
486     }
487
488     /** Returns DBType where maps specified java type */
489     public static int getType(String JavaDoc type)
490     {
491         if (type.equals("java.sql.Types.ARRAY")) return java.sql.Types.ARRAY; // NOI18N
492
if (type.equals("java.sql.Types.BIGINT")) return java.sql.Types.BIGINT; // NOI18N
493
if (type.equals("java.sql.Types.BINARY")) return java.sql.Types.BINARY; // NOI18N
494
if (type.equals("java.sql.Types.BIT")) return java.sql.Types.BIT; // NOI18N
495
if (type.equals("java.sql.Types.BLOB")) return java.sql.Types.BLOB; // NOI18N
496
if (type.equals("java.sql.Types.CHAR")) return java.sql.Types.CHAR; // NOI18N
497
if (type.equals("java.sql.Types.CLOB")) return java.sql.Types.CLOB; // NOI18N
498
if (type.equals("java.sql.Types.DATE")) return java.sql.Types.DATE; // NOI18N
499
if (type.equals("java.sql.Types.DECIMAL")) return java.sql.Types.DECIMAL; // NOI18N
500
if (type.equals("java.sql.Types.DISTINCT")) return java.sql.Types.DISTINCT; // NOI18N
501
if (type.equals("java.sql.Types.DOUBLE")) return java.sql.Types.DOUBLE; // NOI18N
502
if (type.equals("java.sql.Types.FLOAT")) return java.sql.Types.FLOAT; // NOI18N
503
if (type.equals("java.sql.Types.INTEGER")) return java.sql.Types.INTEGER; // NOI18N
504
if (type.equals("java.sql.Types.JAVA_OBJECT")) return java.sql.Types.JAVA_OBJECT; // NOI18N
505
if (type.equals("java.sql.Types.LONGVARBINARY")) return java.sql.Types.LONGVARBINARY; // NOI18N
506
if (type.equals("java.sql.Types.LONGVARCHAR")) return java.sql.Types.LONGVARCHAR; // NOI18N
507
if (type.equals("java.sql.Types.NUMERIC")) return java.sql.Types.NUMERIC; // NOI18N
508
if (type.equals("java.sql.Types.OTHER")) return java.sql.Types.OTHER; // NOI18N
509
if (type.equals("java.sql.Types.REAL")) return java.sql.Types.REAL; // NOI18N
510
if (type.equals("java.sql.Types.REF")) return java.sql.Types.REF; // NOI18N
511
if (type.equals("java.sql.Types.SMALLINT")) return java.sql.Types.SMALLINT; // NOI18N
512
if (type.equals("java.sql.Types.TIME")) return java.sql.Types.TIME; // NOI18N
513
if (type.equals("java.sql.Types.TIMESTAMP")) return java.sql.Types.TIMESTAMP; // NOI18N
514
if (type.equals("java.sql.Types.TINYINT")) return java.sql.Types.TINYINT; // NOI18N
515
if (type.equals("java.sql.Types.VARBINARY")) return java.sql.Types.VARBINARY; // NOI18N
516
if (type.equals("java.sql.Types.VARCHAR")) return java.sql.Types.VARCHAR; // NOI18N
517

518         return -1;
519     }
520 }
521
Popular Tags