1 21 22 package org.apache.derby.impl.tools.dblook; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.DatabaseMetaData ; 30 31 import java.util.HashMap ; 32 import org.apache.derby.tools.dblook; 33 34 public class DB_Alias { 35 36 39 45 46 public static void doProceduresAndFunctions(Connection conn) 47 throws SQLException { 48 49 Statement stmt = conn.createStatement(); 51 ResultSet rs = stmt.executeQuery("SELECT ALIAS, ALIASINFO, " + 52 "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES " + 53 "WHERE ALIASTYPE='P'"); 54 generateDDL(rs, 'P'); 56 rs = stmt.executeQuery("SELECT ALIAS, ALIASINFO, " + 58 "ALIASID, SCHEMAID, JAVACLASSNAME, SYSTEMALIAS FROM SYS.SYSALIASES " + 59 "WHERE ALIASTYPE='F'"); 60 generateDDL(rs, 'F'); 62 rs.close(); 63 stmt.close(); 64 return; 65 66 } 67 68 77 private static void generateDDL(ResultSet rs, char aliasType) 78 throws SQLException 79 { 80 81 boolean firstTime = true; 82 while (rs.next()) { 83 84 if (rs.getBoolean(6)) 85 continue; 87 88 String 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 aliasName = rs.getString(1); 101 String fullName = dblook.addQuotes( 102 dblook.expandDoubleQuotes(aliasName)); 103 fullName = procSchema + "." + fullName; 104 105 String 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 126 127 private static String createProcOrFuncString(String aliasName, 128 ResultSet aliasInfo, char aliasType) throws SQLException 129 { 130 131 StringBuffer alias = new StringBuffer ("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 params = aliasInfo.getString(2); 140 141 alias.append(params.substring(params.indexOf("("), params.length())); 143 alias.append(" "); 144 145 alias.append("EXTERNAL NAME '"); 147 alias.append(aliasInfo.getString(5)); 148 alias.append("."); 149 alias.append(params.substring(0, params.indexOf("("))); 151 alias.append("' "); 152 153 return alias.toString(); 154 155 } 156 157 164 public static void doSynonyms(Connection conn) throws SQLException 165 { 166 Statement stmt = conn.createStatement(); 167 ResultSet 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 continue; 175 176 String 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 aliasName = rs.getString(1); 187 String 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 |