001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.DB_Index
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:
030: import java.util.HashMap;
031: import java.util.StringTokenizer;
032:
033: import org.apache.derby.tools.dblook;
034:
035: public class DB_Index {
036:
037: /* ************************************************
038: * Generate the DDL for all indexes in a given
039: * database.
040: * @param conn Connection to the source database.
041: * @return The DDL for the indexes has been written
042: * to output via Logs.java.
043: ****/
044:
045: public static void doIndexes(Connection conn) throws SQLException {
046:
047: Statement stmt = conn.createStatement();
048: ResultSet rs = stmt
049: .executeQuery("SELECT TABLEID, CONGLOMERATENAME, "
050: + "DESCRIPTOR, SCHEMAID, ISINDEX, ISCONSTRAINT FROM SYS.SYSCONGLOMERATES "
051: + "ORDER BY TABLEID");
052:
053: boolean firstTime = true;
054: while (rs.next()) {
055:
056: if (!rs.getBoolean(5) || // (isindex == false)
057: rs.getBoolean(6)) // (isconstraint == true)
058: // then skip it.
059: continue;
060:
061: String tableId = rs.getString(1);
062: String tableName = dblook.lookupTableId(tableId);
063: if (tableName == null)
064: // then tableId isn't a user table, so we can skip it.
065: continue;
066: else if (dblook.isExcludedTable(tableName))
067: // table isn't specified in user-given list.
068: continue;
069:
070: String iSchema = dblook.lookupSchemaId(rs.getString(4));
071: if (dblook.isIgnorableSchema(iSchema))
072: continue;
073:
074: if (firstTime) {
075: Logs
076: .reportString("----------------------------------------------");
077: Logs.reportMessage("DBLOOK_IndexesHeader");
078: Logs
079: .reportString("----------------------------------------------\n");
080: }
081:
082: String iName = dblook.addQuotes(dblook
083: .expandDoubleQuotes(rs.getString(2)));
084: iName = iSchema + "." + iName;
085:
086: StringBuffer createIndexString = createIndex(iName,
087: tableName, tableId, rs.getString(3));
088:
089: Logs.writeToNewDDL(createIndexString.toString());
090: Logs.writeStmtEndToNewDDL();
091: Logs.writeNewlineToNewDDL();
092: firstTime = false;
093:
094: }
095:
096: rs.close();
097: stmt.close();
098:
099: }
100:
101: /* ************************************************
102: * Generate DDL for a specific index.
103: * @param ixName Name of the index.
104: * @param tableName Name of table on which the index exists.
105: * @param tableId Id of table on which the index exists.
106: * @param ixDescribe Column list for the index.
107: * @return The DDL for the specified index, as a string
108: * buffer.
109: ****/
110:
111: private static StringBuffer createIndex(String ixName,
112: String tableName, String tableId, String ixDescribe)
113: throws SQLException {
114:
115: StringBuffer sb = new StringBuffer("CREATE ");
116: if (ixDescribe.indexOf("UNIQUE") != -1)
117: sb.append("UNIQUE ");
118:
119: // Note: We leave the keyword "BTREE" out since it's not
120: // required, and since it is not recognized by DB2.
121:
122: sb.append("INDEX ");
123: sb.append(ixName);
124: sb.append(" ON ");
125: sb.append(tableName);
126: sb.append(" (");
127: sb.append(dblook.getColumnListFromDescription(tableId,
128: ixDescribe));
129: sb.append(")");
130: return sb;
131:
132: }
133:
134: }
|