KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > dblook > DB_Alias


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.DB_Alias
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.dblook;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.DatabaseMetaData JavaDoc;
30
31 import java.util.HashMap JavaDoc;
32 import org.apache.derby.tools.dblook;
33
34 public class DB_Alias {
35
36     // Prepared statements use throughout the DDL
37
// generation process.
38

39     /* ************************************************
40      * Generate the DDL for all stored procedures and
41      * functions in a given database and write it to
42      * output via Logs.java.
43      * @param conn Connection to the source database.
44      ****/

45
46     public static void doProceduresAndFunctions(Connection JavaDoc conn)
47         throws SQLException JavaDoc {
48
49         // First do stored procedures.
50
Statement JavaDoc stmt = conn.createStatement();
51         ResultSet JavaDoc rs = stmt.executeQuery("SELECT ALIAS, ALIASINFO, " +
52             "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES " +
53             "WHERE ALIASTYPE='P'");
54         generateDDL(rs, 'P'); // 'P' => for PROCEDURES
55

56         // Now do functions.
57
rs = stmt.executeQuery("SELECT ALIAS, ALIASINFO, " +
58             "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES " +
59             "WHERE ALIASTYPE='F'");
60         generateDDL(rs, 'F'); // 'F' => for FUNCTIONS
61

62         rs.close();
63         stmt.close();
64         return;
65
66     }
67
68     /* ************************************************
69      * Generate the DDL for either stored procedures or
70      * functions in a given database, depending on the
71      * the received aliasType.
72      * @param rs Result set holding either stored procedures
73      * or functions.
74      * @param aliasType Indication of whether we're generating
75      * stored procedures or functions.
76      ****/

77     private static void generateDDL(ResultSet JavaDoc rs, char aliasType)
78         throws SQLException JavaDoc
79     {
80
81         boolean firstTime = true;
82         while (rs.next()) {
83
84             if (rs.getBoolean(6))
85             // it's a system alias, so we ignore it.
86
continue;
87
88             String JavaDoc procSchema = dblook.lookupSchemaId(rs.getString(4));
89             if (dblook.isIgnorableSchema(procSchema))
90                 continue;
91
92             if (firstTime) {
93                 Logs.reportString("----------------------------------------------");
94                 Logs.reportMessage((aliasType == 'P')
95                     ? "DBLOOK_StoredProcHeader"
96                     : "DBLOOK_FunctionHeader");
97                 Logs.reportString("----------------------------------------------\n");
98             }
99
100             String JavaDoc aliasName = rs.getString(1);
101             String JavaDoc fullName = dblook.addQuotes(
102                 dblook.expandDoubleQuotes(aliasName));
103             fullName = procSchema + "." + fullName;
104
105             String JavaDoc creationString = createProcOrFuncString(
106                 fullName, rs, aliasType);
107             Logs.writeToNewDDL(creationString);
108             Logs.writeStmtEndToNewDDL();
109             Logs.writeNewlineToNewDDL();
110             firstTime = false;
111
112         }
113
114     }
115
116     /* ************************************************
117      * Generate DDL for a specific stored procedure or
118      * function.
119      * @param aliasName Name of the current procedure/function
120      * @param aliasInfo Info about the current procedure/function
121      * @param aliasType Indicator of whether we're generating
122      * a stored procedure or a function.
123      * @return DDL for the current stored procedure is
124      * returned, as a String.
125      ****/

126
127     private static String JavaDoc createProcOrFuncString(String JavaDoc aliasName,
128         ResultSet JavaDoc aliasInfo, char aliasType) throws SQLException JavaDoc
129     {
130
131         StringBuffer JavaDoc alias = new StringBuffer JavaDoc("CREATE ");
132         if (aliasType == 'P')
133             alias.append("PROCEDURE ");
134         else if (aliasType == 'F')
135             alias.append("FUNCTION ");
136         alias.append(aliasName);
137         alias.append(" ");
138
139         String JavaDoc params = aliasInfo.getString(2);
140
141         // Just grab the parameter part; we'll get the method name later.
142
alias.append(params.substring(params.indexOf("("), params.length()));
143         alias.append(" ");
144
145         // Now add the external name.
146
alias.append("EXTERNAL NAME '");
147         alias.append(aliasInfo.getString(5));
148         alias.append(".");
149         // Get method name from parameter string fetched above.
150
alias.append(params.substring(0, params.indexOf("(")));
151         alias.append("' ");
152
153         return alias.toString();
154
155     }
156
157     /* ************************************************
158      * Generate the DDL for all synonyms in a given
159      * database. On successul return, the DDL for the
160      * synonyms has been written to output via Logs.java.
161      * @param conn Connection to the source database.
162      * @return
163      ****/

164     public static void doSynonyms(Connection JavaDoc conn) throws SQLException JavaDoc
165     {
166         Statement JavaDoc stmt = conn.createStatement();
167         ResultSet JavaDoc rs = stmt.executeQuery("SELECT ALIAS, SCHEMAID, " +
168             "ALIASINFO, SYSTEMALIAS FROM SYS.SYSALIASES A WHERE ALIASTYPE='S'");
169
170         boolean firstTime = true;
171         while (rs.next()) {
172             if (rs.getBoolean(4))
173             // it's a system alias, so we ignore it.
174
continue;
175
176             String JavaDoc aliasSchema = dblook.lookupSchemaId(rs.getString(2));
177             if (dblook.isIgnorableSchema(aliasSchema))
178                 continue;
179
180             if (firstTime) {
181                 Logs.reportString("----------------------------------------------");
182                 Logs.reportMessage("DBLOOK_SynonymHeader");
183                 Logs.reportString("----------------------------------------------\n");
184             }
185
186             String JavaDoc aliasName = rs.getString(1);
187             String JavaDoc aliasFullName = dblook.addQuotes(
188                 dblook.expandDoubleQuotes(aliasName));
189             aliasFullName = aliasSchema + "." + aliasFullName;
190
191             Logs.writeToNewDDL("CREATE SYNONYM "+aliasFullName+" FOR "+rs.getString(3));
192             Logs.writeStmtEndToNewDDL();
193             Logs.writeNewlineToNewDDL();
194             firstTime = false;
195         }
196
197         rs.close();
198         stmt.close();
199         return;
200
201     }
202 }
203
Popular Tags