KickJava   Java API By Example, From Geeks To Geeks.

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


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

42
43     public static void doViews(Connection JavaDoc conn)
44         throws SQLException JavaDoc {
45
46         Statement JavaDoc stmt = conn.createStatement();
47         ResultSet JavaDoc rs = stmt.executeQuery("SELECT V.VIEWDEFINITION, " +
48             "T.TABLENAME, T.SCHEMAID, V.COMPILATIONSCHEMAID FROM SYS.SYSVIEWS V, " +
49             "SYS.SYSTABLES T WHERE T.TABLEID = V.TABLEID");
50
51         boolean firstTime = true;
52         while (rs.next()) {
53
54             String JavaDoc viewSchema = dblook.lookupSchemaId(rs.getString(3));
55             if (dblook.isIgnorableSchema(viewSchema))
56                 continue;
57
58             if (!dblook.stringContainsTargetTable(rs.getString(1)))
59                 continue;
60
61             if (firstTime) {
62                 Logs.reportString("----------------------------------------------");
63                 Logs.reportMessage("DBLOOK_ViewsHeader");
64                 Logs.reportString("----------------------------------------------\n");
65             }
66
67             // We are using the exact text that was entered by the user,
68
// which means the view name that is given might not include
69
// the schema in which the view was created. So, we change
70
// our schema to be the one in which the view was created
71
// before we execute the create statement.
72
Logs.writeToNewDDL("SET SCHEMA ");
73             Logs.writeToNewDDL(dblook.lookupSchemaId(rs.getString(4)));
74             Logs.writeStmtEndToNewDDL();
75
76             // Now, go ahead and create the view.
77
Logs.writeToNewDDL(dblook.removeNewlines(rs.getString(1)));
78             Logs.writeStmtEndToNewDDL();
79             Logs.writeNewlineToNewDDL();
80             firstTime = false;
81
82         }
83
84         // Set schema back to default ("APP").
85
if (!firstTime) {
86             Logs.reportMessage("DBLOOK_DefaultSchema");
87             Logs.writeToNewDDL("SET SCHEMA \"APP\"");
88             Logs.writeStmtEndToNewDDL();
89             Logs.writeNewlineToNewDDL();
90         }
91
92         rs.close();
93         stmt.close();
94         return;
95
96     }
97
98 }
99
Popular Tags