KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.DB_Index
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
30 import java.util.HashMap JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.derby.tools.dblook;
34
35 public class DB_Index {
36
37     /* ************************************************
38      * Generate the DDL for all indexes in a given
39      * database.
40      * @param conn Connection to the source database.
41      * @return The DDL for the indexes has been written
42      * to output via Logs.java.
43      ****/

44
45     public static void doIndexes(Connection JavaDoc conn)
46         throws SQLException JavaDoc
47     {
48
49         Statement JavaDoc stmt = conn.createStatement();
50         ResultSet JavaDoc 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) || // (isindex == false)
58
rs.getBoolean(6)) // (isconstraint == true)
59
// then skip it.
60
continue;
61
62             String JavaDoc tableId = rs.getString(1);
63             String JavaDoc tableName = dblook.lookupTableId(tableId);
64             if (tableName == null)
65             // then tableId isn't a user table, so we can skip it.
66
continue;
67             else if (dblook.isExcludedTable(tableName))
68             // table isn't specified in user-given list.
69
continue;
70
71             String JavaDoc 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 JavaDoc iName = dblook.addQuotes(
82                 dblook.expandDoubleQuotes(rs.getString(2)));
83             iName = iSchema + "." + iName;
84
85             StringBuffer JavaDoc 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     /* ************************************************
101      * Generate DDL for a specific index.
102      * @param ixName Name of the index.
103      * @param tableName Name of table on which the index exists.
104      * @param tableId Id of table on which the index exists.
105      * @param ixDescribe Column list for the index.
106      * @return The DDL for the specified index, as a string
107      * buffer.
108      ****/

109
110     private static StringBuffer JavaDoc createIndex(String JavaDoc ixName, String JavaDoc tableName,
111         String JavaDoc tableId, String JavaDoc ixDescribe) throws SQLException JavaDoc
112     {
113
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("CREATE ");
115         if (ixDescribe.indexOf("UNIQUE") != -1)
116             sb.append("UNIQUE ");
117
118         // Note: We leave the keyword "BTREE" out since it's not
119
// required, and since it is not recognized by DB2.
120

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