001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.DB_Check
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.ArrayList;
032: import java.util.StringTokenizer;
033:
034: import org.apache.derby.tools.dblook;
035:
036: public class DB_Check {
037:
038: /* ************************************************
039: * Generate the DDL for all checks in a given
040: * database.
041: * @param conn Connection to the source database.
042: * @return The DDL for the indexes has been written
043: * to output via Logs.java.
044: ****/
045:
046: public static void doChecks(Connection conn) throws SQLException {
047:
048: Statement stmt = conn.createStatement();
049: ResultSet rs = stmt
050: .executeQuery("SELECT CS.CONSTRAINTNAME, "
051: + "CS.TABLEID, CS.SCHEMAID, CK.CHECKDEFINITION FROM SYS.SYSCONSTRAINTS CS, "
052: + "SYS.SYSCHECKS CK WHERE CS.CONSTRAINTID = "
053: + "CK.CONSTRAINTID AND CS.STATE != 'D' ORDER BY CS.TABLEID");
054:
055: boolean firstTime = true;
056: while (rs.next()) {
057:
058: String tableId = rs.getString(2);
059: String tableName = dblook.lookupTableId(tableId);
060: if (dblook.isExcludedTable(tableName))
061: // table isn't specified in user-given list; skip it.
062: continue;
063:
064: if (firstTime) {
065: Logs
066: .reportString("----------------------------------------------");
067: Logs.reportMessage("DBLOOK_ChecksHeader");
068: Logs
069: .reportString("----------------------------------------------\n");
070: }
071:
072: StringBuffer chkString = createCheckString(tableName, rs);
073: Logs.writeToNewDDL(chkString.toString());
074: Logs.writeStmtEndToNewDDL();
075: Logs.writeNewlineToNewDDL();
076: firstTime = false;
077:
078: }
079:
080: stmt.close();
081: rs.close();
082: return;
083:
084: }
085:
086: /* ************************************************
087: * Generate DDL for a specific check.
088: * @param tableName Name of the table on which the check
089: * exists.
090: * @param aCheck Information about the check in question.
091: * @return The DDL for the specified check has been
092: * generated returned as a StringBuffer.
093: ****/
094:
095: private static StringBuffer createCheckString(String tableName,
096: ResultSet aCheck) throws SQLException {
097:
098: StringBuffer sb = new StringBuffer("ALTER TABLE ");
099: sb.append(tableName);
100: sb.append(" ADD");
101:
102: String constraintName = dblook.addQuotes(dblook
103: .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: }
|