KickJava   Java API By Example, From Geeks To Geeks.

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


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

45
46     public static void doChecks(Connection JavaDoc conn)
47         throws SQLException JavaDoc
48     {
49
50         Statement JavaDoc stmt = conn.createStatement();
51         ResultSet JavaDoc rs = stmt.executeQuery("SELECT CS.CONSTRAINTNAME, " +
52             "CS.TABLEID, CS.SCHEMAID, CK.CHECKDEFINITION FROM SYS.SYSCONSTRAINTS CS, " +
53             "SYS.SYSCHECKS CK WHERE CS.CONSTRAINTID = " +
54             "CK.CONSTRAINTID AND CS.STATE != 'D' ORDER BY CS.TABLEID");
55
56         boolean firstTime = true;
57         while (rs.next()) {
58
59             String JavaDoc tableId = rs.getString(2);
60             String JavaDoc tableName = dblook.lookupTableId(tableId);
61             if (dblook.isExcludedTable(tableName))
62             // table isn't specified in user-given list; skip it.
63
continue;
64
65             if (firstTime) {
66                 Logs.reportString("----------------------------------------------");
67                 Logs.reportMessage("DBLOOK_ChecksHeader");
68                 Logs.reportString("----------------------------------------------\n");
69             }
70
71             StringBuffer JavaDoc chkString = createCheckString(tableName, rs);
72             Logs.writeToNewDDL(chkString.toString());
73             Logs.writeStmtEndToNewDDL();
74             Logs.writeNewlineToNewDDL();
75             firstTime = false;
76
77         }
78
79         stmt.close();
80         rs.close();
81         return;
82
83     }
84
85     /* ************************************************
86      * Generate DDL for a specific check.
87      * @param tableName Name of the table on which the check
88      * exists.
89      * @param aCheck Information about the check in question.
90      * @return The DDL for the specified check has been
91      * generated returned as a StringBuffer.
92      ****/

93
94     private static StringBuffer JavaDoc createCheckString (String JavaDoc tableName,
95         ResultSet JavaDoc aCheck) throws SQLException JavaDoc
96     {
97
98         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("ALTER TABLE ");
99         sb.append(tableName);
100         sb.append(" ADD");
101
102         String JavaDoc constraintName = dblook.addQuotes(
103             dblook.expandDoubleQuotes(aCheck.getString(1)));
104         sb.append(" CONSTRAINT ");
105         sb.append(constraintName);
106         sb.append(" CHECK ");
107         sb.append(dblook.removeNewlines(aCheck.getString(4)));
108
109         return sb;
110
111     }
112
113 }
114
Popular Tags