001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.dblook.DB_View
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.ResultSet;
027: import java.sql.SQLException;
028:
029: import java.util.HashMap;
030:
031: import org.apache.derby.tools.dblook;
032:
033: public class DB_View {
034:
035: /* ************************************************
036: * Generate the DDL for all views in a given
037: * database.
038: * @param conn Connection to the source database.
039: * @return The DDL for the views has been written
040: * to output via Logs.java.
041: ****/
042:
043: public static void doViews(Connection conn) throws SQLException {
044:
045: Statement stmt = conn.createStatement();
046: ResultSet rs = stmt
047: .executeQuery("SELECT V.VIEWDEFINITION, "
048: + "T.TABLENAME, T.SCHEMAID, V.COMPILATIONSCHEMAID FROM SYS.SYSVIEWS V, "
049: + "SYS.SYSTABLES T WHERE T.TABLEID = V.TABLEID");
050:
051: boolean firstTime = true;
052: while (rs.next()) {
053:
054: String viewSchema = dblook.lookupSchemaId(rs.getString(3));
055: if (dblook.isIgnorableSchema(viewSchema))
056: continue;
057:
058: if (!dblook.stringContainsTargetTable(rs.getString(1)))
059: continue;
060:
061: if (firstTime) {
062: Logs
063: .reportString("----------------------------------------------");
064: Logs.reportMessage("DBLOOK_ViewsHeader");
065: Logs
066: .reportString("----------------------------------------------\n");
067: }
068:
069: // We are using the exact text that was entered by the user,
070: // which means the view name that is given might not include
071: // the schema in which the view was created. So, we change
072: // our schema to be the one in which the view was created
073: // before we execute the create statement.
074: Logs.writeToNewDDL("SET SCHEMA ");
075: Logs.writeToNewDDL(dblook.lookupSchemaId(rs.getString(4)));
076: Logs.writeStmtEndToNewDDL();
077:
078: // Now, go ahead and create the view.
079: Logs.writeToNewDDL(dblook.removeNewlines(rs.getString(1)));
080: Logs.writeStmtEndToNewDDL();
081: Logs.writeNewlineToNewDDL();
082: firstTime = false;
083:
084: }
085:
086: // Set schema back to default ("APP").
087: if (!firstTime) {
088: Logs.reportMessage("DBLOOK_DefaultSchema");
089: Logs.writeToNewDDL("SET SCHEMA \"APP\"");
090: Logs.writeStmtEndToNewDDL();
091: Logs.writeNewlineToNewDDL();
092: }
093:
094: rs.close();
095: stmt.close();
096: return;
097:
098: }
099:
100: }
|