001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.DB_Alias
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.dblook;
023:
024: import java.sql.Connection;
025: import java.sql.Statement;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.sql.DatabaseMetaData;
030:
031: import java.util.HashMap;
032: import org.apache.derby.tools.dblook;
033:
034: public class DB_Alias {
035:
036: // Prepared statements use throughout the DDL
037: // generation process.
038:
039: /* ************************************************
040: * Generate the DDL for all stored procedures and
041: * functions in a given database and write it to
042: * output via Logs.java.
043: * @param conn Connection to the source database.
044: ****/
045:
046: public static void doProceduresAndFunctions(Connection conn)
047: throws SQLException {
048:
049: // First do stored procedures.
050: Statement stmt = conn.createStatement();
051: ResultSet rs = stmt
052: .executeQuery("SELECT ALIAS, ALIASINFO, "
053: + "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES "
054: + "WHERE ALIASTYPE='P'");
055: generateDDL(rs, 'P'); // 'P' => for PROCEDURES
056:
057: // Now do functions.
058: rs = stmt
059: .executeQuery("SELECT ALIAS, ALIASINFO, "
060: + "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES "
061: + "WHERE ALIASTYPE='F'");
062: generateDDL(rs, 'F'); // 'F' => for FUNCTIONS
063:
064: rs.close();
065: stmt.close();
066: return;
067:
068: }
069:
070: /* ************************************************
071: * Generate the DDL for either stored procedures or
072: * functions in a given database, depending on the
073: * the received aliasType.
074: * @param rs Result set holding either stored procedures
075: * or functions.
076: * @param aliasType Indication of whether we're generating
077: * stored procedures or functions.
078: ****/
079: private static void generateDDL(ResultSet rs, char aliasType)
080: throws SQLException {
081:
082: boolean firstTime = true;
083: while (rs.next()) {
084:
085: if (rs.getBoolean(6))
086: // it's a system alias, so we ignore it.
087: continue;
088:
089: String procSchema = dblook.lookupSchemaId(rs.getString(4));
090: if (dblook.isIgnorableSchema(procSchema))
091: continue;
092:
093: if (firstTime) {
094: Logs
095: .reportString("----------------------------------------------");
096: Logs
097: .reportMessage((aliasType == 'P') ? "DBLOOK_StoredProcHeader"
098: : "DBLOOK_FunctionHeader");
099: Logs
100: .reportString("----------------------------------------------\n");
101: }
102:
103: String aliasName = rs.getString(1);
104: String fullName = dblook.addQuotes(dblook
105: .expandDoubleQuotes(aliasName));
106: fullName = procSchema + "." + fullName;
107:
108: String creationString = createProcOrFuncString(fullName,
109: rs, aliasType);
110: Logs.writeToNewDDL(creationString);
111: Logs.writeStmtEndToNewDDL();
112: Logs.writeNewlineToNewDDL();
113: firstTime = false;
114:
115: }
116:
117: }
118:
119: /* ************************************************
120: * Generate DDL for a specific stored procedure or
121: * function.
122: * @param aliasName Name of the current procedure/function
123: * @param aliasInfo Info about the current procedure/function
124: * @param aliasType Indicator of whether we're generating
125: * a stored procedure or a function.
126: * @return DDL for the current stored procedure is
127: * returned, as a String.
128: ****/
129:
130: private static String createProcOrFuncString(String aliasName,
131: ResultSet aliasInfo, char aliasType) throws SQLException {
132:
133: StringBuffer alias = new StringBuffer("CREATE ");
134: if (aliasType == 'P')
135: alias.append("PROCEDURE ");
136: else if (aliasType == 'F')
137: alias.append("FUNCTION ");
138: alias.append(aliasName);
139: alias.append(" ");
140:
141: String params = aliasInfo.getString(2);
142:
143: // Just grab the parameter part; we'll get the method name later.
144: alias.append(params.substring(params.indexOf("("), params
145: .length()));
146: alias.append(" ");
147:
148: // Now add the external name.
149: alias.append("EXTERNAL NAME '");
150: alias.append(aliasInfo.getString(5));
151: alias.append(".");
152: // Get method name from parameter string fetched above.
153: alias.append(params.substring(0, params.indexOf("(")));
154: alias.append("' ");
155:
156: return alias.toString();
157:
158: }
159:
160: /* ************************************************
161: * Generate the DDL for all synonyms in a given
162: * database. On successul return, the DDL for the
163: * synonyms has been written to output via Logs.java.
164: * @param conn Connection to the source database.
165: * @return
166: ****/
167: public static void doSynonyms(Connection conn) throws SQLException {
168: Statement stmt = conn.createStatement();
169: ResultSet rs = stmt
170: .executeQuery("SELECT ALIAS, SCHEMAID, "
171: + "ALIASINFO, SYSTEMALIAS FROM SYS.SYSALIASES A WHERE ALIASTYPE='S'");
172:
173: boolean firstTime = true;
174: while (rs.next()) {
175: if (rs.getBoolean(4))
176: // it's a system alias, so we ignore it.
177: continue;
178:
179: String aliasSchema = dblook.lookupSchemaId(rs.getString(2));
180: if (dblook.isIgnorableSchema(aliasSchema))
181: continue;
182:
183: if (firstTime) {
184: Logs
185: .reportString("----------------------------------------------");
186: Logs.reportMessage("DBLOOK_SynonymHeader");
187: Logs
188: .reportString("----------------------------------------------\n");
189: }
190:
191: String aliasName = rs.getString(1);
192: String aliasFullName = dblook.addQuotes(dblook
193: .expandDoubleQuotes(aliasName));
194: aliasFullName = aliasSchema + "." + aliasFullName;
195:
196: Logs.writeToNewDDL("CREATE SYNONYM " + aliasFullName
197: + " FOR " + rs.getString(3));
198: Logs.writeStmtEndToNewDDL();
199: Logs.writeNewlineToNewDDL();
200: firstTime = false;
201: }
202:
203: rs.close();
204: stmt.close();
205: return;
206:
207: }
208: }
|