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 30 import java.util.HashMap ; 31 import java.util.StringTokenizer ; 32 33 import org.apache.derby.tools.dblook; 34 35 public class DB_Index { 36 37 44 45 public static void doIndexes(Connection conn) 46 throws SQLException 47 { 48 49 Statement stmt = conn.createStatement(); 50 ResultSet rs = stmt.executeQuery("SELECT TABLEID, CONGLOMERATENAME, " + 51 "DESCRIPTOR, SCHEMAID, ISINDEX, ISCONSTRAINT FROM SYS.SYSCONGLOMERATES " + 52 "ORDER BY TABLEID"); 53 54 boolean firstTime = true; 55 while (rs.next()) { 56 57 if (!rs.getBoolean(5) || rs.getBoolean(6)) continue; 61 62 String tableId = rs.getString(1); 63 String tableName = dblook.lookupTableId(tableId); 64 if (tableName == null) 65 continue; 67 else if (dblook.isExcludedTable(tableName)) 68 continue; 70 71 String iSchema = dblook.lookupSchemaId(rs.getString(4)); 72 if (dblook.isIgnorableSchema(iSchema)) 73 continue; 74 75 if (firstTime) { 76 Logs.reportString("----------------------------------------------"); 77 Logs.reportMessage("DBLOOK_IndexesHeader"); 78 Logs.reportString("----------------------------------------------\n"); 79 } 80 81 String iName = dblook.addQuotes( 82 dblook.expandDoubleQuotes(rs.getString(2))); 83 iName = iSchema + "." + iName; 84 85 StringBuffer createIndexString = createIndex(iName, tableName, 86 tableId, rs.getString(3)); 87 88 Logs.writeToNewDDL(createIndexString.toString()); 89 Logs.writeStmtEndToNewDDL(); 90 Logs.writeNewlineToNewDDL(); 91 firstTime = false; 92 93 } 94 95 rs.close(); 96 stmt.close(); 97 98 } 99 100 109 110 private static StringBuffer createIndex(String ixName, String tableName, 111 String tableId, String ixDescribe) throws SQLException 112 { 113 114 StringBuffer sb = new StringBuffer ("CREATE "); 115 if (ixDescribe.indexOf("UNIQUE") != -1) 116 sb.append("UNIQUE "); 117 118 121 sb.append("INDEX "); 122 sb.append(ixName); 123 sb.append(" ON "); 124 sb.append(tableName); 125 sb.append(" ("); 126 sb.append(dblook.getColumnListFromDescription(tableId, ixDescribe)); 127 sb.append(")"); 128 return sb; 129 130 } 131 132 } 133 | Popular Tags |