KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > ViewDependency


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl;
21
22 import java.sql.*;
23 import java.util.*;
24
25 public class ViewDependency {
26     private Connection con;
27     private String JavaDoc user;
28     private String JavaDoc view;
29     private DatabaseMetaData dmd;
30     private LinkedList tables;
31     private LinkedList columns;
32
33     /** Creates new ViewDependency */
34     public ViewDependency(ConnectionProvider cp, String JavaDoc user, String JavaDoc view) throws SQLException {
35         con = cp.getConnection();
36         this.user = user;
37         this.view = view;
38         dmd = cp.getDatabaseMetaData();
39         
40         tables = new LinkedList();
41         columns = new LinkedList();
42     }
43     
44     public LinkedList getTables() {
45         return tables;
46     }
47     
48     public LinkedList getColumns() {
49         return columns;
50     }
51
52     public void constructPK() {
53         try {
54             String JavaDoc database = dmd.getDatabaseProductName();
55             if (database==null){
56                 return;
57             }
58             database = database.trim();
59             if (database.equalsIgnoreCase("Oracle")) {
60                 getOraclePKTable(user, view);
61                 getOracleViewColumns();
62                 return;
63             }
64             
65             if (database.equalsIgnoreCase("Microsoft SQL Server")) {
66                 getMSSQLServerPKTable(user, view);
67                 getMSSQLServerViewColumns();
68                 return;
69             }
70         } catch (SQLException exc) {
71             exc.printStackTrace();
72         }
73     }
74
75     private void getOraclePKTable(String JavaDoc user, String JavaDoc view) throws SQLException {
76         PreparedStatement stmt;
77         ResultSet rs;
78         
79         String JavaDoc query = "select OWNER, REFERENCED_OWNER, REFERENCED_NAME, REFERENCED_TYPE from ALL_DEPENDENCIES where NAME = ? AND OWNER = ?";
80
81         stmt = con.prepareStatement(query);
82         stmt.setString(1, view);
83         stmt.setString(2, user);
84         rs = stmt.executeQuery();
85         
86         while (rs.next()) {
87             String JavaDoc type = rs.getString(4).trim();
88             if (type.equalsIgnoreCase("TABLE")) {
89                 tables.add(rs.getString(3).trim());
90                 continue;
91             }
92
93             if (type.equalsIgnoreCase("VIEW"))
94                 getOraclePKTable(rs.getString(2), rs.getString(3));
95         }
96         rs.close();
97         stmt.close();
98     }
99
100     private void getMSSQLServerPKTable(String JavaDoc user, String JavaDoc view) throws SQLException {
101         CallableStatement cs;
102         ResultSet rs;
103         String JavaDoc name;
104         int pos;
105         
106         cs = con.prepareCall("{call sp_depends(?)}");
107         cs.setString(1, view);
108         try {
109             rs = cs.executeQuery();
110             
111             while (rs.next()) {
112                 String JavaDoc type = rs.getString(2).trim().toLowerCase();
113                 name = rs.getString(1).trim();
114                 pos = name.lastIndexOf(".");
115                 name = name.substring(pos + 1);
116
117                 if (type.indexOf("table") != -1)
118                     if (! tables.contains(name)) {
119                         tables.add(name);
120                         continue;
121                     }
122
123                 if (type.equals("view"))
124                     getMSSQLServerPKTable(user, name);
125             }
126             rs.close();
127         } catch (Exception JavaDoc exc) {
128             //no result set produced or unexpected driver error
129
rs = null;
130             return;
131         }
132     }
133     
134     private void getOracleViewColumns() throws SQLException {
135         PreparedStatement stmt;
136         ResultSet rs;
137         String JavaDoc text = null;
138         int startPos, endPos;
139         
140         String JavaDoc query = "select TEXT from ALL_VIEWS where VIEW_NAME = ?";
141
142         stmt = con.prepareStatement(query);
143         stmt.setString(1, view);
144         rs = stmt.executeQuery();
145         
146         if (rs.next())
147             text = rs.getString(1).trim();
148         rs.close();
149         stmt.close();
150         
151         if (text == null)
152             return;
153         
154         startPos = text.indexOf(" ");
155         endPos = text.toLowerCase().indexOf("from");
156         text = text.substring(startPos, endPos).trim();
157         
158         StringTokenizer st = new StringTokenizer(text, ",");
159         String JavaDoc colName;
160         while (st.hasMoreTokens()) {
161             colName = st.nextToken().trim();
162             if (colName.startsWith("\""))
163                 colName = colName.substring(1, colName.length() - 1);
164             columns.add(colName.toLowerCase());
165         }
166     }
167
168     private void getMSSQLServerViewColumns() throws SQLException {
169         CallableStatement cs;
170         ResultSet rs;
171         String JavaDoc text = null;
172         int startPos, endPos;
173         
174         cs = con.prepareCall("{call sp_helptext(?)}");
175         cs.setString(1, view);
176         try {
177             rs = cs.executeQuery();
178             
179             if (rs != null)
180                 while (rs.next())
181                     text += rs.getString(1).trim();
182             rs.close();
183             cs.close();
184             
185             if (text == null)
186                 return;
187
188             startPos = text.toLowerCase().indexOf("select") + 6;
189             endPos = text.toLowerCase().indexOf("from");
190             text = text.substring(startPos, endPos).trim();
191
192             StringTokenizer st = new StringTokenizer(text, ",");
193             String JavaDoc colName;
194             while (st.hasMoreTokens()) {
195                 colName = st.nextToken().trim();
196                 if (colName.startsWith("\""))
197                     colName = colName.substring(1, colName.length() - 1);
198                 columns.add(colName.toLowerCase());
199             }
200         } catch (Exception JavaDoc exc) {
201             //no result set produced or unexpected driver error
202
rs = null;
203             return;
204         }
205     }
206     
207 }
208
Popular Tags